Deep dives Chapter 7 of 9

Work after the request

How Chatto runs slow work as durable background jobs that survive a restart and stay visible when they fail.

5 min Checked against its records on 2026-12-01 Source file

Some work is too slow for a request, or it needs a system that can fail. Examples are the conversion of a video, a call to the webhook of a bot, and a push notification. Chatto does not do this work in the request. It records the work as a job, and a worker does the job later.

A job survives a restart of the server. When an attempt fails, Chatto tries again after a delay. When a job fails permanently, it stays in a list. An operator can look at it, retry it, or delete it.

A second, small log

Chatto keeps its data in NATS JetStream. A JetStream stream is an ordered log of messages that the server keeps on disk. The facts of the chat domain go to the EVT stream. The facts of jobs go to a different stream, JOBS (ADR-123).

Chatto does not keep one record for each job and then change it. It appends small facts. Each fact has a subject, which is the name that JetStream files the message under:

jobs.{type}.{id}.queued
jobs.{type}.{id}.started
jobs.{type}.{id}.attempt_failed
jobs.{type}.{id}.succeeded

The other facts are failed, retried, and deleted. The state of a job is the result of its facts, read in order. Job facts are operational facts, not domain facts. EVT keeps its full history, but JOBS forgets facts.

Chatto appends queued only when the job has no facts. Thus, a repeated request to add the same job changes nothing.

How a job runs

A durable consumer is a named read position on a stream. JetStream stores this position, so it continues to exist while no process reads from it. Each job type has one durable consumer and its own pool of workers. Thus, a slow job type cannot delay a different type.

The queue has no claims or leases of its own. It uses the delivery rules of the consumer (ADR-123):

  • A worker must acknowledge a delivered fact in a set time. This time is the claim on the job. The worker extends it every fifteen seconds while the job runs.
  • The delivery count is the attempt number.
  • A failed attempt returns the delivery with a delay. The delay doubles for each attempt, up to thirty minutes.
  • After a crash, the time ends, and JetStream delivers the fact to a different process.

The worker writes succeeded or failed before it acknowledges the delivery. An attempt that a restart interrupts counts as an attempt.

All processes share each consumer, so its position belongs to the deployment, not to one process (ADR-069). A worker never deletes its consumer when it stops, and JetStream does not remove an idle consumer. An incompatible change gets a new name with a version, such as -v2.

What starts a job

Many jobs start from a fact that is already durable. Chatto calls this work a durable effect: a durable fact starts work in a different storage system or an external service. Because the fact is durable, Chatto can find unfinished work again after a crash.

Video is a good example. A user posts a message with a video. Chatto appends a processing-started fact in the same atomic write as the message. Thus, a rejected message cannot create work, and an accepted message cannot lose its work (ADR-066, FDR-008). A durable consumer on EVT reads that fact and adds a media job. It acknowledges the fact only after JOBS accepts the job. The media worker can run in the server process, or in a separate process that you start with chatto asset-processing.

Other effects use the same pattern, such as the deletion of the stored bytes of a deleted attachment. Such a job stores only a reference to its source fact, never content or credentials. Each attempt reads the source fact again.

Webhooks and push notifications

A bot can register an HTTP endpoint. When a user mentions the bot or sends it a direct message, Chatto adds a webhook job (ADR-097). ADR-123 replaces the queue of ADR-097. Its other rules stay: before each attempt, the handler checks the current endpoint, the access of the bot, and the state of the message. Chatto never follows a redirect.

Push notifications use two steps. First, a durable consumer on EVT writes notification facts to the NOTIFICATIONS stream (ADR-076). Then each new notification that permits a push becomes a notification_alert job. An alert expires two minutes after its source, so an old alert does not arrive late (FDR-013).

Delivery to an external system is at least once. If a process crashes after the other system accepted the request, the next attempt can send it again. Thus, a webhook request or a push can arrive twice.

Failed jobs stay visible

The Jobs page in the server settings shows the jobs with their state and attempt count (FDR-056). The page reads a projection of the JOBS stream that each server process keeps in memory.

  • A succeeded job leaves the list after three days.
  • A failed job stays until an operator retries or deletes it. The setting core.job_failed_retention can remove failed jobs automatically.
  • A retry applies only to the failure that the operator saw. Two operators cannot start two retries of the same failure.

Webhook and alert jobs have an attempt limit. Media, cleanup, and key-deletion jobs have none: an outage of a tool or a store must not leave keys undeleted.

Read the code

The records behind this chapter

Esc