Caching

Backend Fundamentals · 13 lessons

  1. Redis — Redis is the go‑to solution for these patterns in modern backend stacks.
  2. Cache‑aside — Cache‑aside (also called lazy loading) lets an application decide when to read from or write to a cache. It keeps the cache in sync with the source of truth (the DB) while…
  3. Write‑through cache — Motivation In a typical backend you have a primary data store (e.g., PostgreSQL) and a secondary fast store (e.g., Redis). Reads are cheap from Redis, writes are cheap to…
  4. Write‑back cache — When a backend service reads data from a slow storage (e.g., a relational database or a distributed file system), it often keeps a copy in a fast in‑memory store (cache). The…
  5. Read‑through cache — Motivation Backends often need to serve the same data to many users. Fetching that data from a database on every request is expensive in latency, CPU, and network usage. A cache…
  6. TTL — Motivation Backends often serve data that changes over time: user profiles, product prices, session tokens, or computed analytics. Keeping every piece of data in memory forever is…
  7. Cache invalidation — When a backend serves data that changes over time, we often cache that data to reduce latency and load on the database. Cache invalidation is the mechanism that tells the cache to…
  8. Cache stampede — When a highly‑requested key is missing from the cache, many concurrent requests may all try to load the data from the backing store (DB, external API, etc.) at the same time. This…
  9. Cache penetration — Motivation When a cache sits in front of a database, the ideal is that most read requests hit the cache. Cache penetration happens when a request asks for a key that does not…
  10. Cache avalanche — When a cache entry expires, the next request that hits the cache must fetch the data from the origin (DB, API, etc.) and repopulate the cache. If many requests arrive at the same…
  11. Distributed caching — Imagine a library where every patron must ask a librarian for a book. The librarian fetches the book from the shelf, reads it, and hands it over. If many patrons ask for the same…
  12. LRU/LFU — Motivation Backends often need to serve the same data repeatedly (e.g., user profiles, product catalogs, AI model embeddings). Fetching that data from a database or recomputing it…
  13. Redis Cluster — Redis Cluster solves these problems by sharding data across multiple nodes and providing automatic failover.