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

System Design

CI/CD, parallelization and sharding

Tests in the pipeline: stages and quality gates

A quality gate is a rule that blocks a merge or a deploy; technically it is a required status check in the branch rules. Tests are arranged by stage: fast checks (lint, typecheck, unit, smoke) on the PR, more expensive ones later. A gate must be deterministic, fast and have an owner, so a flaky test is moved to quarantine with an owner and a deadline instead of staying in the gate.

System design · CI and parallelism

Smoke on a PR fails because of an unstable test, and the team merges past it. What should block a merge?

A smoke test on a PR goes red once in ten runs. The team gets used to pressing «rerun», then to looking past the red, and one day merges a real defect because «it is the same flaky one». The pipeline existed, but the gate did not. This lesson covers how to arrange tests into stages, what a quality gate is and why the reliability of a gate matters more than its strictness.

How it works and why. Tests differ in price and signal. Cheap ones (lint, typecheck, unit) answer in minutes and catch most breakages; expensive ones (full e2e, cross-browser, performance) are slow but cover what the cheap ones do not see. The pipeline principle: fast feedback from cheap tests, and the expensive ones run when it makes sense. So the suite is arranged by the moment of the run.

What to run and when

PR: lint, unit, API, smokeblocks merge, target 5-10 minutesmain: full API and e2etarget up to 15 minutesNightly: long and cross-browsercostly tests that rarely breakRelease: smoke on staging and proda check after deploy
Figure 1. The time targets are anchors for a discussion, not a norm: they are agreed with the team.

A quality gate is a rule that blocks a merge or a deploy: all unit and API tests are green, smoke is green, coverage of changed files has not dropped. Technically it is a required status check in the branch rules (branch protection or a ruleset in GitHub): the test job is named in the rule, and without a green result the merge button does not activate. A gate has four properties: determinism (the same code gives the same result), speed, an owner and a transparent bypass (an emergency merge is possible but leaves a trace).

What to run on a PR. The full suite on every PR is often too expensive. Selection options:

  • A smoke set with a tag: in Playwright the tag is set in the test declaration, and the run goes through --grep @smoke.
  • Tests tied to changed files: --only-changed=origin/main takes the changes between HEAD and the given ref (without a ref only uncommitted changes, so CI passes the ref explicitly and sets fetch-depth: 0 in actions/checkout, which fetches a single commit by default) and runs the changed test files plus those that import changed modules. For a backend change that tests do not import, it tells nothing.
  • Rerunning only the failed ones: --last-failed, handy locally.

For fintech the PR set must include the critical money scenarios: a transfer, a top-up, a refusal on insufficient balance. The cost of a mistake there is the highest, so they are not deferred to the nightly run.

Example: a PR pipeline. Cheap checks go first, smoke starts only when they are green. Note the --grep-invert filter: it takes unstable tests out of the gate.

name: pr
on:
  pull_request:
