# A Beginner's Guide to Monorepos
'**Monorepo**' has been known as one of those buzzwords you say in a technical gathering that makes you look smart because it sounds so complicated. But is it? In this article, we would explore the concept of monorepo which has become increasingly popular in the software development industry. We'll explain its features, advantages, the potential challenges you may face and the tools around it. By the end of this article, you will have a clear understanding of what a monorepo is and how it can be beneficial to your development projects
## What is a Monorepo?
A monorepo is a single code repository containing many projects. These projects are usually distinct and have a well-defined relationship. Companies use this technology to have all of their products in one place. A good example is Google which is said to have the largest repository in the world having more than 2 billion lines of code. They make use of the monorepo paradigm to organize their code structure
### Difference Between Monorepos and Multi-repos

`Multi-repos` are the opposite of monorepos. The major difference between them is, monorepos consists of only one repository with multiple projects (Think of it as the entire codebase live in one big folder) while multirepos consists of different projects with each having their own repository
## Features Of Monorepos
Monorepos have both positive and negative impacts. Uber, one of the software giants, went from [monorepo to multi-repos and back again](https://www.youtube.com/watch?v=lV8-1S28ycM). This highlights the fact that the decision to use monorepos should be well thought out before being made.
### Advantages Of Monorepos
- **Consistency**: In monorepos, you can ensure code styling and quality across all projects
- **More Efficient Build and Test Processes**: Development teams can save time and resources by centralizing the build and test procedures for all packages in a monorepo. This prevents redundant builds and tests.
- **Improved Collaboration**: It is easier to share code, and track changes when the entire codebase lives in a single repository
- **Improved Dependency Management**: Managing dependency packages, and libraries is easier and with less risk of conflict because they all live in a single codebase
- **Atomic commits**: Packages can be easily updated across all projects in a single commit.
### Challenges Of Monorepos
- **Complexity**: Monorepos to have increasingly complex and difficult to set up especially as the packages/libraries get larger
- **Builds and Testing**: Build and testing processes would get slower due to the increasing codebase.
- **Learning Curve**: New developers joining the company or project would take some time to learn the entirety of the monorepo and how the components fit
- **Scaling and Team Management**: As a codebase grows, it can become more difficult to scale and manage teams working on different parts of the codebase, potentially leading to coordination and communication issues.
## Monorepo Tools
As we have seen above, monorepos can have both positive and negative impacts this is why they have tools that tend to reduce the level of challenges faced by it
Here are some features these tools possess
- **Task Orchestration**: Monorepo tools are capable of running tasks in a specified format and parallel
- **Code Sharing**: They help facilitate sharing distinct pieces of code between team members
- **Dependency Constraints and Visibility**: Monorepo tools can define rules to constrain dependency relationships within the repository, such as marking private projects only visible to specific teams
- **Caching**: They can reduce build time by caching files. This would ensure that if a file remains unchanged, the subsequent build would use the instance on the cache rather the repeat the process again
Example Of Monorepo Tools
1. [Lerna](https://lerna.js.org/): Lerna is a tool for managing JavaScript/TypeScipt projects with multiple packages. Lerna manages monorepos, which can hold projects containing multiple packages within itself. It has features such as distributed caching, caching task results which prevent executing the same commands twice, graph visualizer, etc.
2. [Nx](https://nx.dev/): Nx is a popular set of extensible developer tools that provides a high-level toolkit for building monorepo applications. It also helps for easy integration with popular frameworks and tools like Cypress, Jest, Typescript, React, etc. Nx offers a wide range of features for code generation, dependency management, testing and distributed caching. Find more about Nx by taking a look at their [documentation](https://nx.dev/)
3. [Turborepo](https://turbo.build/repo/docs): Turborepo, like lerna, is a build system for JavaScript/TypeScript codebases. It could be added to any existing JavaScript/TypeScript project or incrementally added to an already existing monorepo. Turborepo was built by vercel (also the creators of [Nextjs](https://nextjs.org/)). Builds in turborepo projects execute blazingly fast thanks to its remote caching. A system where the cache lives on a remote server and more than one developer has access to it rather than running the same builds on their local machine
4. [Rush](https://rushjs.io/): Rush, built by microsoft, makes it easier for Javascript developers to organise and build multiple packages from a common git repo. It is compatible with npm, Yarn and pnpm build projects. Its unique features include easy adoption for new team members as it provides mechanisms for onboardiYHYsince its creation has built a reputation of being extremely fast compared to npm hence the name `Performant NPM`. It supports monorepos out of the box by holding packages and dependencies in a centralized store and using them in different programs on demand
5. pnpm: Short for `Performant NPM` is an open-source tool which has been developed to give more efficiency and better performance to other package managers (npm and yarn). They have built-in support for monorepo by use of a `workspace:` protocol
Other monorepo tools include
[npm workspaces](https://docs.npmjs.com/cli/v7/using-npm/workspaces), [yarn workspaces](https://classic.yarnpkg.com/lang/en/docs/workspaces/), [bit](https://bit.dev/), [lage(by Microsoft)](https://microsoft.github.io/lage/), [Gradle](https://gradle.org/) and Blaze (by Google)
## Real-world use cases for Monorepos
The monorepo technology has been adopted by some of the biggest companies in the world here are a few of them
1. Google: Google, is said to have the biggest codebase in the world. [This research](https://dl.acm.org/doi/pdf/10.1145/2854146) outlines that they have up to 2 billion lines of code in nine million unique files. They make use of an internal tool called **Blaze** to organize .
2. Facebook: Also one of the biggest companies in the world, makes use of a monorepo tool called [Buck](https://buck.build/) to handle their multi-product link. They have noted that monorepo technology helps them to manage dependencies between code modules making it easier to maintain consistency across the codebase
3. Microsoft: Microsoft uses Rush to orchestrate monorepos with hundreds of projects. They also developed another tool called [Lage](https://microsoft.github.io/lage/) which has been optimized for multi-core machines.
4. Uber: Going back of forth between monorepo and multirepo, uber adopted the use of [Bazel](https://bazel.build/) to manage their multiple products and applications. Bazel is a subset of Google's Blaze tool which was released as open-source software in 2015
With the tools becoming more efficient and accessible, many companies are starting to use monorepos more than before.
### Should You Use It?
In this article, we have learnt about monorepo technology, its features and the tools around it.
Deciding to use monorepo or not largely depends on the organizational needs, work culture and how you want the team to communicate. Monorepos are more effective in an open and collaborative culture rather than those with privacy between groups.
Some companies decide to use them for a while and switch to multirepos when necessary. Others might choose to adopt both at the same time (Hybrid system).
Consider using it if you have a large codebase and multiple independent packages or when you have a team of developers and want to enforce the same code style/rules across the entire company. On the other hand, consider using multi-repos in your company if you want to give each team full responsibility for their applications and be independent of other teams.
## Resources
- [**Awesome Monorepos**](https://github.com/korfuri/awesome-monorepo): A github repository containing a list monorepo tools, software and architectures
- [**Monorepo Tools**](https://monorepo.tools/): More features of monorepo tools