A monorepo keeps an organisation’s applications, libraries and infrastructure configuration side by side, with a common change history and common build rules. Google stores its code this way; in the JavaScript ecosystem the tools are Nx, Turborepo and package manager workspaces, and in other languages Bazel and Cargo and Go workspaces.
Benefits and costs
| Aspect | What it means in practice |
|---|---|
| Atomic changes | changing a server interface and updating all its clients land in one commit — no chain of pull requests across several repositories and no window in which consumers are broken |
| Shared code without publishing | a library is used straight from a folder in the repository: no need to publish package versions and wait for consumers to upgrade |
| One configuration | style, check and build rules are declared once and apply everywhere, instead of differing from repository to repository |
| One set of dependencies | every project uses the same version of each third-party library, so outdated ones are noticed at once |
| Cost of scale | history and index grow to cover the whole organisation: Google built its own version control system for its repository |
| Access rights | by default everyone with access to the repository can see all the code; private projects are kept separately |
How work in a monorepo is organised
Projects inside the repository are called packages or workspaces: each has its own folder, its own manifest with dependencies and its own tests. The build rests on two rules. First, links between packages are declared explicitly, and tools use them to work out what a given change affects and what it does not, so checks run only for affected packages. Second, external dependencies are pinned to versions and downloaded through a shared cache, so that all projects and pipelines build the same way.
{ "name": "my-app", "private": true, "workspaces": ["packages/*", "apps/*"]}Moving code into one repository is not enough: without tools that understand the links between packages, the pipeline will run every test on every change, and the boundaries between teams will blur. That is why package manager workspaces, which merely link local packages at install time, are usually paired with a build system — Nx, Turborepo, Bazel or Rush — that builds a task graph, caches results and finds the packages a change affects.
When to choose a monorepo
- several applications and libraries live in one organisation and often change together;
- teams reuse shared code and want the same tools, styles and standards;
- refactoring across all the code at once is cheaper than going through dozens of repositories and package versions;
- a shared pipeline and shared checks matter more than the independence of individual teams.
A monorepo is a strategy for storing code, not a matter of size: the benefits of a shared repository apply just as well to five projects owned by a small team. The line is drawn not by the number of lines of code but by how closely projects are tied by joint changes and common standards. In 2016 Google described its repository, shared by tens of thousands of developers, in an article in Communications of the ACM.
Sources
the term is written from these pages- Rachel Potvin, Josh Levenberg. Why Google Stores Billions of Lines of Code in a Single Repository (CACM, 2016)research.google/pubs/why-google-stores-billions-of-lines-of-code-in-a-single-repository/(opens in a new tab)
- Nx — What is a monorepo?nx.dev/docs/kb/what-is-a-monorepo(opens in a new tab)
- Turborepo — Docsturborepo.dev/docs(opens in a new tab)
Verified 9 Nov