jobs:
  fast:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v7
      - uses: actions/setup-node@v7
        with:
          node-version: lts/*
          cache: npm
      - run: npm ci
      - run: npm run lint && npm run typecheck && npm test
  smoke:
    needs: fast
    runs-on: ubuntu-latest
    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 chromium
      - run: npx playwright test --grep @smoke --grep-invert @quarantine

A test is tagged like this:

test('transfer reduces the balance', { tag: '@smoke' }, async ({ funded }) => {
  // ...
});

The @quarantine tag works the same way but in reverse: a test with it does not block a PR, a separate job runs it (so you can see whether it is still unstable), and it has an owner and a deadline. Without a deadline quarantine becomes a graveyard.

An unstable test in the gate

Typical

A gate on an unstable test

Smoke fails for no reason. The team reruns until green, and then starts merging past it.

Better

Quarantine with an owner

The unstable test leaves the gate but stays in a separate run with an owner and a deadline.

Figure 2. A gate works as long as it is trusted.

Limits and alternatives. Required checks are tied to job names: a matrix job with eight shards gives eight names, so a separate summary job that waits for all and shows the overall result is handier. There is also a trap with path filters: if a workflow did not start because of paths, the required status stays «Pending» and the merge is blocked, so it must be taken into account when configuring. A merge queue checks changes in the queue rather than separately and catches conflicts between PRs, but adds time. For a small project two stages are enough: PR and main.

Common mistakes.

  • Making an unstable test a gate: the team learns to ignore red, and the gate loses meaning.
  • Running the full e2e on every PR without need: expensive and slow, feedback arrives late.
  • Relying on --only-changed for changes that tests do not import: smoke or API tests are needed as a floor.
  • Quarantine with no owner and no deadline: a test lives in it for years.
  • A gate with no bypass for an emergency: people get around it through repository rules instead of legally.

Check yourself

Smoke on a PR fails in roughly one of ten runs with no code change. What to do, and what happens if nothing is done?

Answer

If nothing is done, the team gets used to rerunning and ignoring, and the gate loses trust: a real defect slips through as «the same flaky one». The right move: take the test into quarantine (outside the gate) with an owner and a deadline, find the cause and bring it back, and keep only deterministic tests in the gate.

A PR changes only the code of a backend service that tests do not import. Why does --only-changed not protect here?

Answer

It selects tests that import the changed files. Tests do not import the service code, so there is no link and the needed test may not be selected. Such changes need a smoke or API set as a required floor.

In an interview. Junior: I build the pipeline in layers: fast checks and smoke on a PR, the full set on main, long and cross-browser tests at night, smoke in production after a deploy. The gate blocks a merge only with stable tests. Middle/Senior: A gate is a required status check, so it must be deterministic, fast and have an owner. I move unstable tests into quarantine with a deadline instead of leaving them in the gate, otherwise the team stops trusting red. On a PR I take tagged smoke and the critical money scenarios, and treat --only-changed as a supplement, not a floor. For a matrix I make a summary job and remember path filters.

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.

Feedback speed and cost

Speeding up a pipeline starts with measuring, not with adding shards. Time is broken down into runner startup, dependencies, data and login, slow tests and environment waits. Overhead is multiplied by the number of shards, so a prebuilt image with browsers is often cheaper than new machines. Cheap levers are maxFailures and cancelling outdated runs; PR feedback time is judged by p95.

System design · CI and parallelism

The pipeline got 4 minutes longer and nobody knows why. Where exactly does the time go?

Last month a run took 15 minutes, now 19. The team proposes adding shards, because that is quick to do. But a Senior must talk not only about speed but also about money and about where the real bottleneck is: the four minutes might have come from dependency installation, new slow tests or an overloaded staging, and in none of these cases will new machines help as much as it seems. This lesson covers how to break time down, when to add machines and what they cost.

How it works and why. Pipeline time consists of parts, and each has its own lever. So the first step is measuring, not deciding. GitHub Actions shows the time of each job step, and Playwright can show the slowest tests. Only then do you choose where to invest.

Where to look for time in a pipeline

Runner start and dependenciesnpm cache, a ready image with browsersData and loginAPI instead of UI, storageStateThe tests themselvesthe slowest: rewrite or move to APIWaiting for the environmenta health check instead of sleepReport and artifactsblob reports, only what is needed
Figure 1. Measure first, then choose a lever: without measuring the wrong thing gets optimised.
  • Runner start and dependencies. An npm cache (cache: npm in setup-node caches downloaded packages, not node_modules) and a ready image with browsers. The Playwright documentation does not recommend caching browser binaries: the gain is minimal, so a mcr.microsoft.com/playwright container with a tag matching the @playwright/test version is better.
  • Data and login. The API instead of the UI, storageState instead of logging in in every test.
  • Slow tests. Find the longest tests and rewrite or move them to the API. reportSlowTests in the config lists the slowest files; individual tests are visible in the report.
  • Waiting for the environment. A health check before the start, not sleep: sleep either waits needlessly or does not wait enough.

Example: fixed costs against cost. Suppose 8 shards of 15 minutes, 50 merges a day, 3 minutes of overhead per shard:

8 machines x 15 min x 50 merges = 6000 machine-min a day
Overhead: 3 min x 8 x 50       = 1200 (about 20%)
If an image cuts it to 1 min:    -800 a day

In GitHub Actions each job is billed separately and rounded up to the minute, so 8 shards are 8 separate charges for time, and the overhead is not shared between them. Two conclusions follow. First: a prepared image is cheaper than new shards, because it cuts both time and cost. Second: the cost of sharding grows with their number, while the time gain is nonlinear. Concrete prices depend on the plan and the runner type, so they are taken from the current documentation, not from memory.

When to add machines and when not. If tests run into a shared staging (the DB, request limits), adding shards will not speed up the run but will only increase timeouts. First the bottleneck must be found.

When to add machines

Typical

Add shards without measuring

The bottleneck is the shared staging DB. New shards add timeouts, not speed, and the cost grows.

Better

Find the bottleneck first

They saw the time goes on installation and login. An image and storageState removed it without new machines.

Figure 2. Parallelism helps only where the bottleneck is the tests themselves and the environment can take it.

Two cheap optimisations that need no machines. The first: fail fast. The maxFailures option stops the run after N failures, and on a PR that saves minutes when something basic (login) is broken. The second: cancel outdated runs. When a new commit appears in a PR, the previous run is no longer needed by anyone:

export default defineConfig({
  maxFailures: process.env.CI ? 10 : 0,
  reportSlowTests: { max: 20, threshold: 30_000 },
});
concurrency:
  group: pr-${{ github.ref }}
  cancel-in-progress: ${{ github.event_name == 'pull_request' }}

The condition in cancel-in-progress limits cancellation to PR runs, so runs on main are not cut off. The config above: maxFailures on CI, and zero means no limit locally.

Limits and alternatives. Optimisation has a downside: an overly aggressive maxFailures hides part of the failures, and the PR author sees only the first ones, fixes them and then the rest surface. Cancelling runs suits a PR but not release runs. The strategy «fewer tests on a PR, the full suite on main» saves money but moves defect detection later, and that is a deliberate trade-off to be stated out loud. For PR feedback time look at the percentile (p95), not the mean: the rare slow runs create the feeling of a slow CI.

Common mistakes.

  • Adding shards without measuring: the wrong thing gets optimised and the cost grows.
  • Using sleep instead of a readiness check: the test either waits too long or fails because the environment is not ready.
  • Not cancelling outdated runs: money goes on a result nobody reads any more.
  • Caching browser binaries and spending time maintaining it instead of a ready image.
  • Talking about speed and staying silent about cost: for a Senior the answer is incomplete.

Check yourself

8 shards, 3 minutes of overhead on each, 50 merges a day. How many machine-minutes a day go to overhead, and what does an image that cuts it to 1 minute change?

Answer

3 x 8 x 50 = 1200 machine-minutes a day. With the image 1 x 8 x 50 = 400, a saving of 800 a day without new machines and without changing the tests. That is cheaper than adding shards, which multiply the overhead.

Shards were added and the time did not drop, only timeouts appeared. What is the likeliest cause and what to do?

Answer

The bottleneck is not the tests themselves but the shared environment: the DB or staging limits cannot take more parallel sessions. Confirm it by measuring, then scale the environment, give each shard its own, or reduce the load (data through the API, fewer calls), instead of adding machines.

In an interview. Junior: Before adding machines I look for the bottleneck: dependency installation, login through the UI, the slowest tests or an overloaded environment. Parallelism helps only when the environment can take the load. Middle/Senior: I break pipeline time down into start, data and login, the tests themselves, waiting for the environment and the report, and take a lever for each. I count the cost: overhead multiplies by the number of shards, so a prepared image is cheaper than new machines. I add maxFailures and cancelling of outdated runs. The metric: p95 of PR feedback time and the cost of a run in machine-minutes, not only the average time.