QA Interview Hub · Knowledge base · Quizzes · Українська

System Design › CI/CD, parallelization and sharding

Workers and shards: how parallelisation works

Playwright splits work on two levels: workers (processes on one machine) and shards (parts of the suite on different machines, --shard). A run lasts as long as the longest shard, because work is split by the number of groups, not by time, and every shard pays its own startup cost. fullyParallel evens the shards out, and the blob reports of the shards are merged with merge-reports.

System design · CI and parallelism

We added 8 machines, but the run got only twice as fast. Where did the time go?

It was 40 minutes. We set up 8 shards, expected five minutes and got twenty. The same machines, the same tests, and the calculation «40 divided by 8» did not come true. This lesson covers how Playwright splits work into workers and shards, why one shard can drag everyone and where the overhead hides.

How it works and why. Playwright has two levels of parallelism: workers (processes on one machine, each with its own browser, the number limited by CPU and memory) and shards (splitting the suite between several machines through --shard=1/4, --shard=2/4 and so on). They multiply: 8 machines of 8 workers give up to 64 slots. By default the number of workers is half of the machine's logical cores, and it can be set as a number or a percentage (--workers=50%).

Two levels of parallelism

Inside a machine

Workers

Processes on one machine, each with its own browser. The limit: CPU and memory of the machine.

Between machines

Shards

The suite is split into parts with --shard=i/N, each run by a separate machine. The limit: overhead on every machine.

Figure 1. They multiply: 8 machines of 8 workers is 64 slots.

Why two knobs rather than one. Workers are cheap, because they share downloaded dependencies and one checkout, but they run into one machine. Shards bypass that limit at the price of a separate machine and therefore a separate start: checkout, installing dependencies and browsers, login. Every shard pays this overhead.

Example: sharding in GitHub Actions. A matrix starts a job per shard, each writes a blob report into an artifact:

test:
  runs-on: ubuntu-latest
  strategy:
    fail-fast: false
    matrix:
      shardIndex: [1, 2, 3, 4, 5, 6, 7, 8]
      shardTotal: [8]
  steps:
    - uses: actions/checkout@v7
    - uses: actions/setup-node@v7
      with:
        node-version: lts/*
        cache: npm
    - run: npm ci
    - run: npx playwright install --with-deps
    - run: npx playwright test --shard=${{ matrix.shardIndex }}/${{ matrix.shardTotal }}
    - uses: actions/upload-artifact@v7
      if: ${{ !cancelled() }}
      with:
        name: blob-report-${{ matrix.shardIndex }}
        path: blob-report
        retention-days: 1

A separate job collects the blob reports and merges them into one HTML report with merge-reports:

merge-reports:
  needs: [test]
  if: ${{ !cancelled() }}
  runs-on: ubuntu-latest
  steps:
    - uses: actions/checkout@v7
    - uses: actions/setup-node@v7
      with:
        node-version: lts/*
    - run: npm ci
    - uses: actions/download-artifact@v8
      with:
        path: all-blob-reports
        pattern: blob-report-*
        merge-multiple: true
    - run: npx playwright merge-reports --reporter html ./all-blob-reports

For this the reporter in CI must be blob, while locally it can be html. Without merging, each shard would leave its own fragment and there would be no picture of the run.

Where the time goes. Three mechanisms explain the gap between «40 / 8» and reality:

  • The unit of distribution. By default Playwright parallelises files, while tests inside a file run sequentially. Sharding also splits into groups (files, and with fullyParallel: true individual tests), evenly by the number of groups, not by time. If a file with 200 tests sits next to files of 5, without fullyParallel one shard turns out much longer.
  • The longest shard sets the time. The run ends when the last one finishes, so the unevenness shows in the figure below.
  • Overhead multiplies. Each shard pays for its start separately, and worker-scoped fixtures run on every worker.

Uneven shards: the longest sets the time

shard 16 minshard 26 minshard 37 minshard 414 min
Figure 2. An example: the average shard is 8 minutes, the longest 14. Feedback arrives after 14.

The efficiency metric is the difference between the longest and the average shard. If the longest is 14 minutes and the average 8, the time is set by 14, while the other shards finished earlier. In the cloud a finished runner costs nothing, so mostly time is lost; on your own machines with fixed capacity capacity is lost too.

Limits and alternatives. The number of workers on a machine must not exceed its resources: 8 browsers on 2 vCPU give timeouts and flaky tests, because a slow browser does not fit into the waits. Tests in serial mode (test.describe.configure({ mode: 'serial' })) run in one worker as a group, so they should be minimised. If the bottleneck is not CPU but a shared environment, adding shards will not help (see the lesson on cost). The alternative «everything on one powerful machine» is simple to configure but hits the machine's ceiling.

Common mistakes.

  • Forgetting fullyParallel and wondering why one shard is twice as long: it holds all the heavy files.
  • Running more workers than the machine has resources for: timeouts look like flakiness.
  • Not merging blob reports: analysing a failure becomes collecting pieces from eight jobs.
  • Leaving fail-fast on in a shard matrix (it defaults to true): the first failure cancels the rest, and you see only part of the problems.
  • Treating shards as free: each one is extra dependency installation and login.

Check yourself

Without fullyParallel the shards come out very uneven: one is much longer. Why?

Answer

Without it the unit of distribution is a file, and all tests of a file run in one worker. A file of 200 tests lands whole in one shard, while the other shards get small files. With fullyParallel: true individual tests are distributed, and the shards even out.

Why do 8 workers on a 2 vCPU machine give timeouts and flakiness although the test logic is correct?

Answer

The browsers compete for CPU, so pages respond more slowly, and waits and asserts calibrated for normal speed expire by timeout. It is a resource problem, not a logic one, so the number of workers is chosen for the machine and measured.

In an interview. Junior: There are two levels: workers on a machine and shards between machines. I enable fullyParallel for even distribution and merge blob reports in a separate job. Middle/Senior: I pick the number of workers for the machine's resources, because overload at once gives timeouts. I know sharding splits by the number of groups, not by time, so I watch the gap between the longest and the average shard. Overhead multiplies by the number of shards, so a prepared image and a cache matter. In the matrix I set fail-fast: false to see all the failures, and I merge reports with merge-reports.

More articles in this chapter