Hoang Nguyen

Monorepo is a new trend emerging recently in many open source projects

December 30, 2018 • ☕️ 4 min read

There is no doubt that microservices has dictated the current landscape in the web development world for the last 2 years. And a new trend emerging recently when many open source projects or startups moved from multiple repos to monorepo

Monorepo is not new, Google and Facebook have been using it for years. So why now? I belive it depends a lot on the muturity of building systems

Thanks to awesome work at Lerna and Git workspaces, many big javascript based projects have adopted monorepo flawlessly. I personally also refactored my project to monorepo and I can tell you that has been one of the decisions I was so sastified

What is an monorepo?

  • The repository contains more than one logical project (e.g. an iOS client and a web-application)
  • These projects are most likely unrelated, loosely connected or can be connected by other means (e.g via dependency management tools)
  • The repository is large in many ways:

    • Number of commits
    • Number of branches and/or tags
    • Number of files tracked
    • Size of content tracked (as measured by looking at the .git directory of the repository)

Advantages

  • Unified versioning, one source of truth;
  • Extensive code sharing and reuse;
  • Simplified dependency management;
  • Atomic changes;
  • Large-scale refactoring;
  • Collaboration across teams;
  • Flexible team boundaries and code ownership; and
  • Code visibility and clear tree structure providing implicit team namespacing.

Costs and trade-offs

  • Tooling investments for both development and execution;
  • Codebase complexity, including unnecessary dependencies and difficulties with code discovery; and
  • Effort invested in code health.

Enormous monorepos

  • Facebook: With thousands of commits a week across hundreds of thousands of files, Facebook’s main source repository is enormous—many times larger than even the Linux kernel. Read more about Buck and Scaling Mercurial at Facebook

  • Google: Early Google employees decided to work with a shared codebase managed through a centralized source control system. This approach has served Google well for more than 16 years, and today the vast majority of Google’s software assets continues to be stored in a single, shared repository. They use Bazel as build system

  • Twitter is using a custom build of Git which includes patches enhancing it to better deal with very large repositories in the range of multiple gigabytes

Lerna based monorepos

  • babel : compiler for writing next generation JavaScript
  • create-react-app : create React apps with no build configuration
  • react-router : Declarative routing for React
  • jest : Delightful JavaScript Testing
  • storybook : Interactive UI component dev & test
  • primer : the design system that powers GitHub
  • reactotron : A desktop app for inspecting your React JS and React Native

Build systems

  • Bazel is Google’s monorepo-oriented build system. More on Bazel: awesome-bazel
  • Buck is Facebook’s monorepo-oriented build system.
  • Pants is a monorepo-oriented build system, used by Twitter, Foursquare and multiple other companies.
  • Yarn is a JavaScript dependency management tool that supports monorepos through workspaces.
  • Lerna is a tool for managing JavaScript projects with multiple packages, built on Yarn.
  • OAO is a Yarn-based, opinionated JavaScript monorepo management tool.
  • MBT is a build tool with differential build support.
  • Symplify\Monorepo is a PHP monorepo management tool.

Conslusion

Keep in mind monolithic source control does not necessarily result in monolithic software. And microservices can play well with monorepo

There are two basic types of Monorepos are huge repositories containing all the code maintained by a company or project specific Monorepos like Babel, React

Now you have a good overview of monorepo. I’ll share about how to setup a monorepo, how it played nicely with CICD and how it affect developer experience later in another posts.

I recommend developers to give monorepo a try in coming year 2019 :D

References


Hoang Nguyen

Senior Software Engineer
iOS, React Native