jonnifyechomq

The Queue · module 01

The lifecycle

A job has a life, and the whole of it is four sorted sets and one row.

A producer enqueues a job; a worker claims it, runs it, and completes it; a failure retries after a delay or dies into a morgue; a lapsed lease returns the job to the queue when a client sweeps. There is no separate state table, no lock service, no second index — a job moves between four sorted sets (pending, active, schedule, dead) and its own row carries a state field that says where it is. This module reads that machine: the four sets, the claim and its lease, and the token-fenced completion and recovery.

Four sets, one row

Every transition moves a job between two of the four sets and rewrites its row's state (completion deletes the row instead). Pick a transition and read which set a job leaves, which it enters, what the row says afterward, and which native command, if any, runs the same transition. The map below is the real machine: the five transitions of EchoMQ.Jobs over a fixed example job.

The state machine · pick a transition

pending ZSET · score 0 active ZSET · lease done row DEL'd schedule ZSET · run-at dead ZSET · morgue each transition is one atomic Lua script on one slot
@claim2 · JOB0KHTOWnGLuC
pending → active · row state active
ZPOPMIN pending, skip a ghost (no row), HINCRBY attempts, HSET state active, ZADD active at now + lease — the oldest pending id becomes active and gets a lease deadline
native twin: EMQ.CLAIM — the same pop, token and lease, with the pause read folded in and lease_epoch stamped

Each transition runs as an inline Lua script that EchoMQ.Connector evaluates (wire 4), and most of them also exist as native commands in the trafikey module (wire 6): the claim, the lease extension, completion, the retry-or-dead verdict, reprocess and the counted stall sweep each have an EMQ.* twin, while the reaper runs only as a script. No sweep runs on its own on either executor: a lapsed lease returns to pending only when a client process calls the sweep. The native module teaches the mechanism.

The lifecycle is not a status column you update — it is a set a job is a member of. Move the id, and the state has changed.

The dives

Three dives read the machine in depth, each grounded in real echo/apps/echo_mq code: the four sets and why the members are the ids themselves, the claim and its lease-and-token, and the token-fenced completion plus the recovery transitions.

built page ready to read

Pattern & implementation

Where Redis Patterns Applied teaches the reliable queue — a job's state is the collection it lives in, and every move is atomic so a crash loses nothing — this module is where the pattern lands as one shipped state machine.

the pattern (Redis Patterns Applied)

A reliable queue moves a job between named locations atomically and recovers a crashed worker's job from a visibility timeout. States as locations and The reliable queue teach the move.

the implementation (echo_mq)

EchoMQ.Jobs runs @claim2 / @complete / @retry / @reap — four sorted sets and one row, every transition one atomic script on one slot; trafikey runs the same transitions, the reaper aside, as native commands.

Take the dives in order, or jump to completion-and-recovery if you already know the four sets and the claim. The next module — Jobs, lanes & the consumer — reads the producer, the fair worker, and the loop that owns the rhythm.

References

Sources

Related in this course