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:
- Browser — with
Cache-Control: max-age=3600, an image or JS file isn't even requested again. - CDN — static files are served from a server close to the user.
- Reverse proxy (Nginx) — can store a whole HTTP response for a short time.
- Local cache (Caffeine) — inside the application's memory, nanosecond-fast, but separate in every instance.
- Distributed cache (Redis) — shared by all instances, over the network, around 1 ms.
- 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.
Sign up to mark studied questions and save your progress.