Phase 3: Caching
Explore the concepts of Caching.
Caching Strategies (Cache-Aside/Write-Through/Write-Back)
A caching strategy defines how an application keeps a fast, temporary copy of data in a cache and how that copy stays in step with the underlying data store. The four core patterns are cache-aside, write-through, write-back, and write-around, and each strikes a different balance between read latency, write latency, and staleness. 🧠 Mental model: A cache is the top of your desk. You keep the papers you're working on right there for speed. The filing cabinet (database) has everything, but walking over to it every time is slow. The caching strategy decides which papers get desk space.
Content Delivery Networks
A content delivery network (CDN) is a geographically distributed set of edge servers that cache content close to users so requests are served from a nearby location instead of the distant origin server. By shortening the network path, a CDN cuts latency, absorbs traffic that would otherwise hit the origin, and improves availability during spikes. 🧠 Mental model: A CDN is like a chain of local newspaper stands. Instead of everyone driving to the printing press (origin server), each neighborhood has its own stand (edge server) stocked with today's papers.
Cache Eviction Policies (LRU/LFU)
A cache eviction policy is the rule a cache uses to decide which entry to remove when it is full and a new entry needs space. The three policies you must know are least recently used (LRU), least frequently used (LFU), and first in first out (FIFO), and each answers the same question differently: which entry is the least valuable to keep? 🧠 Mental model: Your desk is full of papers. LRU = toss the paper you haven't touched in the longest time. LFU = toss the paper you barely ever use. FIFO = toss the oldest paper, no matter how useful it is.
Proxy & Reverse Proxy
A proxy is an intermediary server that sits between a client and a destination server, intercepting and forwarding requests. A forward proxy sits in front of clients and protects them from the internet, while a reverse proxy sits in front of servers and protects them from clients. 🧠 Mental model: A forward proxy is a personal shopper - you tell them what you want, and they go out to the stores (the internet) to buy it for you, hiding your identity. A reverse proxy is a receptionist at a large company - outside callers talk to the receptionist, who routes the call to the right employee (server) without revealing their direct extension.