Video in progress

At which layers can a system have a cache?

At every step from the user to the database: browser cache, CDN, reverse proxy, a local in-process cache, a shared cache like Redis, and the database's own buffer cache. The closer to the user, the faster, but the harder to control.

📖 Answer

Follow the path of a request:

  1. Browser — with Cache-Control: max-age=3600, an image or JS file isn't even requested again.
  2. CDN — static files are served from a server close to the user.
  3. Reverse proxy (Nginx) — can store a whole HTTP response for a short time.
  4. Local cache (Caffeine) — inside the application's memory, nanosecond-fast, but separate in every instance.
  5. Distributed cache (Redis) — shared by all instances, over the network, around 1 ms.
  6. Database — PostgreSQL also keeps frequently read pages in memory.

The problem is that when the data changes, the stale copy at every layer has to be updated. And you can't clear the cache in a browser at all — you can only wait for it to expire. That's why a version goes into the name of static files: app.3f9a1c.js.