DEV Community

Pacharapol Withayasakpunt
Pacharapol Withayasakpunt

Posted on

REST API and caching?

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.)

Top comments (3)

Collapse
 
brandinchiu profile image
Brandin Chiu • Edited

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.

Collapse
 
n1try profile image
Ferdinand Mütsch • Edited

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

Collapse
 
souksyp profile image
Souk Syp.

Use Redis or service workers ?