Optimizing Pipelines for Cost, Time, Performance and Reliability
This lesson works through the main optimization techniques for each goal.
Learning outcomes
After completing this lesson, you will be able to:
- Configure dependency caching.
- Distinguish caches from artifacts.
- Identify ways of reducing cost.
- Choose a technique for a symptom.
Measure first
A pipeline usually has one or two steps that dominate its duration, so effort elsewhere achieves little. Begin by finding the slowest steps from the logs or analytics, and record a baseline. Then decide which goal matters. Time, cost, performance and reliability sometimes conflict: running tests on four platforms in parallel improves time, and raises cost.
Reducing time
Caching
Caching reuses files between runs, usually downloaded dependencies. A cache key usually includes a hash of the lock file, so the cache changes only when dependencies do.
# Azure Pipelines
- task: Cache@2
inputs:
key: 'npm | "$(Agent.OS)" | package-lock.json'
restoreKeys: |
npm | "$(Agent.OS)"
path: $(Pipeline.Workspace)/.npm
- script: npm ci --cache $(Pipeline.Workspace)/.npm
# GitHub Actions
- uses: actions/cache@v4
with:
path: ~/.npm
key: ${{ runner.os }}-npm-${{ hashFiles('**/package-lock.json') }}
restore-keys: ${{ runner.os }}-npm-
- run: npm ciCache and artifacts should not be confused. A cache speeds up later runs, and losing it does no harm. An artifact is the output of a run, and it passes results between jobs. Setup actions such as actions/setup-node also have a built-in cache option. GitHub provides 10 GB of cache per repository.
Parallelism
Splitting work across jobs shortens elapsed time, and test slicing across agents is often the largest gain. It does not reduce the total compute used, and it needs parallel capacity.
Doing less
Path filters skip builds unaffected by a change, Test Impact Analysis runs only related tests, and shallow clones reduce checkout time. Docker layer caching avoids rebuilding unchanged layers, so a Dockerfile should copy dependency manifests before source code. Incremental builds on self-hosted agents can reuse earlier work.
The following diagram shows where the time goes in an 18-minute Node.js pipeline run, and the cache step that shortens it.

A cache that never hits
Reducing cost
- Avoid unnecessary runs through branch and path filters, and
batch: trueon busy branches. - Cancel superseded runs with concurrency settings when a newer commit arrives.
- Choose cheaper runners, because Linux minutes cost less than Windows, and much less than macOS.
- Reduce storage by shortening retention for artifacts and logs.
- Use self-hosted capacity for very heavy use, where it costs less than hosted minutes after including maintenance.
- Delete temporary environments created by pipelines.
Cutting pipeline cost
Improving performance
If jobs are CPU-bound, a larger machine can cost less overall, since it finishes sooner. Managed DevOps Pools and GitHub larger runners offer such sizes. A custom image with tools preinstalled removes setup time. A private package feed close to the build reduces download time, and it reduces the risk of hitting public registry limits.
Improving reliability
- Pin versions of tools and actions, as a floating version can change a build with no code change.
- Use retries for transient failures with
retryCountOnTaskFailureon a step, for known network-related steps. - Set timeouts with
timeoutInMinutesortimeout-minutes, so a hung job does not hold an agent. - Reduce external dependencies with upstream feeds and private registries.
- Fix flaky tests, which are the largest reliability problem in many pipelines.
- Keep pipelines simple, since a pipeline that nobody understands will not be maintained.
The following table recaps how a symptom points to a technique.
| Symptom | First technique to try |
|---|---|
| Dependency restore takes minutes on every run | Cache keyed on the lock file |
| Many runs for documentation changes | Path filters |
| Long test stage | Parallelize, or test slicing |
| Jobs wait in a queue | More parallel jobs or runners |
| Builds break with no code change | Pin tool and image versions |
| Bills are dominated by macOS minutes | Move what you can to Linux |
Symptom to first technique
Choosing the optimization
A Node.js pipeline runs for 18 minutes on Microsoft-hosted agents, and 11 minutes of that is npm ci downloading packages. The lock file rarely changes. What should you do first?
Add a Cache@2 step keyed on package-lock.json, because the largest cost is a repeated download of unchanged content. Buying a parallel job would not shorten a single run. A self-hosted agent could hold a cache, but it adds maintenance, and an artifact is the wrong tool, as artifacts are for run outputs.
A file is needed by a later job in the same run. Should it be a cache or an artifact?
Show answer
An artifact. Caches speed up later runs, and losing one must do no harm.
Exam Tips
Remember for the exam
- Find the slowest step before optimizing anything.
- A cache speeds up later runs; an artifact passes output between jobs.
- Key a cache on the lock file hash.
- Path filters and cancelled superseded runs reduce cost.
- Pin versions and set timeouts for reliability.