High availability

Stateless active-active nodes over one PostgreSQL primary: finding the primary, leases with fencing, drains, commit stalls, health endpoints, topologies and drills.

On this page

Status. Tested: the HA compose topology (PostgreSQL 18 primary with a synchronous streaming standby, two structam serve nodes, one structam worker, Caddy) on one Docker host. In the drill "promote the standby", readiness through Caddy came back within 1 s of the promotion, every lease was held again and no process restarted; in the drill "stop a serve node", 60 of 60 API requests through Caddy were answered during the drain. The product's automated drills (kill of a serve node, drain, PostgreSQL restart, worker scaling, mixed versions, synchronous commit stall) exist but were not run for these docs. Not yet run: Patroni or CloudNativePG failover, and nodes on separate machines.

How it works

Structam nodes are stateless and active-active. They coordinate only through PostgreSQL rows (leases and queues) and LISTEN/NOTIFY wake-ups; there is no node-to-node protocol and no node to elect. Keeping the data available is PostgreSQL's job: a primary with a synchronous standby under Patroni, CloudNativePG or a managed service. Structam does not replicate or fail over the database itself.

RoleRunsHow many
structam serveAPI, web UI, agent gateway, event stream, a small ingest pool, background tasks2 or more behind a load balancer
structam workeringest, discovery and integration worker pools, background tasks0 or more (scaling)
structam allserve plus full worker poolssingle-node installations
PostgreSQL 16+all dataa primary and a standby, failover by Patroni, CloudNativePG or the provider

Finding the primary

Give every node a connection string that lists every database host, or your operator's writer endpoint:

STRUCTAM_DATABASE_URL=postgres://structam:[email protected]:5432,db2.example.net:5432,db3.example.net:5432/structam?sslmode=verify-full

Structam adds target_session_attrs=primary&connect_timeout=3 when they are missing, so it connects only to the host that accepts writes, and after a failover its next connection finds the new primary. No HAProxy or virtual IP is needed for the database. Connections are recycled every 10 minutes and use TCP keepalives and, on Linux, a 30 s TCP_USER_TIMEOUT, so a connection to a dead primary is noticed in about 30 s.

Leases: one holder per background task

Work that must run on one node at a time (NetBox sync, poll and write-back, InfraDB sync and alarms, the SIEM forwarder, the TAXII publisher, the audit chainer, the licence heartbeat, estate fold and reconcile, retention, blob garbage collection, the discovery scheduler, expiry tasks, the agent-offline evaluator) is guarded by a lease row:

  • a lease lasts 20 s and is renewed every 6 s on a connection reserved for it;
  • a holder whose last successful renewal is older than 15 s stops the task;
  • every acquisition increments the lease's epoch, and writes that must not happen twice check the epoch in their transaction (fencing), so a node that lost its lease cannot overwrite the new holder's work;
  • any node of any role may hold any lease. System → Nodes shows the holders.

When a node dies, its leases expire within 20 s and other nodes take them over on their next attempt.

Agents and jobs survive a node

  • Agents keep one WebSocket to whichever node the load balancer chose. When that node stops they reconnect with backoff (at once when the node drains). Agents queue inventory on disk and the server acknowledges only after commit, so nothing acknowledged is lost, and a message sent but not acknowledged is sent again and recognised as a duplicate.
  • Jobs claimed by a node that dies return to the queue once the claim expires or the node's heartbeat is 45 s old, so they are back within about 50 s. A claim is fenced by a token, so a job is never applied twice.
  • LISTEN/NOTIFY is only a wake-up: every consumer also polls, so a lost notification delays work but never loses it.
  • Nodes write a heartbeat every 10 s; a node unseen for 60 s is stale.

Health endpoints

EndpointAnswers 200 whenUse
GET /livez (alias /healthz)the process's runtime watchdog ticked within 30 s; never touches the databaseliveness: restart only on this
GET /readyzthe primary answers within 2 s, the schema is compatible, the node is not drainingload balancer health checks, readiness

On the worker role both are on STRUCTAM_WORKER_ADDR (:8081). structam healthcheck (with -worker on a worker) checks the node's own /readyz.

Stopping a node

