> For the complete documentation index, see [llms.txt](https://docs.currents.dev/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.currents.dev/guides/parallelization-guide/balanced-sharding.md). Agents can also search and read this documentation through its [MCP server](https://docs.currents.dev/~gitbook/mcp).

# Balanced Sharding

Split Playwright shards by test duration, using the history Currents records

Playwright's `--shard=i/N` splits tests by count and file order, not by duration. One shard can get a few long spec files and finish minutes after the others, while their machines sit idle. With balanced sharding, each shard asks Currents for the recent durations of your spec files and splits the tests so the shards finish at about the same time. You keep the same CI setup: the same `--shard` jobs, now with a duration-based split.

[Orchestration Setup](https://docs.currents.dev/guides/ci-optimization/playwright-orchestration.md) also balances by duration, but it assigns tests while the run goes, so it also handles slow runner start-up and lost machines. Balanced sharding fixes each shard's tests before the shard starts, like Playwright's split.

In a replay of 900+ real Playwright sharded runs, balanced sharding shortened the median run by about 15%, and most teams would have seen 10% or more. Results depend on how uneven your suite is: suites with a few long spec files gain the most. These are estimates from replaying recorded runs with `fullyParallel` set, not measurements of balanced runs. Not every run in the replay was faster. Balanced sharding shortens test time only: CI queueing and runner start-up don't change.

{% hint style="info" %}
Balanced sharding is off unless you turn it on. It isn't available on self-hosted Currents: an opted-in build splits the tests the way Playwright does and says that balanced plans are turned off for this organization.
{% endhint %}

## Requirements

* `@playwright/test` 1.62 or later. On an older version an opted-in build warns and splits the tests the way Playwright does.
* `@currents/playwright` 2.10.0 or later. Use the latest version.
* A sharded run, `--shard=i/N` with N above 1, started with `npx playwright test` or `npx pwc`. Runs started with `pwc-p` use [Orchestration Setup](https://docs.currents.dev/guides/ci-optimization/playwright-orchestration.md) instead, which balances on its own.
* The same [CI Build ID](https://docs.currents.dev/guides/parallelization-guide/ci-build-id.md) on every shard, either the one Currents detects from your CI or an explicit one. Without one, each shard generates its own ID and becomes a separate run.
* History. The split uses the last 7 days of runs of the same Currents project. Until at least one spec file has 3 recorded runs, every shard splits the way Playwright does. Those runs record durations, so balancing starts on a later run without any change.

## Turn it on

Set `balancedSharding: true` in `currents.config.ts`:

```typescript
// currents.config.ts
import { CurrentsConfig } from "@currents/playwright";

const config: CurrentsConfig = {
  recordKey: process.env.CURRENTS_RECORD_KEY,
  projectId: process.env.CURRENTS_PROJECT_ID,
  balancedSharding: true,
};

export default config;
```

You can also set `CURRENTS_BALANCED_SHARDING=true`, pass `--pwc-balanced-sharding true` to `pwc`, or set it in the reporter options in `playwright.config.ts`. The environment variable wins, then the flag, then the reporter options, then `currents.config.ts`. Only `true` and `false` count: any other value prints a warning and counts as unset, which is off. See [Configuration](https://docs.currents.dev/resources/reporters/currents-playwright/configuration.md#balancedsharding).

{% hint style="warning" %}
Every shard of a build needs the same value. A shard with balancing on and a shard with it off split differently, so some tests run twice and others never. `currents.config.ts` is the safest place for it, since every shard reads the same file.
{% endhint %}

To turn it off, set `balancedSharding: false` or `CURRENTS_BALANCED_SHARDING=false` on every shard.

The `CURRENTS_SUITE_HOOK` setting must be unset: with `false` or `capture`, an opted-in build warns and splits the tests the way Playwright does.

## How it works

1. Each shard creates the run in Currents and receives the durations of the run's spec files: the median of the recent samples for each Playwright project and spec file.
2. Each shard computes the same plan from the same durations and the same test list, and runs only its own part of it. Tests that Playwright keeps in one worker, such as the tests of a file that isn't [Fully Parallel Mode](https://docs.currents.dev/guides/ci-optimization/fully-parallel-mode.md) or a `describe.serial` group, stay on one shard.
3. The run records the new durations for the next plan.

Every shard of a build gets the same durations, so all shards agree on the plan without talking to each other. A new spec file gets an estimate: its duration in another Playwright project if it has one, otherwise the project's median test duration.

## What a shard prints

When a shard is planned:

```
Balanced sharding: shard 1/4: 25 tests planned, about 29s (plan 3cacceecf32d)
```

Every shard of a build shows the same plan ID. With `--last-failed`, the line adds that the shard re-runs only the failed tests among them.

When the shards split the tests the way Playwright does, the line says why:

```
Balanced sharding: splitting the tests the way Playwright does, because <reason>
```

| Reason                                                              | What to do                                                                                                      |
| ------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------- |
| this project's spec files don't have enough history yet             | Nothing. Balancing starts once the spec files have enough recorded runs.                                        |
| balanced plans are turned off for this organization                 | Contact [support](mailto:support@currents.dev) if you didn't expect this.                                       |
| each shard generated its own CI build ID, so they are separate runs | Give every shard the same [CI Build ID](https://docs.currents.dev/guides/parallelization-guide/ci-build-id.md). |
| the attempt this re-run repeats split the tests this way            | Nothing. A re-run keeps the split of the attempt before it.                                                     |
| another reporter already decides how the tests are split            | Remove the other reporter that changes Playwright's sharding, or turn balanced sharding off.                    |

When a shard prints `Balanced sharding planned no tests for this shard, so it ran none`, the plan put all the tests on other shards, for example when there are more shards than spec files.

### When a shard stops

A shard that can't follow the plan stops with an error instead of splitting the tests the way Playwright does. It can't tell how the other shards split, and a Playwright split next to a balanced one would run some tests twice and others never. The error says how to turn balancing off.

| Error                                                                          | What to do                                                                                                                                                                                                                                                                                          |
| ------------------------------------------------------------------------------ | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| Balanced sharding could not get durations from Currents within 120 s           | Re-run the shard. `CURRENTS_BALANCED_SHARDING_TIMEOUT_MS` sets the wait.                                                                                                                                                                                                                            |
| Currents can't link this re-run to its original shard plan                     | The attempt before this one recorded several runs, and the CI build ID doesn't say which is this job's. Include `${{ github.run_attempt }}` in the CI build ID, set `CURRENTS_PREVIOUS_CI_BUILD_ID` to the earlier attempt's CI build ID, or start a new build. See [#re-runs](#re-runs "mention"). |
| Currents can no longer compute the plan the previous attempt of this build ran | Use **Re-run all jobs**: the new attempt plans afresh.                                                                                                                                                                                                                                              |
| This CI build ID names a run that has expired                                  | Start a new build, or give this one a new CI build ID.                                                                                                                                                                                                                                              |
| Balanced sharding planned shard N differently from what Playwright would run   | A reporter bug. Contact [support](mailto:support@currents.dev) with the run URL.                                                                                                                                                                                                                    |

### Time limit

A shard waits up to 2 minutes for durations from Currents. Each request waits at most 30 seconds, and the shard prints a line before each retry:

```
Balanced sharding: the durations request to Currents failed (no answer within 30 s), retrying in 5 s (85 s left of 120 s)
```

After the time limit, the shard stops. So if Currents is unreachable, an opted-in build fails instead of running unrecorded with a different split on each shard. Set `CURRENTS_BALANCED_SHARDING=false` to run it with Playwright's split. `CURRENTS_BALANCED_SHARDING_TIMEOUT_MS` changes the time limit, in milliseconds.

## Re-runs

A re-run attempt keeps the split of the attempt before it, so each shard gets the same tests it had the first time and a failed test runs again on its own shard. To keep the split, Currents links the attempt to the one before it:

* On GitHub Actions, with any CI build ID. Currents links the re-run to the latest earlier attempt of the same workflow run that recorded a run in the project. When that attempt recorded several runs, for example one per job, include `${{ github.run_attempt }}` in the CI build ID so Currents can tell which run is this job's.
* On GitLab CI 19.3 or later, with a CI build ID that ends in `$CI_PIPELINE_ID-$CI_JOB_RETRY_COUNT`, such as `reporter-$CI_PIPELINE_ID-$CI_JOB_RETRY_COUNT`. Requires `@currents/playwright` 2.13.0 or later.
* On other CI providers, set `CURRENTS_PREVIOUS_CI_BUILD_ID` on the re-run to the CI build ID of the attempt before it.
* On a CI that keeps the build ID when it re-runs a job, the re-run joins the same run and gets the same split, if the run started less than 12 hours before.

Which re-run to use:

* **Re-run failed jobs** is safe when the attempt is linked. Without the link, a fresh split could move a failed test to a shard that passed, and that test would never run again. On GitHub Actions, the reporter stops an unlinked shard and says how to fix it. On other CI providers, link the attempt as described above.
* **Re-run all jobs** always works: every shard runs a fresh split of the whole suite.

With [Sharded runs](https://docs.currents.dev/guides/ci-optimization/re-run-only-failed-tests/re-run-only-failed-tests-sharded.md), a shard that restores its last run re-runs its own failed tests with `--shard=1/1`, whatever the split. A shard that runs its share again with `--shard=i/N`, after a cache miss or a run that failed before any test, needs the link.

Use these versions for re-runs:

* `@currents/playwright` 2.11.0 or later, or 2.13.0 or later for a GitLab CI build ID from `CI_JOB_RETRY_COUNT`
* `currents-dev/playwright-last-failed@v2`, which is v2.3.0 or later
* `@currents/cmd` 3.3.0 or later


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.currents.dev/guides/parallelization-guide/balanced-sharding.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
