Events and replication · Engineering
Consumer groups without a second log
How NonoDB streams combine writer-assigned offsets, range leases, dead letters, and retention with the documents already in the database.
Durable work queues usually need another system: a broker, a separate log, and another set of credentials and backups. NonoDB already stores outbox events next to the data. Version 0.35 adds server-side offsets and consumer groups on an ordinary collection.
This is not a full message broker. It is a durable log you can query with the same API, back up with the same snapshot, and replicate with the same cluster.
The log is the collection
A stream is a standard collection with a stream definition. After create_stream, every insert gets a _seq value allocated by the single writer:
{"op":"create_stream","coll":"_events", "retention":{"max_events":1000000,"max_age_ms":604800000}}{"op":"create_stream_group","coll":"_events", "group":"order-sync","start":"latest"}_seq is a strictly increasing u64 in commit order. It is reserved: an insert that supplies it, or an update that changes or removes it, is rejected. Events written before the stream existed have no offset and are not delivered by group reads, so define the stream first or backfill.
Only standard collections can back a stream. Time-series buckets and vault collections do not carry _seq; create_stream rejects them instead of returning empty batches. Automatic _events writes go through the same writer path and get offsets too.
Claim a range, not a message
A group member asks for work with watch:
{"op":"watch","coll":"_events","mode":"group", "group":"order-sync","member":"worker-1", "limit":100,"lease_ms":60000,"timeout_ms":1000, "filter":{"payload.tenant":"acme"}}The response contains events, lease_id, and the claimed range:
{"events":[{"_id":"01J...","_seq":412901,"type":"order.paid"}], "group":"order-sync","member":"worker-1","lease_id":"01J...", "from_seq":412901,"to_seq":413000,"attempts":1, "timed_out":false,"has_more":false}Several members can share one group; each claim is a contiguous range, and no offset is handed out twice. A caught-up member gets timed_out: true and no lease. If no event in the claimed range matches the filter, the server acknowledges the range and keeps polling.
Commit the whole range
Acknowledgement is one request:
{"op":"commit_stream_group","coll":"_events","group":"order-sync", "seq":413000,"lease_id":"01J..."}seq must be the end of the leased range. A partial ack returns commit_out_of_order and leaves the batch leased. Make the batch the unit of work that is safe to redo.
The committed offset advances only across a contiguous prefix. If A finishes 11..=20 while B is still on 1..=10, the offset stays at 0 until B commits, then jumps to 20. Delivery is at-least-once, so handlers need idempotency keys.
nack_stream_group releases a batch early. An expired lease makes the range available with attempts incremented; after max_attempts it becomes a dead letter. Dead letters do not block the group.
Failure is a lease, not a pending list
Redis Streams uses a pending-entries list and XAUTOCLAIM. NonoDB uses leases: valid for lease_ms, with a primary-side janitor that requeues or dead-letters expired batches even when no member is asking.
Leases are node-local and not replicated. A promoted replica derives its claim position from committed_seq plus local leases, so it resumes at the committed offset and redelivers ranges the old primary claimed but never committed. The guarantee is at-least-once, not silent loss.
Retention follows the slowest group
Pruning drops events at or below the lowest committed offset among the stream’s groups, bounded by max_events, max_age_ms, and min_seq_keep. A stopped consumer pins retention; nothing at or above its offset is dropped. Alert on nonodb_stream_lag_events. If retention overtakes a group, its next claim fails with stream_truncated (HTTP 410) and records truncated_below.
retention.max_bytes is validated but ignored. Age pruning only removes a contiguous prefix of provably old events, so a missing timestamp stops the boundary rather than deleting a fresh event.
Scope, metrics, and operations
Group reads, commits, and nacks require consume:<collection> or admin:<collection>. A read-only principal can tail in the default mode but cannot claim a lease or advance a group. Creating, resetting, deleting, and pruning require admin scope.
/metrics/prometheus exposes committed offset, lag, in-flight, dead letters, redeliveries, truncation, and commit failures. stream_stats, the CLI, and the GUI report the same state.
What changes from Redis Streams
| Redis Streams | NonoDB |
|---|---|
XADD |
insert into the stream collection |
XREADGROUP |
watch with mode: "group" |
XACK per message |
commit the whole leased range |
| pending entries | leases with fencing |
XAUTOCLAIM |
automatic redelivery after expiry |
XTRIM |
max_events / max_age_ms + min_seq_keep |
XINFO |
get_stream / stream_stats |
The migration is mostly mechanical, but the acknowledgement unit and retention rule change consumer code. There is one stream per request, no blocking read over several streams, and no Lua or built-in pub/sub. One writer still sets the throughput ceiling, and Redis remains faster for ephemeral fan-out or sub-millisecond delivery.
When to use it
Use NonoDB streams when the event must survive a crash and failover; consumers can process a batch idempotently; you want to query the log like other data; or one database, backup, auth model, and replication setup is the point.
Use a dedicated broker for topics, partitions, and independent retention; blocking reads across many streams or arbitrary Lua; sub-millisecond latency and horizontal write scaling; or when the stream is the primary product.
The streams guide has full request shapes, client examples, metrics, and limits. The outbox events guide covers producers and atomic data-plus-event writes.