ArchitectureAdvanced

System Design & Distributed Scalability

System design balances trade-offs across latency, consistency, availability, throughput, and operational complexity.

Key Mental Models & Invariants

  • -CAP theorem: Under network partitions, a distributed system must choose between Consistency or Availability.
  • -Horizontal scaling requires stateless application tiers and distributed caching/storage.
  • -Rate limiting (Token Bucket, Leaky Bucket, Sliding Window) protects backend capacity.
  • -Single Points of Failure (SPOF) must be mitigated with redundant active-active or active-passive replicas.

Deep Dive Architecture

### The Core Building Blocks 1. **DNS & Anycast**: Route users to nearest geographic edge. 2. **CDN**: Cache static and cacheable dynamic responses at edge PoPs. 3. **Load Balancers (Layer 4 TCP vs Layer 7 HTTP)**: Distribute traffic across server pools. 4. **Stateless App Servers**: Session tokens stored in JWT or Redis, allowing autoscaling. 5. **Distributed Cache (Redis/Memcached)**: Sub-millisecond reads for hot data. 6. **Asynchronous Message Queues (Kafka/RabbitMQ)**: Decouple slow write operations from user request path. 7. **Database Primary/Replica**: Route writes to primary, reads to read replicas.
Code Exampletext
[Client] -> [Cloudflare CDN] -> [ALB Load Balancer]
                                        │
                         ┌──────────────┴──────────────┐
                         ▼                             ▼
                 [API Server 1]                 [API Server 2]
                         │                             │
                ┌────────┴────────┐           ┌────────┴────────┐
                ▼                 ▼           ▼                 ▼
          [Redis Cache]     [Kafka Queue]  [Redis Cache]  [Kafka Queue]
                │                 │
                ▼                 ▼
        [PostgreSQL DB]     [Worker Fleet]

Standard distributed read-heavy architecture with asynchronous write queue.