Saturday, 31 August 2013

Rails cache fetch with failover

Rails cache fetch with failover

We use this to get a value from an external API:
def get_value
Rails.cache.fetch "some_key", expires_in: 15.second do
# hit some external API
end
end
But sometimes the external API goes down and when we try to hit it, it
raises exceptions.
To fix this we'd like to:
try updating it every 15 seconds
but if it goes offline, use the old value for up to 5 minutes, retrying
every 15 seconds or so
if it's stale for more than 5 minutes, only then start raising exceptions
Is there a convenient wrapper/library for this or what would be a good
solution? We could code up something custom, but it seems like a common
enough use case there should be something battle tested out there. Thanks!

No comments:

Post a Comment