System Design Basics — Scale, Cache, Queue

লেসন ৪৪ / ৫০

Scale

  • Vertical — bigger machine (simple, limit)
  • Horizontal — more machines (scalable, complex)

Load Balancer ⭐

Client → LB → [Server 1, 2, 3...]

Algorithms:
  Round Robin, Least Connections,
  IP Hash (sticky), Weighted,
  Least Response Time

Types: L4 (TCP), L7 (HTTP). Tools: Nginx, HAProxy, AWS ALB/NLB।

Caching Layers

Browser cache → CDN → Reverse Proxy cache →
  Application cache (Redis) → DB query cache

Cache Patterns

  • Cache-aside — app reads cache, miss → DB → populate
  • Write-through — write DB + cache together
  • Write-behind — write cache, async DB
  • Read-through — cache reads DB itself

Cache Invalidation

"There are only two hard things in CS: cache invalidation and naming things."
  • TTL (simple, stale risk)
  • Event-based (on write, invalidate)
  • Version key (v2:users:123)

CDN

Edge server globally — static assets (images, JS, CSS)। Providers: Cloudflare, Akamai, Fastly, CloudFront।

DB Scaling

  • Read replica (scale reads)
  • Sharding (scale writes)
  • Partitioning (big tables)
  • Caching layer (Redis)
  • NoSQL for specific access
  • CQRS (separate read/write)

Message Queue ⭐

Producer → [Queue] → Consumer

Use cases:
  Async processing (email, image resize)
  Decoupling services
  Load leveling (spike absorb)
  Reliable delivery (retry)

Tools: RabbitMQ, Kafka, AWS SQS, Azure Service Bus।

Database Choice

NeedDB
Transaction, relationPostgreSQL, MySQL
Flexible schema, scaleMongoDB
Time-series, write-heavyCassandra, InfluxDB
Full-text searchElasticsearch
Cache, sessionRedis
Graph (social)Neo4j
Analytics OLAPClickHouse, Redshift

Consistency Models

  • Strong — every read sees last write (slow, expensive)
  • Eventual — eventually consistent (fast, may see stale)
  • Read-your-writes — user sees own writes
  • Monotonic reads — no going back in time

CAP Theorem

Consistency, Availability, Partition Tolerance — Partition inevitable, pick CP or AP।

Replication

  • Master-Slave (single writer)
  • Master-Master (multi writer, conflict)
  • Quorum (N, W, R)

Back-of-Envelope Numbers ⭐

L1 cache:        0.5 ns
L2 cache:        7 ns
Memory ref:      100 ns
SSD random:      150 μs
Network RTT:     500 μs (datacenter)
HDD seek:        10 ms
Network intercontinental: 150 ms

1 req/s = 86,400 req/day
1K QPS peak = ~2x avg

Storage Estimates

User: 1KB → 1M users = 1GB
Photo avg 2MB → 1M photos = 2TB
100K req/s × 1KB response = 100MB/s egress