Durable streams
Durable streams provide tenant-scoped topics for publishing data once and reading it later or as it arrives. The service keeps ordered messages for a limited time, supports resume cursors, and uses batch work so the platform can serve replay and live delivery.
System purpose
The durable-stream service connects producers and consumers through retained topic data. A producer writes to a topic, and consumers can read the same data from the oldest retained point, from a saved position, or from the live end.
The stored message is the source of truth. Live delivery can use a short-lived wake signal to reduce delay, but polling still finds committed data when that signal is lost.
References: pkg/…/streams.go• L144-177 pkg/…/streams.sql• L176-198
Tenant and topic model
Every stream belongs to a tenant. Within a tenant, a namespace and topic name identify one independent ordered stream. An empty namespace is the default namespace.
The service creates a topic when its first message is published. Topic creation and message publishing use tenant limits, and the tenant must have the durable-stream entitlement before it can use the service.
The tenant boundary applies to topics, messages, payload uploads, and reads. A payload reference or cursor from one tenant cannot be used to read another tenant's data.
References: cmd/…/20261002182802_v1_0_160_durable_streams.sql• L3-8 pkg/…/streams.go• L73-101 pkg/…/streams.go• L203-240 pkg/…/streams.sql• L243-275
Publish lifecycle
Publishing validates the stream address, producer sequence, and payload before it accepts data. The service assigns each stored message an increasing offset within its topic. This offset gives readers one ordered position even when several producers publish at the same time.
Each producer supplies a producer ID and sequence number. The pair makes retries safe: a stored sequence is treated as a duplicate, while a sequence that skips the producer's current position is reported as a gap. Different producers can publish to the same topic without sharing one sequence.
The service groups waiting publishes into bounded transactions. A batch returns one result per request, so callers can distinguish stored messages, duplicates, and gaps without losing the topic's order. Batch entries use consecutive producer sequence numbers and report an invalid entry by its position.
Usage accounting follows the durable result. The service counts only messages that it stores, uses the size of inline or staged payload data, and does not count retries. Publish analytics aggregate the stored message count and bytes by tenant and source rather than by API token.
When a producer publishes data, the main stages are:
- The service checks tenant access and limits.
- The topic and producer sequence are checked.
- The message receives a topic offset and is committed.
- Live readers are prompted to check the committed data.
References: pkg/…/streams.go• L107-161 pkg/…/streams.go• L259-403 pkg/…/streams.sql• L36-102 internal/…/server.go• L39-176 internal/…/server.go• L183-200 internal/…/usage_test.go• L121-175
Payload staging
Payload staging separates a large upload from the publish request. A producer uploads the payload first and receives a versioned reference. The later publish stores that reference with the message, and consumers fetch the payload through the tenant-scoped reference.
The service keeps an uploaded payload longer than its message because the upload can precede the publish. If the upload is too old to share the message's retention life, the service copies it during the publish. An unpublished upload remains independent and is removed when its storage partition expires.
Payload upload and download require the durable-stream entitlement. Download also requires permission to view payloads. The service counts delivered bytes only after a complete response body is written, so a missing or interrupted download is not counted as delivered.
References: pkg/…/streams.go• L29-45 pkg/…/streams.go• L163-170 pkg/…/streams.go• L383-435 pkg/…/streams_payload_ref.go• L13-48 pkg/…/streams.sql• L200-241 api/…/payload.go• L24-136
Retention and replay
Retention gives each tenant a bounded history instead of an unbounded event log. Readers see only messages inside that tenant's retention window, while maintenance removes expired messages and any payloads attached to them.
A cursor is a resumable position in one topic. It identifies the topic position and its creation time. A read without a cursor starts at the oldest retained message. A read with a cursor returns messages after that position.
The service can reset a topic's starting offset to a supplied cursor or to the latest reserved offset. The reset waits for in-flight publishes and caps a supplied offset at the latest reserved offset. A reset start cursor has no creation time, so it does not expire; reads begin with the oldest retained message after that offset.
If retention has removed data at or before a supplied cursor, the service rejects the resume. This prevents a consumer from silently skipping messages. The consumer must start a new read from the oldest retained point or use its own recovery policy.
References: pkg/…/streams.go• L50-59 pkg/…/streams.go• L159-177 pkg/…/streams.go• L437-465 pkg/…/streams.go• L562-590 pkg/…/streams.go• L667-696 pkg/…/streams.sql• L153-174 pkg/…/streams.sql• L277-291
Subscription delivery
Subscription combines replay with live delivery. A consumer first reads retained messages after its cursor. It then remains attached while the service checks for new messages and returns them in bounded batches.
Each delivered batch advances the consumer's cursor. This lets a reconnect resume from the last confirmed position. The delivery path must therefore preserve topic order and must not move a cursor past data that it did not return.
The service keeps the durable store separate from the live prompt. A wake can make a new message arrive quickly, while a later poll provides recovery when no wake arrives. This relationship lets live delivery remain responsive without making a transient signal part of the data contract.
Delivery accounting follows the send result. External subscribers are charged for inline payload bytes only after a frame is sent; referenced payload bytes are counted when the subscriber fetches them. In-process subscribers used by the platform are not billed.
An idle subscription can close after a long period without activity. A reconnect with the last cursor resumes from the durable stream rather than from the lost live session.
This diagram shows how a publish becomes retained data and then reaches replaying or live consumers.
flowchart LR
P[Producer publish] --> V[Access and input checks]
V --> S[Retained topic data]
S --> R[Replay after cursor]
S --> W[Live wake]
W --> L[Live read]
R --> B[Bounded consumer batches]
L --> B
B --> C[Next cursor]
References: pkg/…/streams.go• L159-177 pkg/…/streams.sql• L176-198 internal/…/server.go• L231-355 internal/…/server.go• L255-285 internal/…/streams.go• L73-114 internal/…/usage_test.go• L227-290
Platform relationships
Durable streams rely on shared tenant controls and database maintenance. Tenant entitlements decide whether the feature is available. Tenant limits define topic count, message use, and retention. Database partitions keep message and payload retention work bounded over time.
The stream repository is the storage boundary for topic registration, ordered publishing, replay reads, payload references, metadata, cursor retention checks, and topic start resets. The database query layer supplies the ordered offsets and cleanup actions that make those behaviors consistent.
The service exposes publish, subscribe, reset, and topic metadata operations. The HTTP handler provides staged payload upload and download for consumers that need data outside the message frame. Platform components can also subscribe in process through the same delivery path.
The service owns the publish workers and topic pollers that coordinate these operations. It can cancel open subscriptions during shutdown and close the workers and wake subscriptions during cleanup.
Read Durable stream contracts and clients for the public stream contract and client access. Read Database schema and migrations for the wider database lifecycle. Read Repositories, messaging, and configuration for shared repository and message-system relationships.
References: pkg/…/streams.go• L144-177 pkg/…/streams.sql• L104-174 pkg/…/streams.sql• L277-291 cmd/…/20261002182802_v1_0_160_durable_streams.sql• L60-119 internal/…/server.go• L29-48 internal/…/server.go• L288-304 internal/…/streams.go• L22-30 internal/…/streams.go• L96-125 api/…/payload.go• L24-117
Change checklist
When these files or sections are changed, remember to consider these:
- Changes to topic addresses, cursors, producer sequence rules, or topic start resets must stay aligned between validation, stored ordering, and client resume behavior.
- Changes to payload staging must preserve tenant ownership, reference decoding, message retention, and cleanup of linked payloads.
- Changes to retention limits must preserve the relationship between tenant settings, partition removal, explicit message deletion, and cursor-expiry results.
- Changes to batch ordering or offset reservation must preserve the guarantee that a reader cannot move past a message that becomes visible later.
- Changes to message or payload size accounting must stay aligned with transport limits, tenant message limits, and delivered-byte usage.
- Changes to publish result handling must count only stored messages and bytes. Retries and entries after a gap must not create duplicate usage or analytics records.
- Changes to subscription send paths must preserve the split between externally billed delivery, in-process delivery, inline payload bytes, and referenced payload bytes.
- Changes to payload access or response writing must preserve the rule that only complete downloads count as delivered.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| Tenant is not entitled to durable streams | Stream use is denied | Set the tenant entitlement before publish or subscribe access |
| Producer retries a stored sequence | No second message is stored | Retry with the same producer ID and sequence |
| Producer skips the next sequence | The publish is reported as a gap | Advance each producer sequence in order |
| Consumer uses a cursor older than retention | Resume is rejected instead of silently skipping data | Start from the oldest retained point and apply recovery logic |
| Uploaded payload belongs to another tenant or has expired | The publish or payload read fails | Keep the reference with its owning tenant and publish within retention |
| Live wake is lost | Delivery waits for the fallback poll | Keep the subscription active and persist the latest cursor |
| Topic, message, or retention limit is reached | Topic creation or publishing is rejected | Check tenant limits before increasing stream use |
| Payload download is denied or ends early | The payload is not returned or counted as delivered | Grant payload-view access and retry an interrupted download |
| Subscriber send fails | The failed frame is not counted as delivered | Reconnect and resume from the last confirmed cursor |
Key files
| File | Why to read it |
|---|---|
| internal/…/server.go | Defines the service flow for publishing, subscribing, resetting topics, reading metadata, resolving cursors, and metering delivery. |
| internal/…/streams.go | Defines service ownership of publish workers, topic pollers, in-process subscriptions, and shutdown cleanup. |
| internal/…/usage_test.go | Demonstrates tenant usage and analytics accounting for stored publishes and delivered payloads. |
| api/…/payload.go | Defines the staged payload upload and download boundary, access checks, limits, and delivery accounting. |
| pkg/…/streams.go | Defines the storage boundary for topics, ordered messages, replay, payloads, metadata, cursor retention, and topic resets. |
| pkg/…/streams_payload_ref.go | Defines the stable reference used to upload and later fetch large payloads. |
| pkg/…/streams.sql | Shows how offsets, producer order, retention reads, payload links, cleanup, and topic resets act as one data flow. |
| pkg/…/streams_repository_test.go | Demonstrates the repository guarantees for ordering, cursor safety, batching, retention, and staged payloads. |
| pkg/…/streams_partition_job_test.go | Demonstrates the retention life of message, payload, and producer-cursor storage. |