Catalogue / samsara-2022-ci-merge-races

Samsara kept monorepo CI usable by fighting flaky tests, testing selectively, sharding Go tests and auto-rebasing stale PRs instead of a merge queue

Samsara, 2022. Continuous integration for the Samsara monorepo.

Organisation
Samsara
Evidence
Reported from production
System
Continuous integration for the Samsara monorepo
As of
2022
Source
Improving CI/CD with a Focus on Developer Velocity

Samsara blog, published 2022-09-16

Architecture

Samsara keeps its code in a monorepo whose CI runs frontend, Go and Python tests, service build checks and linters, choosing the checks for each pull request from the files it changes; Go packages are tested in parallel across CI job shards.

Near: Because we have a monorepo, our CI | Section: Selective testing (reactive) | Near: we parallelized the testing of packages across

Figures, as the source states them

FigureAs statedNoteWhere
Growth in Go unit tests from 2019 to 20225 timesNear: the number of unit tests increased by 5x
CI flakiness2 percentAround; early 2022Near: By early 2022, our flakiness was around
CI run time15 minutesAround; stabilised by early 2022Near: our CI speeds had stabilized around 15

Records

Decision#d1

Auto-rebase pull requests whose base commit is old, accepting an occasional broken main instead of doubling CI time

samsara-2022-ci-merge-races#d1 at corpus version 26a1b3a7e3dd
Chosen

A worker driven by GitHub pull request webhooks rebases a pull request automatically when it is created or pushed with a base commit more than 12 hours behind main, and at merge time when the base is more than 72 hours behind.

Near: To determine which PRs present a merge | Near: Our automatic rebasing is implemented by a

Rejected

Merge queue, as tried in 2019. Rolled out in 2019 and soon reverted: it made flakiness worse, since a failing pull request forced every one behind it to rerun CI, it adds a second CI run even with no flakes, and it hurt time-to-merge.

Near: At Samsara, we rolled out a merge | Near: We realized a merge queue exacerbates CI | Near: even with zero flakes, a merge queue

Reintroducing a merge queue. With CI at about 15 minutes a queue would make it about 30; the team doubted that doubling CI time was worth it and felt it did not need a guaranteed healthy main if that cost productivity.

Near: 15 minutes of CI would become 30 | Near: we felt like we didn’t need guaranteed

What forced it

As Samsara grew into 2021 merge races became more and more frequent; they broke main until someone merged a fix, often for hours.

Near: As we scaled into 2021, with merge | Near: These breakages would often last for hours

Reasoning

The team's guiding goal was developer velocity rather than CI reliability alone; the rebase on push takes seconds, so it can be applied aggressively, and the team says it greatly lowers the chance of a merge race breaking main.

Near: the main principle that guided our tradeoffs | Near: This all happens in seconds so that | Near: it also drastically reduces the chances of

Decision#d2

Cut CI network dependencies and run new tests ten times before merge

samsara-2022-ci-merge-races#d2 at corpus version 26a1b3a7e3dd
Chosen

Node modules install from tarballs in the repo, Go dependencies are vendored and as much as possible is baked into the CI Docker image. New tests run 10 times before merge, and CI attempts the same for existing tests touched directly in the branch.

Section: Upstream dependency flakiness | Section: CI test grinder (proactive)

Rejected

Grinding every test on every CI run. It would make tests ten times slower, which the team judged not worth it.

Near: then it’d make our tests 10x slower

What forced it

Flaky tests and infrastructure made a healthy main look broken, and engineers often lost time debugging or retrying failed runs.

Near: Flaky tests/infrastructure causes an otherwise healthy main | Near: engineers often wasted time debugging or retrying

Reasoning

CI reliability is capped by the reliability of every upstream it calls over the network, and some tests flake less than 0.1% of the time, for which the team puts the chance of detection at only 10% even after 100 runs; reactive measures are needed as well.

Near: our CI reliability is going to be | Near: We found that some tests flake <0.1%

Decision#d3

Run only the CI checks and Go test suites a pull request can affect

samsara-2022-ci-merge-races#d3 at corpus version 26a1b3a7e3dd
Chosen

CI maps a pull request's changed files through glob patterns to the checks it needs, and statically analyses the Go import graph so that only test suites depending on changed code run.

Section: Selective testing (reactive)

Reasoning

In the monorepo every branch ran a large set of checks by default though most changes stay in one area; running fewer tests also lowers the odds of hitting a flaky one.

Near: Recall that Samsara is a monorepo, which | Near: Note that selective testing reduces the chances

Outcome

Selective testing helped keep CI fast, but a pull request touching a widely imported package still ran the whole Go suite.

Near: Selective testing (mentioned in the previous section) | Near: some PRs ended up touching a frequently

Decision#d4

Shard Go package tests across CI jobs, balanced by historical test time, and split the slowest packages

samsara-2022-ci-merge-races#d4 at corpus version 26a1b3a7e3dd
Chosen

Go package tests run in parallel across CI job shards; historical test times per package are used to spread work evenly, and the longest-running packages are broken into chunks that can go to different shards.

Section: The CI speed problem (and mitigations)

What forced it

CI grew slow as the Go test suite grew, hurting developers iterating on pull requests; shards became unbalanced, and some packages took longer than 10 minutes, setting a floor on the longest shard.

Near: As our codebase grew, CI became increasingly | Near: We found that the shards would become | Near: a few of our Go packages began

Cost

Parallelism buys speed with money in more EC2 instances, and it plateaus because every shard repeats setup, compilation, metrics and cleanup.

Near: Parallelization means paying for speed with money | Near: Parallelization will plateau on the longest-running operation

Outcome

The author says parallelization did wonders for CI times, though it is no silver bullet.

Near: Parallelization did wonders for CI times

Decision#d5

Report failed main-branch CI jobs daily and open pull requests that skip flaky Go tests automatically

samsara-2022-ci-merge-races#d5 at corpus version 26a1b3a7e3dd
Chosen

A daily cron reads the previous day's failed main-branch builds, picks out the failing jobs from their logs and posts a report card to a Slack channel. Another cron scans every build's Go test results and, for a test seen passing, failing and then passing, opens a pull request to skip it and files a JIRA ticket for the owning team.

Section: CI reliability report cards (reactive) | Section: Automated flaky test skipping (reactive)

Reasoning

The team found that both proactive and reactive measures are needed against flakiness, because grinding cannot catch tests that flake very rarely.

Near: a mixture of proactive mitigations and reactive | Near: That’s why reactive measures are necessary too

Review

Extracted by
Claude (feed 72)
Human review
Not started. Status recorded on 2026-12-02.