Events and replication · Engineering
The outbox pattern, inside the database
Why NonoDB writes events in the same transaction as data, how automatic change events work, and why delivery is at-least-once.
A common failure in event-driven systems looks like this: the order is saved, then the process crashes before publishing the event. The database and the message broker disagree, and no retry can recover an event that was never written.
The outbox pattern fixes this by writing the event into the same database transaction as the data change. A separate consumer reads the outbox and performs the side effect.
NonoDB implements the outbox as an ordinary collection, _events.
The model
producer ──write_batch(data + event)──▶ _events ──poll _id > checkpoint──▶ consumer │ └── handlerThe event and the data commit together or not at all. Consumers poll with an indexed _id range query. Each consumer stores its own checkpoint in a normal collection.
There are two producers:
- an application writes data and an event atomically with
write_batch; - a collection enables automatic change events, and the writer appends events for insert/update/delete.
Both produce ordinary documents in _events, so the same consumers work.
The event document
{ "_id": "01J...", "type": "order.paid", "doc_id": "o1", "payload": {"total": 1200}, "created_at": 1762800000000}_id is a ULID-style 26-character sortable id. type selects a handler, doc_id links to a related document, and payload carries application data. The id embeds a millisecond timestamp and is monotonic within one producer process.
Consumers use the id for ordering and checkpointing, not as a global commit order.
Atomic production
from nonodb import NonoDBfrom nonodb_worker import EventProducer
db = NonoDB()producer = EventProducer(db.collection("_events"))
producer.emit( "order.paid", {"total": 1200}, doc_id="o1", data_ops=[{ "op": "update", "coll": "orders", "filter": {"_id": "o1"}, "update": {"$set": {"status": "paid"}}, }],)Every data_ops entry includes its own coll. The event insert is appended to the same batch. Omit data_ops to emit only an event.
Automatic change events
Enable events for one collection:
{ "op": "set_collection_events", "coll": "orders", "enabled": true, "ops": ["insert", "update"], "fields": ["status", "total"], "filter": {"payload.after.status": "paid"}, "include_before": false, "include_after": true}ops=[]means all operations.fields=[]emits updates for any changed top-level field.filteris matched against the complete event document before it is stored.include_beforeandinclude_aftercontrol the payload.- The event is written in the same writer transaction as the data change.
- Inserts, updates, deletes,
find_and_modify, TTL expiry, and SQL writes emit. - Vault collections are not auto-evented.
The event shape includes before, after, and changed_fields.
Consuming with checkpoints
from nonodb_worker import EventConsumer, EventRunner
consumer = EventConsumer( db.collection("_events"), db.collection("_event_checkpoints"), "order-sync", overlap_seconds=2.0,)runner = EventRunner(consumer)
def on_order_paid(event): print(event["doc_id"], event["payload"]["total"])
runner.register("order.paid", on_order_paid)runner.run_forever()Delivery is at-least-once. If a handler succeeds but the checkpoint write fails, the event is replayed. Handlers must be idempotent.
The consumer processes events in _id order. Concurrent producers can commit a lower id after a higher one, so the helper uses an overlap window and deduplication rather than pretending there is a global total order.
Retry, retention, and failure
- A failed handler is retried according to the consumer’s policy.
_eventsgrows unless you add TTL or prune old events.- A checkpoint older than retained events produces a gap; rebuild from a snapshot.
- A poison event can block a naive consumer. The Python helper supports quarantine after repeated failures.
- The daemon does not run handler code, so a bad handler cannot crash the writer.
What it is not
- It is not exactly-once delivery.
- It is not a full message broker with topics, partitions, and independent retention. Streams now add consumer groups over a collection.
- It does not emit every update automatically unless you enable that collection.
- It does not replay old changes from a log; it reads documents from a collection.
- It does not replace a queue for cross-service work with independent retention.
When it fits
Use the outbox when:
- the event must not be lost if the process crashes after the write;
- consumers can be idempotent;
- the event volume fits an ordinary collection;
- you want events next to the data with one backup and one auth model.
Use a dedicated broker when:
- you need massive fan-out and independent retention;
- you need exactly-once semantics with transactional consumers;
- the event stream is the primary product.
The outbox events guide and streams guide cover producers, checkpoints, watch, and delivery semantics.