Sub topic 2
Sharding Jobs Versus Running Tests in Parallel on One Machine
This chapter assumes you can run a test suite from the command line, read a CI YAML file, and know what a CI job and runner are. It compares two ways to make a suite finish sooner and explains when to use each.
Two kinds of parallelism
A test suite can run in parallel at two levels.
- In-process or single-machine parallelism runs several test workers on one runner. Examples are Jest
--maxWorkers, pytest-xdist-n, and Playwright--workers. The workers share the machine's CPU, memory, disk, network ports, and the checked-out code. - Job-level sharding splits the suite into parts and runs each part on its own runner, as a separate CI job. Each job has its own CPU, memory, disk, and filesystem.
The two combine. A common setup runs four shards, and each shard runs two workers.
What each approach gives you
| Property | Workers on one machine | Shards across jobs |
|---|---|---|
| Compute available | Limited by the runner's cores and memory | Grows with each added job |
| Setup cost | Paid once | Paid once per job (checkout, install, build) |
| Shared state | Workers can collide on ports, files, and databases | Jobs are separated by default |
| Results | One process tree, one report | One report per job, which you must merge |
| Configuration | A flag on the test command | A CI matrix plus a shard option |
Where each approach hits a limit
Workers on one machine stop helping when the runner runs out of cores or memory. Past that point, more workers only add contention. Browser tests are a typical case. Each browser instance uses a lot of memory, so too many workers cause slow, flaky runs.
Sharding stops helping when the fixed cost of each job is a large share of the total time. If install and build take five minutes and the tests take six, splitting into six shards leaves a run of about six minutes, not one. Sharding also uses more total runner minutes, so it speeds up feedback but not cost.
Choosing between them
Start with workers on one machine. It is a one-line change and it adds no setup cost. Add sharding when the suite still takes too long on the largest runner you are willing to use.
When you combine them, set the worker count from the runner's core count, not from the shard count. Sharding does not change how many cores one job has.
A Playwright example sets both in one command. The --shard option picks this job's share, and --workers controls parallelism inside the job.
# Run shard 2 of 4, using two workers inside this job
npx playwright test --shard=2/4 --workers=2Shared resources can break sharding
Workers on one machine often share a local database, a temp directory, or a fixed port. Sharding avoids those clashes, because each job has its own machine. It creates a different risk. If every shard talks to the same external staging database or the same test account, the shards can interfere with each other. Give each shard its own data, or make tests create uniquely named records. The next chapter covers this in detail.
Exercise
A suite takes 12 minutes of test time on one job. Install and build take 3 minutes in every job. Ignoring imbalance, how long does one run take with 4 shards, and why is it not 3 minutes?
Show answer
About 6 minutes. Each shard runs 12 / 4 = 3 minutes of tests, but it also pays the 3-minute setup, so each job takes 3 + 3 = 6 minutes. Setup is repeated in every job and does not shrink as you add shards.