Availability Patterns¶
Two complementary patterns support high availability:
- Fail-over — a standby takes over when the primary fails.
- Replication — keep redundant copies of data.
Fail-over¶
Active-passive (a.k.a. master-slave)¶
- Heartbeats are sent between the active and the passive (standby) server.
-
If the heartbeat is interrupted, the passive server takes over the active's IP address and resumes service.
-
Only the active server handles traffic.
- Downtime length depends on whether the passive is in hot standby (already running) or cold standby (must start up).
Active-active (a.k.a. master-master)¶
- Both servers handle traffic, spreading the load between them.
- If public-facing, the DNS must know both public IPs.
- If internal-facing, the application logic must know about both servers.
Disadvantages of fail-over¶
- Adds more hardware and complexity.
- Risk of data loss if the active system fails before newly written data is replicated to the passive.
Replication¶
Redundant copies of data. Discussed in depth in the database notes:
Availability in numbers¶
Availability is quantified by uptime/downtime as a percentage of time. Measured in "nines" — 99.99% availability = "four nines".
99.9% availability (three nines)¶
| Duration | Acceptable downtime |
|---|---|
| Per year | 8h 45min 57s |
| Per month | 43m 49.7s |
| Per week | 10m 4.8s |
| Per day | 1m 26.4s |
99.99% availability (four nines)¶
| Duration | Acceptable downtime |
|---|---|
| Per year | 52min 35.7s |
| Per month | 4m 23s |
| Per week | 1m 5s |
| Per day | 8.6s |
In sequence vs in parallel¶
For components with availability < 100%, overall availability depends on whether they are in sequence or in parallel.
In sequence — availability decreases:
Availability(Total) = Availability(Foo) * Availability(Bar)
Two 99.9% components in sequence → 99.9% × 99.9% = 99.8%.
In parallel — availability increases:
Availability(Total) = 1 - (1 - Availability(Foo)) * (1 - Availability(Bar))
Two 99.9% components in parallel → 1 - (0.001 × 0.001) = 99.9999%.
Common Interview Questions¶
Design a system with 99.99% availability
Why it's asked here: availability patterns (failover, replication, redundancy) are the tools that answer this.
Key points to discuss:
- Compute allowed downtime: four nines = ~52 min/year.
-
Redundancy in parallel raises availability; serial dependencies lower it (show the formulas from this page).
-
Active-passive vs active-active failover, and failover detection (heartbeats).
- Eliminate single points of failure; replicate data; use multiple AZs/regions.
flowchart LR
LB[Load Balancer] --> A[Server A active]
LB --> B[Server B standby]
A -.heartbeat.-> B
B -->|failover on failure| A
Design a system that scales to millions of users
Why it's asked here: scaling out requires the availability patterns on this page.
Key points to discuss:
- Stateless servers + externalized sessions enable horizontal scaling.
- Replication (master-slave) for read availability; failover for write availability.
- Availability "in numbers": calculate the combined availability of the chain.
- Trade hardware/complexity (more nines) against cost.
flowchart LR
U[Users] --> DNS[DNS]
DNS --> LB[Load Balancer]
LB --> W[Web tier - stateless]
W --> C[(Cache)]
W --> M[(DB master)]
M --> R[(Replicas)]
Key takeaways¶
- Redundancy (parallel components) improves availability.
-
Serial dependencies reduce availability — a chain is only as available as the product of its parts.
-
Each "nine" roughly translates to order-of-magnitude less downtime.
Design a System with 99.99% Availability — Full Walkthrough¶
To design a system that provides 99.99% availability, first define the workload. Suppose the service must support 100 million registered users, 10 million daily active users, 10,000 requests per second at peak, and 100 writes per second. The availability budget is 99.99%, which permits only about 52 minutes of downtime per year, so the architecture must be built from redundant components and avoid any single point of failure. The functional requirements are to let users browse content or products, create sessions or carts, and submit durable writes such as orders. The non-functional requirements are high availability, low latency under normal and failover conditions, and the ability to survive the loss of an entire availability zone or server.
The high-level architecture places a managed DNS service in front of a content delivery network and multiple redundant load balancers. The load balancers route requests to stateless application servers deployed across at least three availability zones in a single region. The app servers use an external session store in Redis, read product or content data from caches or replica databases, and write transactional data to a primary database with a hot standby. Static content is served from object storage through the CDN. This means every component in the request path is either stateless and duplicated or stateful and replicated.
The data model separates transactional, cached, and static data. Orders and user accounts require atomicity, consistency, and durability, so they live in a relational database. The primary database is replicated synchronously to at least one standby within the region and asynchronously to a remote region for disaster recovery. Hot product data and sessions are stored in a replicated Redis cluster because they are read-heavy and can tolerate a short recovery delay. Product images and other large binaries live in object storage because object storage is already replicated across availability zones and can be served from edge caches. This storage choice is driven by access patterns: SQL for transactions, Redis for hot state, and object storage for immutable media.
The core mechanism that achieves four nines is automated failover combined with parallel redundancy. Every user request must be able to follow an alternative path when a node fails. DNS health checks direct traffic away from an unhealthy region or external endpoint. Load balancers perform continuous health checks and stop routing to failed app servers, while the app tier is active-active so surviving instances absorb the load. The database uses active-passive failover: the primary and standby exchange heartbeats, and when the primary fails, the standby is promoted and takes over the writer role without depending on a human operator. The availability math is central. Parallel redundant components such as two load balancers or two app servers combine via 1 minus the product of their failure probabilities, which increases availability, while serial dependencies multiply and reduce it. The design checks every serial path and either duplicates it or makes it failover-capable.
Scaling and availability are handled together. The stateless app servers run in an auto-scaling group across availability zones, so the system scales horizontally and replaces failed nodes automatically. Caching reduces database load and absorbs hot-read spikes. The database is sharded by customer or order ID and replicated to read replicas for read-heavy traffic. Replication and sharding allow the write path to stay available when a single database instance fails. The load balancer tier is itself deployed as multiple active-active nodes, so it is not a single point of failure. Failure handling is layered: a failed app instance is immediately removed from rotation, a failed availability zone is bypassed by the load balancer and DNS, a failed primary database is replaced by its standby, and a failed region is avoided using the remote disaster-recovery copy.
The important trade-offs are cost versus availability and consistency versus latency. Synchronous replication to the database standby improves durability and failover correctness but adds write latency, especially if the standby is in another availability zone. Asynchronous cross-region replication improves disaster recovery but can lose recent writes if a whole region fails. Stateless application servers allow fast horizontal scaling and fast failover, but they require the session store itself to be highly available and replicated. Likewise, adding more zones and regions increases availability but increases infrastructure cost, operational complexity, and cross-zone or cross-region data-transfer cost. For 99.99% availability, the design usually accepts slightly higher write latency and cost to avoid any single component whose failure would exceed the annual downtime budget.
flowchart LR
U[Users] --> DNS[Managed DNS with health checks]
DNS --> CDN[CDN]
CDN --> LB[Redundant Load Balancers]
LB --> A1[Stateless App AZ1]
LB --> A2[Stateless App AZ2]
LB --> A3[Stateless App AZ3]
A1 & A2 & A3 --> R[(Replicated Redis)]
A1 & A2 & A3 --> P[(Primary SQL DB AZ1)]
P -->|synchronous standby| S[(Standby SQL DB AZ2)]
P --> RDB[(Read Replicas)]
P -.async.-> DR[(Disaster Recovery Region)]
CDN --> OS[(Object Storage)]