Subminute CI

5 min read Original article ↗

Over a weekend in August, we migrated our entire codebase to a monorepo and brought its median CI time to under 1 minute.

We've held it there, while significantly increasing our test count.

Datadog dashboard showing a 58.6 second median CI execution time, with weekly medians under one minute from August 29 to November 26 Datadog chart from September 8 to November 24: test count grew to about 280 percent of its starting level while CI runtime stayed between about 90 and 130 percent

Moab's product is an enterprise resource planning (ERP) system for equipment businesses. Our customers rely on it as their financial source of truth.

In the early days of Moab, fast CI was easy. With a small codebase using pytest and xdist on a GitHub Actions runner, CI can complete in under a minute. The hard part is keeping it fast while scaling. Moab now powers some very large businesses. Our codebase contains millions of lines of code, and the quantum of tests we had added to ensure correctness at scale considerably slowed down CI. In advance of publishing this post, we tried running our CI on GitHub Actions using our initial pytest and xdist setup. We'd like to tell you how long it took to complete, but the initial run timed out after 3 hours.

Fast CI shortens our iteration loop, especially for our agents. Most engineers are now juggling tens of project threads, and CI is the bottleneck on closing each one out. With fast CI, we can respond to customer requests, fix bugs, and generally ship the broad range of capabilities required for an ERP extremely quickly.

Apart from the obvious benefits, fast CI is meaningful as it's a sort of internal diagnostic test for technical understanding and team alignment. Lowering CI time requires understanding every layer of the stack. Any team can claim they do, but this exercise shows some hard proof. Keeping it low requires total team alignment, since any one engineer can degrade the objective by pushing aberrantly slow tests. We think CI time is one of the best proxies for engineering culture.

Our stack is AWS, Terraform, Python, Flask, SQLAlchemy, Postgres, and React.js with TypeScript. We achieved subminute CI by collectively sweating every second of wasted time at every layer of the stack.

Inspired by Boris Cherny's claimed use of Fable to reduce CI time, we used LLMs to uncover many of these optimization opportunities.

The changes we made follow four themes: Parallelism, Caching, Hardware, and Elimination.

Parallelism

More than anything, this keeps our CI time flat as our test count rises.

We initialize our app, freeze it, and then fork it to match the number of available vCPUs in the runner pool.

Unit tests are distributed across runners using execution time metrics from previous runs. Within each runner, the tests are distributed among processes using the Longest Processing Time heuristic. Tests without time metrics are deterministically spread out across runners using hash partitioning.

We also minimize interdependence between actions in the CI workflow. We run every check as a separate job. If jobs in the critical path don't have to be sequential, we make sure that they aren't.

Caching

We attach a storage volume to test runners that persists data across executions. We use this scheme to save on both repeated computation and network requests.

We have a git mirror of our application code on the disk, avoiding a full GitHub checkout. Now each run only requires a shallow fetch from GitHub.

Docker images live on the disk as well, avoiding a lot of build time. We have a job to bake new images as needed. We restart Docker with the entire Docker data root directory coming from the disk. We warm the disk by running reads on the Docker images and application source code, so subsequent reads hit loaded pages.

A snapshot of the development database lives in Amazon S3. Pulling the snapshot avoids DDL operations to construct the schema and reduces time spent loading fixtures. Migrations are applied to this snapshot if the PR contains them.

Hardware

We use a vendor to access fast runners.

The time between a code push and the runner starting to execute can add a significant amount of CI time. Most of our jobs use 16 vCPU Ubuntu x64 runners and start within 8 to 12 seconds. We profiled startup time across different machine sizes and in our testing found that two 16 vCPU machines start faster than a single 32 vCPU machine.

Elimination

When striving to measure CI time in seconds, small wins are worth pursuing. Every abstraction and ounce of complexity has to pay its weight. Understanding the layers of abstraction in your stack such that you can delete the ones you don't need is key to eking out speed boosts.

  • We use lazy imports to avoid importing the whole application up front.
  • Garbage collection is skipped whenever possible, such as during the initial application import.
  • We don't run actions that are unaffected by the current changes. For example, the database migration step is skipped for PRs with no schema changes, and Semgrep runs only on diffs instead of the whole codebase.
  • We use the fastest tools available (uv, ruff, ty, bun check, oxlint, oxfmt) in place of slower predecessors.
  • We moved from xdist to native forking.
  • Our pipeline reports its results before exiting the runner to keep cleanup time out of the critical path, and we poll for completed jobs rather than using the GitHub API for dependencies.
  • Telemetry and pytest extensions are disabled during the run.
  • Pydantic model compilation is deferred until the model is used. Flask route building is also deferred.
  • Our FactoryBoy database fixtures persist with a flush instead of a commit.
  • We squashed migrations.

Notably, we intentionally avoided trying to predict which unit tests are in scope based on the PR changes. We always run every single unit test. We're the source of truth for our customers, so we decided this tradeoff wasn't worth the speed boost.


Thank you to Pavel Asparouhov, Shreyan Jain, Yunyu Lin, Jiajun Xu, and Jason Katzer for reading drafts of this.