Setup High Availability Fail Over

Setup High Availability Fail Over

Status: applies to OpenELIS Global 3.x installed with a Docker Compose distro. Scripts tested on Oct 2, 2026 with openelis-distro-png (main @ e11b3a4, OpenELIS :develop images, PostgreSQL 14.24) on three VMs. The other Compose distros (openelis-docker, Madagascar, Indonesia, the distro template) use the same service and container names and should work the same way, but test on yours first. Replaces the 2024 procedure, which targeted the old Python installer.

Site runbooks that follow this page: CPHL High Availability Fail Over (Papua New Guinea). Record your own site's server names, addresses and open decisions on your country page, and keep this page generic.

What this gives you

High availability here means a second (and third) copy of the OpenELIS database that is never more than a moment behind, and a tested way to switch to it. It uses PostgreSQL streaming replication.

  • Primary: the one server that runs the full OpenELIS stack and accepts writes.

  • Standby: a server that runs only the database, continuously copying the primary. You can have several. A common layout is:

    • an on-site standby in the same building, on the same LAN, for fast failover when the primary hardware fails;

    • an off-site standby (a cloud VPS or a second building) for disaster recovery. It can also be the VPN hub for remote access and host a nightly snapshot copy (below).

  • OpenELIS cannot run against a read-only standby. To use a standby you promote it to primary, and the old primary must be fenced (stopped) first. Never run two writable servers.

  • Replication is asynchronous. On an unplanned failover you lose whatever had not reached the standby yet: under a second on a LAN, longer over a slow WAN link.

  • Replication is not a backup. A record deleted on the primary is deleted on every standby within a second. Keep nightly scripts/backup.sh backups.

  • Failover is manual and deliberate: a person decides, runs one script, and follows the "after failover" checklist. Nothing switches automatically.

What was tested

Drill

Result

Drill

Result

Move production from one server to another (old server becomes a standby)

OK, no data loss; rejoin needed no rewind

On-site standby over the LAN, off-site standby over WireGuard

both streaming, 0 bytes behind

Unplanned failover: primary hard power-off, promote on-site standby, re-point off-site standby

OK, last row written before the crash present

Old primary boots as a primary after a failover

boot guard fenced it in about 8 s; rejoin rewound 53 MB

Old primary cut off from the network (not rebooted) while a standby was promoted

periodic guard fenced it within 40 s of reconnecting

Off-site link down while 50,000 rows were written

slot held the WAL, status.sh --check alerted, caught up in 20 s after the link returned

Full-stack planned switchover and failback

webapp healthy in about 2 min, admin login works, no configuration CSVs reloaded

Real-divergence rewind immediately after a promotion

OK

Nightly snapshot copy on the off-site server

rebuilt in 2 to 5 min, warning banner shown, login works, outbound integrations off

Reboot ordering

PostgreSQL binds to the WireGuard address after a reboot

Scripts

All in scripts/ha/ of the distro, run as root from the distro directory (for example sudo ./scripts/ha/status.sh). See Getting the scripts if your distro does not have them yet.

Script

Run on

What it does

Script

Run on

What it does

primary-init.sh [--restart]

primary

replicator role, WAL settings, pg_hba for cluster members, replication slots

standby-init.sh <primary>

new standby

full copy with pg_basebackup, starts the database only, as a standby

status.sh [--check]

any

role, timeline, standbys, lag, slots; --check is quiet and exits 1 on a problem, for cron

fence.sh

old primary

stops and removes every container so the server cannot accept writes

promote.sh [--force] [--db-only]

a standby

promotes it, creates slots, starts OpenELIS, rebuilds the search index; safe to re-run

rejoin.sh <new primary>

old primary, other standbys

pg_rewind and follow the new primary; falls back to a full copy

config-sync.sh push / restore

primary (cron) / promote

carries OpenELIS's configuration checksum files to the standbys through the database

install-guard.sh

every server

split-brain guard at boot and every 2 minutes; makes Docker wait for WireGuard

snapshot-refresh.sh [--stop]

off-site server

rebuilds the nightly snapshot copy

Checklist A: before you start (every server)

