Jobs that run on their own: an outbox, a queue and Little's law
How we made Tesar's ingestion run with nobody at the keyboard, what we broke on purpose to prove it, and why a 60-year-old formula explained both our throughput curve and our worst bug.
At the start of this phase, someone at Tesar still had to type a command to make data arrive. At the end of it, nobody does.
An operator creates an asset over HTTP. In the same database transaction, the platform writes eighteen months of ingestion Jobs for it. A relay carries them to NATS. A worker that is always running picks each one up, fetches the data, writes it, and reports how it ended. The next day, a tick adds one more day. Nobody types anything.
That sentence hides most of the work. This post is about the parts that are worth stealing: the outbox, the slots, the retries, the proof, and the one formula that explained our numbers better than any dashboard.
A Job is born inside the change that owes it
The classic mistake in event-driven systems is the dual write: commit a row to Postgres, then publish a message to the broker. Sooner or later the process dies between the two, and you have an asset with no Jobs, or Jobs for an asset that was rolled back.
We don't publish from the code that changes data. Ever. The rule is simple: every message leaves through an outbox, committed in the same transaction as the change it announces. Creating an asset and writing its eighteen Jobs is one commit. Either both exist or neither does.
A relay, running in every copy of the service, drains the outbox:
- Take up to 100 rows with
select … for update skip locked. - Publish each one to NATS JetStream, with the outbox row id as
Nats-Msg-Id. - Wait for every ack.
- Delete the rows and commit.
skip locked lets many copies drain the same table without coordinating: each row is taken by exactly one of them. The message id makes the publish safe to repeat: if the relay dies after publishing and before deleting, the next attempt sends the same id and JetStream drops the copy.
That last point generalises into the rule I care about most in this system: everything is safe to repeat. Delivery is at least once, so every call and every message must be harmless the second time. A Job carries a deterministic run key (a UUID v5 of what it covers), so a repeated Job lands in the same run, and the store skips what is already there. A JobDone delivered twice still leaves one outcome row, enforced by a partial unique index. Two copies reaching midnight together still produce one tick, because only one of them can insert today into the tick table.
Workers pull, they are never pushed
A worker has a fixed number of slots. It asks NATS for a Job only when a slot is free. That's the whole back-pressure story: the queue never pushes more work than the worker can hold, and the queue depth is the honest signal for autoscaling (KEDA, in production).
While a Job runs, the worker tells NATS it is still in progress every third of the ack wait. It appends in batches of up to 10,000 rows, finishes the run, publishes JobDone, and only then acks the message. If the worker is killed anywhere before that ack, the Job comes back, lands in the same run, and writes nothing twice.
When the provider has no file yet (NASA publishes a day's rain at about 14:30 UTC the next day), the Job is handed back with a wait that doubles from one second up to an hour. A message that keeps failing is eventually parked, and the Jobs behind it keep running.
Little's law, or why slots are the lever
There is a note in my knowledge base that I keep coming back to. Little's law:
L = λ × W
- L: how many items are in the system (Jobs in flight)
- λ: how fast they arrive, and in steady state, how fast they leave (Jobs per minute)
- W: how long each one stays (seconds per Job)
It holds for any stable system, regardless of the distribution of arrivals or service times. That generality is what makes it useful: you don't need a model of your workers to use it, only three numbers you can measure.
In a worker with slots, L is the number of slots when the queue has work. So throughput is λ = slots / W. If W stays constant as you add slots, throughput grows in a straight line. If it doesn't, W is telling you where the bottleneck moved.
We measured exactly this in the phase proof, with 1,800 Jobs from 100 assets and a recorded provider that takes 0.1 s per file, like a real one or faster:

| Slots (L) | Jobs a minute (λ) | W = L / λ |
|---|---|---|
| 2 | 37 | 3.24 s |
| 8 | 145 | 3.31 s |
| 16 | 280 | 3.43 s |
| 32 | 548 | 3.50 s |
W barely moves. Each Job spends about 3.3 s waiting on the network, and the worker is idle for almost all of it. So the lever is concurrency, not more machines: two workers of 16 slots did 543 Jobs a minute, the same as one worker of 32. A slot costs about 0.3 MiB next to a worker's 55 MiB baseline, so slots are nearly free.
Now take the network out. With a provider that answers instantly, the only cost left is the worker and the database:

From 8 to 16 slots, λ stays flat (7,689 → 7,614 Jobs a minute) and W doubles (62 → 126 ms). That's Little's law telling you the worker itself became the server: extra slots no longer do work, they just wait in line inside the process. At that point the lever flips. A second worker on the same queue took us from 7,689 to 11,384 Jobs a minute.
Same formula, two opposite answers. With an I/O-bound provider, add slots. With a CPU-bound path, add workers. You don't need to guess which one you're in. Compute W.
The bug Little's law would have caught on paper
The most interesting defect the proof found was not a crash. It was arithmetic.
JetStream consumers have a max_ack_pending: the most messages that can be out with a consumer and not yet acked. We had set it to 1,000, which sounds generous. But a Job waiting for tomorrow's file is still pending while it waits, and it waits about 14.5 hours.
Run that through the formula. If every asset produces one daily Job and each waits 14.5 h, then L = assets × 14.5 / 24. At roughly 1,650 assets, L crosses 1,000:

Past that point, every slot of the consumer is held by a Job that is only waiting, and every Job of that source stops. Including the ones whose files are already there. A long W quietly turned into head-of-line blocking.
The same reasoning found its sibling: our retry window parked a Job after about four hours of doubling waits, but the provider's file shows up about fourteen hours after the day ends. Every daily Job for yesterday parked, and a backfill created early in the morning lost most of its newest month for good.
Both fixes come from sizing to the real W instead of a round number: the retry window is now a full day (max_deliver 36, with waits doubling to an hour), and the ack-pending cap is gone. The alert for "a Job with no outcome" moved from 2 hours to 24 hours, or a limit each source declares for itself, because a 2-hour alert would have fired every single day on honest data.
This is where my old note on queue strategy connects. It argues that queues should be separated by the time their items take to complete, not by feature or team: real time, near real time, batch. Each class gets its own consumers and its own expectations. We split JetStream consumers by source, and each source carries its own W: how long its provider takes to publish, how long its retries may run, when it's overdue. A rain Job that waits half a day for its file and a radar Job that answers in seconds should never share a ceiling sized for either one.
Breaking it on purpose
Nothing ships unproven. Every phase ends with a proof: run the demo end to end, break the stack on purpose, measure, and fix what breaks. This one injected six failures, each expected to end with nothing lost and nothing written twice:
- Kill the database service in the middle of a write, then retry the Job.
- Kill a worker after it wrote, before it reported
JobDone. - Tell a worker to stop while all its slots are busy.
- Stop NATS during ingest.
- Stop one of two service copies while both serve.
- Send a message that always fails.
The one I like showing is NATS going away for ten minutes in the middle of a heavy ingest: two service copies, a worker running the backfill of 50 assets, and four writers at 100 appends a second.

Nothing fell over. The outbox kept growing in Postgres, 60,092 rows on one side and 360 Jobs on the other, because the outbox is just a table and Postgres was fine. When NATS came back, both outboxes drained in 2.2 s. Afterwards: 806,000 rows, each stored once; 70,580 events, exactly the 69,680 writer appends plus 900 Jobs; 900 outcomes, one per Job. Fourteen Jobs ran a second time and wrote zero rows.
That's what "safe to repeat" buys you. The broker can disappear and the worst outcome is latency.
A few more numbers from the proof:
- The outbox under steady load: 1,890 to 3,228 appends a second, never more than 256 rows deep, lag p95 around 0.1 s.
- A burst of 500 assets from 8 callers wrote 9,000 Jobs. All of them were on NATS 0.35 s after the last commit.
- Six defects found and fixed, each with a test that fails without its fix. Two of them are the Little's law bugs above.
What I'd tell another team
- Never publish from the code that changes data. Commit the message with the change, and let a relay move it. It's a table, a loop and a unique message id.
- Make every step safe to repeat before you make it fast. At-least-once delivery is the only delivery you actually get.
- Pull into slots. The worker decides how much it can hold; the queue depth tells the autoscaler the truth.
- Measure W. Little's law turns "should we add slots or machines?" into division. And size every cap and timeout to the real W of the slowest honest case, not to a round number.
- Break it on purpose before production does. The two worst bugs of this phase showed up only when we ran real provider timing against real queue settings.
Next phase, every free source we use comes in the same way: a catalog row and one file of code. The machinery in this post is what makes that true.