Monorepos: Making Git Blame a Family Affair
Tags: softwaredevelopment, architecture, learning
Published: 2024-06-12 06:13:09

Monorepos have gained a lot of popularity recently, especially among web developers. There are many resources that cover this topic, so I will briefly cover What is a monorepo and the benefits it provides. At the end I will cite the resources I used to get to this point.

## What is a monorepo?
> *A monorepo is a single repository containing multiple distinct projects, with well-defined relationships. - [Monorepo.tools](https://monorepo.tools/#what-is-a-monorepo)*

But a monorepo is about more than just code co-location because without the well defined relationships or boundaries, a monorepo can quickly become monolithic. And a good monorepo is the opposite of monolithic ([Misconceptions about Monorepos: Monorepo != Monolith](https://blog.nrwl.io/misconceptions-about-monorepos-monorepo-monolith-df1250d4b03c))

## Why use a Monorepo?
The opposite of a monorepo is a polyrepo or multirepo; that is a new repo for each project, i.e.. "1 repo = 1 build artifact". This approach is popular because it encourages team/developer autonomy. Developers can dictate their own tech stack, testing strategies, deployment, and (most importantly to them) who can contribute to their codebase.

Now those are all good things, but there are drawbacks to this multi-repo approach that most developers are aware of, but put to down to 'that’s just how it is'. Drawbacks like cumbersome code sharing, significant code duplication and inconsistent practices, cross-repo collaboration problems, inconsistent tooling, dependency hell when updating, automation difficulties, scalability issues, and so on.

> *"... this autonomy is provided by isolation, and isolation harms collaboration" - [Monorepo.tools](https://monorepo.tools/#polyrepo-concept)*

**So how is a monorepo any better?**
For starters, if all your related code is co-located, its much easier to manage dependency updates across all apps, the same shared libraries can be made available to all relevant apps and provide a single source of the truth. Tooling and coding standards can be enforced more consistently. Developers can work across different projects or components more seamlessly, facilitating better teamwork and knowledge sharing. Changes or refactoring that affect multiple components can be made in a single PR, ensuring that all related changes are coordinated and reducing the risk of breaking changes.

While monorepos offer these (and more) benefits, they also come with their own challenges, such as potential scalability issues with very large code bases, increased build times, the need for more sophisticated tooling to manage the codebase's complexity. However, many organizations find that the advantages of a monorepo outweigh the disadvantages, particularly in terms of improving collaboration, consistency, and overall code quality.

## Misconceptions
Misconceptions about monorepos (or any tool or technology) often deter teams from adopting this development approach. Lets shed some light on a few and expose the reality of using a monorepo.

**Monorepos should contain all a company's code**
While large companies like Google or Facebook practice this behaviour it is not the norm. Many teams will only group very similar, closely related apps that can benefit from a shared workspace.

**Monorepos mean all projects must be tightly coupled**
Monorepos can still support modularity and separation of concerns. In fact many monorepo management tools will advocate for decoupling components and maintaining an modular architecture, otherwise the monorepo is just monolithic.

**Monorepos lead to longer build times and slower CI/CD pipelines**
With proper tooling and optimization, such as incremental builds and distributed CI/CD systems, build times can be managed effectively. Tools like Nx, Bazel & Lerna utilize incremental builds to only build affected components and caching to prevent building the same thing twice.

**Monorepos are harder to scale**
While monorepos do present unique scaling challenges, they also offer solutions for consistent and scalable development practices. 

**Monorepos are harder to secure**
Security in monorepos can be effectively managed with proper practices. By only including related projects in a monorepo along with access controls, code reviews and [CODEOWNERS](https://docs.github.com/en/repositories/managing-your-repositorys-settings-and-features/customizing-your-repository/about-code-owners) can ensure that the codebase remains secure.

## The Real Challenges
Just because the many misconceptions are manageable, does not mean monorepos don't have challenges of their own, like migration complexity, tooling adjustments, and workflow changes. Proper planning and best practices are essential to navigate these hurdles and successfully transition to a unified codebase. 

1. **Migration Complexity:**
   Moving code from multiple repos into a single repo while ensuring dependencies are mapped and managed correctly can be complex, especially if projects have different histories, dependencies and configurations. 
2. **Tooling and infrastructure:**
   Not all your existing build systems, CI/CD pipelines or development tools are designed to handle large monorepos and may require significant updates or replacements.
3. **Development Workflow Changes:**
   Ensuring the effectiveness of code reviews in a larger, more integrated codebase and adapting or redefining branching strategies to suit the monorepo setup while maintaining workflow efficiency
4. **Cultural and Organizational Resistance:**
   Convincing a team that use to working independently to adopt to the new approach and workflow can be challenging. Resistance is common and training may be required while they adjust to the more integrated and collaborative development environment
5. **Consistency and Maintenance:**
   Maintaining consistent coding standards, practices and docs will need to be applied across the repo. Managing and addressing technical debt will be more critical as the monorepo grows.
6. **Learning Curve:**
   New team members may require additional training to understand the structure, tools, processes and workflows of monorepo management and development.

## Final Thoughts?
Monorepos are not a silver bullet. And there will be challenges and trade-offs and a culture shift and training and yes, it will be a lot of work. But the benefits could far outweigh those challenges; less time worrying about boilerplate and config per project leads to faster turnaround time on new features or bug fixes, easier maintenance leads to faster adaption of the newer technology trends or tech stacks, more test coverage leads to a more stable product and better code quality which in turn leads to easier adaption by new developers and easier dependency maintenance and more.

{% details Experiences with negative Monorepo research/posts %}
In my research I did find posts that describe negative experiences with monorepos. Posts that talk about how the benefits of monorepos are not so beneficial and that anyone using a monorepo should abandon all hope. However, nothing stirs up a the software community like telling them that a technology they are using is just the worst. So it’s the comment sections of these post that are more valuable than the articles, with developers denouncing the article and coming to the defence of monorepos, posting how they were able to overcome, mitigate or avoid the problems that the original author encountered.
{% enddetails %}

{% details Sources %}
 - [monorepo.tools](https://monorepo.tools/)
 - [Misconceptions about Monorepos: Monorepo != Monolith](https://blog.nrwl.io/misconceptions-about-monorepos-monorepo-monolith-df1250d4b03c)
 - [Monorepos - How the Pros Scale Huge Software Projects // Turborepo vs Nx](https://www.youtube.com/watch?v=9iU_IE6vnJ8&ab_channel=Fireship)
 - [Monorepo – How to do frontend faster, better and safer - Kari Meling Johannessen - NDC Oslo 2023](https://www.youtube.com/watch?v=_iqcMdEOrF4&ab_channel=NDCConferences)
 - [Monorepos - The Benefits, Challenges and Importance of Tooling Support by Juri Strumpflohner](https://www.youtube.com/watch?v=15VeTQLnWrs)
 - [Why You Should Use a Monorepo (and Why You Shouldn’t)](https://lembergsolutions.com/blog/why-you-should-use-monorepo-and-why-you-shouldnt)
 - [The Pros and Cons of Monorepos, Explained](https://betterprogramming.pub/the-pros-and-cons-monorepos-explained-f86c998392e1)
 - [Pros and Cons of Keeping Your Code in Monorepo](https://how-to.dev/pros-and-cons-of-keeping-your-code-in-monorepo)
 - [Guide to Monorepos for Front-end Code](https://www.toptal.com/front-end/guide-to-monorepos)
 - [Benefits and challenges of monorepo development practices](https://circleci.com/blog/monorepo-dev-practices/)
 - [What is monorepo? (and should you use it?)](https://semaphoreci.com/blog/what-is-monorepo)
{% enddetails%}