Skip to content

High availability: a second server

Argavon exists to wake someone up, so the server that sends pages should not be the one thing that can stop pages. On one VPS it is. This page explains the two-server setup in deploy/vps: what it protects, how long a failover takes, what you can lose, and how to prove every month that it still works. The steps to set it up are in deploy/vps/README.md, section "Second host".

What runs where

Primary Standby
Postgres takes reads and writes, streams every change to the standby over TLS a hot standby: replays the changes, read-only
Backend, web, Caddy running, sending pages pulled for the current release, stopped
failover-watch.sh optional, checks the primary every 10 seconds

All state that matters for paging lives in Postgres: incidents, deliveries and their leases, escalations, on-call schedules, sessions, correlation state. Nothing is kept only in a backend's memory, so a backend started on the promoted database carries on where the old one stopped.

What fails over

  • The primary machine dies (hardware, the hypervisor, its data centre, a kernel panic). The standby is promoted, by you with failover.sh or by the watcher, and starts the app. Pages that were waiting go out within 5 seconds of the backend starting (the dispatcher runs every 5 seconds). Pages the old primary had claimed but not finished are sent again once their 2 minute lease runs out. Escalations continue from the step they were on.
  • The standby dies. Nothing changes for users. The primary keeps WAL for it, at most 8 GB (max_slot_wal_keep_size), so a dead standby can never fill the primary's disk. If it was gone long enough to fall past that, rebuild it with replication.sh standby --wipe. Set FAILOVER_WATCH_PING_URL so you hear about it.

What does not fail over by itself:

  • A primary that is up but broken: its backend crashed, its disk is full, its database is hung while the machine still answers. The watcher sees the primary answering on its ports and holds back on purpose (below). WATCHDOG_URL pages you about these, and you decide: fix it, or stop it and run failover.sh.
  • DNS, unless you set FAILOVER_DNS_CMD. Hostinger VPS plans have no floating IP, so traffic moves by changing the domain's A record.

How the watcher decides

Every 10 seconds the standby looks at two things:

  1. the primary's /readyz, asked over HTTPS at the primary's own address;
  2. its own replication heartbeat: is the standby still streaming from the primary, and did it hear from it in the last 30 seconds?

It promotes only when both have failed 6 times in a row and nothing answers on the primary's ports 22, 443 and 5432. If the ports still answer, it logs "fenced" and waits for a person.

Why both, and why the fence: the worst outcome is not a slow failover, it is two servers that both think they are the primary. Each would send pages from its own copy of the data: people get paged twice, an acknowledgement on one does not stop escalation on the other. Requiring both signals means a backend crash alone (readyz fails, replication fine) or a broken link between the servers alone (replication fails, readyz fine) never promotes. Requiring silence on the ports means a machine that is still alive is never promoted over. The cost is that some real failures wait for a person.

