What a merge queue costs: the arithmetic behind keeping main green
A merge queue keeps main green by testing each change on top of the ones ahead of it. The price is waiting and CI time, and you can work it out for your team before you turn one on.
Buse Senol

Before a release of Uber's rider app, hundreds of changes landed on the main branch within minutes. Each one had passed its own tests, but together they caused a serious performance regression. A merge queue exists to stop exactly this: it tests every change on top of the changes ahead of it, and only what passes together reaches main.
Uber's engineers described the episode in their 2019 paper on SubmitQueue, the queue they built for their monorepos. Finding the culprits took "several hours bisecting the mainline", and because new changes kept arriving while they searched, the problem grew until the main branch had to be blocked. The same paper measured how often this kind of breakage was the normal state of things. Over one week before SubmitQueue, the iOS main branch "was green only 52% of the time". At that pace, then, a broken main is not a rare accident. It is a condition the branch spends half its life in.
Search for "merge queue" today and the top results answer a different question. There is GitHub's documentation on how to enable its queue, Bitbucket's equivalent page, a launch announcement, several vendors' product pages and a Reddit thread asking what a merge queue is for. They all explain how to switch one on. None of them answers the question a team lead has before switching: will the queue keep up with us, and what will it cost in waiting and in CI time? That question matters because a merge queue does not make the problem disappear. It turns a correctness problem into a capacity problem. Main stops breaking, but every change now waits its turn, and how long that turn takes depends on numbers your team already has.
This piece works through those numbers with one team from start to finish. It is the team from The anatomy of a release cadence: about 30 merges a day, and a CI run that takes 25 minutes. That piece said, in one paragraph, that a queue verifying one change at a time would clear about two changes an hour and fall behind by mid-morning. Here we take that paragraph apart. No real team's data covers all four ways of running a queue side by side, so we wrote a simulation: a short Python script that invents 1,000 working days for this team and counts how long each change waits under four setups, namely no queue, a serial queue, a batching queue and a speculative queue. The waiting times, CI counts and breakages quoted in the sections below come from that script. They are not measurements from any company, and the next section explains exactly what the script assumes.
How the simulation works
Each simulated day is ten working hours long. During that time, 30 changes become ready to merge at random moments, and each change is either good or bad. A bad change passes CI on its own branch but breaks main once it is combined with the others, which is the soft conflict described in the next section. The script then sends the same day through each of the four setups and records, for every change, the minutes between "ready to merge" and "on main". It repeats this for 1,000 days and reports the median wait, the wait that one change in ten exceeds, the number of CI runs the queue needed, the number of bad changes that reached main, and the number of good changes the queue sent back because of a flaky run.
| Assumption | Value in the simulation | Where it comes from |
|---|---|---|
| Merges per day | 30 | The example team in "The anatomy of a release cadence" |
| CI time per run | 25 minutes, every run | The same example team |
| Changes that break main when combined | 10% | Our assumption, for illustration |
| CI runs that fail for no reason (flaky) | 5% | Our assumption, for illustration |
The two percentages are guesses chosen to be plausible, not values anyone measured, and changing them changes the results. That is why the full script and its output are at the end of this piece: you can run it with your own team's numbers in place of ours.
What breaks when there is no queue
The failure a merge queue prevents has a name at Shopify: the soft conflict. Their engineering team defines it as "two pull requests that pass CI independently, but fail when merged together". Git reports no conflict, because the two changes touch different lines, and each pull request shows a green check. The problem is that each check ran against the version of main that existed when the branch was last updated, not against the version that will exist after the other change lands. Martin Fowler gives the classic example in his article on branching patterns, where he calls it a semantic conflict: one change renames a function while another adds a new call to the old name. Both are correct on their own, both pass, and the build breaks the moment the second one merges.
The cost of that breakage spreads outward from the change that caused it. Everyone who pulls main after the bad merge gets a build that fails for reasons unrelated to their own work. Someone has to find which change did it, which means bisecting a stretch of history that other people keep adding to. In a 2014 post on Jane Street's blog, Yaron Minsky summarized Graydon Hoare's argument on this point: teams that run their test suite after integration rather than before have to scramble to find out who broke the build so that the right person can fix it. Every minute of that scramble is a minute when nobody can deploy from main with confidence.
In our simulation, the team without a queue merges every change the moment its own branch CI is green. Nobody waits, and the queue spends no extra CI time, because there is no queue. With 10% of changes breaking main when combined, though, three bad changes reach main on an average day. If the team doubles to 60 merges a day, that becomes six. The script does not model how long each breakage lasts, because that depends on how fast someone notices and reverts. At Uber it was hours. Even at a few minutes each, three or six breakages a day mean that main is something engineers have learned not to trust.
The serial queue and its ceiling
The simplest queue enforces what Graydon Hoare called the Not Rocket Science Rule. As Minsky restates it, the rule is that "you should automatically maintain a repository that never fails its tests". In practice, the queue takes the first waiting change, merges it with the current tip of main, runs CI, and lands it only if CI is green. Then it takes the next change and does the same. Because every change is tested against exactly the main it will join, a soft conflict can never land.
The price is that the queue is a single line, and a single line has a ceiling. Minsky put the arithmetic plainly: if verifying a pull request takes m minutes and you have n pull requests, releasing them takes at least m times n minutes. For our team that means a hard limit of 24 changes in a ten-hour day, because 600 minutes divided by 25 minutes is 24. The team produces 30. So the queue is already behind before the first coffee break, and every change that arrives later inherits the backlog of the changes before it.
The simulation shows what that backlog looks like. In the simulated days with 30 merges, the median change waits 120 minutes in the serial queue, and one change in ten waits 211 minutes or more. That is the time between "ready to merge" and "on main", on top of the branch CI the change already passed. The queue also bounces about 1.4 good changes a day, because a flaky CI run looks exactly like a real failure when only one change is in the run. Then we doubled the team in the script. At 60 merges a day, the median wait becomes 476 minutes, nearly eight hours, and the slowest tenth wait 839 minutes or more. In other words, a change that is ready at lunchtime lands the next day.
Real teams have hit the same wall at larger scale. At Jane Street, Minsky wrote, a full build took "two hours and up", and things backed up until a change would sometimes take 24 hours to get through. The Uber paper gives the extreme version: with a thousand changes a day and 30 minutes of build steps each, the last change to join a single queue would wait more than 20 days. The lesson is the same at every size. A serial queue works when your merges per day multiplied by your CI time fits comfortably inside the working day. When the product approaches the length of the day, the queue becomes the slowest part of your process, and you should know that before you switch it on.
Batches: testing several changes in one run
The first way past the ceiling is to stop testing changes one at a time. A batching queue takes several waiting changes, merges them all onto main together, and runs CI once. If that run is green, every change in the batch lands at once. If it is red, the queue does not know which change is at fault, so it splits the batch in half and tests each half, and keeps splitting until it has isolated the bad change.
In the simulation, batches of four change our team's day considerably. The median wait falls from 120 minutes to 43, and the slowest tenth wait 141 minutes or more instead of 211. The queue also spends less CI time than the serial queue, about 23 runs a day for 30 changes, because a green batch lands four changes on one run. At 60 merges a day the batching queue still struggles, with a median wait of 196 minutes and a slowest tenth of 468 minutes or more. The median is less than half of the serial queue's 476 minutes, but the slowest tenth improves less, from 839 minutes to 468.
The reason batching does not scale forever is a piece of arithmetic about how often a batch passes. If 10% of changes are bad, the chance that four random changes are all good is 0.9 to the fourth power, about 66%. Add a 5% chance of a flaky run and the batch passes about 62% of the time. A batch of eight passes only about 41% of the time. So a larger batch lands more changes when it passes, but it fails more often, and every failure starts a round of splitting that costs another CI run at each level. Past a certain size, the splitting eats the gain.
Shopify reached the same conclusion from experience. In their 2019 write-up of Merge Queue v2, they explain that larger batches give higher theoretical throughput but cause failures that are harder to isolate and more rollbacks, and they settled on a batch size of eight "as a balance between throughput and risk". GitHub's history shows what happens when batches grow without that discipline. Before its merge queue, GitHub shipped changes in "trains" that could hold 15 pull requests. Large trains often derailed, and developers sometimes waited more than eight hours after joining a train, only to have their change removed because of a conflict with another one. The batch size, in short, is a bet that the whole batch will pass, and the bet should be sized to your failure rate.
Speculation: testing the future in parallel
The second way past the ceiling is to stop waiting for each result before starting the next test. Zuul, the gating system built for OpenStack, does this with what its documentation calls speculative execution: "it assumes that all jobs will succeed and tests them in parallel accordingly". If five changes A, B, C, D and E are approved in that order, Zuul starts five test runs at once. The run for A tests A alone. The run for B tests A and B together, the run for C tests A, B and C, and so on, so that each run checks a change against the main it would join if everything ahead of it passed.
When everything passes, all five changes land after one CI duration instead of five. When something fails, the queue throws away work. If C fails, A and B still land, C is removed and reported to its author, and the runs for D and E are discarded, because they were testing a future in which C had landed. D and E start again on top of the new main. Zuul controls how much work it is willing to risk with a window, the number of changes at the head of the queue that may run at once. Its documentation states the trade-off directly: if builds usually succeed, the window should be large to maximize throughput, and if they often fail, it should be small to minimize waste.
In the simulation, a speculative queue with a window of four changes our team's numbers the most of any setup. The median wait is 36 minutes and the slowest tenth wait 49 minutes or more. More importantly, the wait barely grows when the team doubles: at 60 merges a day, the median is 46 minutes and the slowest tenth wait 75 minutes or more. The cost appears in the CI bill. At 30 merges a day the speculative queue runs about 32 CI jobs a day, more than the serial queue's 30, and at 60 merges it runs about 71 against 60. The difference is the discarded runs, the futures that did not happen.
Larger systems push the same idea further. Jane Street's post describes two variants it tried: simple speculation, which tests several pull requests in parallel and lets the first success become the new tip, and merging speculation, which merges several requests together and releases them as a group. Uber's SubmitQueue predicts which builds are likely to succeed with a logistic regression model and spends its machines on the most probable outcomes, and it also builds a graph of which pending changes can conflict, so that independent changes can commit in parallel instead of waiting in one line. Speculation, then, buys waiting time with machine time. The window is how you set the exchange rate.
Flaky tests: how a queue makes them everyone's problem
Every setup above treats a failed run as evidence that something in it is broken. A flaky test breaks that assumption, and a queue spreads the damage. Without a queue, a flaky failure delays one developer, who reruns their branch. Inside a queue, the same flaky failure can bounce a good change back to its author, and in a speculative queue it also discards the runs for every change behind it.
To see the effect, we ran the simulation again with the flake rate raised from 5% to 15% of runs. At 30 merges a day, the serial and speculative queues each bounce about 4.2 good changes a day, up from about 1.4. The batching queue bounces fewer, about 1.7, because a flaky run only bounces a change once splitting has narrowed the batch down to that change. Batching pays in time instead: its slowest tenth now waits 170 minutes or more, up from 141. One limitation matters here. The script does not send bounced changes back into the queue, so in a real team, where authors re-queue them, waits would be longer than the numbers above.
Shopify's answer was to make the queue tolerate a few failures before acting on them. Their reasoning was that a real failure shows up in every later run that includes the bad change, while a flake usually does not repeat. So the queue removes a change only after several failures in a row. Their write-up works through the numbers for a flakiness rate of 25%: the chance that a run of failures is a false alarm is 25% for a single failure, 6.25% after two, about 1.5% after three, 0.39% after four and 0.097% after five. They chose a tolerance of three, which means a change is removed on the fourth consecutive failure. The cost of that choice is time, because a genuinely bad change now stays in the queue for several runs before it leaves. The conclusion for a team considering a queue is simple: measure your flake rate before you turn the queue on, because the queue will turn every flaky test into a shared delay.
What teams reported after switching
The teams that have written about their queues all report gains, and each of them also changed something around the queue to get them. GitHub, which builds and sells a merge queue, is the most direct case, and its account of the switch gives the numbers. By 2016, nearly 1,000 pull requests a month were merging into its largest monorepo through the train system, and internal surveys consistently rated deployment as the most painful part of a developer's day. After moving to the merge queue, over 500 engineers merge 2,500 pull requests a month into that monorepo, the average wait to ship a change has fallen by 33%, and a deploy can safely carry 30 or more changes instead of the trains' 15. The migration took years, with small repositories first in mid-2021 and the large monorepo afterwards, because GitHub could never stop deploying for more than about an hour to swap systems.
Shopify's account is useful because it describes a queue that did not work first. Its first merge queue tracked pull requests but did not run CI on them while they waited, so it could not catch soft conflicts. On days with production incidents, when deploys were halted, more than 50 pull requests could pile up, and clearing them took hours. The second version runs CI on the predicted future of main as soon as a change joins the queue, keeps three batches of eight under test at once to bound CI cost, and adds a separate emergency command that skips the queue and is recorded when used. That last detail is easy to miss, but it matters: every team needs a way to land a fix during an incident without waiting behind thirty other changes.
Jane Street's biggest improvement did not come from the queue at all. Its post describes moving code review to happen before integration, keeping each feature on its own branch until it is fully reviewed, and the side effect was that its main repository went from accepting hundreds of requests a week to 30 or 40. Fewer, larger requests made the build-bot's queue manageable. Minsky also noted that the tricks they used changed the constants but not the shape of the problem, and that he expected the approach to break down if the team grew tenfold or a hundredfold. Across all three accounts, then, the queue kept main green, but the waiting time was fixed by faster CI, the right batch size, flake handling or fewer requests entering the queue.
Which queue fits which team
The four setups are not stages that every team must pass through. Each one fits a range of merge rates, CI times and failure rates, and each one breaks in a predictable way when a team leaves that range.
| Setup | Fits when | What it costs | Where it breaks |
|---|---|---|---|
| No queue | Main rarely breaks and the team is small enough that everyone sees each merge | A red main, and the search for who broke it | When soft conflicts start landing several times a day |
| Serial queue | Merges per day times CI minutes is well under the minutes in a working day | Waiting time that grows with every change ahead of yours | When changes arrive faster than one per CI duration |
| Batching queue | Most changes pass when combined, so most batches are green | Splitting runs when a batch fails, and slower isolation of the culprit | When the failure rate or the batch size makes most batches fail |
| Speculative queue | You have CI capacity to spare and your tests are reliable | Extra CI runs for futures that did not happen | When flaky tests or a low pass rate keep discarding the window |
For our example team, the table and the simulation agree. With 30 merges a day and 25-minute CI, a serial queue is already over its ceiling, small batches work, and in the simulation a speculative queue with a modest window keeps the median wait under an hour even if the team doubles, at the cost of roughly a fifth more CI runs at that size.
What to do on Monday
Pull four numbers from the last month of your repository and CI history. The first is merges per day, both the average and the busiest hour, because a queue fills up at its peak, not on average. The second is CI duration at the median and the 90th percentile, because the slow runs set the queue's pace. The third is how often a change breaks main once combined with others, which you can estimate from reverts and "fix the build" commits on main. The fourth is your flake rate, which is the share of failed CI runs that pass when rerun without changes.
Then do the multiplication. Merges per day times the 90th-percentile CI time, compared with the minutes in your working day, tells you whether a serial queue can keep up. If the product is less than about half the day, a serial queue is enough and the simplest one to reason about. If it is close to the whole day, start with small batches and measure how often they fail. If it is more than the day, a queue alone will not save you: speed up CI first or plan for speculation, and budget the extra CI runs. In every case, fix or quarantine your flakiest tests before the queue goes live, and add an emergency path that skips the queue and leaves a record.
Uber's rider app shipped late because hundreds of individually green changes landed in minutes and broke main together. A merge queue would have caught them, but at that rate a serial queue would also have made each of those changes wait behind hundreds of others. Whether that trade is worth it is not a question about the queue. It is a question about four numbers you can collect this week.
The script
The simulation behind every number in this piece is below. It runs 1,000 simulated working days for each setup, with changes arriving at random times across a ten-hour day. Run it with no arguments for the example team, or pass your own merges per day and flake rate, as in python queue_sim.py 60 0.05. The 10% rate of changes that break main when combined is set at the top of the file.
"""Four ways to land 30 merges a day through a 25-minute CI.
Illustrative only: every rate below is an assumption, not a measurement.
"""
import random
import statistics
import sys
DAY = 10 * 60 # a ten-hour working day, in minutes
MERGES_PER_DAY = int(sys.argv[1]) if len(sys.argv) > 1 else 30
CI = 25 # minutes per CI run, the same for every run
P_BAD = 0.10 # share of changes that break main when combined
P_FLAKY = float(sys.argv[2]) if len(sys.argv) > 2 else 0.05 # chance a CI run fails for no reason
def arrivals(rng):
times = sorted(rng.uniform(0, DAY) for _ in range(MERGES_PER_DAY))
return [(t, rng.random() < P_BAD) for t in times]
def run_passes(changes, rng):
"""One CI run over a set of changes: green only if none is bad and it doesn't flake."""
return not any(bad for _, bad in changes) and rng.random() >= P_FLAKY
def no_queue(day, rng):
# Each change merges the moment its own branch CI is green: no wait, no check.
waits = [0 for _ in day]
broke_main = sum(bad for _, bad in day)
return waits, 0, broke_main, 0
def serial(day, rng):
now, waits, runs, rejected_good = 0.0, [], 0, 0
for t, bad in day:
now = max(now, t) + CI
runs += 1
if run_passes([(t, bad)], rng):
waits.append(now - t)
elif not bad:
rejected_good += 1
return waits, runs, 0, rejected_good
def batched(day, rng, size=4):
now, waits, runs, rejected_good = 0.0, [], 0, 0
queue = list(day)
def land(batch):
# Test the batch; on red, split it in half and test each half in turn.
nonlocal now, runs, rejected_good
now += CI
runs += 1
if run_passes(batch, rng):
waits.extend(now - t for t, _ in batch)
elif len(batch) == 1:
rejected_good += not batch[0][1]
else:
half = len(batch) // 2
land(batch[:half])
land(batch[half:])
while queue:
now = max(now, queue[0][0])
ready = [c for c in queue if c[0] <= now][:size]
queue = queue[len(ready):]
land(ready)
return waits, runs, 0, rejected_good
def speculative(day, rng, window=4):
# Zuul-style: change k is tested on top of changes 1..k-1, all at once.
now, waits, runs, rejected_good = 0.0, [], 0, 0
queue = list(day)
while queue:
now = max(now, queue[0][0])
ready = [c for c in queue if c[0] <= now][:window]
results = [run_passes(ready[: k + 1], rng) for k in range(len(ready))]
runs += len(ready)
now += CI
landed = 0
for change, ok in zip(ready, results):
if not ok:
break
waits.append(now - change[0])
landed += 1
queue = queue[landed:]
if landed < len(ready):
t, bad = queue.pop(0) # the first red change leaves the queue
rejected_good += not bad
return waits, runs, 0, rejected_good
def report(name, strategy, days=1000):
# Separate random sources, so every setup sees the same 1,000 days
# no matter how many CI runs it makes.
day_rng, ci_rng = random.Random(7), random.Random(8)
waits, runs, broke, rejected = [], 0, 0, 0
for _ in range(days):
w, r, b, x = strategy(arrivals(day_rng), ci_rng)
waits += w
runs += r
broke += b
rejected += x
p90 = statistics.quantiles(waits, n=10)[-1]
print(f"{name:12} median wait {statistics.median(waits):5.0f} min"
f" | p90 {p90:5.0f} min | queue CI runs/day {runs / days:4.1f}"
f" | broke main/day {broke / days:3.1f}"
f" | good changes bounced/day {rejected / days:3.1f}")
for name, strategy in [("no queue", no_queue), ("serial", serial),
("batches of 4", batched), ("speculative", speculative)]:
report(name, strategy)Its output for the example team, 30 merges a day with 5% flaky runs:
no queue median wait 0 min | p90 0 min | queue CI runs/day 0.0 | broke main/day 3.0 | good changes bounced/day 0.0
serial median wait 120 min | p90 211 min | queue CI runs/day 30.0 | broke main/day 0.0 | good changes bounced/day 1.4
batches of 4 median wait 43 min | p90 141 min | queue CI runs/day 23.3 | broke main/day 0.0 | good changes bounced/day 0.6
speculative median wait 36 min | p90 49 min | queue CI runs/day 32.4 | broke main/day 0.0 | good changes bounced/day 1.4The same team at 60 merges a day (python queue_sim.py 60 0.05):
no queue median wait 0 min | p90 0 min | queue CI runs/day 0.0 | broke main/day 6.0 | good changes bounced/day 0.0
serial median wait 476 min | p90 839 min | queue CI runs/day 60.0 | broke main/day 0.0 | good changes bounced/day 2.7
batches of 4 median wait 196 min | p90 468 min | queue CI runs/day 39.5 | broke main/day 0.0 | good changes bounced/day 0.4
speculative median wait 46 min | p90 75 min | queue CI runs/day 71.2 | broke main/day 0.0 | good changes bounced/day 2.8And at 30 merges a day with 15% flaky runs (python queue_sim.py 30 0.15):
no queue median wait 0 min | p90 0 min | queue CI runs/day 0.0 | broke main/day 3.0 | good changes bounced/day 0.0
serial median wait 119 min | p90 211 min | queue CI runs/day 30.0 | broke main/day 0.0 | good changes bounced/day 4.2
batches of 4 median wait 47 min | p90 170 min | queue CI runs/day 24.4 | broke main/day 0.0 | good changes bounced/day 1.7
speculative median wait 38 min | p90 56 min | queue CI runs/day 34.3 | broke main/day 0.0 | good changes bounced/day 4.2Buse Senol
Editor, How They Ship




Comments
Loading comments…