DB Guardian - Database Availability System
Purpose
The DB Guardian system ensures that services across the infrastructure are aware of the availability of centralised databases hosted on the Database Coordinator. It provides graceful startup and shutdown coordination between the database host and dependent services on other servers.
Entry Point
- Main file:
modules/nixos/server/database/guardian.nix
Architecture / Services / Scope
The system consists of two components:
-
Guardian Server (runs on client servers)
- WebSocket-over-TLS server that listens for commands from the coordinator
- Requires a client certificate issued by the DB Guardian intermediate CA and only accepts the common names in
server.database.guardian.allowedClientCommonNames - Runs as the unprivileged
db-guardianuser; a Polkit rule grants it start and stop ondb-databases.targetonly - Executes drain/undrain commands by controlling
db-databases.target
-
Guardian Client (runs on the Database Coordinator)
- WebSocket client that connects to all guardian servers over
wss:// - Verifies each guardian’s certificate chain and hostname, and presents the single
db-guardian-coordinatorclient certificate - Sends
undraincommand after databases are online (start dependent services) - Sends
draincommand before database shutdown (stop dependent services)
- WebSocket client that connects to all guardian servers over
How It Works
System Startup
- Client servers boot and run
wait-for-db-databases.service - This service waits (with retries) until PostgreSQL and Redis on the Database Coordinator are reachable
- Once databases are confirmed available, the service completes
- The
db-databases.targetis now ready to be activated - When the Database Coordinator’s
db-database-coordinator.servicestarts, it sendsundrainto all clients - Clients start
db-databases.target, which starts all dependent services - If any client is unreachable, the coordinator fails and retries every 30 seconds until every client acknowledges the
undrain
Database Shutdown (Graceful Drain)
- When
db-database-coordinator.servicestops (before databases stop) - It connects to all guardian servers via WebSocket
- Sends
draincommand to each server - Guardian servers stop
db-databases.target - Dependent services stop gracefully before databases go down
db-database-coordinator.service is bound to PostgreSQL and Redis.
A database stop or restart therefore stops the coordinator and triggers the drain.
The coordinator is started again whenever either database starts, so a database restart cannot leave clients drained indefinitely.
Database Startup (Undrain)
- When databases come online on the Database Coordinator
db-database-coordinator.servicestarts- It sends
undraincommand to all guardian servers - Guardian servers start
db-databases.target - All dependent services start
If a guardian server is not reachable yet, the coordinator exits non-zero and systemd retries it every 30 seconds. This closes the race where a client boots slower than the coordinator and would otherwise stay drained.
Reconciliation
While db-database-coordinator.service is active, db-database-coordinator-reconcile.timer re-sends undrain every 5 minutes.
The command is idempotent, so clients that are already undrained are unaffected.
This covers failures that happen after the initial undrain succeeded:
- A client boots while the databases are down, so its readiness check times out and its target fails. When the databases return, the next reconciliation starts the target again.
- A client’s readiness check is restarted and fails, stopping the target. The next reconciliation starts it again.
- A client’s target is stopped for any other reason while the coordinator is active.
The timer is started and stopped with the coordinator. An intentional drain stops the coordinator, which stops the timer, so reconciliation cannot undo the drain. If a reconciliation run fails, the timer retries at the next interval.
Systemd Units
On Client Servers
| Unit | Type | Description |
|---|---|---|
db-guardian.service | simple | WebSocket server for receiving commands |
db-databases.target | target | Represents “databases are online” |
wait-for-db-primary.service | oneshot | Waits for the database primary host to be reachable |
wait-for-db-databases.service | oneshot | Waits for databases at boot (runs once) |
The target is bound to wait-for-db-databases.service and is also wanted by it.
Restarting the readiness check therefore restarts the target instead of leaving it inactive.
On the Database Coordinator
| Unit | Type | Description |
|---|---|---|
db-database-coordinator.service | oneshot | Sends undrain on start, drain on stop; retries failed undrains |
db-database-coordinator-reconcile.service | oneshot | Re-sends undrain to all clients |
db-database-coordinator-reconcile.timer | timer | Runs reconciliation every 5 minutes while the coordinator is active |
Protocol Reference
The guardian uses a JSON-based WebSocket protocol over mutual TLS. The TLS handshake authenticates both ends, so the first frame on a connection is a command:
Commands
Coordinator sends one of the supported actions (drain, undrain, or ping):
{ "type": "command", "action": "drain" }
Server responds:
{ "type": "response", "action": "<action>", "status": "ok", "message": "..." }
A client that sends any other message type receives an error response naming the type, and no command is executed.
That is how a stale client that still opens with an authentication frame is diagnosed.
Secrets
Connections are mutually authenticated with TLS. There is no shared secret in the protocol: the coordinator proves its identity with a client certificate, and the guardian proves its own with a per-host server certificate. Both sides trust the DB Guardian Intermediate CA and nothing else, so a certificate issued by another service’s intermediate is rejected during the handshake.
| Identity | Leaf profile | Issued to | Allowed by the guardian |
|---|---|---|---|
<host> guardian server | serverAuth | Every guardian host, one leaf each | n/a |
db-guardian-coordinator | clientAuth | The database primary host only | Common name allowlist |
The guardian rejects a connection whose client certificate common name is not in server.database.guardian.allowedClientCommonNames, and refuses to start at all when that list is empty.
An empty list is a Nix evaluation error, so a misconfigured generation cannot be built.
Declared secrets
Certificate material is stored as binary SOPS files and passed to the units as systemd credentials. No secret is a YAML value.
| Secret key | Owner | Group | Restart unit | Purpose |
|---|---|---|---|---|
db-guardian/CA_CERT | root | root | db-guardian.service (clients), db-database-coordinator.service (coordinator) | Intermediate CA certificate used as the trust store |
db-guardian/SERVER_CERT | root | root | db-guardian.service | Server leaf and intermediate chain |
db-guardian/SERVER_KEY | root | root | db-guardian.service | Server leaf private key |
db-guardian/CLIENT_CERT | root | root | db-database-coordinator.service | Coordinator leaf and intermediate chain |
db-guardian/CLIENT_KEY | root | root | db-database-coordinator.service | Coordinator leaf private key |
The files live in hosts/server/<host>/db-guardian-tls/ and are encrypted to that host’s recipients only, so a guardian cannot decrypt another guardian’s key.
Issue and rotate them with the procedure in Internal mTLS CA.
Provisioning certificates
The provision-db-guardian-certs helper issues the guardian leaves and SOPS-encrypts them into hosts/server/<host>/db-guardian-tls/.
It reads the DB Guardian intermediate certificate from a KeePassXC vault and writes it to the work directory.
The intermediate private key is streamed from the vault into memory and passed to OpenSSL on standard input, so it is never written to disk.
The work directory still holds plaintext leaf keys until they are encrypted; protect it, and note that a --work-dir you supply is not removed.
Create the intermediate with the offline Fleet root key first, as described in Internal mTLS CA; the helper only issues leaves.
provision-db-guardian-certs --vault /path/to/fleet.kdbx
The guardian hosts and the database primary are derived from the flake.
Every non-primary server with PostgreSQL or Redis databases receives CA_CERT, SERVER_CERT, and SERVER_KEY; the database primary receives CA_CERT, CLIENT_CERT, and CLIENT_KEY.
Pass host names to restrict the guardian set, --skip-client to leave the coordinator certificate alone, or --force to rotate.
Pass --vault-password-file to avoid an interactive prompt.
A key-file-only vault needs both --vault-key-file and --vault-no-password; --vault-key-file alone still prompts for the password.
The helper selects the KeePassXC entry intermediate/db-guardian/DB Guardian Intermediate CA and reads the cert.pem and key.pem attachments.
Override the entry with --intermediate-entry, or the attachment names with --cert-attachment and --key-attachment.
Whatever the layout, the helper rejects a self-signed certificate or another service’s intermediate before issuing anything.
Operational Notes / Assumptions
Configuration
Port: The guardian WebSocket server listens on port 9876 by default. This port is automatically opened to local subnets on servers with database dependencies.
Dependent Services: Dependent Services will be automatically populated with service names where
there is a systemd.service.<name> defined from the names in server.database.postgres or server.database.redis.
To manually add a service bind to the database availability target, add it to the server.database.dependentServices option.
Services listed here will start only when db-databases.target is active, stop when it stops, and restart when the target restarts.
Troubleshooting
Checking Guardian Status
On client servers:
systemctl status db-guardian.service
systemctl status db-databases.target
systemctl status wait-for-db-databases.service
journalctl -u db-guardian.service -f
On the Database Coordinator:
systemctl status db-database-coordinator.service
journalctl -u db-database-coordinator.service
Manual Commands
To manually start/stop dependent services on a client:
systemctl start db-databases.target
systemctl stop db-databases.target
Common Issues
Guardian server won’t start:
- Check that the certificate secrets decrypted and are non-empty:
for f in /run/secrets/db-guardian/*; do test -s "$f" || echo "empty: $f"; done - Check that at least one client common name is configured:
systemctl show db-guardian.service -p ExecStart - A startup line
Refusing to start: at least one --allowed-cn is requiredmeans the allowlist came through empty.
Connections fail with a certificate error:
certificate verify failed: unable to get local issuer certificateon the guardian means the coordinator’s chain does not reach the DB Guardian intermediate CA; confirmCLIENT_CERTcarries the leaf followed by the intermediate.Hostname mismatchorIP address mismatchon the coordinator means the dialled name is missing from the guardian’s server certificate; the SAN set must contain the bare hostname used in--hosts.certificate has expiredmeans the leaf is past its validity; reissue it with the procedure in Internal mTLS CA.
Guardian rejects the client with certificate common name ... is not in the allowlist:
- The client certificate common name is not listed in
server.database.guardian.allowedClientCommonNames; the observed name is in the log line.
Services not starting after boot:
- Check wait service logs:
journalctl -u wait-for-db-databases.service - Verify network connectivity to the Database Coordinator on ports 5432 (Postgres) and 6379 (Redis)
- Ensure the Database Coordinator has sent the undrain command
- Check the coordinator is active:
systemctl status db-database-coordinator.service - If the coordinator is inactive, start it:
systemctl start db-database-coordinator.service
Clients stay drained after a database restart:
- Check the coordinator’s last start and stop times:
systemctl show db-database-coordinator.service -p ActiveEnterTimestamp,InactiveEnterTimestamp,Result - Check for repeated undrain attempts:
journalctl -u db-database-coordinator.service - Check the reconciliation timer:
systemctl status db-database-coordinator-reconcile.timer - Check reconciliation runs:
journalctl -u db-database-coordinator-reconcile.service - The coordinator is restarted by
postgresql.serviceandredis.service, retries failed undrains every 30 seconds, and re-asserts undrain every 5 minutes while active
Drain or undrain fails with a Polkit error:
Access deniedor anorg.freedesktop.systemd1.manage-unitsdenial injournalctl -u db-guardian.servicemeans the Polkit rule did not match; confirm the daemon runs asdb-guardianand that the request namesdb-databases.targetwith verbstartorstop.journalctl -u polkitrecords the action, unit and verb the rule evaluated.