i2goSignals

goSignals

goSignalsServer Clustering Design

This document describes the clustering approach implemented in goSignalsServer to support running multiple instances in a cluster.

Overview

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:

Node Identity

Each node identifies itself using a NodeID, which is determined at startup:

  1. I2SIG_CLUSTER_NODE_ID environment variable.
  2. POD_NAME environment variable (for Kubernetes).
  3. Fallback: hostname + process start timestamp.

Lease Mechanism

Leases are stored in the cluster_leases collection in MongoDB.

Document Schema

{
  "_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
}

Atomic Acquisition and Renewal

Leases are acquired or renewed using an atomic FindOneAndUpdate operation:

Reading the Owner

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.

Release on Stop

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.

Local WAL: Drain Before Release

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):

  1. Ingest closes. A SET arriving now gets 503 + Retry-After, and the transmitter resends it, possibly to another node.
  2. The background drain worker stops between batches, and the WAL is drained to the store synchronously, retrying store failures, for at most I2SIG_STORE_WAL_DRAIN_TIMEOUT (default 20s).
  3. Only then are the delivery runners stopped. Each releases its lease as it exits (above), so the next holder finds every acknowledged SET in the store.

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.

Local WAL: Multi-Node Guard

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:

Even in local mode, only streams whose per-stream durability is local use the WAL. All other streams keep the majority contract.

Lease-Tenure Acks

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:

Parameters

Feature Implementation

Poll Receivers

When a stream is configured as a POLL receiver, the node attempts to acquire the lease poll-receiver:<streamId>.

Push Transmitters

Similarly, for PUSH transmitters, the node attempts to acquire the lease push-transmitter:<streamId>.

SSTP Pair Clients

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).

Poll Transmitters and SSTP Pair Servers

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).

Serving Non-Owner Requests (Claim)

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.

Intra-Cluster Coordination (Wake-up Mechanism)

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:

  1. It identifies the current lease owner for that stream (e.g., push-transmitter:<streamId>).
  2. If the current node is the owner, it enqueues the event locally for immediate transmission.
  3. If another node is the owner, it sends a lightweight POST /_cluster/wake-transmitter request to that node.
  4. The receiving node, upon validation of the request, triggers an immediate backfill from MongoDB to fetch and transmit any pending events.

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 wake-up endpoints

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:

Stream-table reconciliation

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):

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.

Deleted streams

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.

Orphan pending events

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.

Cluster-row GC

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):

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.

Periodic Backfill

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).

Observability

Nodes register themselves in the cluster_nodes collection with metadata:

Advertised wake-up address

Each node writes the address its peers use for wake-up calls into address, at registration and on every heartbeat:

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).

Failure Modes and Handling

Deployment and Demonstration

To demonstrate clustering in a Docker environment, use the provided cluster configuration files:

In this setup, you can observe that:

  1. Both 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.
  2. They will compete for leases for any stream defined in that database.
  3. If you stop the container holding a lease, the other node will automatically take over after the lease expires (approx. 30 seconds).

(C)2026 Independent Identity Inc.