Every server runs the same distro release tag (record it).
.env is identical on every server for OE_DB_PASSWORD, ADMIN_PASSWORD, OE_ADMIN_PASSWORD and SSL_*. A standby carries the primary's database, including its passwords.
The stock database passwords are changed (OE_DB_PASSWORD, ADMIN_PASSWORD, and POSTGRES_PASSWORD in configs/database/database.env) before port 5432 is published anywhere.
Disk: the primary has room for HA_MAX_SLOT_WAL_KEEP (default 20 GB) on top of the database.
Docker images are already on every server (about 2 GB). On a slow link pull once and copy across the LAN: docker save --platform linux/amd64 <image> | ssh <server> docker load.
Servers can reach each other on port 5432: directly on the LAN, and over WireGuard (Checklist B) for anything off-site or behind NAT.
The HA block is in .env on each server, and compose.ha.yaml and scripts/ha/ are present.
sudo ./scripts/ha/install-guard.sh has been run on every server, then sudo sync (or reboot once).
You run promote.sh, rejoin.sh and standby-init.sh inside tmux or screen, so a dropped SSH session does not stop them.
# --- High availability (scripts/ha) --- COMPOSE_FILE=docker-compose.yml:compose.ha.yaml # add :compose.letsencrypt.yaml if you use it HA_NODE=site-a # this server's name in the cluster # every server: name=address[,address] (WireGuard address first, then LAN address) HA_MEMBERS="offsite=10.88.0.1 site-a=10.88.0.2,192.168.1.10 site-b=10.88.0.3,192.168.1.11" HA_PG_BIND_WG=10.88.0.2 # this server's WireGuard address HA_PG_BIND_LAN=192.168.1.10 # this server's LAN address (off-site server: 127.0.0.1) HA_REPLICATION_PASSWORD=<long random value, same on all servers>

List only servers that exist: the primary keeps a replication slot, and WAL, for every member in HA_MEMBERS. If a site has no off-site server and no WireGuard, set HA_PG_BIND_WG to the LAN address and leave HA_PG_BIND_LAN at 127.0.0.1.

Checklist B: WireGuard (off-site server as the hub)

Use this when a standby is off-site or the primary is behind NAT. Every on-site server, and every remote user, connects out to the off-site server, so nothing needs to be opened inbound at the site.

Off-site server firewall / cloud security group: allow UDP 51820 in. Nothing else new.
Each machine: umask 077; wg genkey | sudo tee /etc/wireguard/privatekey | wg pubkey | sudo tee /etc/wireguard/publickey
Off-site server /etc/wireguard/wg0.conf:
[Interface] Address = 10.88.0.1/24 ListenPort = 51820 PrivateKey = <off-site private key> # Docker sets the FORWARD policy to DROP; without these lines the hub cannot # relay remote users to the on-site servers PostUp = iptables -I FORWARD -i wg0 -o wg0 -j ACCEPT PostDown = iptables -D FORWARD -i wg0 -o wg0 -j ACCEPT [Peer] # site-a PublicKey = <site-a public key> AllowedIPs = 10.88.0.2/32 [Peer] # site-b PublicKey = <site-b public key> AllowedIPs = 10.88.0.3/32 [Peer] # remote user (one block per person or device, 10.88.0.10 and up) PublicKey = <user public key> AllowedIPs = 10.88.0.10/32
Off-site server: echo net.ipv4.ip_forward=1 | sudo tee /etc/sysctl.d/90-wg.conf && sudo sysctl -p /etc/sysctl.d/90-wg.conf
On-site servers and remote users /etc/wireguard/wg0.conf:
[Interface] Address = 10.88.0.2/24 # .3 for site-b, .10 and up for users PrivateKey = <this machine's private key> [Peer] PublicKey = <off-site public key> Endpoint = <off-site public IP>:51820 AllowedIPs = 10.88.0.0/24 PersistentKeepalive = 25 # keeps the NAT mapping open
Every server: sudo systemctl enable --now wg-quick@wg0
Test from site-b: ping 10.88.0.1 (off-site) and ping 10.88.0.2 (site-a, relayed through the hub).
  • After WireGuard restarts on the hub, peers can take up to 2 minutes to reconnect.

  • Remote users reach the primary at its WireGuard address (or a name that resolves to it).

Checklist C: build the cluster

