Caching
The notes
Cache and origin server
Caching stores frequently accessed data in fast, temporary storage to reduce latency and server load. It acts as a speed layer between users and the main data source.
Cache (temporary storage)
- A high-speed storage layer that holds copies of frequently accessed data
- Located closer to the user or application, so access is much faster than from the origin server
- Examples: Redis, Memcached, CDN, browser cache
Origin server (main data source)
- The primary database or server that stores the original, authoritative data
- Slower to access but contains all data
- The cache retrieves data from here on a miss and stores copies for future requests
How caching works
When a client requests data, the system first checks the cache. This decides whether the response is fast (cache hit) or needs the origin server (cache miss).
Cache hit
- Data exists in the cache
- The cached copy is returned immediately to the client
- The ideal case: a fast response with no origin server load
Cache miss
- Data is not in the cache
- The system fetches it from the origin server, returns it to the client and stores a copy in the cache for future requests
- Slower, but ensures data freshness
Cache eviction techniques
A cache has limited space. When it is full, eviction policies decide which data to remove to make room for new entries.
LRU (least recently used)
- Removes items that haven't been accessed for the longest time
- Assumes recently used data will be used again soon
- Best for applications with temporal locality of access patterns
LFU (least frequently used)
- Removes items with the lowest access count
- Prioritizes popular data over recent data
- Best when some data is consistently more popular than others
FIFO (first in, first out)
- Removes the oldest cached item regardless of usage
- Simple to implement but doesn't consider access patterns
- Best for time-sensitive data with predictable lifecycles
Key benefits
- Faster response times: a cache responds in milliseconds vs seconds from origin servers, so frequently accessed content loads near-instantly
- Reduced server load: origin servers handle fewer requests, freeing resources and preventing database bottlenecks during traffic spikes
- Lower network costs: reduces data transfer from origin servers; CDNs cache content closer to users, minimizing expensive long-distance transmission
- Improved scalability: handle more users without upgrading origin infrastructure; the cache absorbs traffic increases
System design deck
1. Fundamentals
2. Scaling
3. Frontend vs backend
4. Client-server architecture
5. Databases
6. Load balancer
7. Caching
8. Latency vs throughput
9. Availability and redundancy
10. CAP theorem
and many more
Make them stick
Sign in to see every card full size, tick the ones you've got, come back to the rest later, and keep your own notes on each card.
Watch the lesson
System design, Lesson 4, from 4:39: Caching in Computing World