SteelReed

Search

Search posts, questions…

7. System Design

Design principles: CAP, PACELC, scalability, performance and availability

The foundational principles of system design and the common trade-offs, CAP stated correctly (P is not optional) and PACELC, four scalability levers, performance tuning by p99 and by CPU- vs I/O-bound load, availability through MTBF/MTTR, failover, degradation, SLOs and error budgets, with an exercise computing the availability of a service chain.

18 Nov 20269 min readSenior

–

1. The problem

The ordering flow calls a gateway (99.95%), order (99.9%), inventory (99.9%), payment (99.5%) and PostgreSQL (99.95%) synchronously. The whole flow is only ~99.2%: about 70 hours down per year.

2. Key idea

Think of a restaurant: scalability is adding kitchens when it's busy, performance is dishes coming out fast, availability is staying open when a cook is sick. Set targets as numbers first.

2.1 CAP and PACELC, stated correctly

With two or more nodes, the network will sometimes split. CAP asks: at that moment do you return errors (keep C: reads always see the latest write) or return possibly stale data (keep A)? There's no "CA" distributed system. PACELC adds: in normal operation, latency or consistency.

System During a partition Normally
PostgreSQL + synchronous standby C: lose the standby and writes hang C: every commit waits for the standby
PostgreSQL/MySQL async replicas failover may lose the last few transactions L: replica reads may be stale
Cassandra, DynamoDB A: still accepts writes, reconciles later L, tunable with quorums

CAP's C isn't ACID's C (not breaking constraints within one database). CAP is chosen per kind of data.

2.2 Three goals, which levers?

Goal Measured by Levers
Scalability load grows with added resources stateless; split reads/writes, cache; a DB per service; sharding last
Performance p99 (averages hide the slow tail), RPS find the bottleneck first; CPU: algorithms; I/O: batching, indexes, no N+1
Availability MTBF / (MTBF + MTTR) redundancy, health checks, automatic failover, degrade non-essentials
Scaling Use when Don't use when
Scale up (bigger machine) databases, before you must shard you've hit the machine's limit, need fault tolerance
Scale out (more machines) stateless application tiers sessions still live in RAM

Failing every 30 days with 1-hour recovery: ≈ 99.86%; 5-minute failover: ≈ 99.99%. Serial chains multiply availability; two 99.9% copies in parallel: 99.9999%.

3. Worked example

Ordering flow, serial:      0.9995 × 0.999 × 0.999 × 0.995 × 0.9995 ≈ 99.2%
Payment off the sync path ("awaiting payment" order, processed via a queue):
                            0.9995 × 0.999 × 0.999 × 0.9995         ≈ 99.7%
Inventory as 2 copies in 2 AZs: 0.9995 × 0.999 × 0.999999 × 0.9995  ≈ 99.8%
Order 99.95%, DB 99.99% (automatic failover):                        ≈ 99.9%
 
Thread pool for I/O-bound work ≈ cores × (1 + wait time ÷ compute time)
8 cores, 45 ms waiting on the DB, 5 ms computing:  8 × (1 + 9) = 80 threads

Synchronous calls are the first thing to cut when you need high availability. Under overload, sacrifice the pawn to save the rook: turn off recommendations and reviews with feature flags to keep ordering alive.

Consistency, tail latency and error budgets
  • Consistency levels from strong to weak: linearizable, sequential, causal, read-your-writes (read from the primary for a few seconds after writing), monotonic reads, eventual.
  • Quorums: N replicas, writes wait for W, reads ask R; if R + W > N the read and write sets overlap. N = 3, W = 2, R = 2 is common.
  • Tail latency multiplies with fan-out: if each call is slow 1% of the time, a request fanning out to 100 calls hits at least one slow call ~63% of the time. Reduce it with hedged requests, budget-based timeouts, less fan-out.
  • SLI / SLO / SLA: a measured number / an internal target / a contract with penalties, looser than the SLO. Error budget = 100% − SLO: 99.9% over 30 days is ~43 minutes. When it's spent, stop shipping features. Alert on burn rate: burning 2% of the monthly budget in an hour is worth waking someone up.
  • Choose "boring" technology the team knows: fewer unknown unknowns.

4. Working with AI

Delegate to AI: chain availability calculations, thread-pool and capacity estimates, load tests with p99 thresholds.

Check yourself:

  • Targets have numbers: p99, RPS, availability.
  • No "CA system"; each kind of data states whether it picks C or A under partition.
  • The bottleneck was measured before adding a cache, broker or shards.
  • Synchronous calls on the main path are counted; there's a list of features you can switch off.

5. Exercises

Exercise 1: C or A for three features

Under partition, should these favour consistency or availability: (a) a shopping cart, (b) a wallet balance, (c) a like counter? AI proposes "Cassandra for everything, since AP tolerates failures well". How do you answer?

Solution to Exercise 1
  • (a) Cart: A. Let people add items even when replicas aren't in sync, merge later. Losing an "add to cart" is worse than a duplicate item (the classic Amazon Dynamo example).
  • (b) Wallet balance: C. Rejecting a transaction beats spending money twice. Write through one primary with a non-negative balance constraint, and read from the primary when deciding.
  • (c) Likes: A. Being a few off for a few seconds goes unnoticed; distributed counters merged later.
  • One AP store for all three is wrong for (b).

Exercise 2: Read the "more threads for speed" incident

An image-processing service (CPU-bound) runs on 4 cores with p99 300 ms. AI raises the pool from 4 to 64 threads "to process more in parallel"; p99 goes to 900 ms and throughput doesn't change. Why?

Solution to Exercise 2
  • Pure CPU work: only 4 cores really run, the other 60 threads queue and compete, adding context switches and cache misses. Throughput is capped by cores, and each job's latency grows because time is sliced.
  • Fix: pool ≈ cores (4–5), a bounded queue, reject early (429) when full. To go faster, optimise the algorithm or scale out.
  • By contrast, I/O-bound work (a 45 ms DB call) is what needs many threads or virtual threads.

Summary

  • CAP: under partition choose C or A; PACELC: normally choose latency or consistency.
  • CAP's C isn't ACID's C; choose per kind of data.
  • Scale out stateless tiers first, shard the database last.
  • Measure p99, find the bottleneck, then optimise.
  • Availability multiplies along a chain; cut MTTR and synchronous calls.

Further reading: Trade-off analysis like a senior.

Practice interview questions on this topic

All System Design questions →

Loading comments…
Next on the roadmapPragmatic software architecture: architectural drivers, the architect's role and context-driven decisions