Scaling Reads (Replication, CQRS & Multi-Level Caching)
Scaling Reads (Replication, CQRS & Multi-Level Caching)
Most consumer internet platforms (Twitter, YouTube, Wikipedia) experience extreme 100:1 or 1,000:1 read-to-write ratios.
1. Multi-Tier Caching Architecture
- L1 Client / Browser Cache: Cache-Control headers, Service Worker caches, local storage.
- L2 Edge CDN: Cloudflare / CloudFront caches static media, API responses, and HTML fragments close to the user's geographic location.
- L3 Application Local Memory: In-memory Guava / Caffeine cache in service RAM ($< 1\text{ µs}$ access).
- L4 Distributed In-Memory Cache: Redis / Memcached cluster ($< 1\text{ ms}$ access).
2. Database Read Replicas & Replication Lag
- Master handles writes; multiple Read Replicas serve read queries.
- Read-Your-Own-Writes Consistency: If a user updates their profile picture, their next read might query an asynchronous replica that is 500ms behind.
- Solution: Route queries to the primary database for 5 seconds after a write, or track the client's write $LSN$ (Log Sequence Number).
Key Architecture Trade-Offs & Decision Matrix
| Decision Factor | Choice A | Choice B | Selected Architectural Rationale |
|---|---|---|---|
| Communication Protocol | REST over HTTP/2 | WebSockets / gRPC | Use REST for stateless CRUD APIs; use WebSockets for low-latency bidirectional streaming. |
| State Management | Centralized Distributed Store | Local Memory with Sticky Sessions | Centralized Redis / Database state allows seamless container auto-scaling and zero-downtime rolling deploys. |
| Concurrency Control | Optimistic Locking (version column) | Pessimistic Locking (SELECT FOR UPDATE) | Optimistic locking delivers superior throughput for low-conflict consumer operations. |
Related Topics
Expand your knowledge by learning about adjacent concepts in system design.
Design a Distributed Caching System (Redis Architecture)
Comprehensive architecture guide for designing production-grade Redis distributed caching clusters: master-replica replication, Redis Sentinel, Redis Cluster sharding, and memory eviction.
Consistent Hashing Architecture & Implementation
Deep dive into consistent hashing algorithms, virtual nodes, token rings, and minimal key remapping during cluster resizing in distributed databases.
Design a URL Shortener (TinyURL / Bitly Architecture)
Design a scalable URL shortening service capable of handling billions of redirects: Base62 encoding, distributed ID generation, database indexing, and 301 vs 302 redirect trade-offs.
Cheat Sheet Utility
View and print a concise system design reference card.