Video in progress

Staff 🌿 Git

Why is a merge queue needed, and what problem does it solve?

Two PRs can each be green on their own but break main together, because each was tested without the other. A merge queue puts PRs in a line and tests each one merged with the ones ahead of it, and only then adds it to main.

📖 Answer

A classic example: PR A changes the PriceCalculator.calculate(...) method, and PR B calls that method in a new place with the old signature. Both are green on their own branches. Merged one after another, main does not compile.

The "Require branch up to date" rule solves this, but in a large team after every merge, all the PRs have to be rebased and retested. With 30 people, they spend the whole day pressing the "Update branch" button.

How a merge queue works:

  • once a PR is approved, it joins the queue;
  • the system creates temporary branches: main + A, main + A + B, main + A + B + C;
  • CI runs them (often in parallel);
  • if B fails, it is removed from the queue and C is retested as main + A + C;
  • main advances only with a green combination.

The trade-offs:

  • more CI resources: every PR is tested at least twice;
  • flaky tests jam the queue. Before turning on a merge queue, clean up the flaky tests, otherwise the team will come to hate it;
  • batch checking gives speed, but when it fails you have to split to find the culprit.

A small team (5-6 people, 5-10 merges a day) usually does not need it. With 50+ merges a day, though, without it main goes red several times a week.