← All posts

Engineering · 2026.10.10 · 6 min

Speeding Up Rust and Next.js CI on a 2 vCPU GitHub Runner

After moving the backend to Rust, the PR backend job went from 11.5 minutes to 1.5 and frontend checks from 81 seconds to 30, mostly through test databases, bcrypt, and caches that broke silently.

Reading
Theme
Size
Width

After moving the backend from Java to Rust (previous post), the backend job on the first PR ran for 11 and a half minutes and then failed because the disk filled up. I later got it down to 1 min 33 s, and frontend checks from 81 seconds to 30. Most of the time came out of how tests hit Postgres, bcrypt in tests, and a few bugs that silently broke caching.

The constraints were a private repo, a 2,000-minute monthly Actions quota, a single backend crate, and GitHub's standard runners.

Measure before changing anything

Standard runners for private repos have 2 vCPUs. Compared with an M-series Mac, compilation is about 2.2 times slower and Postgres-heavy work about 4 times slower. Anything that runs fast locally gets multiplied several times over in CI.

Of those first 11 and a half minutes, downloading crates took 3 seconds. The bulk was cold compilation with no cache, plus DROP DATABASE. I would never have guessed that without measuring.

Backend: the time went to Postgres and bcrypt

Rust repositories have no DI, so nearly every test hits a real Postgres. Each test used to create its own test DB and never drop it, and on CI they piled up to about 2,300 and filled the disk. I changed it to reuse them: when a test finishes, a background task clears the data and restores the seed data, then hands the DB to the next test.

The restore doesn't need to clear every table. pg_stat keeps cumulative insert, update, and delete counts per table, and comparing them with the baseline from the last restore tells you which tables the previous test wrote to. There were also two helpers that used ALTER SEQUENCE ... RESTART to change where ids start. That's DDL, so the DB could only be dropped and recreated, and each DROP DATABASE stalled for 2.8 seconds, 139 times per run. Switching to setval has the same effect and isn't DDL. On top of that, the CI test Postgres runs with fsync off and trust authentication. After this step, 11 min 33 s came down to 3 min 51 s.

Before and after: each test used to create its own DB and never drop it, piling up to about 2,300 and filling the CI disk, and ALTER SEQUENCE forced a DROP DATABASE that stalled 2.8 s, 139 times per run. Now test DBs are reused: after each test a background task compares pg_stat write counts, clears only the tables that were written, restores the seed data, resets ids with setval, and hands the DB to the next test. The PR backend job fell from 11 min 33 s to 3 min 51 s.

Next was CPU. More than 60% of the test process's time went to bcrypt: registration and login tests go through the real flow and compute cost 12 every time. Tests now use cost 5, with a separate test guarding that production is still 12. Each test DB also keeps persistent connections now, and the average number of connections opened per test fell from about 3.3 to 1.4.

Caching: a few traps that fail silently

At this point the median PR job with a warm cache was about 2.6 minutes, but 6 out of 25 runs were cold and took 7.5 to 12 minutes. I compared against well-known Rust projects that also test against Postgres, and my config was already near the front of the pack. What made it slow was a few bugs.

A half-built cache squatted on the key. rust-cache's cache-on-failure let a cancelled run save a 72MB, nearly empty cache. GitHub won't let you overwrite an existing key, so every run after that hit the empty cache exactly and didn't save a new one. Every build was effectively cold, and it was never going to fix itself.

Cold compiles happened too often. The toolchain wasn't pinned, so on the day stable shipped a new release every run compiled cold. The content hash of the vendored sqlx-postgres was part of the cache key, so any change to the vendored code rebuilt all 300-plus dependencies. The version is now pinned in rust-toolchain.toml, and the vendored crate uses option_env! so cargo only rebuilds that one crate.

Rule changes don't send you a notice. Since cargo 1.99, the presence of a CI environment variable turns incremental compilation off by default, so the speedup would have broken silently on upgrade day. Compilation now goes through a wrapper that removes CI.

Resource quotas scale with core count. A new test took a DB and never gave it back. The DB quota is twice the number of parallel tests, which on 2-core CI means 4. With 3 of them held, other tests had to wait out a 30-second timeout, and the test step went from 51 seconds to 191. Locally, with 10 cores and a quota of 20, none of this showed. Acquiring a DB now returns a guard that hands it back automatically on drop.

Slot diagram: the DB quota is twice the number of parallel tests, so 2-core CI has 4 and a leaking test held 3 of them, making other tests wait out a 30-second timeout and pushing the CI test step from 51 s to 191 s; locally, 10 cores and a quota of 20 hide the same 3 held slots.

The incremental cache key also changed from the commit SHA to content (the toolchain version plus the git tree of backend/), so nothing is re-saved when the backend hasn't changed. Total cache size fell from 11.4GB to about 2GB.

Frontend: 81 seconds to 30

Frontend time was mostly next build at 28 seconds and ESLint at 18. Two things only came out after digging. Over 80% of ESLint's time was the React Compiler rule set, and when Next 16 removed next lint, the cache that came built in went away with it. And the type check inside next build can't use incremental mode, so it starts from scratch every time.

The fixes: ESLint runs with --cache in the background, in parallel with the build. Turbopack's build file cache is on. Type checking is its own step using the native TypeScript 7, and a full cold run takes 2 to 3 seconds. On a cache hit, node_modules is restored directly instead of running npm ci. Turbopack's file cache is still experimental, so only PRs read it. Main always builds cold, and artifacts headed for STG never depend on the cache.

Results, and what I didn't do

Bar chart of CI before and after: PR backend job 11 min 33 s to 1 min 33 s (warm cache, arm64), tests within that job 196 s to 33 to 36 s, merge to STG about 7.5 min to about 4 min 10 s, total Actions cache 11.4GB to about 2GB, PR frontend checks 81 s to about 30 s.

The last step was switching to arm64 runners. Their 2 vCPUs are 2 physical cores, while x64 gives you 1 core with SMT. DB tests that saturate both cores went from 42.7 seconds to 33.3, and the per-minute price is lower too.

Splitting into multiple crates was the direction I originally wanted to go. Simulating with 250 commits from the Java era, cutting straight along module lines only reduced average compilation to 0.96x, and another way of cutting made it 1.20x, because the modules that change most often are also the ones the most other modules depend on. I also evaluated a self-hosted runner. The idle 4-core ARM machine I have was actually slower at incremental builds, and I'd have to maintain it myself.

The big public Rust CI speedups (nextest, mold, sharding, splitting crates) mostly rely on many cores and many test binaries, which you don't get with a single crate on 2 vCPUs. What actually worked this time was first looking at where the CPU time goes, and keying caches on content instead of the commit SHA.

#Next.js#GitHub Actions#CI/CD#Rust

Letters

Loading comments…

Next

Investing

Why I Dollar-Cost Average Everything Into QQQ on IBKR

Why I dropped my 30% drawdown cap and now dollar-cost average monthly into QQQ on IBKR, covering the QQQ vs SPY history, the drawdown I accept, my rules for crashes, and long-term costs.