SIGTERM (a systemd stop, docker stop, a pod deletion) drains:

  1. /readyz answers 503 at once, and agent connections are closed with WebSocket code 4001 ("draining"), so agents reconnect elsewhere immediately.
  2. The node keeps serving for STRUCTAM_DRAIN_DELAY (5 s), so the load balancer's health check takes it out of rotation before its listener closes.
  3. In-flight jobs get up to 20 s; unfinished claims return to the queue.
  4. HTTP shuts down within 10 s.

The systemd units and compose files allow 45 s before a forced kill; the Kubernetes serve pods 60 s, with a 5 s preStop sleep before SIGTERM.

Commit stalls

With a synchronous standby, PostgreSQL makes every commit wait for the standby; if the standby stops answering, commits hang. Every node checks pg_stat_activity every 5 s. While backends have waited on SyncRep for more than 5 s, agents are told busy and keep their data on disk, API requests that change something answer 503 busy with Retry-After, and heartbeat writes are skipped. Liveness never depends on the database, so nothing restarts. Fix the standby, or remove it from synchronous_standby_names, and everything resumes on its own. The metric structam_db_sync_commit_stalled is 1 meanwhile.

Cluster settings

Settings marked cluster-wide in the configuration reference must be identical on every node, workers included (for example STRUCTAM_SECRET_KEY, STRUCTAM_PUBLIC_URL, STRUCTAM_TRUSTED_PROXIES and the retention settings). Each node records a hash of them, and System → Nodes flags a node whose hash differs and nodes on different versions. Set STRUCTAM_MIGRATE=off on every node and run structam migrate once per upgrade (upgrading).

Production

  • Two or more structam serve hosts behind a load balancer that health-checks GET /readyz every few seconds and passes WebSocket upgrades: the shipped Caddy configuration with STRUCTAM_UPSTREAMS="192.0.2.11:8080 192.0.2.12:8080", or your own (HAProxy, nginx, a cloud load balancer). Install each with install.sh --role serve --listen :8080 --secret-key-file FILE --trusted-proxies <load balancer addresses>. Only the first node to start creates the administrator.
  • Workers as needed (install.sh --role worker --public-url <the serve nodes' URL> --secret-key-file FILE --trusted-proxies <the serve nodes' list>), on private addresses. Every node gets the same --public-url and --trusted-proxies.
  • PostgreSQL with automatic failover: Patroni with etcd (three nodes), CloudNativePG, or a managed service with a synchronous standby. Structam needs only the multi-host connection string or the writer endpoint.
  • Kubernetes: see installing on Kubernetes.

Drills

Run these before you rely on a cluster, and after changing its topology. Watch System → Nodes and the logs while they run. The commands below use the compose HA topology; the expectations hold for any topology.

Stop a serve node

docker compose -f compose.ha.yaml stop app1          # or: systemctl stop structam-serve on one host

Expect: requests through the load balancer keep succeeding; agents connected to app1 reconnect to app2 within seconds (close code 4001); app1's leases move within about 20 s.

Kill a node without a drain

docker kill -s KILL structam-app1-1                  # or kill -9 the structam process

Expect: its agents reconnect elsewhere within 30 s, its claimed jobs return to the queue within 60 s, its leases move within 30 s, and nothing acknowledged is lost or applied twice.

Restart PostgreSQL

docker compose -f compose.ha.yaml restart pg1

Expect: /readyz answers 503 while the database is away (liveness stays 200, nothing restarts); agents are told busy or lose their connection and keep data on disk; everything resumes on its own when the database is back.

Promote the standby

Fence the old primary first: two primaries would split the data.

docker compose -f compose.ha.yaml stop pg1                                                         # 1. fence
docker compose -f compose.ha.yaml exec pg2 psql -U structam -d structam -c 'select pg_promote()'   # 2. promote
curl -sk -o /dev/null -w '%{http_code}\n' https://structam.example.com/readyz                       # 3. 200 again

Expect: /readyz is 503 from the moment pg1 stops until the promotion, then 200 within a few seconds without restarting any node; leases are taken again within about 30 s. pg2 now has no standby of its own: rebuild the old primary as its standby to get redundancy back. In production, Patroni or your operator does all of this.

The synchronous standby stops answering

docker compose -f compose.ha.yaml stop pg2

Expect: within about 10 s the nodes report the stall (structam_db_sync_commit_stalled is 1), agents are answered busy, writes through the API answer 503 busy, and no node restarts. Start pg2 again and everything resumes.

Questions the docs do not answer?

Ask the people who build Structam. Installation media and licences come from KazLabs.

Talk to us