Distribute Your Cache with Nx Cloud

Share this video with your friends

Send Tweet
Published 5 months ago
Updated a month ago

By default Nx stores the cache locally. We can gain additional benefits if we distribute our cache so a CI or our coworkers may benefit from it.

The easiest way to set it up is to just run this command pnpx nx connect-to-nx-cloud

Instructor: [0:00] If we enable caching in Nx with the setting off the default runner here as well as specifying the cache block operations for our workspace, we enable basically a local caching. Meaning that all of the cache output is stored in the node modules folder by default in the .cache/nx folder. [0:19] That said, you could also customize the cache location by specifying the cache directory and giving it a path or by using the environment variable Nx cache directory. While this speeds up the local computation, obviously, we get a lot more benefits if we distribute that cache to a remote location, such that also our CI system or our co-workers can benefit from it.

[0:42] We can do this by changing this runner here to the Nx cloud runner rather than the local default one. Let's go to nx.app and there's the docks which allow us to understand how to set up distributed caching. The easiest way to do that to an existing workspace is by running connect-to-nx-cloud.

[1:09] This installs the Nx Cloud package into our devDependencies at the root level here. It also adjusts the runner to point to it now on Nx Cloud, and enters here an access token to be able to access the remote cache API.

[1:25] You also get a link where you can claim the workspace setup. This basically gives you much more insights and statistics on Nx Cloud and also allows you to do things such as integrating it into GitHub URLs.

[1:37] To test this out, we can now run the build, for instance, of all our projects in the workspace. This will now populate the cache. You can also see that there is an Nx Cloud run Details page. If we open it up, we basically get some information about what has been executed, what type of builds have been run, and also some console output here.

[2:00] If we rerun the same build, it would get pulled out of the local cache. If we remove the local cache folder, which is the .cache folder in the node_modules, and we rerun again the computation, it would still be cached, but this time it would be fetched from the remote location on Nx Cloud.