Back to explorer
Core Fundamentals• 30 Min

Scaling Reads (Replication, CQRS & Multi-Level Caching)

MEDIUM

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

  1. L1 Client / Browser Cache: Cache-Control headers, Service Worker caches, local storage.
  2. L2 Edge CDN: Cloudflare / CloudFront caches static media, API responses, and HTML fragments close to the user's geographic location.
  3. L3 Application Local Memory: In-memory Guava / Caffeine cache in service RAM ($< 1\text{ µs}$ access).
  4. 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 FactorChoice AChoice BSelected Architectural Rationale
Communication ProtocolREST over HTTP/2WebSockets / gRPCUse REST for stateless CRUD APIs; use WebSockets for low-latency bidirectional streaming.
State ManagementCentralized Distributed StoreLocal Memory with Sticky SessionsCentralized Redis / Database state allows seamless container auto-scaling and zero-downtime rolling deploys.
Concurrency ControlOptimistic Locking (version column)Pessimistic Locking (SELECT FOR UPDATE)Optimistic locking delivers superior throughput for low-conflict consumer operations.