I record that ENQUEUE behavior is: asks a replication-acknowledging node to queue a job for the first time; if lost, some node retries after the retry time unless retry is set to zero.
I record that WILLQUEUE behavior is: 500 milliseconds before a job is re-queued, to nodes that may have a copy; already-queued receivers can reply with QUEUED to prevent another queue attempt and avoid multiple delivery when possible.
I record that QUEUED behavior is: updates other nodes’ retry-to-queue time; a node with the same job queued de-queues it if its node ID is lexicographically smaller than the sending node’s, as a best-effort deduplication measure.
I record that SETACK behavior is: marks a job as successfully delivered, no longer active, and never to be re-queued by the receiving node; is also sent when a QUEUED or WILLQUEUE receiver already has the job acknowledged.
I record that GOTACK and cluster garbage-collection behavior is: GOTACK acknowledges SETACK; nodes garbage-collect a message cluster-wide when they believe all nodes that may have a copy are informed of the worker acknowledgement.
I record that DELJOB garbage-collection conditions and the source limit are: DELJOB asks the receiver to remove a job during garbage collection when the sender is sure it was delivered correctly; usually this means all nodes that may hold a copy have marked it delivered; captured description ends mid-sentence.
When I pause a queue in input, it never creates new jobs for that queue or puts active jobs back in the queue while the pause lasts.
When I pause a queue in output, GETJOB blocks even if jobs are available; clearing the output pause later unblocks GETJOB.
An output-paused queue does not provide jobs to other nodes through federation, so it stops acting as a message source for local and non-local clients.
PAUSE supports in, out, all, none, state, and bcast; all is equivalent to specifying both in and out, and the command returns the resulting state as in, out, all, or none.
PAUSE myqueue out bcast
PAUSE myqueue in out
PAUSE myqueue all
PAUSE myqueue state
"none"
I never evict queues paused in input or output to reclaim memory, even if they are empty and inactive for a long time, because eviction would forget the paused state.
I retain messages with at-most-once delivery semantics after their first delivery, and they should be acknowledged like other messages; acknowledgment is not mandatory because a RETRY-0 message cannot be received again.
Under memory pressure, I can evict an unacknowledged job when its retry is set to 0 and it has already been delivered.
I retain delivered RETRY-0 jobs to keep the API the same for all job types and to preserve observability; unacknowledged jobs are eventually evicted during memory pressure.
./disque -h <redacted> -p 7714 cluster meet <redacted> 7711
I can add a node with CLUSTER MEET, and the old and new nodes can meet in either direction.
Abruptly removing a node that held one replica can reduce a job's replication factor from 3 to 2, even though no actual failure occurred; queued messages on the removed node may need to wait for their retry time before being queued again.
CLUSTER LEAVING yes
INFO jobs
CLUSTER FORGET <old-node-id>
When a node is leaving, it externally replicates ADDJOB messages without using itself as a replica, so no new messages are created in the context of that node.
A leaving node sends -LEAVING to GETJOB clients that would otherwise block, including clients already blocked; clients should connect to another node.
While leaving, a node stops sending NEEDJOBS through federation, and it and other nodes advertise it with a bad priority in HELLO so clients select another node.
A leaving node stops creating dummy acknowledgments in response to ACKJOB for an unknown job and participates only as a source of messages until its message count drops to zero.
Disque uses the Redis protocol, with default port 7711 rather than 6379.
I recommend that clients try random known node addresses until a connection succeeds, then use HELLO to retrieve the Node ID and potentially useful server information.
A consumer can check the Node ID prefix in message IDs to identify message sources and, when useful, retrieve messages directly from producer nodes.
- The documentation lists these PHP clients: phpque (PHP/HHVM), disque-php, and phloppy. It lists these Python clients: disq, pydisque, and django-q.
Disque tries to replicate to W-1 reachable nodes, or W during out-of-memory conditions, in shuffled order.
If the required replication is not reached promptly, Disque tries one additional node every 50 milliseconds and resends REPLJOB to nodes that may already hold a copy to refresh the copy-holder list.
When the synchronous replication timeout expires, the node that received ADDJOB returns an error and makes a best-effort attempt to delete the job from nodes that may already have received a copy.
Disque is a full mesh, with each node connected to every other node; gossip-based failure detection adjusts replication strategy and informs clients about unreachable nodes.
Because Disque is multi-master, node failures receive no special handling.
PING, PONG, and FAIL check reachability and mark nodes as failing; failure detection is used to adjust replication strategy.
REPLJOB asks a receiver to replicate a job; an accepted job is confirmed with GOTJOB, while a receiver near out of memory may reject it without sending GOTJOB.
I describe FASTACK as faster and exchanges fewer messages, but less reliable.
With FASTACK, an unreachable node holding a copy may deliver the message again after a partition heals.
With FASTACK, the receiving node evicts the job and sends a best-effort DELJOB to nodes that may hold a copy, or to the whole cluster if it does not know where a copy is.
I consider FASTACK a likely choice when the network is reliable, performance matters, and multiple deliveries are not an issue for the application.
Dead-letter queues collect messages that cannot be processed, such as messages delivered too many times, messages whose TTL reaches zero, or messages explicitly flagged by a worker.
Administrators commonly inspect dead-letter queues, often through automated systems, to find errors preventing expected processing.
I choose a message TTL so that the message is no longer meaningful to process once the TTL is reached.
The nacks counter increases when a worker uses NACK to report unsuccessful processing and request that the job be put back on the queue ASAP.
The additional deliveries counter increases for requeueing conditions other than NACK, including lost messages being enqueued again and partition-related requeues.
Before processing, an application can compare the counters with its own limits and respond by emailing operations, setting a monitoring flag, using a special queue, or attempting processing and reporting a stack trace.
I expect best-effort counters eventually to rise enough for a message with issues to reach the application's warning threshold, although they need not be consistent during failures or partitions.
I can operate Disque in memory using synchronous replication as a durability guarantee, or use an AOF that logs job creation and eviction for reload at restart.
Normally, Disque reloads job data into memory without populating queues, because unacknowledged jobs are eventually requeued.
Reloading queue data is unsafe for at-most-once jobs whose retry value is 0.
Generating a fresh AOF while the server no longer accepts client commands removes the race condition when reloading full state, including retry-0 jobs.
Even in memory-only mode, Disque can dump memory to disk and reload it during a controlled restart.
After the controlled restart procedure, the server is configured to load the full state only at the next restart.
The aof-enqueue-jobs-once option turns itself off after restart and causes the AOF to load even when AOF support is switched off.
I identify the job ID components as the D- prefix, the first 8 bytes of the generating node ID, a 144-bit pseudo-random component encoded in base64, and the job TTL in minutes.
ADDJOB returns a job ID on successful creation, GETJOB includes it in its output, and workers use it to acknowledge successful processing.
Including part of the generating node ID lets workers infer where jobs for a queue were created and connect directly rather than listen on a node that must fetch jobs from elsewhere.
For 100 Disque nodes, the stated probability of two identical 32-bit node-ID prefixes is P(100,2^32) = .000001164; a collision can lead workers to make a non-efficient choice.
The 144-bit ID is HIGH_144_BITS_OF_SHA1(seed || counter); the seed comes from /dev/urandom at startup and the counter is 64 bits and increments for every generated ID.
The stated number of uniformly selected possible IDs is 22300745198530623141535718272648361505980416; although collision probability is non-zero mathematically, I regard IDs as unique in practice.
The TTL encoded in a job ID is used to expire IDs for jobs the node does not itself hold a copy of; this does not mean job TTLs have two-minute precision.
I can compile Disque with make if I can compile Redis; the project has no external dependencies, and the disque and disque-server binaries appear in src.
I configure multiple nodes on different ports with separate disque.conf files based on the source distribution's example, then use CLUSTER MEET <ip> <port> from one node for each other node.
Each Disque node needs two TCP ports open: the base port for clients and the cluster bus port, which is the base port plus 10000 for communication between Disque processes.
I start a node with ./disque-server and can join example nodes on ports 7712 and 7713 to the node on port 7711 with the disque cluster meet commands.
I must acknowledge jobs; otherwise they remain in server memory until their TTL is reached.
I can use any string as a queue name; Disque creates a queue automatically when needed and removes it when it has no jobs.
Disque treats a job as a message to deliver without interpreting its meaning, and the maximum job size is 4GB.
Without ASYNC, ADDJOB returns an error if the requested replication level is not reached within the timeout and attempts best-effort cluster cleanup, but the job may still be delivered later.
With the default server hz, ADDJOB's actual timeout resolution is 1/10 of a second or worse.
REPLICATE count specifies the number of nodes to which the job should be replicated.
DELAY sec specifies how many seconds elapse before any server queues the job; the default is no delay.
I describe the preferred approach as application-managed handling using counters rather than a built-in dead-letter queue.
Each node’s message representation has a nacks counter and additional deliveries counter.
I can inspect the counters using GETJOB with WITHCOUNTERS or SHOW.
The AOF logs job creations and evictions on disk with configurable fsync policies and reloads them at restart.
I note that AOF is especially recommended for a single availability zone where a mass reboot of all nodes is possible.
CONFIG SET aof-enqueue-jobs-once yes; CONFIG REWRITE; SHUTDOWN REWRITE-AOF
Disque job IDs are exactly 40 characters and start with D-.
The ID contains a 144 bit ID pseudo-random part encoded in base64.
The encoded TTL is even for at most once jobs with retry value 0 and odd otherwise; encoded TTL precision is 2 minutes.
I keep these notes on Disque, the distributed, in-memory job broker documented in a repository on @antirez’s GitHub. The README presents it as an experiment in bringing the Redis-as-a-jobs-queue use case into a self-contained, scalable, fault-tolerant design.
My Disque repository metadata is C, 8,075 stars, 531 forks.
The Disque repository dates are Started 2015-04, last updated 2021-03.
As of 2 Jan 2016, the project was in release candidate state, and people were encouraged to evaluate it and report bugs and experiences.
I described its beta and API status as: beta code may not be suitable for production; API considered stable except for details that may change in next release candidates.
My default replication guarantee is replicated to W nodes before acknowledgement; W-1 nodes can fail and the message will still be delivered.
My delivery design priority is at-least-once delivery; approximate single delivery when possible.
My at-most-once configuration is retry time 0 and replication factor 1.
The two delivery modes can coexist in the same queues and nodes because they are configured per message. Jobs are strings, and the documentation uses “job” and “message” interchangeably.
All nodes have the same role: producers and consumers can connect to any node, and nodes exchange messages based on load and client requests. The README describes Disque as available and eventually consistent in CAP terms.
My availability model is eventually consistent AP system; progress is possible while a single node is reachable.
My asynchronous replication example is replication factor 3 with best-effort background replication.
My unacknowledged-message retry behavior is automatically re-queues after a message-specific retry time.
Consumers explicitly acknowledge delivery or processing; they do not need to re-queue a message themselves when it was not processed. At-least-once is the main design focus, and duplicates can still occur, although the system tries to avoid them.
My ordering policy is best effort ordering; creation time uses local wall clock and an incremental counter within the same millisecond.
My ordering disruptions are retries, local clock drift, and node transfers for load balancing and federation can violate ordering.
Jobs created on one node are normally delivered in creation order, and earlier-created messages are usually delivered first. I do not promise strict FIFO or causal ordering; “message broker” is technically more accurate, though the README uses “message queue” in the broader industry sense.
My job controls are replication factor, delay time, retry time, and expire time.
My disk persistence setting is optional and disabled by default; may help in single data center setups and during restarts.
My job memory and queue state distinction is jobs may be replicated to multiple nodes but usually queued on a single node.
My ACK retention behavior is ACKs are replicated and garbage collected; under memory pressure or certain failure scenarios they are eventually discarded.
My ACK semantics are ACK is proof of delivery retained for some time to make multiple deliveries less likely.
ACK propagation can stop a partitioned node from re-queuing a job if it learns of the ACK before the retry time; ACKs issued during a partition can propagate after it heals. The README says this normal garbage-collection outline describes what happens 99% of the time, while failure cases are more complex.
My retry-time-zero behavior is queued exactly once; delivered once or never; replication factor greater than 1 is useless and signaled as an error.
My retry-time-zero crash-restart behavior is guaranteed after crash restart whatever the persistence configuration is.
My manual restart queue persistence behavior is manual restarts persist and atomically reload queues without a delivery-and-persistence race.
My normal ACK flow is ACKJOB, SETACK, GOTACK, DELJOB.
- The normal acknowledgement sequence is: the client sends ACKJOB to one node; that node sends SETACK to nodes it believes hold a copy; those nodes reply with GOTACK; then the original node sends DELJOB to them. The captured README says that for a job replicated to 3 nodes, acknowledging requires 1+2+2+2 messages; the excerpt ends immediately after this figure.
My faster acknowledgement path is FASTACK is a faster acknowledgement path; normal acknowledgement is expensive in inter-node messages.
My additional features are blocking queues, queue statistics, stateless iterators, job visibility controls, cluster resizing, graceful node removal.
The README also describes duplicate email delivery and repeated expensive idempotent operations as cases where duplicates may be manageable but are still undesirable. The remaining FASTACK explanation, later state-machine details, API changes in later release candidates, and operational performance evidence are not present in this captured excerpt.
I describe Disque queues as named communication channels: processes that fetch from a given queue receive messages sent to that queue.
I allow multiple processes to listen for messages in a queue and multiple processes to send messages to the same queue.
I treat eventual delivery despite failures as an important guarantee for a message queue.
The default retry time is 5 minutes, with the exception of jobs having a TTL so small that 10% of TTL is less than 5 minutes. In this case the default RETRY is set to TTL/10 (with a minimum value of 1 second).
If RETRY is 0, the job has at-most-once delivery semantics.
If not specified, the default TTL is one day.
A job’s TTL is its maximum lifetime in seconds; after that time, it is deleted even if it was not successfully delivered.
MAXLEN refuses a message and reports an error if the specified queue already has the requested count of messages queued.
The add-job command returns the Job ID if ASYNC is specified or if the job was replicated correctly to the specified number of nodes; otherwise it returns an error.
GETJOB returns NULL if its timeout is reached.
GETJOB returns one job per call unless a count greater than 1 is specified, and each job is a three-element array containing the queue name, Job ID, and job body.
When GETJOB has no jobs in the specified queues, it blocks and exchanges messages with other nodes to move messages about those queues to this node so the client can be served.
NOHANG asks GETJOB not to block when all specified queues are empty, allowing the caller to check for available jobs without blocking.
WITHCOUNTERS returns best-effort counts of NACKs received by a job and its additional deliveries; the documentation points to the Dead Letters section for more information.
A dummy ACK retains an acknowledgement during a netsplit when ACKJOB reaches a node without a copy of the job; job garbage collection is attempted after the partition heals.
WORKING cannot be sent to a node that does not know the job, which returns a NOJOB error; it also errors if the job is already acknowledged.
WORKING returns the number of seconds the message visibility was likely postponed, basically the job retry time, and a worker should send another WORKING before that time elapses.
A worker can fetch the retry value with SHOW when starting to process a message, or simply send WORKING immediately.
NACK is intended for a worker unable to process a message that wants it put back in the queue for processing again.
INFO returns generic server information and statistics.
QLEN returns the length of the queue.
QSTAT returns NULL if a queue does not exist.
An evicted queue is immediately created again when needed to serve requests.
QSTAT import-from lists node IDs this node is importing jobs from for the queue in order to serve client requests.
QSTAT’s import-rate is the instantaneous amount of jobs per second imported to handle outgoing GETJOB traffic.
QSTAT’s blocked value is the number of clients currently blocked on the queue; age and idle are reported in seconds.
The jobs-in and jobs-out counters increment every time a job is enqueued or dequeued for any reason.
ENQUEUE queues jobs if they are not already queued.
DEQUEUE removes a job from the queue.
DELJOB completely deletes a job from one node and is limited to that node because no DELJOB cluster bus message is sent to other nodes.
QSCAN’s COUNT option is a hint about how much work to do per iteration; BUSYLOOP blocks and returns all elements in a busy loop.
QSCAN MINLEN excludes queues with fewer than the specified number of queued jobs, and MAXLEN excludes queues with more than that number.
QSCAN IMPORTRATE returns only queues whose job import rate from other nodes is greater than or equal to the specified rate.
The QSCAN cursor can appear anywhere; the first non-matching option that is a valid unsigned number is sensed as a cursor.
JSCAN’s COUNT option is a hint about how much work to do per iteration, and BUSYLOOP blocks and returns all elements in a busy loop.
JSCAN QUEUE returns only jobs in the specified queue.
JSCAN’s cursor can appear anywhere; the first non-matching option that is a valid unsigned number is sensed as a cursor.
When a queue is paused for input, the node no longer accepts requests from other nodes to replicate jobs for that queue; because ADDJOB uses synchronous replication by default, adding jobs at a specified replication level may fail if enough nodes pause the queue.
When a queue is paused for input, the node generally does not create new local jobs for that queue.
Jobs in an input-paused queue no longer accept ENQUEUE messages from other nodes; those messages can be used by nodes under out-of-memory conditions that replicate jobs externally without holding a copy, to put a job in the queue of a random node that has a copy.
The ASYNC option asks the server to let the command return ASAP and replicate the job to other nodes in the background; the job gets queued ASAP, while normally the job is put into the queue only when the client gets a positive reply.
If jobs are available in multiple queues, queues are processed left to right.
A node receiving the ACK will replicate it to multiple nodes and will try to garbage collect both the job and the ACKs from the cluster so that memory can be freed. A node receiving an ACKJOB command about a job ID it does not know will create a special empty job, with the state set to "acknowledged", called a "dummy ACK"; the dummy ACK is only created for at-least-once jobs.
FASTACK performs a best-effort cluster-wide deletion of the specified job IDs. On a well-connected network with no node failures this is equivalent to ACKJOB but much faster; during failures it is more likely to result in multiple deliveries of the same messages.
WORKING postpones the next delivery for the job retry time on a best-effort basis. Disque refuses the command if 50% of the job time to live has already elapsed; the example sends another WORKING when the timer reaches 80% of the retry time.
NACK tells Disque to put the job back in the queue ASAP and increments the job nacks counter instead of the additional-deliveries counter.
HELLO returns the hello format version, this node ID, all the nodes IDs, IP addresses, ports, and priority (lower is better, means a node is more available). Clients should use this as a handshake command when connecting with a Disque node.
QSTAT fields may be removed or added, so implementations should not rely on their order or existence. A queue’s nonexistence does not mean there are not jobs in the node or in the whole cluster about this queue, because queues are automatically evicted after some time if empty and without clients blocked waiting for jobs, even if there are active jobs for the queue.
In the QSTAT foo example, len is 56520, age is 601, idle is 3, blocked is 50, import-rate is 19243, jobs-in is 3462847, jobs-out is 3389522, and pause is none.
QPEEK returns jobs without consuming them: a positive count returns jobs from the oldest to the newest, while a negative count shows the newest jobs from newest to oldest.
QSCAN and JSCAN start with cursor 0 and continue with the cursor returned by the previous call; the iterator guarantees to return all elements but may return duplicated elements. JSCAN can filter by queue and by multiple states using logical OR, and REPLY all returns the full job state like SHOW.
PAUSE can broadcast queue pause state to other nodes and pause input, output, or both. For input pause, ADDJOB returns a -PAUSED error, the node no longer accepts replication or ENQUEUE messages for the queue, and active jobs reaching retry time are not put back into the queue; instead their retry timer is updated and the node tries again later.