Skip to content

Fair queuing across tenants

A way to share one worker pool among many tenants so a tenant's burst waits behind its own earlier work rather than everyone else's: the scheduler picks the next tenant, then that tenant's next job.

On this page

What it is

Fair queuing is a scheduling rule for a shared resource: instead of serving the oldest waiting item, the scheduler divides turns among the tenants that currently have work. The need comes from how a plain queue behaves. A regular queue delivers older messages first, so when one tenant sends a large volume or its messages take longer to process, the backlog it creates raises waiting time for all tenants in the queue. Amazon's Builders' Library puts it bluntly: a single queue and multitenancy pull against each other, because once work sits in a shared queue it is hard to isolate one workload from another.

The classic algorithm is deficit round robin (DRR), proposed by Shreedhar and Varghese in 1995 for network packets. Each flow gets its own queue and a quantum. The scheduler visits the non-empty queues in turn; on each visit it adds the quantum to that queue's deficit counter, then sends items from the head while the counter is at least the next item's size, subtracting each size as it goes. Whatever is left carries to the next round, and the counter resets to zero when the queue empties. The result is a minimum long-term share for each flow regardless of item sizes, in O(1) time per decision when the quantum is at least the largest item. The weights are left to the operator to set, and DRR's latency sits further from the ideal fluid model than weighted fair queuing's, which costs O(log n). For a job pool, read "size" as expected machine-minutes and "flow" as tenant: a tenant with sixty-minute jobs then earns no more machine time per round than one with one-minute jobs.

Two more levers work at the pool rather than in the queue. A per-workload concurrency limit, often a semaphore or a counter per tenant, bounds how many of a tenant's jobs run at once, and separate thread pools per workload stop one of them from taking all the threads. Sizing the pool starts from Little's Law: concurrency in a system equals arrival rate times average time per item, so a service handling 100 messages a second at 100 ms holds about 10 in flight, and a latency spike to 10 seconds would push that to about 1,000. That product is the average number of jobs in flight; our advice is to divide it by how many jobs one machine runs at once and add headroom for utilization to get a machine count. The fairness rule then decides who waits when demand is above it.

When there are too many tenants for a queue each, shuffle sharding gives most of the isolation for a fixed cost. The service provisions a fixed number of queues and hashes each tenant to a small subset of them. Before enqueueing it checks which of that tenant's queues has the fewest messages and uses that one. A tenant's burst then backs up its own few queues while other tenants' traffic is steered away from them. A related trick is sidelining: when a consumer dequeues a message for a tenant that is over its configured rate, it moves the message to a spillover queue and deletes it from the primary one; the spillover queue is worked as resources free up, which approximates a priority queue without building one.

Managed queues have started to offer this built in. SQS fair queues apply to standard queues when producers set a MessageGroupId as the tenant identifier. The service watches how in-flight messages (received but not yet deleted) are spread across groups; when one group holds a disproportionate share it is marked noisy, and delivery is reordered to favor the quiet groups while total throughput is kept. The noisy tenant's wait rises until its backlog drains. The noisy tenant's consumption rate is not limited, though: consumers still receive its messages when there is capacity and no other message to return.

That last point is the property to insist on in your own design: a fair scheduler is work-conserving. The Builders' Library describes stacking capacity rather than hard-allocating it: a client whose quota is 1,000 requests a second can burst into capacity that other clients aren't using, and if those clients start using their share, their traffic takes priority over the burst. The same holds for a job pool: a tenant alone in the system gets the whole pool, and fairness changes who waits when the pool is full, not how much work gets done.

When to use it

Reach for fair queuing when many tenants share one pool of workers, arrivals come in bursts, and the wait a small tenant sees matters more than raw throughput. The choice of mechanism follows the tenant count. With a handful of customers or adjacent internal systems, a separate queue per customer is simple and helpful. With tens or hundreds the per-queue polling cost starts to bite, and with millions (each device, each function) a queue each is out of the question, which is where shuffle sharding over a fixed set of queues comes in.

