REST API and caching?

patarapolw profile image Pacharapol Withayasakpunt ・1 min read

If I use GET requests for ones with dynamic database calls, will it be over-cached?

Or, does it depends on response code, e.g. other than 200, no-cache?

How do I enable caching in the first place? Another new database just for caching?

Lastly, can POST requests be cached? (Because, sometimes, supposedly GET requests would have a too big querystring.)


markdown guide

You typically only cache get requests, and there are services that already exist to do this.

Redis is a very popular one. When a get request is made, you push the result set to redis and store it there for x seconds.

Then every subsequent identical get request would pull the results from redis instead of the database as long as the result set hasn't expired yet.

How you implement this is typically by watching for http caching headers, and determining which of your get requests have low enough turnover that caching is effective.

For example, if your get request relies on having live data, (like getting a count of invoices), then caching might not make sense. You have to decide what level of caching is appropriate for your application.


Here's a very good explanation on how browser caching can be controlled using different kinds of HTTP headers: web.dev/http-cache/


Use Redis or service workers ?