Getting everything ready for you…

Loading courses, problems and your progress. This takes just a moment.

0s

Notes › System Design

Caching, CDN and Databases at Scale

Caching

A cache stores the results of expensive work (DB queries, API calls, rendered pages) in fast memory so the next request is served instantly.

Where caches live

Layer Example
Browser HTTP cache (Cache-Control), localStorage
CDN edge servers cache images, JS, CSS, even pages
Load balancer / reverse proxy Nginx, Varnish
Application in-memory map / LRU inside the process
Distributed cache Redis, Memcached — shared by all servers
Database buffer pool, query cache

Cache hit ratio = hits / total requests. Aim high (90%+) for hot data.

Caching strategies

Cache-aside (lazy loading) — most common

read:  check cache → hit? return
                   → miss? read DB → put in cache (with TTL) → return
write: update DB → delete the cache key
async function getUser(id) {
  const key = `user:${id}`;
  const cached = await redis.get(key);
  if (cached) return JSON.parse(cached);                 // hit
  const user = await User.findById(id).lean();           // miss → DB
  if (user) await redis.set(key, JSON.stringify(user), { EX: 300 });   // 5-minute TTL
  return user;
}
async function updateUser(id, data) {
  await User.updateOne({ _id: id }, data);
  await redis.del(`user:${id}`);                         // invalidate
}

Only requested data is cached; a cache failure isn't fatal. First read is slow.

The others

Strategy How it writes Pros Cons
Read-through cache itself loads from DB on a miss simpler app code cache library must know the DB
Write-through write to cache and DB together cache always fresh slower writes, caches unread data
Write-back (write-behind) write to cache, DB updated later in batches very fast writes data loss if the cache dies
Write-around write only to DB; cache fills on read no cache pollution first read after a write misses

Eviction policies (cache is full — what to remove?)

Policy Removes Good for
LRU (least recently used) item not used for the longest time most workloads (default choice)
LFU (least frequently used) item used the fewest times stable popularity
FIFO oldest inserted simple
TTL items after a fixed time data that goes stale

LRU = hash map + doubly linked list → O(1) get and put (a classic coding question).

Cache problems

Redis — more than a cache

Strings, hashes, lists, sets, sorted sets (leaderboards), pub/sub, streams, TTLs, atomic INCR (rate limiting, counters), distributed locks. Data lives in RAM with optional persistence (RDB snapshots / AOF log).

CDN — Content Delivery Network

A network of edge servers around the world that cache your static content close to users.

User in Mumbai → CDN edge in Mumbai (hit: ~10 ms)
                     ↓ miss
                 origin server in US (~250 ms), then cached at the edge

Databases at scale

Replication

Copies of the same data on several servers.

Partitioning and sharding

Sharding method How Watch out
Range user_id 1–1M → shard 1 … hot spots (new users all on the last shard)
Hash hash(user_id) % N adding shards moves most data → use consistent hashing
Directory lookup table key → shard the directory is a dependency

Problems: cross-shard joins and transactions, rebalancing, choosing a good key (high cardinality, even spread, matches query patterns). Details in Database Sharding and Replication.

Other tools

Interview one-liners

More System Design notes