ADR-0005: Exactly-once storage with coverage ranges, a row lock per device, and an exclusion constraint¶
Status: accepted
Date: 2026-11-09
Context¶
Delivery from device to database is at least once: the device resends until an end-to-end ack arrives, MQTT QoS 1 and Kafka may repeat messages, and two bridges may forward one device’s messages in any order. Storage must still hold each seq exactly once, and the ack must say exactly how far the stored data is complete.
Decision¶
Per device, the database keeps
devices.acked_through(every seq at or below it is stored or a reported gap) anddevice_ranges, the merged ranges above it that are already covered.device_rangesis normally empty; it only has rows while batches arrive out of order.Ingest computes, for each batch, the parts not yet covered (
coverage.Uncovered), stores only those, records the uncovered parts of reported gaps, and moves the watermark over every range that now touches it (coverage.Advance). The interval logic is pure Go ininternal/coverage, shared with the checker and tested with property tests against a bitmap model and a fuzz test.The device row is locked
FOR UPDATEat the start of the transaction, so two ingest instances working on repeats of the same batch serialize.chunkshasEXCLUDE USING gist (device_id WITH =, int8range(seq_lo, seq_hi, '[]') WITH &&), so overlapping chunks are impossible even if the application has a bug.Seqs are
bigint, and PostgreSQL storesint8range(lo, hi, '[]')as[lo, hi+1), so the highest seq it can store is 2^63-2. Validation refuses a higher seq before the batch reaches the database, and the batch goes to the dead-letter topic with reasonseq_overflow.A seq is covered exactly once in total: if a gap report arrives first, a later batch with the same seq is not stored; if the batch arrives first, only the rest of the gap is recorded.
Change from the first design draft:
device_rangesdoes not keep akindcolumn. The permanent records arechunks(stored) anddevice_gaps(gaps);device_rangesis only a cache of their union above the watermark, and merging across kinds keeps it at one row per hole.One batch is one transaction, sent as two pipelines (
store.DB.IngestBatch): the reads withBEGIN, then the writes withCOMMIT. Sent one statement at a time it took eight round trips, during which the row lock was held.Change after the 10,000-device runs:
chunks.key_idhas no foreign key totenant_keys(migration 00004). The check locked the tenant’s one active key rowFOR KEY SHAREin every ingest transaction, so all of a tenant’s concurrent transactions shared that row’s lock through multixacts. With 10,000 devices of one tenant on the test VM, ingest stored 505 readings/s with the constraint and 787/s without it, 64 workers each. The key still exists for every chunk: ingest seals only with keys it read fromtenant_keys, and key rows are never deleted.
Alternatives considered¶
Kafka exactly-once transactions. They cover Kafka to Kafka, not the device to PostgreSQL path, and do not help with device resends.
ON CONFLICT DO NOTHINGper reading. Works, but needs one row per reading and cannot compute a watermark without scanning.Order by seq in a single consumer per device. Order is not guaranteed with two bridges and Kafka retries; requiring it would make correctness depend on the transport.
Consequences¶
Repeats cost one cheap transaction and no storage.
The per-device lock serializes a device’s batches; different devices run in parallel.
Readings that reach the server after the device declared their seqs a gap are not stored. The device made that decision, and the checker counts those seqs as gaps, not missing.
Without the foreign key, the database no longer refuses a chunk whose key row is missing. Only a bug could write one; it would fail to decrypt, and the checker and the API report that. The composite key to
devices (tenant_id, id)stays: the same transaction already holds that row’s update lock, so it adds no contention.