Events and replication · Engineering
Replication, failover, and an honest RPO
Pull-based replicas, quorum leases, promotion readiness, and the small data-loss window that asynchronous failover cannot hide.
Replication is easy to describe and hard to make honest. A replica that is “a few seconds behind” is not the same as a backup, and a failover that promotes a lagging node is not the same as zero data loss.
NonoDB uses a pull-based primary → replica model. Replicas read locally, writes go to the primary, and failover is controlled by quorum and promotion readiness.
Topology
global router / GeoDNS │ ├── primary region (all writes) ├── hot standby (sync or async) └── read replicas (local reads)Replication is pull-based:
replica ──outbound mTLS──▶ primaryThis works across NAT and providers without inbound access to every replica. Node-to-node replication requires TLS 1.3 and mTLS; the replication identity is a node certificate, not a shared bearer token.
What is replicated
The replication log records each committed write transaction, preserving transaction grouping and writer order:
{ "seq": 1042, "origin": "eu-1", "created_at_ms": 1794600000000, "changes": [ { "collection": "orders", "op": "update", "id": "o-123", "before": {"_id":"o-123","status":"pending"}, "after": {"_id":"o-123","status":"paid"} } ]}The log carries document payloads and the exact order of document, metadata, TTL, index, subscription, and outbox operations. Older logs without ops still apply document changes sequentially.
Replicated:
- documents and deletes (with tombstones);
- collection metadata, TTL settings, history settings, index definitions;
- subscriptions and outbox events;
- auth registry and database catalog;
- backup job definitions and run state;
- vault payloads as encrypted envelopes.
Not replicated:
- TLS private keys and node-local transport identity;
- data/socket paths, log level, worker counts, node-local settings;
- vault tenant keys (client-supplied, never stored);
- backup artifacts (use object storage or shared storage).
Apply rules
- Apply one whole replication transaction as one local write transaction.
- Reject older
owner_epochvalues after failover. - Apply
(origin, seq)idempotently. - Preserve deletes with tombstones.
- Apply metadata and indexes at their exact position.
- Apply replicated
_eventsdocuments; do not re-evaluate subscriptions or generate a second set of events. - Generate local history from replicated before/after values.
- Notify live streams after commit; never write live events into
_eventson the replica. - Build or backfill indexes after the metadata operation.
- Expose
promotion_ready; automatic election refuses a candidate that has not applied the last observed source head.
Quorum and leases
With voters configured, the primary renews a quorum lease. Client writes are accepted only when the node is both primary and holds a valid lease. A primary with configured voters starts read-only until it reaches a voter majority.
If it loses the majority for the lease TTL, it self-demotes and becomes read-only. Automatic candidates wait out the lease TTL before campaigning, so a fast election cannot create two writers.
Use odd voter counts, normally three or five. Without voters there is no quorum; stop or demote the old primary before promoting a replica.
Promotion readiness
Automatic election requires promotion_ready=true: the candidate must have applied the last observed source head and the replicated control-plane snapshot. A lagging candidate waits instead of promoting stale data or missing auth/backup configuration.
This is a safety check, not a zero-loss guarantee. The last few transactions may still be missing if the old primary accepted writes that had not reached the candidate.
The honest RPO
Failover is asynchronous. A small RPO window exists unless writes are stopped before promotion.
- If writes are stopped and the replica is caught up, RPO is zero for those writes.
- If the primary fails abruptly, any acknowledged write not yet applied to the candidate is lost.
- Divergent writes from an isolated old primary are discarded on resync, not merged.
Anyone claiming zero RPO with asynchronous failover is either stopping writes or not measuring the window.
Returning the old primary
When a partitioned primary loses its lease, it self-demotes and becomes read-only. After connectivity returns, source discovery queries the voters for the current primary and installs it as the source.
If the old log diverged, the replica resets its local replication state and re-pulls; divergent local writes are discarded rather than merged. If the source log was pruned, normal gap/snapshot recovery applies.
Write routing
The current write endpoint is exposed in:
GET /v1/replication/primaryGET /readyz/writeA read-only node returns a Location header and error.write_url on rejected writes. Clients support write_url/auto_primary; proxies can retry once against the new primary.
What replication is not
- Not a backup. It does not protect against an application bug that deletes the wrong rows; both nodes apply it.
- Not multi-master. There is one writer.
- Not zero RPO under asynchronous failover.
- Not a substitute for monitoring lag, certificate expiry, and apply errors.
- Not complete for node-local settings or backup artifacts.
Use backups for history and replication for availability. Test failover and restore, and document the RPO the deployment actually provides.
The replication design and cluster guide cover topology, TLS, CLI, GUI, and known limitations.