Sub topic 11
Controlling parallelism with max-parallel and concurrency
This chapter assumes you can read a workflow file, know that jobs run in parallel unless needs says otherwise, and have used a matrix strategy. Two settings limit parallelism in different places. max-parallel limits the legs of one matrix. concurrency limits how many runs of a workflow, or how many runs of a job, can be active for the same group.
Limiting a matrix with max-parallel
A matrix creates one job per combination of values. By default, GitHub starts as many of them as the runners available to your account allow. The max-parallel key under strategy sets an upper limit on how many legs of that matrix run at the same time.
jobs:
test:
runs-on: ${{ matrix.os }}
strategy:
max-parallel: 2
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
steps:
- run: echo "Testing on ${{ matrix.os }}"
release:
needs: test
runs-on: ubuntu-latest
steps:
- run: echo "All matrix legs passed"The matrix has three legs, but only two run at once. The third starts when one of the first two finishes. The release job waits for every leg of test through needs, so max-parallel changes how long the matrix takes but not when release starts.
Common reasons to set it:
- A test suite uses a shared external resource, such as a staging database, that cannot handle many clients at once.
- You want to stay under an API rate limit.
- You want to save runner minutes by letting an early failure show up before every leg has started.
max-parallel applies only to one matrix in one job. It has no effect on other jobs in the workflow.
Limiting runs with concurrency
The concurrency key works at a different level. You give it a group name. GitHub allows only one running job or workflow with that group name at a time. Any other one with the same group waits as "pending".
name: Deploy
on:
push:
branches: [main]
concurrency:
group: deploy-production
cancel-in-progress: false
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- run: echo "Deploying"If two pushes to main arrive close together, the second run waits until the first finishes. Placed at the top level, as here, concurrency applies to the whole workflow run. Placed inside a job, it applies only to that job, so other jobs in the same workflow keep running in parallel.
Cancelling older runs
Setting cancel-in-progress: true cancels the run already in progress when a new one joins the group. This suits checks on a branch, where only the newest commit matters.
concurrency:
group: ci-${{ github.ref }}
cancel-in-progress: trueThe group name here includes github.ref, so each branch has its own group. A new push to feature-a cancels an older run on feature-a and leaves runs on feature-b alone.
Choosing between them
| Setting | Scope | Controls | Typical use |
|---|---|---|---|
| `max-parallel` | One matrix in one job | Legs running at once | Protect a shared resource |
| `concurrency` | Workflow or job | Runs sharing a group name | Serialize deploys, cancel stale runs |
Common errors
- Cancelling a deployment halfway. Do not use
cancel-in-progress: truefor deploy jobs. Stopping a deploy partway can leave an environment in a broken state. Usefalseso runs queue instead. - Losing queued runs. A group holds at most one running and one pending run. If a third run arrives, the older pending run is cancelled, even with
cancel-in-progress: false. Do not rely on every push getting its own run. - Group names that are too broad. A fixed name such as
cimakes every branch and pull request compete. Includegithub.refor a similar value. - Expecting
max-parallelto order jobs. It sets no order among legs. Useneedswhen one job must run after another.
Exercise
A workflow tests on six Node versions with a matrix, and the tests share one database that fails if more than three clients connect at once. Which setting do you add, where, and with what value?
Show answer
Add max-parallel: 3 under strategy in the test job, next to matrix. Six legs are created, but at most three run at a time. concurrency is not the right tool, because it limits runs that share a group name, not the legs of one matrix.
Exercise
Why is cancel-in-progress: true a poor choice for a production deploy workflow, and what should you use instead?
Show answer
Cancelling a running deploy can stop it partway and leave the environment in a half-updated state. Use a fixed concurrency group with cancel-in-progress: false, so a new deploy waits for the current one to finish.