Lesson 69.3 · 3 of 5 · 4 min read · Reviewed 24 Nov 2026
Fair queuing and priority lanes
By the end, you can
- Design fair queues with priority lanes.
- Keep interactive traffic fast while background work continues.
A single first-in, first-out queue is fair to requests, not to people. One tenant submitting 5,000 jobs makes everyone else wait.
Lanes
interactive lane ===> served first, small requests
agent lane ===> served next
background lane ===> served with what's left, plus a reserved slice
Fair queuing inside a lane
Keep a sub-queue per tenant and serve them round robin: one job from A, one from B, one from C, then back to A. A tenant with 5,000 jobs gets the same turn rate as one with 5.
Weighted versions give paying tiers more turns, for example 3 turns for enterprise to 1 for free.
Avoiding starvation
Strict priority means the background lane may never run on a busy day. Two common fixes:
- Reserved share: background always gets, say, 10% of worker slots.
- Ageing: a job’s priority rises the longer it waits.
Deadlines
Give each job a deadline. If a support reply can’t start within 60 seconds, it’s better to show a fallback message than to answer late. Drop or downgrade jobs past their deadline rather than spending tokensToken: The unit a model reads and writes: a word, part of a word, a space or a symbol, mapped to an integer id. on them.
Key points
- Put work in separate lanes: interactive first, then agents, then background.
- Within a lane, take turns between tenants (fair queuing) so one big tenant can't block others.
- Protect low-priority work from waiting forever with ageing or a reserved share.
Fair-queue jobs across tenants
See how round robin between tenants stops a big backlog from blocking a small tenant.
You will have: A round_robin() scheduler and a comparison of worst waits per tenant.
Steps
- Run the starter: round robin behaves like first in, first out.
- Implement round_robin() with one queue per tenant.
- Compare the small tenant's worst wait in both schedulers.
- Check how much the big tenant's worst wait changes.
- Sketch how you would add a reserved share for a background lane.
Done when
- Round robin gives the small tenant a worst wait of 5 ticks instead of 52.
- The big tenant's worst wait barely changes.
- Every job still runs exactly once.
Open in the Playground No API key needed.
Starter code (Python)
# Fair queuing inside a lane: take turns between tenants instead of first in, first out.
from collections import deque
JOBS = [("big", i) for i in range(50)] + [("small", i) for i in range(3)] # all queued at t=0
def fifo(jobs):
return list(jobs)
def round_robin(jobs):
# TODO: put each tenant's jobs in its own deque, then take one job from each tenant in turn
return list(jobs)
def worst_wait(order, tenant):
"""One job finishes per tick; wait = position in the order."""
return max(i for i, job in enumerate(order) if job[0] == tenant)
for name, scheduler in (("first in, first out", fifo), ("round robin", round_robin)):
order = scheduler(JOBS)
print(f"{name:<20} small tenant worst wait {worst_wait(order, 'small')} ticks, big tenant worst wait {worst_wait(order, 'big')} ticks")
Show a solution
# Fair queuing inside a lane: take turns between tenants instead of first in, first out.
from collections import deque
JOBS = [("big", i) for i in range(50)] + [("small", i) for i in range(3)] # all queued at t=0
def fifo(jobs):
return list(jobs)
def round_robin(jobs):
queues = {}
for tenant, n in jobs:
queues.setdefault(tenant, deque()).append((tenant, n))
order = []
while any(queues.values()):
for tenant in list(queues):
if queues[tenant]:
order.append(queues[tenant].popleft())
return order
def worst_wait(order, tenant):
"""One job finishes per tick; wait = position in the order."""
return max(i for i, job in enumerate(order) if job[0] == tenant)
for name, scheduler in (("first in, first out", fifo), ("round robin", round_robin)):
order = scheduler(JOBS)
print(f"{name:<20} small tenant worst wait {worst_wait(order, 'small')} ticks, big tenant worst wait {worst_wait(order, 'big')} ticks")