Fairness

Fairness

How a queue caps the messages out with workers and shares delivery fairly across routing keys.

A queue shares its workers among routing keys. Two settings cap how many messages are out with workers at once: concurrency for the whole queue and key_concurrency for one key. Delivery also takes turns across keys, so a busy key does not hold up the others.

Here the queue has key_concurrency: 2. tenant-a has four messages waiting and tenant-b has one. A claim for ten messages returns two of tenant-a's and the one of tenant-b:

curl -s -X POST 'localhost:8080/v1/queues/apps/thumbnails/claims?max=10&wait=5s' \
  | jq -c '.claims[] | {routing_key, body}'
{"routing_key":"tenant-a","body":"a1"}
{"routing_key":"tenant-a","body":"a2"}
{"routing_key":"tenant-b","body":"b1"}

a3 and a4 wait until one of tenant-a's claims ends. You set both limits when you create the queue, or update it later.

The two limits

A claim counts toward the limits from its delivery until its ack, nack, or deadline.

SettingCapsDefaultUse it to
concurrencyClaims out across the whole queue0, unboundedProtect something all the queue's workers share, such as a database or a rate-limited API
key_concurrencyClaims out for one routing key0, unbounded. Always 1 on a key-ordered queue.Cap how hard one tenant or resource is worked

A key at its key_concurrency is skipped until one of its claims ends, and other keys go on. Messages sent without a routing key count against no key.

A change to either limit applies within about 5 seconds, and counts the claims already out. A queue whose limit drops below the claims it has out delivers nothing more until enough of them end.

How the queue-wide limit is shared

A queue stores its messages in ranges, as a scalable stream does, and each range enforces its own share of concurrency:

share = concurrency ÷ ranges delivering, rounded down, at least 1

A range is delivering while it is open, and after a split or merge closes it, until it has no messages left.

QueueShare per rangeClaims out, at most
concurrency: 64, 4 ranges1664
concurrency: 10, 4 ranges28
concurrency: 2, 4 ranges14

The limit is approximate. Rounding down can leave part of it unused, and an idle range keeps its share. With more ranges delivering than concurrency, every range still gets one, so the total can pass the setting. For an exact limit, give the queue one range. The shares change when the queue's ranges change, when a closed range empties, and when you update concurrency.

Fair delivery across keys

Within a range, Nova takes turns across routing keys. Each key delivers up to 64 KiB of messages in its turn, then the next key goes. A key with a million messages waiting and a key with ten each get one turn per round, so the small key does not wait behind the large one.

Messages have no priority, so every key's turn is the same. To deliver urgent messages first, send them to a queue of their own.

The turns cover the messages a node has read ahead for the range, and a node reads a bounded amount ahead. A key that floods a range can fill all of it, and then the other keys wait behind the flood even though their turns are fair. Operate › Queues gives the bound.

Noisy keys

A noisy key is a routing key whose messages make up more than half of a range's backlog, by bytes. Nova checks when the node has read as far ahead as it may, or when the range has had a backlog for 10 seconds. Keys that share a routing hash count as one.

Nova gives a noisy key an isolated range that holds only that key. It does not drop, copy, or rate-limit the key's messages. It splits the range around the key, and from then on the key's new messages go to the isolated range. The original range delivers its other keys' messages first, then the noisy key's remaining messages, in order. Once its backlog is gone, the isolated range is a range like any other, and Nova can merge it back.

A queue has at most 16 isolated ranges open at once. Past that, a noisy key stays in its range.

Isolated ranges go last

While an isolated range has a backlog, its messages are leftover work. Every other range's messages are quiet work. Nodes and workers serve quiet work first:

WhereRule
One nodeWhile the queue's quiet ranges on the node have messages waiting, its leftover ranges get one claim for every nine the quiet ranges make
One-shot claimsQuiet work first. Leftover work only when no node has quiet work waiting.
SDK workersThe same as one-shot claims, and at least one claim in ten for leftover work

A leftover range is delivered at full speed when nothing else waits. It turns quiet again once it has nothing left to deliver.

See also

On this page