Imagine a busy single-lane bridge with a traffic controller. Cars (PRs) don't just drive onto the bridge whenever they feel ready — they line up, and the controller ensures each car safely crosses before the next one enters. The bridge (your main branch) is never in a broken state because the controller enforces order.
The Serialization Layer
To solve the "Broken Trunk" problem, we must ensure that PRs are tested in the exact order they will be merged.
A Merge Queue sits between the "Merge" button and the actual main branch. Here is the precise sequence of events:
- A developer clicks "Add to Queue" (or the equivalent) on their approved PR.
- The PR does not go directly to
main. It enters the queue. - The queue takes the PR and merges it with the latest
mainand any other PRs already ahead of it in the queue. - It runs the full test suite on this Speculative Branch — a temporary branch that represents what
mainwould look like if this PR merged. - If tests pass, the PR is merged to
mainand the speculative branch is discarded. - If tests fail, the PR is ejected from the queue, the developer is notified, and the queue continues processing the remaining PRs.
This guarantees that every commit on main was tested in the exact state it landed — not against a stale base.
main ──────────────────────────────────────────────► (always green)
▲ ▲
│ │
[Speculative: main+PR1] [Speculative: main+PR1+PR2]
│ │
PR1 ✅ PR2 ✅
Key Terminology
- Speculative Branch: A temporary branch created by the queue to test a PR in its future merged state.
- Queue Position: The order in which PRs will be merged. Earlier position = tested against fewer speculative changes.
- Ejection: When a PR fails its speculative test and is removed from the queue without touching
main.
Interviewers often ask: "What's the difference between a merge queue and just requiring PRs to be up-to-date before merging?" The key distinction is who does the work. "Require branches to be up to date" forces every developer to manually rebase and re-run CI before merging — creating a race condition where two developers can both rebase, both go green, and still break main. A merge queue automates the rebase and serializes the test runs, eliminating the race condition entirely.