Sub topic 15
Dynamic Work Queues as an Alternative to Static Shards
This chapter assumes you have run a static shard setup (a CI matrix with --shard=index/total), and that you know how greedy timing-based splitting works. Here you will see where static shards still leave idle time, and how a shared work queue removes it.
The limit of static shards
A static shard fixes its list of tests before the job starts. Even a good greedy split relies on recorded durations, and those drift. A test gets slower, a runner is busier than usual, or a new file has no timing data yet. One shard then finishes in 6 minutes while another takes 10. The pipeline finishes only when the slowest shard does, so the other jobs sit idle or exit early.
How a work queue works
In a dynamic setup, no job owns a fixed list. All tests go into a shared queue. Each worker takes the next test, runs it, reports the result, and takes another. A worker stops only when the queue is empty.
A slow worker simply takes fewer tests. Balance comes from the pull model, not from a prediction, so stale timing data matters much less. Longest-first ordering still helps: if the queue starts with the slowest files, no worker is left running one huge file at the end.
A minimal queue in Python
This example simulates the idea on one machine. Threads stand in for CI jobs, and time.sleep stands in for running a test.
import queue
import threading
import time
durations = {"a.test": 0.6, "b.test": 0.45, "c.test": 0.3,
"d.test": 0.25, "e.test": 0.2, "f.test": 0.15}
work = queue.Queue()
for name in sorted(durations, key=durations.get, reverse=True):
work.put(name)
results = []
lock = threading.Lock()
def worker(wid: int) -> None:
while True:
try:
name = work.get_nowait()
except queue.Empty:
return
time.sleep(durations[name]) # pretend to run the test
with lock:
results.append((wid, name))
threads = [threading.Thread(target=worker, args=(i,)) for i in range(1, 3)]
for t in threads:
t.start()
for t in threads:
t.join()
for wid, name in sorted(results):
print(f"worker {wid} ran {name}")Real CI has no shared memory between jobs, so the queue must live in a service that every job can reach.
What a real queue needs
- A coordinator. Something must hold the queue and hand out work. This can be a hosted CI feature, a commercial test-orchestration service, or a small service you run yourself, backed by something like Redis.
- Claim and retry rules. If a worker crashes after taking a test, the coordinator must put that test back after a timeout. Otherwise the run hangs or silently skips it.
- Result collection. Each worker still produces its own report and coverage, so merging works as in the previous chapters.
Trade-offs
| Concern | Static shards | Dynamic queue |
|---|---|---|
| Balance | Depends on timing data quality | Adapts to actual speed |
| Infrastructure | Only a CI matrix | Needs a coordinator |
| Reproducing one job locally | Re-run the same `--shard` value | The test list differs per run |
| Per-test setup | Shared inside one shard | Can repeat if tests are handed out one by one |
Because each run assigns tests differently, a failing job cannot be reproduced from its index alone. Have each worker log the tests it ran. To limit setup repetition, hand out whole files or small batches instead of single test cases.
Exercise
Your static shards finish at 5, 6 and 11 minutes, even though you rebalance weekly from timing data. Name one reason a dynamic queue could help, and one cost it adds.
Show answer
Workers pull tests as they finish, so a slow runner or a test that got slower since the last timing run only reduces that worker's share, and the idle time on the fast shards disappears. The cost is a coordinator service that must hand out work, reassign tests from crashed workers, and make failures harder to reproduce because the test list per job changes each run.