Bookmarks

Monorepo

A repository that holds the source code of several projects belonging to one organisation and builds them under shared rules.

Also: monorepo, single repository, mono-repo

checked 9 Nov

All terms

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

AspectWhat it means in practice
Atomic changeschanging 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 publishinga library is used straight from a folder in the repository: no need to publish package versions and wait for consumers to upgrade
One configurationstyle, check and build rules are declared once and apply everywhere, instead of differing from repository to repository
One set of dependenciesevery project uses the same version of each third-party library, so outdated ones are noticed at once
Cost of scalehistory and index grow to cover the whole organisation: Google built its own version control system for its repository
Access rightsby 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.

jsonThe root package.json with a workspaces field declares the repository’s packages
{  "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

Verified 9 Nov

Not to be confused with

what it is mixed up with and how it differs

  • monolith

    A monolith is a single application built and deployed as a whole; a monorepo is a single repository whose packages are built and released independently.

  • microservices

    Microservices are about deploying services and connecting them over the network; a monorepo is about where the code lives. Microservices can live happily in one repository.

In depth in the references

where the topic is covered in detail