This document describes the clustering approach implemented in goSignalsServer to support running multiple instances in a cluster.
The clustering mechanism ensures that specific tasks are owned by a single node at a time to avoid conflicts and redundant processing. This is achieved using MongoDB-backed leases.
The following features are owned by a single node per stream:
poll-transmitter:<streamId>): Only one node holds a poll stream’s queue; every node still answers POST /poll/{id} (see Serving non-owner requests).sstp-server:<txSid>): Only one node holds an accepting pair’s outbound queue; every node still answers POST /sstp/{id} and ingests its inbound SETs.Each node identifies itself using a NodeID, which is determined at startup:
I2SIG_CLUSTER_NODE_ID environment variable.POD_NAME environment variable (for Kubernetes).hostname + process start timestamp.Leases are stored in the cluster_leases collection in MongoDB.
{
"_id": "resource_key",
"ownerNodeId": "node-123",
"leaseUntil": "2026-01-25T12:00:30Z",
"createdAt": "2026-01-25T11:00:00Z",
"updatedAt": "2026-01-25T12:00:00Z",
"fencingToken": 42
}
Leases are acquired or renewed using an atomic FindOneAndUpdate operation:
ownerNodeId to current node and extend leaseUntil. fencingToken is kept when the current owner renews a live lease, and incremented when a new tenure starts (a first acquire, a takeover, or a re-acquire after the lease expired). The token therefore names one tenure of the lease.GetLeaseOwner reports an expired or released lease as unowned (empty owner, token 0), so a caller never mistakes a lapsed lease for a live one.
Push transmitter and poll receiver runners release their lease (ReleaseLeaseIfOwned) when they stop, after their heartbeat has stopped, so another node can take the stream at once instead of waiting out the TTL. The SSTP client releases its pair lease the same way.
With I2SIG_STORE_WAL=local (ADR 0045), a SET is acknowledged once it is in this node’s WAL, before it reaches the store. A lease must not pass to another node while such SETs are only on this node’s disk. So in local mode the stop order is (#341):
Retry-After, and the transmitter resends it, possibly to another node.I2SIG_STORE_WAL_DRAIN_TIMEOUT (default 20s).If the drain times out, the node logs the residual depth at ERROR, and the entries stay on disk. Its leases are still released at stop and then expire normally. The residue is replayed when this node starts again. At start, a WAL that holds entries from a previous run is replayed to the store first. Until it is empty, ingest answers 503 + Retry-After. In the default majority mode, none of this applies and the stop order is unchanged.
A SET acknowledged into one node’s WAL is not in the store until the drain moves it. Another node that owns the stream’s delivery lease cannot see it until then. Ring-fed delivery (I2SIG_STORE_WAL_RING_FED=true, #342) closes that gap on the buffering node. So local mode in a cluster requires ring-fed delivery (#343). The guard has a startup half and a runtime half:
I2SIG_STORE_WAL=local and ring-fed off first registers itself with the cluster coordinator, then asks for the active nodes. If any active node other than itself is registered, it logs an ERROR and refuses to start. The error names the condition and both fixes: set I2SIG_STORE_WAL_RING_FED=true, or return to majority (unset I2SIG_STORE_WAL). Registering before reading means two such nodes started at the same time see each other; at least one refuses, and if both do, the refused entries age out of the active window (60s) and the next start succeeds.durability=local are acknowledged at majority from then on (their effective durability reads majority), entries already in the WAL keep draining, and goSignals_wal_local_ingest_suspended reads 1. There is no re-arm; the node stays at majority until it restarts, so its durability contract does not flap with membership. A failed membership read is retried on the next heartbeat, not acted on.Even in local mode, only streams whose per-stream durability is local use the WAL. All other streams keep the majority contract.
Acknowledgements are not fenced on write. The router’s lease manager records, on every acquire or renewal, a tenure deadline of min(leaseUntil, renewCallStart + leaseDuration) - margin (margin I2SIG_LEASE_SAFETY_MARGIN, default 5s, capped at half the lease duration with a one-time WARN) and answers “does this node still own the stream” from memory:
When a stream is configured as a POLL receiver, the node attempts to acquire the lease poll-receiver:<streamId>.
Similarly, for PUSH transmitters, the node attempts to acquire the lease push-transmitter:<streamId>.
PushStreamHandler loop for that stream.For the client (initiator) side of an SSTP pair, the node attempts to acquire the lease sstp-client:<PairId> (note: keyed by the pair’s PairId, not a per-direction SID).
A poll transmitter is leased as poll-transmitter:<streamId> and the accepting (server) side of an SSTP pair as sstp-server:<txSid>, with the same 30 s duration / 10 s heartbeat (#365).
A POST /poll/{id} or POST /sstp/{id} that reaches a node which does not own the stream is answered by the owner through one POST /_cluster/claim call. The call carries the request’s acknowledgements and asks for up to maxEvents references; the owner applies the acknowledgements in its one write, claims the references from its queue and returns them, and the receiving node signs and sends them. Only a stream no node knows answers 404.
I2SIG_POLL_DEFAULT_TIMEOUT / I2SIG_POLL_MAX_TIMEOUT) less one second, so the owner answers before the receiver’s own deadline; returnImmediately sends no wait.I2SIG_CLUSTER_CLAIM_INFLIGHT (default 256) caps the waiting Claims one node holds open to one owner. Past the cap a request is sent with returnImmediately, and an empty answer is held on the receiving node for the wait plus one second so a client cannot spin. The budget-exhausted count is goSignals_router_peer_claim_budget_exhausted_total; the claims an owner answers are counted in goSignals_router_peer_claims_total{mode,result}.NotOwner answer or a failed call forgets the cached owner and resolves once more; a second failure returns an empty answer with the acknowledgements unapplied, so the client resends them.returnEvents=false) makes no Claim./_cluster/claim route uses the wake-up authentication (SPIFFE mTLS peer certificate, else the I2SIG_CLUSTER_INTERNAL_TOKEN shared-HMAC bearer).To ensure low-latency event delivery without relying on MongoDB Change Streams (which can be resource-intensive and complex to manage), goSignalsServer uses a lease-aware wake-up mechanism.
When a node receives or generates an event that needs to be routed to an outbound stream:
push-transmitter:<streamId>).POST /_cluster/wake-transmitter request to that node.This mechanism is secured using a shared HMAC secret (I2SIG_CLUSTER_INTERNAL_TOKEN) and includes rate-limiting to prevent denial-of-service.
Wake-ups are coalesced per target over a 250 ms window, on the sending and on the receiving node, and on both edges (#347): the first wake of a burst goes out (or is acted on) at once, and any further wake inside the window arms one trailing wake at the window’s end, shared by the rest of the burst. A wake says only “this target has work”, so the last wake of a burst is never lost.
On a push stream the owner’s wake backfill reads the store past what is already queued or in flight, up to a cap of ten backfill batches per wake. A read that stops at the cap, or a periodic backfill that comes back full, marks a backlog: the loop then reads again each time a batch completes and the buffer runs low, so a burst deeper than the cap drains at delivery speed rather than one batch per backfill interval.
SSTP adds two wake-up routes that mirror /_cluster/wake-transmitter but are kept separate for telemetry. Both reuse the wake-transmitter authentication (SPIFFE mTLS peer certificate, else the I2SIG_CLUSTER_INTERNAL_TOKEN shared-HMAC bearer) and the same two-edge coalescing window, so duplicate wake-ups inside the window collapse into one trailing wake:
POST /_cluster/wake-sstp-client — the request body’s sid field carries the pair’s PairId. Broadcast to all cluster nodes when a node receives an inbound event whose target SSTP-client pair is owned (via the sstp-client:<PairId> lease) by a different node, so the lease owner drains the pending event into the next outbound cycle.POST /_cluster/wake-sstp-server — the request body’s sid field carries the pair’s tx-side SID. Sent to the sstp-server:<txSid> lease owner, with the new event references, when a node receives an outbound event matching an SSTP-server pair, so a long-poll held open on the owner, or a Claim waiting there for a peer, returns the event immediately.Each node keeps its own in-memory table of the outbound streams it serves (push transmitters, poll receivers, SSTP pairs). A stream created, updated, re-statused or deleted through one node changes only the shared store, so every node reconciles its table against the store (#349, #350):
POST /_cluster/stream-changed on every other active node with an advertised address. The receiving node reconciles only the stream the call names — it reads that one stream from the store and applies it to the router and the receivers, or removes it if the store no longer has it — and then answers 202, the acknowledgement the caller waits for. When the stream was removed or is stored as not enabled, the node answers 202 only once its push runner for the stream has stopped, waiting up to 4 seconds and answering 503 if it has not (an SSTP pair’s pause is only reconciled; its client runner stops on its own status check). The full store scan and the cluster-row GC stay on the periodic sync, so the call finishes inside the caller’s bound however large the store; if the one-stream read fails, nothing changes and the node answers 503, so the caller retries. The call uses the wake-up authentication (SPIFFE mTLS peer certificate, else the I2SIG_CLUSTER_INTERNAL_TOKEN shared-HMAC bearer, minted with its own stream-changed mode so a wake-up token is not accepted here). Repeated calls for one stream that a peer has not yet acknowledged are coalesced into one on the sending node: the peer reads the store’s latest state either way, so a create followed at once by a delete is still seen as a delete.4xx other than 408/429 (for example a cluster-token mismatch; logged at WARN), drops everything when the peer leaves the active nodes, and gives up on a stream after about 60 seconds (WARN). A peer that is never told catches up on its next periodic sync, within 40 seconds. A status the runner writes itself (a recovery or signing-key pause, its end, a disable) is announced to the other nodes the same background way, with nothing waiting.PUSH-SRV: ; the stream-status API refuses an operator reason with that prefix). Every node reads that from the store alike, so standby runners keep waiting for the lease. The runner then exits at its next event or batch boundary without delivering more, leaving the SETs pending and releasing the lease; “done”, for the holder’s acknowledgement, means that runner has stopped. A runner that takes a stream’s lease reads the stream from the store before it pushes, and exits if the store no longer has it or an operator holds it paused or disabled, so a runner started from a stale enabled copy delivers nothing. A stream stored with a runner’s own pause is taken over: its last holder has lost the lease (it died mid-recovery, say), so the new runner writes enabled, only while the store still holds that pause, and retries, pausing again if the receiver still fails or the key is still missing. When the runner ends its own pause it re-reads the store and writes enabled only while the stored pause is still its own: an operator’s pause or disable stored meanwhile wins, and the runner exits leaving it in place. A re-enable starts a runner wherever the lease lands.A periodic sync snapshots the streams the router serves before it reads the store, so a stream created locally after the snapshot is never removed. If the store read fails, nothing is removed and the cluster-row GC below is skipped.
When a reconcile finds that a stream is gone from the store, the router removes it: its runner is told to stop, and each lease it holds (push-transmitter, poll-transmitter, sstp-server, sstp-client) is released when its holder stops, not left to expire; later events write no pending marker for it.
On the deleting node, the stream delete holds the stream-table lock from the receiver teardown through the router’s RemoveStream to the store’s DeleteStream, so no reconcile can run between them and re-add the stream. An SSTP pair delete does not take the lock, because DeleteSstpPair may make a courtesy call to the peer. A reconcile that runs between its RemoveStream and the store delete can re-add the pair for one cycle, and the next reconcile removes it again.
DeleteStream removes the stream document only. Pending-event markers already written for the stream stay in the store, but they are inert: nothing reads them once no node serves the stream, and no new marker is written for it. They are not purged.
After each successful reconcile, a node purges cluster rows left behind by nodes and streams that no longer exist. The GC window is 90 seconds (three lease TTLs):
cluster_nodes — a node row whose lastSeenAt is older than the window is deleted.cluster_leases — a lease row whose leaseUntil is older than the window is deleted only when its resource (push-transmitter:<sid>, poll-receiver:<sid>, poll-transmitter:<sid>, sstp-server:<sid>, sstp-client:<PairId>) names a stream or pair no longer in the store. Lease rows of unknown kinds are kept.A live stream’s lease row is never deleted, however long it has been expired, so its fencing token keeps rising across holders; deleting the row would restart the token at 1 and make the diagnostic history misleading. Acknowledgements do not check the token: ownership before an ack is answered from the node’s own lease tenure (LeaseManager). A stream that no longer exists has no holder left, so its row can go.
As a fallback and to ensure eventual consistency, transmitter loops periodically perform a “backfill” by polling MongoDB for any pending events that might have been missed by the wake-up mechanism (e.g., due to network transient issues). The backfill interval and batch size are configurable.
An SSTP-client pair loop whose primary long-poll is held by the peer re-checks its outbound work on the same interval (1 second), whether or not a wake was seen, so a wake lost to coalescing or a failed wake call delays those SETs by at most one interval rather than until the long-poll returns (#347).
Nodes register themselves in the cluster_nodes collection with metadata:
_id: Node ID.address: The wake-up address peers call (see below).version: Build version.startedAt: Startup timestamp.lastSeenAt: Last heartbeat timestamp.Each node writes the address its peers use for wake-up calls into address, at registration and on every heartbeat:
I2SIG_CLUSTER_ADVERTISE_URL, when set, is stored verbatim (e.g. http://goSignals1b:8898).http://<BASE_URL host>:<port>, where the port is I2SIG_CLUSTER_INTERNAL_PORT if set, else the BASE_URL port. The scheme is always http, so pair a derived address with I2SIG_CLUSTER_INTERNAL_PORT (the internal listener serves plain HTTP unless SPIFFE mTLS is on) rather than a TLS main port.When every node shares one BASE_URL (a load-balanced or public name), the derived addresses collide: every node advertises the same address, wake-up calls reach only one node, and cross-node delivery silently falls back to the periodic backfill (SSTP stalls). Give each node its own I2SIG_CLUSTER_ADVERTISE_URL (or a per-node BASE_URL). A node that finds another live node advertising its own address logs one WARN per peer naming both node ids (nodeID, peerNodeID).
To demonstrate clustering in a Docker environment, use the provided cluster configuration files:
docker-compose-cluster.yml: Runs goSignals1 as a two-node cluster (goSignals1a and goSignals1b) and goSignals2 as a standalone instance.docker-compose-cluster-dev.yml: Development version of the cluster configuration.In this setup, you can observe that:
goSignals1a and goSignals1b connect to the same MongoDB database (goSignals1).
Both share BASE_URL=https://gosignals1:8888/, so each sets I2SIG_CLUSTER_INTERNAL_PORT=8898 and its own I2SIG_CLUSTER_ADVERTISE_URL; cluster_nodes shows http://goSignals1:8898 and http://goSignals1b:8898.(C)2026 Independent Identity Inc.