fluxgatedocs
Dashboard ↗GitHub

10. Dead letters are indexed for reading, not moved

Status: Accepted

Context#

A batch that exhausts its delivery attempts is forwarded by Pub/Sub to a dead-letter topic, where an inspection subscription holds it for seven days. That preserved the message and told nobody. The tenant whose data it was had a 202 and an empty query. An operator had an alert that the subscription was not empty, and to learn more had to pull messages by hand: a subscription cannot be searched, counted by tenant, or read without leasing what it returns, and after seven days the evidence is gone.

A dashboard that answers "are there any dead letters, and whose" needs them somewhere it can query. Three designs were credible:

  • Let the query API pull the subscription on request. No new storage, but Pub/Sub has no peek: every read leases messages away from the operator who is trying to replay them, returns an arbitrary subset, and cannot filter by tenant without reading everyone's. It would also give the read service Pub/Sub access, which ADR 6 deliberately withheld.
  • Move dead letters into Postgres -- consume the inspection subscription, store each payload, acknowledge it -- and build replay on the table. One place to look, but it replaces the broker as the system of record for exactly the messages that are hardest to lose, makes the existing alert and runbook wrong, and stores a second copy of the traffic at the moment the database is most likely to be the thing that failed.
  • Index them. A second subscription on the dead-letter topic, read by the aggregator, which stores a bounded description of each message. The broker keeps the payload and stays the only source for a replay.

Decision#

Index them: internal/deadletter, the dead_letters table of migration 0011, and GET /v1/dead-letters on the query API.

  • A second subscription, not a second reader. Each subscription receives its own copy of every message, so the index acknowledges what it has recorded without taking anything from the inspection subscription. The alert on that subscription and the replay procedure are unchanged.
  • A description, not the payload. A row holds counts, at most 20 metric names, at most 20 sampled points within 16 KiB, the windows the batch feeds, and for an undecodable payload its first 2 KiB as text. A database outage dead-letters everything in flight; the index must not respond by writing all of it back into that database.
  • The reason is recomputed, because the broker does not record one. The recorder decodes the payload exactly as the aggregator does. One that fails was rejected on every attempt and cannot be replayed unchanged. One that succeeds was failed by something outside it and can be.
  • Resolution is read from the delivery ledger. A dead letter is resolved when every window its batch feeds has a claim in processed_batches (ADR 2). Nothing has to tell the index that a replay happened, and a message dead-lettered after its data committed -- a lost acknowledgement -- is recorded as resolved from the start. Committed windows are accumulated on the row, since the ledger is pruned on its own schedule.
  • The aggregator hosts it, and it cannot fail the aggregator. It already holds a subscriber identity and write access to Postgres. Every other component of that process stops it on failure; this one logs, backs off and retries, because a subscription Terraform has not created yet must cost the index and nothing else.
  • An empty list has to be trustworthy. Since the index may be switched off or unable to read, it records when it last confirmed it was reading the queue, and the API returns that beside the counts. "No dead letters" with a recent checked_at is good news; without one it is no news.
  • Tenancy. Rows are read by equality on the credential's tenant. A readable payload names its tenant; an unreadable one is attributed by the attribute the ingest API published. A message with no usable tenant is stored with none, and no credential reaches it.

Consequences#

Measured. Against the Pub/Sub emulator and PostgreSQL, a message with an unsupported schema version and a valid message whose consumer always failed were both forwarded after five attempts and indexed with the broker's attempt count, source subscription and publish time. Committing the valid batch's two windows resolved its entry on the next pass and left the unreadable one open. Pulling a subscription that does not exist failed outright, the recorder kept running, and none of its status reports claimed to be reading. The packaged verifier repeats the path through the restricted database roles and both tenant keys. A fuzz target feeds the recorder arbitrary bytes and attributes and requires every entry to be storable and bounded.

What it costs. One subscription, two tables, a pass over open entries every 15 seconds (an index-only scan when there are none), and one small upsert per pass. An entry is at most a few tens of kilobytes; a flood of dead letters adds rows, not payloads, and they expire at PUBSUB_DLQ_RETENTION.

What it does not do. It cannot replay or discard anything, by design: replay republishes under an original identity and stays an operator's decision (recovery). It does not say why delivery of a valid payload failed; the aggregator's logs for the request_id do. Messages dead-lettered before the index subscription existed are not indexed. A dead letter with no usable tenant is counted in metrics and logs and listed for nobody. Entries are per message, so a batch dead-lettered twice has two.

When to revisit. If operators need a cross-tenant view, add an operator credential and a route that is not scoped to one tenant; the table already holds what it needs. If replay from the dashboard is wanted, it belongs behind that same credential, republishing from the broker's copy and not from this table. If the cause of an exhausted delivery matters enough to store, have the aggregator record its last failure per message where the recorder can join it.

Something to improve? Edit this page View source

Search the complete documentation.