Skip to content

Instantly share code, notes, and snippets.

@jraines
Created May 18, 2011 20:32
Show Gist options
  • Save jraines/979482 to your computer and use it in GitHub Desktop.
Save jraines/979482 to your computer and use it in GitHub Desktop.
Horizontal scaling with the Worker pattern

Note - reference these slides; good diagrams

##High traffic

Problem: Heavy lifting inside the request cycle

  • Bound by slowest API
  • Potential redundant retrieval (popular search term)

The Worker Pattern decouples web request from services required to respond to request

Controller looks in cache, which is populated by workers. Client polls controller.

Fragments

Controller asks Fragment if data exists. If not, controller tells fragment to fetch data asynchronously, tell client to check back later (204 No Content)

  • building a weird API? If an HTTP Status code matches what you're doing, it's a hint you're doing it right
$.ajax({
  success: function(data, status, req {
   //fetch the data
 }
 $(some_div).html(data);
});
def show
  fragment = Fragment.new(params[:name])
  fragment.query = params[:q]

  if fragment.exists?
    @data_for_view = fragment.instance
    render :template => "fragment_partial"
  else
    render :status => 204
  end
end
class Fragment

  def self.search(q,key)
    res = Api.search(q)
    Cache.write(key, res)
  end

  def exists?
    !instance.nil?   
  end

  def instance
    Cache.get(key)
  end

  def fetch
    QC.enqueue(self.search . . .)
  end

###Parallel Execution

With this kind of architecture, you can scale horizontally by using a load balancer and adding web servers like thin.

###New Worker Queue

http://github.com/ryandotsmith/queue_classic

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment