Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to cache RESTful API results of GET calls

Tags:

I'm thinking about the best way to create a cache layer in front or as first layer for GET requests to my RESTful API (written in Ruby).

Not every request can be cached, because even for some GET requests the API has to validate the requesting user / application. That means I need to configure which request is cacheable and how long each cached answer is valid. For a few cases I need a very short expiration time of e.g. 15s and below. And I should be able to let cache entries expire by the API application even if the expiration date is not reached yet.

I already thought about many possible solutions, my two best ideas:

  • first layer of the API (even before the routing), cache logic by myself (to have all configuration options in my hand), answers and expiration date stored to Memcached

  • a webserver proxy (high configurable), perhaps something like Squid but I never used a proxy for a case like this before and I'm absolutely not sure about it

I also thought about a cache solution like Varnish, I used Varnish for "usual" web applications and it's impressive but the configuration is kind of special. But I would use it if it's the fastest solution.

An other thought was to cache to the Solr Index, which I'm already using in the data layer to not query the database for most requests.

If someone has a hint or good sources to read about this topic, let me know.

like image 521
maddin2code Avatar asked Dec 24 '12 22:12

maddin2code


People also ask

Can you cache API calls?

Caching in REST APIs POST requests are not cacheable by default but can be made cacheable if either an Expires header or a Cache-Control header with a directive, to explicitly allows caching, is added to the response. Responses to PUT and DELETE requests are not cacheable at all.

Which of the following is a best practice for caching in restful web service?

Always keep static contents like images, CSS, JavaScript cacheable, with expiration date of 2 to 3 days. Never keep expiry date too high. Dynamic content should be cached for a few hours only.

Does API gateway cache responses?

When you enable caching for a stage, API Gateway caches responses from your endpoint for a specified time-to-live (TTL) period, in seconds. API Gateway then responds to the request by looking up the endpoint response from the cache instead of making a request to your endpoint.


1 Answers

memcached is a great option, and I see you mentioned this already as a possible option. Also Redis seems to be praised a lot as another option at this level.

On an application level, in terms of a more granular approach to cache on a file by file and/or module basis, local storage is always an option for common objects a user may request over and over again, even as simple as just dropping response objects into session so that can be reused vs making another http rest call and coding appropriately.

Now people go back and forth debating about varnish vs squid, and both seem to have their pros and cons, so I can't comment on which one is better but many people say Varnish with a tuned apache server is great for dynamic websites.

like image 69
jsteinmann Avatar answered Oct 07 '22 08:10

jsteinmann