Does npm's local cache serialize or parallelize concurrent requests for identical package versions?
0 reputation · 13 Oct 2020, 16:08 UTC
0 reputation · 13 Oct 2020, 16:08 UTC
npm maintains a local package cache to avoid redundant downloads, but the documentation does not specify how the cache handles simultaneous requests for the same package version from multiple npm install or npm ci processes. In CI pipelines that run parallel jobs against a shared cache directory, or in monorepos where multiple workspaces trigger installs concurrently, the cache could become a contention point.
The resolution phase calculates the full dependency tree before any network activity. If two processes resolve identical trees and then request the same tarball simultaneously, it is unclear whether npm deduplicates the fetch, queues the second request, or allows both to proceed independently. This affects both latency and cache integrity, especially when the cache is on a network filesystem.
Current verification steps focus on lockfile adherence and node_modules structure, not on cache access patterns under load. The behavior may also differ between npm v6, v7+, and the upcoming v10 series.
34775 reputation · 13 Oct 2020, 22:37 UTC
When multiple npm install or npm ci processes request the same package version, npm serializes the download of that tarball. A per‑tarball lock file in ~/.npm/_cacache/_locks ensures only one process fetches the tarball at a time. Once the file is cached, other processes can read it concurrently without contention.
lodash@4.17.21). mkdir test-repo && cd test-repo
npm init -y
npm install lodash@4.17.21 --save-dev
npm install simultaneously:
npm install && npm install
ls ~/.npm/_cacache/_locks
Only one lock file for the lodash tarball should be present.npm --verbose install or tcpdump -i any port 443) to confirm that only one HTTP GET for the tarball occurs.npm --version. If <6, consider upgrading.nolock or using a local cache per job.Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.