Option 1: the current production server stays the primary

  1. ☐ Primary: add the HA block to .env, deploy compose.ha.yaml and scripts/ha/, then docker compose up -d (publishes 5432 on the chosen addresses only).

  2. ☐ Primary: sudo ./scripts/ha/primary-init.sh, then in a quiet moment sudo ./scripts/ha/primary-init.sh --restart (database restart of a few seconds; the webapp reconnects by itself).

  3. ☐ Each standby: install the same release, same .env and HA block, then sudo ./scripts/ha/standby-init.sh <primary address>. On-site standbys use the primary's LAN address; off-site standbys its WireGuard address.

  4. ☐ Primary: sudo ./scripts/ha/status.sh shows every standby streaming, 0 bytes behind, all slots active.

Option 2: move production to a different server at the same time

For example from a cloud server to an on-site server. The new server first becomes a standby of the current one, then you switch roles in a short window. This doubles as your first failover rehearsal.

  1. ☐ Current server: steps 1 and 2 of Option 1.

  2. ☐ New server: sudo ./scripts/ha/standby-init.sh <current server address>. This is a full copy of the database; over a slow link, check its size first (du -sh the database directory) and allow time.

  3. ☐ Current server: status.sh shows the new server streaming, 0 bytes.

  4. ☐ Announce a short window. Current server: sudo ./scripts/ha/fence.sh.

  5. ☐ New server: sudo ./scripts/ha/promote.sh. Wait for "webapp healthy".

  6. ☐ Checklist F (users, analyzers, interfaces, certificates). Log in and check that the last sample entered before the window is there.

  7. ☐ Old server: sudo ./scripts/ha/rejoin.sh <new server address>. It becomes a standby.

  8. ☐ Add any further standbys with standby-init.sh <new server address>.

Checklist D: monitoring and backups (every server)

Replace /opt/openelis with your distro directory.

Cron, every 5 minutes, alerting on failure (mail or your monitoring agent): */5 * * * * root /opt/openelis/scripts/ha/status.sh --check || <alert command>. It reports: a standby not streaming, a disconnected slot, a standby silent for 2 minutes, a slot holding more than 2 GB of WAL, the webapp down on the primary, a webapp container on a standby.
Cron, every 10 minutes: */10 * * * * root /opt/openelis/scripts/ha/config-sync.sh push (does nothing on standbys). It stores OpenELIS's configuration checksum files in the database so a promoted standby does not reload every CSV and overwrite catalog changes made in the UI.
Nightly backup on whichever server is primary (replication is not a backup): 30 1 * * * root cd /opt/openelis && [ "$(docker compose exec -T db.openelis.org psql -U postgres -Atc 'select pg_is_in_recovery()')" = f ] && ./scripts/backup.sh /srv/oe-backups. Copy /srv/oe-backups off the server. It contains .env; keep it mode 0700.

Checklist E: nightly snapshot copy (optional, off-site server)

