Sub topic 14
Spotting skew and rebalancing over time
This chapter assumes you have a working shard matrix and a timing-based splitter like the greedy one from earlier chapters. It covers how to notice when the shards drift out of balance and how to fix them.
What skew is
Skew means the shards do not take equal time. A matrix finishes only when its slowest shard finishes, so one slow shard sets the wall-clock time for the whole run.
Take four shards with these durations: 195, 190, 310 and 185 seconds. The mean is 220 seconds, but the pipeline takes 310. About 90 seconds go to waiting, roughly 29% of the run.
Why balance drifts
A split that was balanced when you built it degrades for several reasons:
- Developers add tests that were not in your timing data.
- A test gets slower, for example after a fixture grows.
- Tests are deleted or renamed, so recorded durations no longer match any file.
- The CI runner fleet changes, and timings shift by a constant factor.
Count-based and hash-based splits drift in the same way. They never looked at duration, so they start out skewed.
Measuring skew
Record each shard's total duration on every run. Most CI systems show job durations, and you can also have each job write one number to an artifact. Then compute the ratio of the slowest shard to the mean.
def skew(durations):
"""Return slowest shard time divided by mean shard time (1.0 is perfect)."""
values = list(durations)
mean = sum(values) / len(values)
return max(values) / mean
shards = {1: 195, 2: 190, 3: 310, 4: 185}
ratio = skew(shards.values())
print(f"skew {ratio:.2f}")
if ratio > 1.2:
print("rebalance")This prints skew 1.41 and then rebalance. A threshold around 1.2 is a common starting point. Pick one that matches your tolerance, and alert on a trend over several runs instead of a single reading. One slow run can come from a noisy runner.
Rebalancing loop
The rebalancing step feeds new durations into the same greedy splitter you already have. Three practices make it reliable:
- Smooth the data. Average each test's duration over the last several runs, so one noisy run does not reshuffle everything.
- Handle unknown tests. Give a file with no history the median duration of known files. Its real time is recorded after its first run.
- Drop stale entries. Remove timings for files that no longer exist, so deleted tests do not claim capacity.
When to rebalance
You can regenerate the assignment on every run, or on a schedule such as nightly. Regenerating every run keeps balance tight, but a changing assignment makes failures harder to compare between runs. A nightly job that commits or caches a timing file is more predictable. Pick the schedule based on how often your suite changes.
Exercise
A suite runs on 5 shards with durations of 240, 250, 245, 400 and 235 seconds. Compute the skew ratio, and say whether rebalancing at a 1.2 threshold is advised.
Show answer
The total is 1370 and the mean is 274 seconds. The slowest shard is 400, so skew is 400 / 274 ≈ 1.46. That is above 1.2, so rebalance. Shard 4 is the likely target, and you should check whether a new or slower test file landed there.