Sub topic 13
Runtime Budgets, Caching, and Parallelism
This chapter assumes you can read a GitHub Actions workflow, know what a job and a step are, and have already sorted checks into pull request, merge, and nightly stages. Here you will keep each stage fast enough to be useful.
Set a runtime budget per stage
A runtime budget is the maximum wall-clock time a stage may take before you treat it as a problem. Without a budget, a stage slowly grows until developers stop waiting for it.
| Stage | Example budget | Why |
|---|---|---|
| Pull request | 10 minutes | Developers wait for this result before asking for review. |
| Merge queue or main | 30 minutes | Fewer runs, and a failure blocks the whole queue. |
| Nightly | 2 hours | No one waits, but the run must finish before the morning triage. |
These numbers are starting points. Pick yours from how long your team will tolerate waiting. Measure wall-clock time, which is what the developer feels, and also total compute minutes, which is what you pay for. Parallelism lowers the first and can raise the second.
Cache what does not change
A cache stores files from one run so later runs can reuse them. Dependency downloads are the usual candidate, because they rarely change between commits.
The setup-node action used in the earlier examples already handles this:
- uses: actions/setup-node@v4
with:
node-version: 20
cache: npmThe action keys the cache on a hash of your lockfile. When package-lock.json changes, the key changes and the cache is rebuilt. Two common errors:
- Caching
node_modulesdirectly. This can hide dependency problems and break across Node versions. Cache the npm download cache and runnpm ci. - Caching build output with a key that does not include its inputs. You then test stale code.
A cache can only save the time it takes to fetch things. It does not make slow tests faster.
Run independent work in parallel
Jobs in a workflow run in parallel unless you add needs. Splitting lint and unit tests into separate jobs makes the stage as slow as the slowest job instead of the sum of both.
name: pr
on:
pull_request:
branches: [main]
jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
cache: npm
- run: npm ci
- run: npm run lint
unit:
runs-on: ubuntu-latest
strategy:
matrix:
shard: [1, 2, 3]
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
cache: npm
- run: npm ci
- run: npx jest --shard=${{ matrix.shard }}/3The matrix creates three copies of the unit job. Each runs one third of the tests, using Jest's --shard option. This example assumes your project uses Jest. Other runners have their own sharding flags.
Each parallel job repeats checkout and npm ci. If setup takes two minutes and tests take three, splitting into many shards stops helping quickly.
Know when to stop optimizing
Use this order, cheapest change first:
- Add caching for dependencies.
- Run independent jobs in parallel.
- Shard the slowest test suite.
- Move the slowest checks to a later stage, as covered in the placement chapters.
Also set timeout-minutes on every job, as the nightly example does. The default limit for a GitHub-hosted job is six hours, so a hung test can hold a runner for a long time.
Exercise
A pull request stage takes 14 minutes against a 10-minute budget. Dependency installation takes 4 minutes on every run, and lint and unit tests run one after another. Name two changes to try before moving any check to the nightly stage.
Show answer
First, cache the dependency downloads (for example with cache: npm), which cuts the repeated install time. Second, split lint and unit tests into separate jobs so they run in parallel, which makes the stage as long as the slower job rather than the sum. Sharding the unit tests is a third option if they still exceed the budget.