How to Keep Build Pipelines Fast
Practical ways to shorten CI build times, from caching and parallel jobs to test selection and lean container images, without sacrificing reliability.
On this page
A slow build pipeline is a tax on every developer, every day. When feedback takes thirty minutes, people switch tasks, forget context, batch up larger changes and start merging without waiting for results. The practice of frequent, small changes that makes CI valuable quietly dies.
The good news is that pipelines rarely slow down for mysterious reasons. They grow slow in predictable ways, and there are well-known remedies. This guide covers how to measure where time goes and the changes that usually give the biggest gains.
Measure Before You Optimize
Guessing wastes effort. Start by recording how long each stage takes: checkout, dependency installation, build, each group of tests, packaging and any deploy step. Most CI systems show per-step timings. If yours does not, add timestamps to the logs.
Look at two numbers:
- Total wall-clock time: how long a developer waits from push to result.
- Queue time: how long a job waits for a free runner before it even starts.
Sometimes the slow part is not the work but the waiting. Long queues point to a shortage of runners, not slow code. Also look at the trend over time. A pipeline that gained a few seconds each week will become painful gradually without anyone noticing.
Cache What Does Not Change
Downloading and installing dependencies on every run is one of the biggest avoidable costs. Use your CI system's cache feature to store dependency directories between runs, keyed on a hash of the lock file. When the lock file does not change, the cache restores in seconds.
Other things worth caching:
- Compiled output and intermediate build artifacts.
- Package manager download folders.
- Docker image layers.
Be careful with cache keys. A key that is too broad can serve stale content and cause confusing failures, while a key that changes every run gives no benefit. Include the lock file hash and the language version, and know how to clear the cache when something looks off.
Run Independent Work in Parallel
Many pipelines run steps one after another that do not depend on each other. Linting, unit tests, type checks and security scans can usually run at the same time on separate runners.
For large test suites, split them into shards and run the shards in parallel. A suite that takes twenty minutes in a single job might finish in five across four jobs, subject to the overhead of setup. Balance shards by past run time rather than by file count, so that one shard does not become the bottleneck.
jobs:
lint:
steps: [ run: "make lint" ]
unit:
steps: [ run: "make test-unit" ]
integration:
steps: [ run: "make test-integration" ]
Parallelism has limits. Each job repeats checkout and setup, so splitting too finely makes total compute use rise while gains shrink.
Fail Fast and Order Checks Sensibly
Put quick, high-signal checks first. A formatting check that takes ten seconds should not wait behind a thirty-minute integration run. If the cheap step fails, cancel the rest and tell the developer immediately.
Also cancel outdated runs. When someone pushes a new commit to the same branch, the earlier run is usually pointless. Most systems can cancel superseded runs automatically, which frees runners and reduces queue times for everyone.
Run Only What Is Affected
Not every change needs the whole pipeline. A documentation edit should not trigger a full build and test cycle. Use path filters to run jobs only when relevant files change.
In larger repositories, build tools can work out which modules depend on the changed files and test only those. This is more complex to set up, but it can transform a repository with many components. Keep a full run on the main branch or on a schedule, so that gaps in the selection logic get caught.
Tame the Tests
Tests are often the largest slice of time. Review them with a critical eye:
- Find the slowest tests and ask whether they need to be that slow.
- Replace real network calls and sleeps with fakes or controllable clocks.
- Reuse expensive setup, such as a database schema, rather than rebuilding it for each test.
- Separate fast unit tests from slower end-to-end tests, and run the slow ones less often or in a later stage.
- Fix or remove flaky tests, because retries add hidden minutes and erode trust.
Aim for a pyramid: many fast unit tests, fewer integration tests and only a small number of end-to-end tests covering critical paths.
Slim Down Containers and Environments
If your jobs run in containers, image size and startup time matter. Use a small base image, pre-install the tools you always need, and keep the image in a registry close to your runners. Avoid installing the same toolchain from scratch on every run.
Order your Dockerfile steps so that rarely changing layers, such as system packages, come before frequently changing ones, such as application code. This lets the layer cache do its job.
Match Resources to the Work
Under-provisioned runners make everything slow. If builds are memory-bound or limited by disk speed, a larger runner may finish sooner and cost less in total than a small one running far longer. Test this with a few experiments instead of assuming.
Keep It Fast Over Time
Speed decays unless someone guards it. Set a target, such as a ten-minute limit for the main feedback loop, and show the trend on a dashboard. When a change pushes the time over the line, treat it like any other regression. Assign an owner for pipeline health and spend a little time each sprint on maintenance.
Frequently Asked Questions
What is a reasonable target for build time?
Many teams aim for the main feedback loop to finish within about ten minutes, since beyond that developers tend to switch tasks. Your own target should reflect your project size and team habits. Splitting a quick pre-merge check from slower nightly suites is a common way to meet it.
Does caching ever cause problems?
Yes. A stale or corrupted cache can make builds behave strangely, and a poorly chosen key can serve the wrong content. Use keys based on lock files, keep a way to bust the cache, and occasionally run a clean build without cache to confirm that everything still works from scratch.
Is it better to buy faster runners or optimize the pipeline?
Start with optimization, because waste in the pipeline will simply run faster on bigger machines. After removing obvious waste, test larger runners for the heaviest jobs. Sometimes the extra capacity shortens the overall time enough to be worthwhile, but measure the result.