Note - reference these slides; good diagrams
##High traffic
- Bound by slowest API
- Potential redundant retrieval (popular search term)
Controller looks in cache, which is populated by workers. Client polls controller.
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