# Failover

## What it is

Failover moves responsibility for serving an operation from a component that is
unavailable to another component that can take over. It can involve changing the
database that accepts writes, routing a network service to a replacement system,
or choosing a different provider to send a message. The details follow the
service's responsibilities. A replacement has to be ready for the particular
work that is being moved.

A failover procedure usually includes detection, a decision about when to
switch, the change itself, and a check of the result. In a replicated database,
a standby can already hold a copy of much of the primary's state. Promotion
makes it eligible to act as primary. The system also needs a way to prevent the
former primary from taking new writes when both sides can be reached again. That
step helps preserve a single authoritative history.

For services that call another provider, failover can choose a different route
for an operation. An application sending mail can try an alternate provider when
the first becomes unavailable. A timeout can leave uncertainty about whether the
first provider accepted a message. Tracking the operation and its eventual
outcome is part of this design. The alternate provider needs compatible
capabilities and enough working capacity to handle the work being moved.

## Why it matters

A recovery design needs to describe time and data. How long can clients wait for
a working route? How recent must the replacement's data be? What happens to a
request that was active during the switch? An available replacement is only part
of the answer. The client may still have an old connection, and queued work
might need a clear retry or recovery path.

Detection requires care. A slow response can be a failure for an application
with a tight deadline, and a missed health signal can reflect a monitoring
problem while the primary still serves requests. An automatic decision needs a
condition that is meaningful to the system and evidence for resolving
uncertainty. A manual decision can take longer but can let an operator confirm
what the system has observed before changing authority.

A recovery target should also name the observer. Infrastructure readiness might
mean that a replacement database accepts connections. The product's recovery
time starts and ends around a user operation, including the discovery path,
applicable client retries and a representative query. If the service has several
entry points, exercise the critical one that reaches the replacement through
normal routing. Record the data that was committed before the interruption and
check which part of that history a recovered client can actually see.

Switching back can be more involved than switching away. A former database
primary can return with an older or diverging history. A provider that responds
again may have accumulated delayed operation updates. Write a return procedure
for the actual state the system can have after a switch. Practice restoring
redundancy separately from the initial recovery, because the service can look
healthy to users while running with fewer available alternatives.

## What it looks like in practice

Engineer's
[email delivery case study](/portfolio/built-email-with-failover-and-delivery-logging/)
describes three providers behind one interface with automatic failover. The case
also records sends and provider delivery events. Together those records help
distinguish getting a request accepted from learning what ultimately happened to
it. The meaningful drill is to interrupt the preferred route and make sure the
application can explain which provider handled a specific message.

An illustrative test sends a message with a stable operation identifier, makes
the initial route fail, and checks the alternate route and recorded outcome.
Test an ambiguous timeout too. The first provider might have accepted the
message before the application lost the response. Track the operation through
the available evidence. Where the external service cannot resolve the ambiguity
immediately, the application needs a documented policy for retry, investigation
or status reporting.

A database failover drill has a different sequence. Record which copy is
authoritative, interrupt the primary and observe when a replacement can serve a
representative read and write. Check which transactions are present and whether
the old primary remains fenced on reconnect. Then restore the number of replicas
the service should have. Make the results visible to people who will have to
repeat the procedure under pressure.

## Where it goes wrong

A common oversight is to test that a standby starts and leave client recovery
unmeasured. An application can continue using a stale address or rely on a
socket whose existing connection is no longer useful. Observe a real request at
the normal entry point. Record the start of the interruption, the first
successful operation after recovery and the data the successful operation
returned.

Be wary of failover paths with common dependencies. Two application routes might
depend on the same credentials service, network path or storage. An outage can
affect both even when their software runs on different machines. Exercise the
shared failure points and make the replacement path easy to inspect. Keep its
configuration current and include its actual capacity in a test.

Finally, require an outcome for work already in flight. The primary may have
completed an operation that the client could not confirm. The replacement may be
unable to reconstruct that work with the information it has. Stable operation
identifiers, durable evidence and a deliberately chosen recovery point help
responders keep track. After the drill, state which operations succeeded, which
requests should be repeated and which outcomes require review. That record makes
failover measurable in the terms users care about.

## Further reading

- [PostgreSQL failover](https://www.postgresql.org/docs/18/warm-standby-failover.html).
  Describes standby promotion and checks around the former primary.
- [Azure SQL failover groups](https://learn.microsoft.com/en-us/azure/azure-sql/database/failover-group-sql-db).
  Documents failover policies and connection behaviour for a managed service.
- [The etcd operations guide for failures](https://etcd.io/docs/v3.6/op-guide/failures/).
  Illustrates quorum-dependent availability and recovery choices.

<https://engineer.company/glossary/failover/>