A second, complete, running OpenELIS on the off-site server (https://<off-site server>:9443/, reachable over the VPN), rebuilt every night from that server's standby. Useful for looking up results and running reports when the site is offline, and for training and demos.

Anything entered in the snapshot is deleted at the next rebuild and is never sent to the live system. Do not use it to enter or change data. Every page shows a red banner saying so.

The off-site server has memory for the standby database plus a full stack (plan 8 GB or more).
First build: sudo ./scripts/ha/snapshot-refresh.sh (about 10 minutes the first time). Check the banner and log in.
Cron: 0 2 * * * root /opt/openelis/scripts/ha/snapshot-refresh.sh >> /var/log/oesnap.log 2>&1
Tell users the snapshot address, that its data is as of 02:00, and the rule above.
  • Switched off in the snapshot: analyzer bridge, FHIR subscriber and remote-source push and poll, client registry, facility and provider list sync, Odoo, freezer monitoring polling. Check any email or SMS notification settings your site uses and switch those off too.

  • promote.sh stops the snapshot automatically if the off-site server is ever promoted.

Checklist F: after ANY failover

Users: tell them the new address, or move the DNS name or service IP.
Analyzers: ASTM and HL7 instruments send to the analyzer bridge on the primary (port 12000). Re-point them, or use an address that moves with the primary. File-drop folders are local to each server.
Interfaces: EMR, consolidated server and other FHIR endpoints that call OpenELIS.
Certificates: the new primary needs a valid certificate for the name users use.
Search index: promote.sh starts a rebuild; patient search can be incomplete for a few minutes.
Number gaps: sequence-generated numbers can jump by up to 32 after a promotion (normal PostgreSQL behaviour).
Rejoin the other servers: sudo ./scripts/ha/rejoin.sh <new primary> on each, then status.sh on the primary.
Backups: confirm the nightly backup now runs on the new primary.

Procedure: unplanned failover (primary is down)

  1. Confirm the primary is really down (power, network, docker compose ps). If it can be fixed in minutes, fix it instead: a failover has a cost.

  2. If you can reach it at all, run sudo ./scripts/ha/fence.sh on it. If you cannot, make sure it cannot quietly come back: the guard fences it automatically if it comes up while another server is primary on a newer timeline, but only if it can reach that server.

  3. On the chosen standby (the on-site one first; the off-site one if the whole site is down): sudo ./scripts/ha/promote.sh. If it refuses because the old primary still answers, go back to step 2. Use --force only when you are certain the old primary is down.

  4. Checklist F.

  5. When the old primary is repaired: sudo ./scripts/ha/rejoin.sh <new primary>. It never comes back as primary by itself.

Procedure: planned switchover or failback

  1. Current primary: sudo ./scripts/ha/fence.sh (a clean shutdown sends the last WAL to every standby).

  2. Target: sudo ./scripts/ha/promote.sh.

  3. Old primary and the other standbys: sudo ./scripts/ha/rejoin.sh <new primary>.

  4. Checklist F.

Decisions to make for your site

  1. HTTPS on servers without a public port 80. Let's Encrypt HTTP-01 needs port 80 reachable from the internet. For on-site servers behind NAT use DNS-01, a certificate from your organisation's own process, or have the off-site server obtain the certificate and copy it to the others.

  2. How users find the primary after a failover: one DNS name with a short TTL that you move, or a service IP on the LAN that you move to the new primary. A moving address also saves re-pointing analyzers.

  3. Notifications in the snapshot: list the email and SMS settings your site uses so they can be switched off in the snapshot.

  4. Sizing: database size (first copy over the WAN), WAL headroom on the primary, memory on the off-site server.

  5. Who watches the alerts from status.sh --check, and who is allowed to run promote.sh.

Troubleshooting

Symptom

Cause / fix

Symptom

Cause / fix

standby-init.sh: "does not answer as a primary (down)"

Network path down, the standby's address missing from HA_MEMBERS on the primary (re-run primary-init.sh), or a wrong HA_REPLICATION_PASSWORD.

password authentication failed for user "replicator"

Password stored as md5. Re-run primary-init.sh; it stores it as SCRAM.

promote.sh: "old primary still answers as a PRIMARY"

Working as intended. Fence the old primary first.

promote.sh interrupted (SSH dropped)

Re-run it; it finishes the remaining steps.

rejoin.sh falls back to a full copy

pg_rewind could not reuse the data. Over a slow WAN a full copy takes time; let it run in tmux.

Database container fails to bind the WireGuard address after a reboot

WireGuard was not up before Docker. Re-run install-guard.sh (adds the ordering), then systemctl restart docker.

Remote users cannot reach the on-site server through the hub

The hub's PostUp FORWARD rule is missing (Docker's FORWARD policy is DROP).

Host firewall rules (ufw, iptables INPUT) do not block 5432 or 443

Docker-published ports bypass INPUT. Bind to specific addresses (compose.ha.yaml does) or use the DOCKER-USER chain.

Primary disk filling with WAL

A standby is disconnected and its slot holds WAL (capped by HA_MAX_SLOT_WAL_KEEP). Fix the standby, or remove it from HA_MEMBERS and drop its slot: SELECT pg_drop_replication_slot('ha_<name>').

Snapshot webapp never healthy: no pg_hba.conf entry for host "172.21.1.x"

Old copy of snapshot-refresh.sh; the current version rewrites the snapshot's pg_hba.

Getting the scripts

Source of truth: scripts/ha/ and compose.ha.yaml in openelis-distro-png, https://github.com/DIGI-UW/openelis-distro-png/pull/10 (branch feat/ha-failover until merged). The tested versions are at commit 9600885:

Until the PR is merged and released, copy them into your distro checkout (works from any distro):

cd /opt/openelis # your distro directory git fetch https://github.com/DIGI-UW/openelis-distro-png.git feat/ha-failover git checkout FETCH_HEAD -- scripts/ha compose.ha.yaml sudo chmod 755 scripts/ha/*.sh