One case can still fool it: the network between the two servers breaks while users can still reach the primary. From the standby everything looks dead. FAILOVER_WITNESS_CMD closes that gap by asking an outside view (for example the healthchecks.io check of the primary's WATCHDOG_URL) before promoting. If you cannot set one up, consider leaving FAILOVER_AUTO off and failing over by hand.

And if an old primary comes back after a failover (its containers restart with the machine), its database asks the other server whether it is a primary before it starts. If yes, it refuses to start, and the backend on it never comes up. It only starts unchecked when it cannot reach the other server at all.

How long it takes

Measured on a laptop on 2026-11-11 with deploy/vps/ha_live_test.sh: two Postgres 16 containers in streaming replication, the primary taken away, the watcher promoting through failover.sh. "Killed" means the primary's processes die and its connections drop at once (a crash). "Cut off" means its network disappears without a word (a power cut, a dead switch), so the standby only notices when replication times out after 20 seconds.

Measured
A committed write visible on the standby 0.16 to 1.4 s (most of it is the test's own polling)
Production defaults (6 checks, 10 s apart), primary killed, until the standby takes writes 59 s
Production defaults, primary cut off, the same 77 s
Fast settings (3 checks, 1 s apart), killed 2.8 to 3.6 s (four runs)
Fast settings, cut off 24 s
The fence check plus pg_promote() inside that 0 to 5 s
Backend start on the promoted database until /readyz answers 0.5 to 2 s in five runs, 14 s in one

So with the defaults, from the primary dying to pages going out again:

  • about 1 to 1.5 minutes until the standby's database takes writes;
  • a few seconds more for the backend, web server and Caddy to start (the images are already there; the local test timed only the backend);
  • then traffic: up to the DNS TTL (60 seconds) for users and agents, and Caddy asks for a certificate as soon as the name resolves to the new server (seconds).

Fewer or faster checks fail over sooner but make a false alarm more likely; a short network blip on the primary's side should not cost you a failover. Six checks ten seconds apart is the default for that reason. These numbers come from one laptop, not two VPSs; time your own monthly drill (below) and keep that number.

Measured with the full stacks

deploy/vps/ha_compose_drill.sh runs both servers as complete compose stacks (Caddy, web, backend, Postgres) on one laptop, set up with configure.sh and replication.sh, sends one alert a second to whichever server "DNS" names (retrying like Alertmanager), and kills the whole primary stack. 2026-11-15, two runs:

Fast watcher (3 checks, 2 s) Production defaults (6 checks, 10 s)
Primary killed → standby database writable 22 s 88 s
Primary killed → app answering through Caddy on the standby 37 s 104 s
Of that: failover.sh promote 1-4 s 4 s
Of that: backend, web and Caddy start, backend ready 15-17 s 17 s
Alerts accepted / found as incidents on the new primary 40 / 40 60 / 60

Each watcher round takes longer than FAILOVER_INTERVAL: the readyz probe of a dead address waits for its 5 s timeout, so with the defaults a round is about 13 s and six rounds about 80 s. Count on about 1.5 to 2 minutes from the primary dying to pages going out from the standby, plus the DNS TTL for traffic coming in. The one alert in flight at the kill waited that long and was then accepted by the standby; none was lost. The sender waited for each answer, so no alert was accepted in the last moment before the kill; the asynchronous-replication window below still applies.

Pages do not wait for DNS: the backend on the new primary sends them as soon as it is up. Agents and webhooks that send alerts reach it once DNS has moved.

Failing over by hand takes as long as it takes you to notice (the WATCHDOG_URL check pages you within its grace period, about 3 minutes) plus about a minute of commands.

What can be lost

Replication is asynchronous: the primary confirms a write before the standby has it. Whatever the primary committed in the last moment before it died and had not yet sent is lost. In the test that window was well under a second; on two VPSs in different data centres expect a similar figure under normal load, more if the link is slow or the primary is very busy. replication.sh status on the primary shows the current replay lag.

In practice that can mean, for the last second or so:

  • an alert that was accepted but never reached the standby: it is gone, and comes back only when its source sends it again (Alertmanager repeats a firing alert at its repeat_interval);
  • an acknowledgement that is lost: the incident shows as unacknowledged and escalation may page again;
  • a page that was sent but whose "sent" mark is lost: it is sent a second time.

So a failover can double a page. It loses only what changed in that last moment. We chose asynchronous replication on purpose: synchronous replication (synchronous_standby_names) loses nothing, but then the primary stops taking writes, and so stops paging, whenever the standby is down or slow. For a pager that is the worse trade.

Backups do not change: the nightly dump still runs from whichever server DEPLOY_HOST names. Point it at the new primary after a failover.

Test it every month

A failover you have never tried is a guess. Once a month, at a quiet hour:

  1. Locally or in CI, deploy/vps/ha_live_test.sh (CI runs it too) proves the scripts still promote, fence and refuse a returning old primary.
  2. On the servers, check replication: deploy/vps/replication.sh status on both. The standby should say "in recovery: t" and a heartbeat under 30 seconds; the primary should list the standby as streaming with a small replay lag.
  3. Fencing: on the standby run deploy/vps/failover.sh while the primary is up. It must refuse with exit code 3 and change nothing.
  4. A real switch (every quarter at least, or after changing anything here): tell your team, then
  5. on the primary: docker compose -f docker-compose.yml -f docker-compose.vps.yml -f docker-compose.primary.yml stop;
  6. on the standby: deploy/vps/failover.sh, note the time it prints, move DNS, and send yourself a test page from an incident;
  7. rebuild the old primary as the new standby ("After a failover" in deploy/vps/README.md). You can switch back the same way, or keep the roles swapped.
  8. Write down the times in your own notes. If a step took longer than this page says, find out why before you need it.