Events and replication · Engineering
Webhooks without blocking the writer
Rules, thresholds, retries, HMAC signing, and dead letters: how asynchronous webhooks keep HTTP failures out of the write path.
Calling an HTTP endpoint from a database write is a bad idea. The endpoint can be slow, down, or returning 500s. If the write waits for it, one broken integration stalls everything.
NonoDB’s webhooks are asynchronous by design. The writer only appends the normal change event. A separate evaluator applies filters and thresholds, creates durable delivery records, and a background worker makes the HTTP request after the transaction commits.
The pipeline
write -> writer transaction -> _events | v async webhook evaluator filter + count/sum/avg state | v _webhook_deliveries | v async HTTP delivery retry / HMAC / dead-letterA slow endpoint cannot block the single writer. The trade-off is eventual delivery, not synchronous confirmation.
Creating a rule
{ "op": "create_webhook", "name": "orders-paid", "coll": "orders", "url": "https://example.test/hooks/orders", "enabled": true, "ops": ["insert", "update"], "filter": {"status": "paid"}, "secret_env": "ORDER_WEBHOOK_SECRET", "max_attempts": 8, "timeout_ms": 10000, "cooldown_seconds": 0}filter is evaluated against the after document for insert/update and the before document for delete. It is never evaluated in the writer.
Creating a webhook enables the collection outbox with no event filter, projections, or before/after suppression, so the evaluator sees the complete change. Other outbox consumers continue to work.
Thresholds
A rule can fire only when a simple aggregate crosses a threshold:
{ "op": "create_webhook", "name": "paid-volume", "coll": "orders", "url": "https://example.test/hooks/volume", "filter": {"status": "paid"}, "threshold": { "aggregate": "sum", "field": "total", "gte": 10000, "filter": {"status": "paid"} }, "once": true, "cooldown_seconds": 300}Supported aggregates are count, sum, and avg. Exactly one comparison is required: gt, gte, lt, lte, or eq. If threshold.filter is omitted, it inherits the rule filter.
once: true is edge-triggered: the rule fires when the condition changes from false to true and re-arms when it becomes false. Without once, every matching change above the threshold can fire, bounded by the cooldown.
The evaluator maintains aggregate state incrementally from change events. It does not run an aggregation query for every write. On startup, a new count/sum/avg rule takes a consistent snapshot to bootstrap its state.
Delivery records
Each trigger creates a document in _webhook_deliveries:
{ "_id": "01J...", "rule": "orders-paid", "url": "https://example.test/hooks/orders", "event_id": "01J...", "status": "pending", "attempts": 0, "max_attempts": 8, "next_attempt_ms": 0, "created_at": 1762800000000}The delivery worker sends JSON with content-type: application/json, sets Idempotency-Key to the source event id, and retries non-2xx responses and network errors with exponential backoff. After max_attempts, the delivery is marked dead.
Manual retry is available through the API, CLI, and GUI.
HMAC signing
When secret_env is configured, the body is signed with HMAC-SHA256:
X-NonoDB-Signature: sha256=<hex digest>The secret is read from the named environment variable at delivery time. It is not stored in the rule document.
A receiver should verify the signature over the raw body before parsing JSON, and deduplicate on Idempotency-Key.
At-least-once
Delivery is at-least-once. A crash after the HTTP request but before the state update can produce a duplicate. That is unavoidable without a transactional protocol with the receiver.
Make endpoints idempotent:
- store the idempotency key;
- return the same result for a repeated key;
- make side effects idempotent where possible;
- do not assume exactly one delivery.
Semantics and limits
- Asynchronous by design. Filters and aggregates never run in the writer.
- Only simple aggregates. No arbitrary pipelines, joins, or user code.
- One collection per rule.
- Rules are per database.
- Primary only. The worker runs only while the node is writable, so replicas do not send duplicates.
- No auth delegation. NonoDB sends configured headers and an HMAC signature; the receiver must authorize.
- URL safety. Only
httpandhttpsare accepted; use egress controls. - State file.
webhooks-state.jsonis local and not part ofdata.mdb. If lost, threshold and cursor state is rebuilt as far as possible.
Operating webhooks
- Monitor pending and dead deliveries.
- Alert on delivery age, not only on failure count.
- Use a cooldown for noisy rules.
- Keep the receiver fast; a slow receiver increases backlog.
- Rotate the HMAC secret with a receiver that accepts both old and new during the transition.
- Test a dead delivery and a manual retry.
When not to use webhooks
Webhooks are not a durable work queue. If the receiver must process every event with long retries and independent retention, use the outbox plus a worker, or a dedicated broker. Webhooks are for notifying an external system, not for owning its work.
The webhooks guide and outbox events guide cover the API, CLI, GUI, and testing.