Skip to main content

Deployments and failover

Every service that traders or the cashier depend on runs at least twice, so a release or the loss of a pod or a host does not stop trading. This page describes what runs how often, what happens during a deploy and when something fails, which alerts watch it, and how to rehearse it.

ComponentInstancesSpread
live-edge2one per host where possible
Engine (fix-positions)2, one active and one standbyone per host where possible
history2one per host where possible
Cashier backend2one per host where possible
RabbitMQ3 nodesone per host
Postgresprimary and 2 standbysone per host

Releases​

Pods are replaced one at a time, and the migrations of a release run before its first pod starts, with lock_timeout=5s and retries. So for a while every release runs next to the previous release of every other service, on the previous schema plus its own migrations. AGENTS.md in the repository lists what that means for database, message, queue and HTTP changes.

live-edge starts all new replicas before it stops an old one. A replica that stops reports itself not ready, waits 5 seconds for the ingress to notice, and then closes its sockets with code 1012 (service restart), each at a random time within 15 seconds, so the apps do not all reconnect at the same moment. Calls to the engine that are refused (connection refused, or 503 from a standby) are repeated for up to 5 seconds.

The cashier backend runs its scheduled jobs, the MT5 worker and the CRM projection on one replica at a time, each on the replica that holds its Postgres advisory lock. A replica that stops lets its running jobs finish and then releases the locks, and the other replica takes them within 5 seconds.

The engine lease​

Only one engine trades: the one holding the lease, a row in the gateway database. It renews the lease every second. The other engine is a standby. It checks the lease four times a second, and passes HTTP requests on to the active engine, marked with the header X-Wintrado-Engine-Passed-On. Without an active engine it answers 503 with Retry-After: 1.

The lease has an epoch that grows with every new holder. Every transaction that records trading events checks it, so an engine that lost the lease cannot commit anything, even if it does not know yet.

When the active engine is stopped for a deploy, it stops taking commands, finishes the requests and order handlers in flight, and hands the lease to the standby that started last. In the load test the handover took 270 ms. The sequence numbers of the FIX sessions are kept in Postgres (schema fix), so the new engine continues the sessions where the old one left them.

When the active engine fails, the lease expires after 5 seconds and the standby takes it. An engine that cannot renew its lease for 4 seconds, for instance because Postgres is unreachable, exits on its own; Kubernetes starts it again and it comes back as the standby.

Postgres failover​

A commit waits until one of the two standbys has it, so a failover loses no committed transaction. With one standby down the other confirms; with both down, every write waits until one is back. Updates of Postgres restart the standbys first and then switch over to one of them, so the primary is only away for the switchover.

When the primary fails, the operator promotes a standby and moves the postgres-rw Service to it. Until then nothing can be written:

  • The active engine exits after 4 seconds without a lease renewal. The standby takes the lease as soon as the new primary accepts writes, which includes the time the remaining standby needs to reattach. Orders in between are refused; no accepted order is lost.
  • If the primary process crashed, its connections are reset and the cashier replaces them. If its host is gone, its connections hang instead. The lock sessions of the cashier give up after 2 seconds, so the jobs stop on the old holder before another replica can take the lock on the new primary. Queries that were in flight hang until the kernel gives up on the connection, which takes minutes. That includes the connection of the health check, so the liveness probe restarts the cashier pods after three failed probes, which also drops the hung connections.

cashier-postgresql-read, the endpoint of Metabase and the CRM sync, points at the primary and follows it through a failover.

RabbitMQ node loss​

The shared queues are quorum queues and the event stream trading.events.log has a replica on every node. Both need two of the three nodes, so one node can be down without losing messages. The clients reconnect to another node and consume again. Queues declared while a node was away get a member on it once it is back (membership reconciliation). See Message bus for the queues.

Alerts​

The rules are in alerts/prometheus/alert.rules of the repository and use the metrics the monitoring already collects:

  • wintrado_engine_none_active: no engine of a namespace held the lease for 15 seconds.
  • wintrado_engine_several_active: more than one engine considers itself active for 15 seconds.
  • wintrado_engine_lease_moved (info): another engine trades now. Expected during a deploy.
  • wintrado_engine_storage_failing: the engine cannot store executions and refuses new orders.
  • wintrado_postgres_no_standby and wintrado_postgres_one_standby: fewer than two standbys stream from the primary. With none, writes wait.
  • wintrado_postgres_replication_lag: a standby is more than 30 seconds behind.
  • wintrado_rabbitmq_node_missing: a broker runs on fewer than three nodes.

Drills​

apps/gateway/loadtest runs two engines, live-edge, RabbitMQ and optionally Postgres with two standbys in Docker, and reports what traders notice of a command run in the middle of a load: failed orders, dropped sockets, the longest time without a quote. Its README lists the commands for a rolling deploy, killing the active engine, and a Postgres failover with a crashed or a lost primary. apps/gateway/bus/clustertest stops the nodes of a three-node RabbitMQ cluster one by one while messages flow.

On a cluster, run the same drill against the ingress (see the README) with one of these as the action, first on staging, then demopips, then utocapital:

kubectl -n <namespace> rollout restart deployment/fix-positions
kubectl -n <namespace> delete pod <active engine>
kubectl -n <namespace> delete pod rabbitmq-server-1
kubectl cnpg -n <namespace> promote postgres <standby>

The active engine is the pod whose wintrado_fix_positions_engine_active is 1, or the one that logged "holding the lease" last.

Not covered​

ClickHouse, backups and point-in-time recovery, the MT5 bridge and the loss of the whole site are outside this page.