I am building a Rails backend to an iPhone app.
After profiling my application, I have found the following call to be especially expensive in terms of performance:
@messages.as_json
This call returns about 30 message objects, each including many child records. As you can see, a single message json response may make many DB calls to be composed:
def as_json(options={})
super(:only => [...],
:include => {
:user => {...},
:checkin => {...}
}},
:likes => {:only => [...],
:include => { :user => {...] }}},
:comments => {:only => [...],
:include => { :user => {:only => [...] }}}
},
:methods => :top_highlight)
end
On average the @messages.as_json
call (all 30 objects) takes almost 1100ms.
Wanting to optimize I've employed memcached. With the solution below, when all my message objects are in cache, average response is now 200-300ms. I'm happy with this, but the issue I have is that this has made cache miss scenarios even slower. In cases where nothing is in cache, it now takes over 2000ms to compute.
# Note: @messages has the 30 message objects in it, but none of the child records have been grabbed
@messages.each_with_index do |m, i|
@messages[i] = Rails.cache.fetch("message/#{m.id}/#{m.updated_at.to_i}") do
m.as_json
end
end
I understand that there will have to be some overhead to check the cache for each object. But I'm guessing there is a more efficient way to do it than the way I am now, which is basically serially, one-by-one. Any pointers on making this more efficient?
I believe Rails.cache
uses the ActiveSupport::Cache::Store
interface, which has a read_multi
method for this exact purpose. [1]
I think swapping out fetch
for read_multi
will improve your performance because ActiveSupport::Cache::MemCacheStore
has an optimized implementation of read_multi
. [2]
Code
Here's the updated implementation:
keys = @messages.collect { |m| "message/#{m.id}/#{m.updated_at.to_i}" }
hits = Rails.cache.read_multi(*keys)
keys.each_with_index do |key, i|
if hits.include?(key)
@messages[i] = hits[key]
else
Rails.cache.write(key, @messages[i] = @messages[i].as_json)
end
end
The cache writes are still performed synchronously with one round trip to the cache for each miss. If you want to cut down on that overhead, look into running background code asynchronously with something like workling.
Be careful that the overhead of starting the asynchronous job is actually less than the overhead of Rails.cache.write
before you start expanding your architecture.
Memcached Multi-Set
It looks like the Memcached team has at least considered providing Multi-Set (batch write) commands, but there aren't any ActiveSupport interfaces for it yet and it's unclear what level of support is provided by implementations. [3]