My job queue said completed. It was lying.
Inside TaskForge: the small queue I built in March, the ways it lost work, and the November rebuild with leases, fencing tokens and a factory floor you can break.
A job queue has one promise to keep: if it says it has your job, your job happens. It’s a very easy promise to make in sixty lines of TypeScript, and a surprisingly easy one to break in the same sixty.
TaskForge started that small in March 2026, as an API, a Redis list and one worker. The frontend is recent. It was built in November 2026, along with the rewrite that made the backend survive crashes, and this post was updated to cover both. So it’s a story in two halves: the queue as it was, and what it took to make it honest.

March: sixty lines of optimism
The first worker took an ID from Redis, loaded the row from Postgres and did this:
// March 2026, trimmed
await query("UPDATE jobs SET status = $1 WHERE id = $2", ["processing", jobId]);
if (job.type === "email") {
await emailJob(job.payload);
}
if (job.type === "report") {
await reportJob(job.payload);
}
await query(
"UPDATE jobs SET status = $1, completed_at = NOW() WHERE id = $2",
["completed", jobId]
);
Read it again with a job of type nonsense. Neither if matches, nothing runs, and the job is marked completed.
That was the funny bug. These are the others that turned up when that version was finally poked properly:
| what happened | what March did | what it does now |
|---|---|---|
| unknown job type | marked it completed | rejects it |
| worker killed mid-job | processing, forever | another worker finishes it |
| Redis down | request hung, job orphaned | accepts it, runs it later |
| Postgres restarted | API crashed | reconnects |
| empty database | every request failed | migrates itself |
None of these show up on the happy path, which is the only path a demo ever takes.
The shape of it now
The important change is who gets to be right. Postgres holds the truth. Redis is only an index. You can delete the whole Redis key and lose nothing, because every worker also runs a small sweeper that puts due jobs back:
await redis.zadd(QUEUE_KEY, "NX", ...members);
It’s a sorted set and not a list, so adding the same ID twice does nothing. That makes “not sure it’s in there? add it again” a safe repair strategy.
A job is borrowed, never owned
A worker that dies can’t tell you it died. So claiming a job only lends it out: the worker gets a lease, and renews it with a heartbeat for as long as the job runs. A killed process stops renewing, and that silence is the signal.
SELECT id FROM jobs
WHERE status = 'processing' AND (lease_until IS NULL OR lease_until < now())
LIMIT 500 FOR UPDATE SKIP LOCKED
SKIP LOCKED is doing quiet, lovely work there. Every worker can run that sweep at the same moment and each gets
different rows, so there is no leader to elect and no leader to lose.
The dead sometimes come back
Leases create a nastier problem. A worker frozen for fifteen seconds looks exactly like a dead one. Its job gets handed to somebody else, and then it wakes up, finishes, and proudly writes a result over the new owner’s.
So every claim comes with a random token, and every write has to show it:
-- trimmed
UPDATE jobs SET status = 'completed', result = $3::jsonb, lease_token = NULL
WHERE id = $1 AND lease_token = $2 AND status = 'processing'
The old token matches zero rows. The zombie can be as confident as it likes. It’s the same trick as the seq number
that throws away stale searches in TuiNime, with higher stakes.
Urgent shouldn’t mean forever
Three queues for high, normal and low is the obvious design, and it starves low the day high never empties. TaskForge has one queue, where priority is a head start measured in time:
// trimmed
const PRIORITY_OFFSET_MS = { high: -60_000, normal: 0, low: 60_000 };
export const queueScore = (priority: Priority, runAt: Date) =>
runAt.getTime() + PRIORITY_OFFSET_MS[priority];
The lowest score runs first. A low job that has waited more than two minutes beats a high job that arrived just now. That’s the whole anti-starvation system: one addition.
November: giving it a face
A backend that recovers from crashes looks exactly like a backend that never crashed, which makes it a terrible demo. So the new frontend is a foundry. Each ingot is a real job, each hammer is a real process, and each worker has a Kill button that really kills it.

There’s no 3D library under it. Isometric projection is two lines of math on a canvas:
export const project = (camera: Camera, x: number, y: number, z = 0) => ({
x: camera.originX + (x - y) * COS * camera.scale,
y: camera.originY + ((x + y) * 0.5 - z) * camera.scale,
});
The tricky part was time. A Postgres trigger sends NOTIFY on commit and the API relays it over Server-Sent Events, so
ingots move immediately. Snapshots arrive every couple of seconds to correct anything missed. But a snapshot can be
older than the event you just animated, and then ingots jump backwards. So each one remembers when it last moved:
if (existing && existing.touched > asked) continue;
Events make it fast. Snapshots make it true.
Testing by breaking it
Mocks would have agreed with every bug in that table, so the integration tests start a real API and real workers against real databases, and then get mean. They kill processes, cut the connection to Redis, drop every Postgres connection and push 500 jobs through four workers to check that each ran exactly once.
The tests got tested too: nine guarantees were broken on purpose, and the suite caught all nine.
Squeezing the whole system into one container with a tenth of a CPU, for a free demo, found one more. A burst of reads from a single visitor could use up the connection pool and break the live view for everyone. Rate limits don’t stop that, because a burst is allowed by definition. A cap on how many lookups run at once does.
Try to lose a job
With Docker installed:
git clone https://github.com/CyberSarvesh/TaskForge.git
cd TaskForge
docker compose up --build
Open localhost:4000, press Rush order, and kill a worker while it’s busy.
One honest limit: this is at-least-once delivery. A worker can send an email and die before writing down that it did, and then that email goes out twice. The real fix belongs in the handler, as an idempotency key. The email job here only pretends to send, so your inbox is safe.
What I’d add next
- Batched claims, because writing to Postgres on every transition is the first ceiling it will hit.
- Partitioned events, since outside the public demo the history is kept forever.
- Something other than
NOTIFYat higher volume, as it serializes the commits that use it.
The general rule: never trust a worker to tell you that it failed. Make it keep proving that it hasn’t.
The code is on GitHub. If you find a way to lose a job, I very much want that issue.