Core Concepts

Failover

Move writes to a standby without letting an old writer continue or silently losing required data.

Free~15 min

Failover moves work from an unavailable component to a replacement. For a database writer, confirm that the replacement has the data the service needs and block writes from the old writer before the new writer accepts them.

Give the new writer authority

In the URL Shortener example, the original PostgreSQL writer is A and its standby is B. A primary accepts writes. A standby follows its data and can be a candidate to take over. Giving the standby permission to act as primary is promotion.

If the controller cannot reach A, A may have crashed or the network path from the controller may have broken. A client on another path may still be reaching it. If A and B both accept writes, each could assign the same custom alias to a different link. This is split brain, two owners making independent decisions for the same data. PostgreSQL guidance on failover

Let a controller with authority over the writer decision coordinate the transfer. It has to fence A, which means preventing it from making protected writes while B serves. One method is an external controller that can stop A and confirm it stays stopped before B starts writing. A design that uses fencing tokens, increasing ownership numbers, must check the token against the active authority at the resource that accepts writes. The old owner's lower number is rejected there. Changing a route, or telling a controller that B holds a new number, does not by itself protect a database that still accepts writes from A.

In the figure, the controller records the end of A's tenure at generation 7 and the start of B's at generation 8 after A is fenced. This is an illustrative controller. The URL Shortener walkthrough requires a fenced, elected writer and lets an API instance with no access to that writer reject a create. The database's replication protocol supplies copies. The controller or managed service must supply the promotion decision and its enforcement. PostgreSQL failover responsibilities

Confirm which writes the standby has

Suppose primary A acknowledges two writes, W101 and W102. W102 creates the mapping for notes. B has durable history through W101. If A stops before transferring W102, a takeover by B can leave the mapping missing. PostgreSQL streaming replication is asynchronous by default, so this gap is possible in deployments that acknowledge writes before a standby receives them. PostgreSQL streaming and synchronous replication

The policy decides whether to wait or accept a loss window. If the service promises to preserve every acknowledged create during this failure, configure acknowledgments to protect those writes and promote only a candidate that holds the required history. Picking the most advanced surviving replica still leaves a gap if no survivor received W102. When the required history cannot be verified, keep writes unavailable while recovering it or finding a qualified standby.

A synchronous replication setting can wait for an appropriate standby acknowledgment before confirming a write. Choose what the standby must have done, such as flushing the relevant log data to its durable storage. Then restrict promotion to a standby with the required history. If the promotion procedure picks a different, lagging standby, the first acknowledgment policy cannot help. The wait adds latency and can prevent new writes from completing when the required standby is unavailable. PostgreSQL synchronous commit settings

Get real callers onto the new path

Once B is the writer, update the route. One caller may use it immediately while another still has an open database connection to A. For this example, fenced A cannot accept those writes. Old connections fail and need replacement. The time until callers use the new route is routing convergence. Test existing connection pools as well as new requests. DNS records may also stay cached, depending on how callers address the service.

A caller may also have sent a create just before the old connection failed. If it lost the response, the outcome is unknown. Retrying with the original idempotency key lets the new writer find the original result when the committed mapping and retry record were preserved. If failover allowed data loss, the retry record may also be gone. The service then needs a way to reconcile that uncertain outcome.

Restore the spare before switching back

If A comes back, keep it fenced from writes. It may have a different database history. Rebuild it from B or use a supported resynchronization procedure, then let it catch up as a standby. PostgreSQL's pg_rewind can help when the old and new servers have the required history and configuration. Switching back to A is another transfer of write authority, so plan and test it separately. PostgreSQL pg_rewind

Exercise both a stopped primary and a network break that leaves the old primary reachable to some clients. Record the last preserved write and the time until requests succeed on the new path. Verify that a client with the stale route cannot write to A. Check that B can carry the actual load and that another standby can take its place later. Promotion gives you one new writer. The drill shows whether users can actually use it.

See also

Used in

  • URL Shortener routes creates to one elected, fenced PostgreSQL writer and rejects creates if the writer is unavailable.
  • ChatGPT checks the Redis reply log's continuity at takeover and falls back to durable turn state when it cannot safely resume replay.

Done reading? Mark it so it sticks in your dashboard.

Discussion