Add a per-tenant concurrency cap when job durations vary widely or depend on a slow downstream: Little's Law means a latency spike multiplies the threads one workload holds, and a cap keeps that multiplication from emptying the pool for everyone. Add weights when tiers pay for different shares. Our advice: give paid tiers a larger quantum rather than strict priority, so a free tier still makes progress while an enterprise burst runs.

When not to

Skip it when there is one tenant, or when the pool is sized so a backlog rarely forms; a plain queue with autoscaling is cheaper to run and reason about. Skip the scheduler, too, with a handful of adjacent systems: a queue each and a consumer each gives the isolation without any round-robin code.

Some workloads care about age more than about who sent the work. Real-time systems often run on first-in, first-out queues but prefer to see fresh data processed first after a surge or an outage, with the older wave handled as capacity frees up. There the better tool is sidelining old messages into a backlog queue, or dropping messages older than the last full synchronization, not a tenant-by-tenant share. And when a customer needs a fixed quota for cost control, they want the hard limit exposed as a feature, not a soft share that bursts.

Fair queuing also doesn't shrink a backlog; it reorders one. If a tenant keeps producing faster than the pool can drain, the right move is to push back at the producer, for example by scaling an inbound throttle inversely with that tenant's queue depth, so a day-long backlog isn't built in the first place. Pair the scheduler with backpressure and load shedding.

Compared

ApproachWho runs nextPick it whenWhat it costs

Shared FIFO queue

The oldest job

One tenant, or a pool that rarely backs up

One burst delays everyone behind it

A queue per tenant, round-robin

The next tenant with work, one job per turn

A handful of tenants

Polling cost grows with tenants; long jobs win if you count jobs, not minutes

Deficit round robin with weights

The next tenant, up to its quantum of machine time

Job sizes vary, or tiers pay for shares

Weights need tuning; more latency than the ideal share

Per-tenant concurrency cap

Any job whose tenant is under its cap

Durations vary or a downstream is slow

A tenant alone in the pool is still capped

Shuffle-sharded queues

Whatever each of a fixed set of queues yields; a tenant enqueues into the least loaded of its few queues

Too many tenants for a queue each

Isolation is probabilistic; tenants sharing a hot queue still wait

Hard per-tenant quota

Jobs within the quota; the rest are rejected

The customer wants a fixed limit, e.g. for cost

Rejects work while the pool sits idle

Managed fair queue

Quiet tenants' messages before the noisy one's

You run on a managed queue and want no scheduler code

The fairness logic lives in the service rather than your code; the noisy tenant waits until its backlog drains

In a picture

Jobs arriveRoute by tenantOrg A queueTeam B queue

Step 1 of 3

Route jobs by tenant

Arriving jobs are split into a queue per tenant, or hashed to a few of a fixed set. With a queue each, a burst from one organization fills its own queue; with shared shards, it backs up the few shards it maps to while other tenants are steered toward their emptier ones.

How to say it in an interview

The common mistake

Quiz

Under fair queuing, suppose one tenant has a large backlog waiting and no other tenant has any work. How many workers should it get?
Per-tenant queues with plain round robin, and suppose tenant A's jobs run far longer than tenant B's. What goes wrong, and what fixes it?
Suppose there are far more tenants than you could afford to poll a queue each for. Why not a queue per tenant, and what instead?
Suppose a tenant keeps producing faster than the whole pool can drain. Fair queuing is in place. What is still missing?

Questions that use this

Related

Sources

  1. Amazon Builders' Library: Avoiding insurmountable queue backlogs
  2. Amazon Builders' Library: Fairness in multi-tenant systems
  3. AWS Compute Blog: Building resilient multi-tenant systems with Amazon SQS fair queues
  4. Wikipedia: Deficit round robin
Sign in to report a mistake