Skip to content

    Guides06

    Dead letters and failed processes

    What happens to a handler run the runtime gave up on, how to retry or discard it, and how a failed process keeps its later events in order.

    This page is about what an operator does with a run the runtime gave up on. When a run is retried and when it gives up is in retries and timeouts; why a failed process parks its events is in When a process fails.

    A dead letter is a handler run the runtime gave up on: a policy or process handler that failed for good, or a scheduled command that was dropped. The subscriber’s checkpoint moved on without it, so it is up to an operator to decide what happens to it. bounda dead-letters is that operator’s tool:

    Terminal windowShell
    bounda dead-letters list
    bounda dead-letters retry 019a0c4e-…
    bounda dead-letters discard 019a0c4e-…

    list prints the failed letters, and its options narrow it or print JSON for a script. retry runs the failed handler once more and marks the letter retried if it succeeds; when it fails again the error is printed and the letter stays failed. discard marks it discarded without running anything. Letters are never deleted by these commands; they are the record of what happened, until the retention you set removes the resolved ones.

    What a retry runs, and when it marks the letter, depends on the kind:

    Kind What the retry runs Marked retried
    Policy The handler again, for the stored event, with the event’s correlation. The inbox is bypassed on purpose: it already says the handler ran, and you are asking for another run. In the same transaction as what the run writes
    Process The handler again for the stored event, with the instance’s current state, or for a deadline’s letter (deadline:<field>) the handler of that deadline, with a new idempotencyKey; an event that completes the process completes it. Then the events parked behind the failure, in order, and the process is started again, its deadlines scheduled at their moments (one already past runs at once); see a failed process. Once the instance has drained what was parked; a retry cut short is taken up by retrying the same letter
    Follow-up of a timed-out process Its handler and nothing else: the process stays timed out, and a handler a deploy removed lets the event through. In the same transaction as what the run writes
    Scheduled command The dropped command again, with the payload the letter recorded, its personal fields opened. A rejection settles the letter too: it is the aggregate’s answer, logged as command rejected. In the same transaction as what the run writes
    Recipient The recipient’s handler again, handler naming its port, for the stored event, with a new idempotencyKey; a notice a later forgetting of the person as a whole superseded is settled without running it. A port no longer declared is refused with the error that filed it, until it is declared again or named in recipient.formerly. Once the handler resolves

    Two retries of one letter, or a retry and a discard, never both settle it, even when they run at once: whichever gets there second is refused with DeadLetterSettledError (DEAD_LETTER_SETTLED). A retry marked in the same transaction as its writes stores nothing when it is refused, so a double click does not store a command’s decision twice, though the handler may have run in both; a process’s retry has already handled its events by then. A letter the app as it now is cannot retry is refused with DeadLetterNotRetriableError (DEAD_LETTER_NOT_RETRIABLE) without running anything: its policy, process or scheduled command is gone from the registry, its policy or process no longer handles the event, its instance failed on another step whose letter comes first, or the person whose data its scheduled command holds was forgotten since.

    The same operations are on the app as app.deadLetters — list, count, get, retry and discard — for a script or an admin route.

    One kind of letter the runtime retries itself: those of a person whose processing was restricted, errorCode: "PROCESSING_RESTRICTED" with the person in person. unrestrict schedules a run of the worker that retries them in the order of list, each as retry would, in its own transaction, so a crash leaves the rest to the next run. A letter that fails again for good is written again under the same id, as that failure’s letter, with its attempts counted up, and stays failed for you; one that fails in a way worth retrying is left as it was and the run is retried; one refused because the person was restricted again waits for the next lift. A letter the app cannot retry is logged and left as it is.

    A letter that was retried or discarded records when, as resolvedAt. The scheduled-command worker removes each one once it was resolved longer ago than runtime.deadLetters.retention, 30 days unless you set another, a batch on each pass, together with its error text and payload. A failed letter stays until someone settles it, even one whose handler the app no longer has: only an operator knows whether the work it dropped is still owed, and discarding it settles it. So that none sits unseen, bounda status and app.status().deadLetterRetention.withoutHandler count the failed letters no policy, process, command or recipient the app runs would retry.

    TypeScript
    export default defineConfig({
    storage: sqlite({ path: "./data/app.db" }),
    runtime: { deadLetters: { retention: "90d" } },
    });

    A resolved letter that names a person whose processing is restricted, in person, statePerson, readPersons or the fields of its command, stays past the retention until the restriction is lifted: a restriction keeps data from being erased. The check is made in the step that removes the letter, so a restriction recorded meanwhile still keeps it, and the letters kept are read again every hour, since a restriction lifted on another instance tells nobody here. A restriction keeps a letter only while what it holds of the person still opens: once a key its error text is sealed under is gone, the person forgotten, as a whole or in that category, or the text sealed for good by a redaction, nothing of theirs is left in the text, and the letter goes, unless the fields of a command a forgetting kept name them. bounda status counts the letters kept this way.

    A host without a polling worker, such as a Durable Object, is woken for it through nextDueAt(). One instance purges at a time, under a lease the others leave alone until it lapses. A process instance whose letter was discarded stays given up once the letter is gone: discarding wrote ProcessAbandoned in its stream, so the events that reach it afterwards are dropped whatever became of the letter.

    Forgetting a tenant removes every letter, failed or not, but those of the notices of the forgetting to its recipients, which go once every recipient was told: no letter of the tenant’s work is retried again, and another instance that still holds keys in memory has no text left to open.

    A letter records what gave up and why: its kind and handler, the event it was about (or the dropped command, with its payload), how many attempts were made and when, the errorType, and the error itself as errorName (its class, ValidationError) and errorCode (its code, when it is one of Bounda’s), with its errorMessage and errorStack.

    The message of an error a handler throws may quote what the handler read of a person: cannot mail ana@example.com. So a letter also names the person its work was about, the one the event names for a policy, process or recipient letter and the one the command’s payload names for a scheduled command, and stores the message and the stack encrypted with that person’s key, as a personal field is. A process letter whose event names one person while the state of its instance names another names that one too, as statePerson, and its text is encrypted under the keys of both, one inside the other: forgetting either leaves it unreadable, and either finds it in their export. So does everyone else whose data the work read, as the aggregates the commands it dispatched loaded: the letter names them in readPersons, and its text is encrypted under their keys too. The export of each holds back a text encrypted under more than one person, which may quote another’s data, as article 15(4) asks. A letter that names nobody keeps them in the clear: a process deadline’s, a policy’s or process’s for an event without personal fields, or whose fields were forgotten before the handler read them, and a scheduled command’s whose payload holds no marked field. A letter filed before the fields its work holds were marked is sealed in place once they are, by the backfill. app.deadLetters and bounda dead-letters list open them for you, and every list, get, retry and discard is recorded in the access log, naming the persons its letters name, with outcome: "failed" for a retry that fails again, whose error may quote them, and outcome: "refused", naming no one, for a call refused before it opened a letter, such as a retry of one already settled. Once the person is forgotten, even while the handler was failing, and even when they are given a new key before it gives up, the letter stays, with its name and code, and the message and the stack read as null: the process can still be retried, and the stream still says it failed. ProcessFailed and ScheduledCommandFailed name the error the same way, errorName and errorCode, and never carry its text.

    While a person a letter names is restricted, list and get keep its message, its stack and its payload’s fields sealed, reading as null, and say restricted: true; so does a scheduled command’s letter whose fields a forgetting kept under an erasure exemption. app.deadLetters.list(args, { includeRestricted: "legal-claims" }) and get(id, { includeRestricted }) open them on a basis of article 18(2), recorded in the access log with those persons.

    A process fails when one of its handlers fails for good: the instance records ProcessFailed, the run is dead-lettered, and the instance stops. Events keep arriving for it, and dropping them would lose them: the OrderPaid that comes while the process is failed on a reminder is exactly the one it must not miss. So the instance parks them. Each event that would do something in it (it has a handler, or completes the process) is recorded in the instance’s stream as ProcessEventParked, in the order it arrived, and nothing of the process runs for that instance meanwhile, deadlines included; every other instance of the process carries on. A follow-up of a timed-out process is the exception: the process has ended, so its failure is only dead-lettered, and retrying the letter runs the handler again.

    Retrying the dead letter of the failure is what brings the instance back:

    1. the failed handler runs again;
    2. the parked events are handled one by one, in order, with the same idempotencyKey each would have had; a deadline that came due before a parked event arrived runs before it, as it would have if the process had not failed;
    3. once none is left the instance records ProcessResumed, acted by whoever retried, as the retried handler is, is started again, and its deadlines are scheduled again.

    A failure whose handler a deploy has since removed, an event’s or a deadline’s, is let through, and the retry goes on with what is parked. When the failure is the process’s timeout, retrying it ends the process as timed out and drops what is parked, as for any timed-out instance; what its at-timeout.ts causes still reaches its handlers.

    An event that arrives during the retry is parked too and handled before the instance resumes, so nothing overtakes an older event. If a parked event fails again, for whatever reason, it becomes the new failure at once: it is dead-lettered without retries, the instance stays failed, and the events after it stay parked until that letter is retried. A parked event the process no longer handles, after a deploy removed its handler, is let through. A parked event that completes the process completes it, and what is parked after it is dropped, as for any completed instance.

    bounda dead-letters list says how many events wait behind a failure (3 events are parked behind it), and so does parked on the letters of app.deadLetters. Discarding the letter gives the instance up: in the same transaction as the discard, ProcessAbandoned (with the letterId, acted by whoever discards) ends it with the status abandoned. Its parked events never run, though they stay in its history, and the events that reach it afterwards are dropped, as for an instance that has ended. Only a discard does that: an instance whose letter is gone for any other reason keeps parking until its failure is retried.

    The failure is state as well: ProcessFailed names its dead letter (letterId), and the two are written in the same transaction, with the claim of the event that failed. A failure that could not be written leaves nothing, and the handler runs again once its claim’s lease expires. The same holds for each step of a retry: the retried handler, each parked event or deadline drained and the final ProcessResumed are one transaction each, so a retry cut short goes on from the last step written on the next retry.

    Parking starts when the failure is final. While a step is still waiting for a retry, the process is handed none of its later events, whatever their instance, so a long back-off holds every instance of the process for as long.