Legacy procedure (old Python installer, 2024)

Kept for reference only. It targets the old setup_OpenELIS.py layout (/var/lib/openelis-global, DB on host port 5432, a backup replication role) and does not work with the Docker Compose distros. Known problems: the "wait for healthy" loop never waits, re-running fails, pg_basebackup -W forces a password prompt, passwords are passed on the command line, the whole live data directory is copied twice, and there is no fencing step.

Overview There are certain situations, particularly in reference labs where the LIS needs to be able to have a high availability failover setup. This kind of setup can minimize downtime in case of any issues, and ensure that a backup is always ready to go in case it’s needed. Because the goal is to ensure as close to 100% uptime as possible, please be sure each OpenELIS instance is hosted on a different physical server. The best practice is to have the server on the same network, but in a different physical location if possible. Prerequisites You will need the following in order to successfully setup your failover. 2 servers capable of running OpenELIS Primary server Secondary server Visibility between 2 servers Everything required for regular OE setup Instructions Setup OpenELIS on the primary server as per normal install/upgrade procedures ensuring the db is listening on port 5432 Setup OpenELIS on the secondary server as per normal install/upgrade procedures ensuring the db is listening on port 5432 Run scripts/configurePrimary.sh on the Primary server Enter the ip address/dns entry that the primary server sits at. The secondary server must be able to reach the primary server at this address on port 5432 Enter the ip address/dns entry that the secondary server sits at. The primary server must be able to reach the secondary server at this address on port 22 for ssh Enter a user that has sudo permissions on the secondary server Enter the password for the user on the secondary server Let the script run, it may look like further passwords are prompted for, but they should be automatically entered by the script Ensure that OpenELIS is started on the Primary server Ensure that information that is entered into the db on the Primary appears in the db on the Secondary. Notes OpenELIS and the other containers aside from the database will not be running on the secondary server at the end of this workflow. This is intentional as the DB is running in standby mode and will reject any updates other than from the primary which disrupts OpenELIS from running. The entire OpenLEIS install directory (/var/lib/openelis-global) is copied over from the primary to the secondary. This will ensure the all config changes are populated to the secondary AT THE TIME THIS WORKFLOW IS RUN. Later changes will need to be done on both servers OR the workflow can be re-run to populate the differences. The etc directory (/etc/openelis-global) is NOT copied over, so ensure that certs are well set up on both servers. It is recommended to use the same keystore, and truststore on both servers, but to use a different client_facing_keystore IF the original key/cert does not cover the secondary server’s ip/DNS address. Executing Failover Prerequisites Minimum 2 servers capable of running OpenELIS Original Primary server (target/new Secondary server or decommissioned) Original Secondary server (target/new Primary server) New Secondary Server (if Primary is decommissioned) Visibility between target Primary and target Secondary servers Everything required for regular OE setup Instructions Confirm that the Original Primary server is down. Try restarting the Primary to see if that fixes the issue. cd /var/lib/openelsi-global sudo docker-compose restart If that doesn’t work, but only some services are down, bring them all down by running 3. cd /var/lib/openelsi-global 4. sudo docker-compose down Setup OpenELIS on the new secondary server as per normal install/upgrade procedures ensuring the db is listening on port 5432 IF using a new secondary server Run scripts/configurePrimary.sh on the target primary server 5. Enter the ip address/dns entry that the target primary server sits at. The target secondary server must be able to reach the target primary server at this address on port 5432 6. Enter the ip address/dns entry that the target secondary server sits at. The target primary server must be able to reach the target secondary server at this address on port 22 for ssh 7. Enter a user that has sudo permissions on the target secondary server 8. Enter the password for the user on the target secondary server Let the script run, it may look like further passwords are prompted for, but they should be automatically entered by the script Ensure that OpenELIS is started on the new Primary server Ensure that information that is entered into the db on the new Primary appears in the db on the new Secondary. Notes The same notes from failover setup also apply to this workflow If a server capable of being the new secondary doesn't exist yet, but one still wants to promote an original Secondary server to be the new Primary server, run the above instructions but leave out instructions 4b, 4c, 4d, and 7. Once a new Secondary has been allocated, rerun the Failover setup workflow.