1
1 Comment

Caching at Reverse Proxy or at Database level?

I'm building a REST Api where i currently cache the data in Redis.

Just before fetching the data from database, i check if its available in redis cache. If exists, i use this data. I invalidate this on every post/patch changes.

Pros:

  1. I have a copy of database in cache which gives me instant data reads

Cons:

  1. I need to make sure the data is invalidated on every database change. I'm currently using database triggers to invalidate the cache which may have some delay(this leads to unexpected errors)

  2. The size of redis may grow with the database fetches which is not scalable.

I was wondering if this is a proper way to do it or caching at reverse proxy(nginx) or node script (upstream) is the ideal way?

Considering caching at reverse proxy, i see the following concerns.

  1. Considering i'm using firebase auth, Will it be easy to cache authenticated rest api requests?

  2. What needs to be done if cache invalidation fails by any chance?

Thanks.

on February 17, 2021
  1. 1

    In order to cache in nginx you will still use redis and you’ll probably need to use its common support for lua. Lua is cool, but I don’t see any big advantage in caching there instead of in your node server, so I’d avoid it since it’s more complex with no upside.

    The main red flag in your question is keeping an entire db in redis. The cache should be a narrower, lighter projection if possible.

    Also, if your system does not have horizontal scaling, consider in-memory LRU cache to reduce complexity. Simpler is usually better :-)