edbzn.dev

Understanding Nx's Affected Graph vs. Cache Hits

November 09, 2025

Nx provides powerful mechanisms to avoid redundant work in a monorepo. Two core concepts often mentioned are the affected graph and cache hits. While both improve efficiency, they address different stages of the build/test process.

This post will clarify what each concept means, how they work, and how they differ.

Note 📌

Thanks to Antoine Pairet for suggesting this blog post topic!

Nx Affected Graph: running only what’s changed

One of Nx’s most impactful features is the affected graph, which allows you to run tasks only on projects impacted by recent changes.

Tip 💡
In simple terms, the affected graph helps us to skip unnecessary work and focus only on the things that have been changed.

How is the affected graph computed?

When you run an affected command (e.g. nx affected -t build), Nx will:

  1. Finds changed files using Git.
  2. Maps files to projects using Nx’s project graph.
  3. Includes dependent projects that rely on the changed projects.

You can visualize this with:

nx graph --affected

Diagram of the Affected Graph concept

Scenario: You modified files in project-a and run nx affected -t build. The diagram below shows how Nx identifies which projects need to be built based on the changes and their dependencies.

nx affected -t build

Find Changed Files via Git

Changed: project-a files

Analyze Project Graph

project-a: Changed

project-b: Depends on A

project-c: Depends on A

project-d: Not affected

✓ Build project-a

✓ Build project-b

✓ Build project-c

✗ Skip project-d

Note 📌
Projects B and C are affected because they depend on A, which was changed.

Nx Cache Computation: reusing previous results

Nx also ensures that when a task is required, you don’t redo work unnecessarily. This is thanks to computation caching.

How does caching work?

Before executing any cacheable task, Nx computes a hash based on the task’s inputs, including:

  • Source files of the project and its dependencies
  • Configuration
  • Runtime environment
  • Command flags

This hash uniquely identifies the state of the project for that task. Nx then checks if it has seen this exact computation before:

  • If a matching hash exists in the cache, Nx will skip executing the task and instead retrieve the previously saved results. It pulls from the cache the task’s outputs (e.g. build artifacts) and even replays the terminal logs.
  • If no cached result is found, Nx will run the task as normal. Once the task completes, Nx stores the output files and terminal output in the cache for future use.
Note 📌

By default, Nx caches task results locally on your machine. For greater benefit in team environments and CI pipelines, Nx can use remote caching so that if one developer or CI job has already built a project, others can get a cache hit and skip rebuilding it.

Diagram of caching concept

Scenario: You run nx build project-a. The diagram below illustrates how Nx checks if this exact build has been done before by computing a hash and looking it up in the cache.

Yes

No

nx build project-a

Collect Task Inputs

Compute Hash

Hash in Cache?

Cache Hit: Replay Cached Output

Cache Miss: Run Task

Store Output in Cache

Tip 💡

Cache ensures repeated work isn’t done if inputs haven’t changed.

Key differences between Affected Graph and Cache Hits

FeatureAffected GraphCache Hit
PurposeDetermines what needs to runSkips task execution if identical work was done
When UsedBefore deciding what tasks to runRight before executing a task
Based OnGit diffs and project dependenciesHash of task inputs and configuration
BenefitLimits work to only impacted projectsReuses previous task results to save time

Conclusion

The affected graph answers “What needs to run?” while cache hits answer “Has this work already been done?”. Combining both ensures that you:

  • Skip unaffected projects entirely.
  • Skip execution for tasks with unchanged inputs.

This is why Nx is effective in managing performance and scalability in monorepos.

Resources

Edouard Bozon

I’m Edouard Bozon, a software engineer focusing on web development and platform engineering. I work extensively with Angular, Node.js, and Nx to build scalable applications. As an open source contributor, I love collaborating with people and enjoy solving problems with clean solutions.

Bluesky profileX profileGithub profileLinkedin profile
Nx Badge 2025

Interested in collaborating?

I provide web development and platform engineering services to help you build scalable applications.

Let's discuss