CAP Theorem

A distributed system can only guarantee two of three:

  • Consistency — every read receives the most recent write
  • Availability — every request receives a response
  • Partition tolerance — system continues despite network failures

In practice, network partitions always happen, so you’re really choosing between CP (consistency) and AP (availability). Most real-world systems are somewhere on the spectrum.

PACELC Theorem

Extends CAP to cover normal operation as well: if there is a Partition, choose Availability or Consistency; Else (no partition), choose Latency or Consistency. Even without failures, stronger consistency requires cross-node coordination and therefore higher latency.

Consistency Patterns

  • Strong consistency / linearisability — reads always return the latest committed write as if there were a single copy of the data. Simpler to reason about, but slower across distributed nodes. Note that single-node ACID transactions are distinct from distributed linearisability: default SQL isolation levels (such as Read Committed) and asynchronous read replicas are not linearisable
  • Eventual consistency — reads may return stale data temporarily, but replicas converge over time. Higher availability and lower latency (e.g. DNS, Cassandra defaults). Note that AWS DynamoDB uses Multi-Paxos leader replication per partition and supports both eventually consistent reads (default) and strongly consistent reads
  • Read-your-writes — a user always sees their own writes, even if other users see stale data

Scaling

Vertical vs Horizontal

  • Vertical (scale up) — bigger machine. Simple, but has a ceiling
  • Horizontal (scale out) — more machines. No single-machine ceiling, though coordination, state management, and network bottlenecks still emerge at scale

Database Scaling

  • Read replicas — offload reads to copies
  • Sharding — split data across databases by a key (e.g. user ID). Hard to change later, so avoid until necessary
  • Connection pooling — reuse database connections (PgBouncer, ProxySQL)

Load Balancing

Distributes traffic across multiple servers. See Load Balancing for full details. Strategies:

  • Round robin — simple rotation
  • Least connections — send to the server with fewest active connections
  • Weighted — route more traffic to stronger servers
  • Consistent hashing — useful for caching layers, minimises redistribution when servers are added/removed
  • Visual guide to load balancing

Caching

  • Cache invalidation is one of the hardest problems in CS
  • Cache-aside — application checks cache first, loads from DB on miss, writes to cache
  • Write-through — write to cache and DB simultaneously
  • Write-behind — write to cache, asynchronously persist to DB. Faster writes, risk of data loss
  • TTL (time to live) — expire entries after a duration. Simple but may serve stale data
  • Layers: browser → CDN → reverse proxy → application cache → database cache

Rate Limiting

  • Protect services from overload
  • Token bucket — tokens refill at a fixed rate, each request consumes a token
  • Sliding window — count requests in a rolling time window
  • Apply at API gateway level, not deep in the application

Retries

  • Use exponential backoff with jitter — don’t hammer a failing service
  • Set a max retry count
  • Make operations idempotent so retries are safe
  • Retries guide

Communication Patterns

  • Synchronous (request/response) — HTTP/REST, gRPC. Simpler, but creates coupling
  • Asynchronous (event-driven) — message Queues, pub/sub. Decoupled, but harder to debug
  • gRPC — binary protocol over HTTP/2, faster than REST, strongly typed via protobuf. Good for service-to-service communication

Resources