Skip to content

Failover

The protections so far make a failing dependency less harmful: a retry hides a blip, a breaker stops the calls, a bulkhead confines the damage. None of them makes the dependency work. Failover does: when a component is lost, another one takes its work. It has three parts. The failure has to be noticed, the work has to be switched to the other copy, and the redundancy has to be restored afterwards, because after a failover the system has one copy fewer.

The database case has its own lesson, failover and lost writes, where a replica is promoted to primary and the writes it never received are lost. This lesson is about the general shape, which covers a stateless service behind a load balancer as well as a database.

Replica servers0.3 ms0.5 ms0.5 ms250 msClientsHealthy<1%APIBusy75%PrimaryHealthy9%ReplicasHealthy36%ReplicationHealthy45%
Replica servers0.3 ms0.5 ms0.5 ms250 msClientsHealthy<1%APIBusy75%PrimaryHealthy9%ReplicasHealthy36%ReplicationHealthy45%
Text version of the diagram
  • Clients (outside Replica servers) sends requests to API.
  • API (outside Replica servers) sends requests to Primary, Replicas.
  • Primary (outside Replica servers) sends requests to Replication.
  • Replicas (inside Replica servers) does not call another component.
  • Replication (inside Replica servers) does not call another component.

In the picture, three API instances share the requests, one primary takes the writes and two replicas answer the reads. Each of those is a copy of something that can be lost, and each has a different kind of failover: an API instance can simply be dropped from the rotation, a replica too, while a primary needs a replica promoted.

Amazon’s Route 53 documentation gives the two shapes names. In active-passive failover, a primary resource is available most of the time and a secondary is on standby: when responding to queries, Route 53 includes only the healthy primary resources, and begins to include the secondary only if all the primaries are unhealthy. In active-active failover, all the resources are available most of the time, and when one becomes unavailable Route 53 detects that it is unhealthy and stops including it. The multi-region module takes the same two shapes to whole regions.

Active-active does not need to wait for a decision: the others are already working. Active-passive keeps a standby that takes no traffic until it is needed.

Nothing switches until the failure is noticed, and a health check notices it late. The health checks lesson works out the delay: with a probe every i seconds and f failures in a row, the server is taken out more than (f − 1) × i and at most f × i seconds after it fails. Take two servers that share 100 requests a second, and a check every 10 s with 3 failures. The server is out of rotation after more than 20 s and at most 30 s. The dead server is sent 50 requests a second, so in the worst case 50 × 30, which is 1,500 requests fail. With a check every 2 s and 3 failures, as in HAProxy’s defaults, the worst case is 6 s and 50 × 6, which is 300.

A retry covers the gap. Under round robin the next request goes to the other server, so a retried request lands on the live one, and the user sees a slower answer, not an error. That is why retries and failover belong together, and also why the retried request must be safe to repeat.

Mental model. Failover is a decision made on old information. It starts late, because the failure is noticed late, and it costs what was in flight when the component was lost.

Two servers that each run at 60% utilisation look comfortable. When one fails, the other must take both loads: 2 × 60%, which is 120%. It cannot, and the failover that was meant to save the service overloads the one server left. Route 53’s documentation has the same warning for weighted records: all the records with nonzero weights must be unhealthy before it begins to respond with the zero-weighted ones, which can make a site unreliable if the last healthy resource cannot handle all the traffic. To survive the loss of one of n servers, each must run below (n − 1) / n of its capacity: below 50% for two, below about 67% for three. The health checks lesson asks the same question of a load balancer.

A failover can also be a mistake. A health check can fail for a reason that has nothing to do with the server, and moving the traffic away from a server that was fine, or promoting a replica while the primary is alive, causes the outage that it was meant to avoid. Route 53 treats this seriously: when all the records of a group are considered unhealthy, it reverts to considering all of them healthy, so that a misconfigured check cannot leave a name with no answer. It also notes that during an internet partition different health checkers may reach different conclusions about the same endpoint.

For state, a wrong decision is worse. The PostgreSQL documentation says that if the old primary comes back, there must be a mechanism to tell it that it is no longer the primary, which it calls STONITH, shoot the other node in the head, to avoid both systems thinking they are the primary, which leads to confusion and in the end data loss. The split brain lesson is about that case. The same page says that after a failover only one server is left, a degenerate state, and a standby must be recreated before the system is redundant again. PostgreSQL itself does not provide the software to identify the failure on the primary, so one has to be added.

The module’s four protections answer four different failures. Sort the situations below under the protection that answers each one.

Challenge

Which protection fits?

Four protections answer four different failures. A retry hides a brief failure. A circuit breaker stops calls to a dependency that is down. A bulkhead keeps one dependency from using up what the others need. A failover moves the work to a standby. Put each situation under the protection that answers it.

Sorting needs JavaScript.

Categories

Retry with backoff and jitter
Try the same call again, after a wait.
Circuit breaker
Stop calling a dependency that keeps failing, and test it now and then.
Bulkhead
Give each dependency or client its own share of the resources.
Failover
Send the work to a standby or to another copy.
  1. Line 1 of 10A read failed because a connection was dropped, and the same read would very probably work a moment later.
  2. Line 2 of 10A dependency has failed five times in a row, and every further call waits the whole timeout before it fails.
  3. Line 3 of 10A slow recommendations service has used every connection in the shared pool, so payment calls cannot get one.
  4. Line 4 of 10The primary database has died, and a replica must start taking the writes.
  5. Line 5 of 10Thousands of clients lost their connection at the same moment and are about to reconnect.
  6. Line 6 of 10One customer sends a flood of calls, and the other customers' calls start to fail.
  7. Line 7 of 10A dependency is known to be down for several minutes, and calling it only keeps threads blocked.
  8. Line 8 of 10A health check shows that the server in zone A cannot be reached, and traffic should go to the copy in zone B.
  9. Line 9 of 10A read timed out once on a congested link, and the next attempt is likely to take a different route.
  10. Line 10 of 10While a dependency recovers, only a trickle of test calls should reach it, and the full traffic only once they succeed.

Check your answers at least once before looking at the answers.

What happens if the standby has never been used?

Section titled “What happens if the standby has never been used?”

A standby that has never taken traffic is a standby whose configuration, capacity and data nobody has checked. The first time the decision is made may be during a real failure. A failover that is rehearsed is a failover that works when needed, and a rehearsal is also a chance to measure the detection time and the load on the survivors, the two numbers in this lesson. The failure engineering module is about doing this on purpose.

  • Failover has three parts: notice the failure, switch the work, and restore the redundancy. After it, there is one copy fewer.
  • Active-passive keeps a standby that takes over when all the primaries are unhealthy. Active-active keeps every copy at work and stops using the one that is unhealthy.
  • A check every 10 s with 3 failures takes a server out after more than 20 s and at most 30 s. At 50 requests a second for the dead server that is up to 1,500 failed requests, and 300 with a 2 s check.
  • Two servers at 60% become one at 120%: for the loss of one of n servers, each must stay below (n − 1) / n.
  • A wrong failover is an outage of its own, and for state it can mean two primaries. Rehearse the switch.

Next: the module in one page, with a quiz.

Your progress

Built by GhaziAlibi.

© ShardCraft. All rights reserved.