How to break up a rails monolith
Tags: architecture, rails, ddd, coupling
Published: 2023-01-20 18:17:39

---
title: How to break up a rails monolith
published: true
description: Steps to migrate from a monolith to a modular monolith
tags: architecture, rails, ddd, coupling
# cover_image: https://direct_url_to_image.jpg
# Use a ratio of 100:42 for best results.
# published_at: 2023-01-20 17:36 +0000
---

My last couple of jobs have both been with companies that were struggling with complex rails applications. There was nothing fundamentally wrong with the technology choices, the software had just organically become difficult to work with.

In this kind of an environment, engineers are unhappy with the technical debt, product are unhappy with the slow delivery. But it's not easy to fix.

There is a temptation to start over with a [big rewrite](https://softwareengineering.stackexchange.com/questions/6268/when-is-a-big-rewrite-the-answer), but rewrites take ages, and divert resources away from improving the user experience.

These are some notes I've gathered about improving the structure of these rails applications without rebuilding the system from scratch or spinning up new infrastructure.

## Context
- your rails app is turning into a [big ball of mud](http://www.laputan.org/mud/)
- you need to think about *everything* to change *anything*
- it's hard to understand what the intended structure is supposed to be.

## Goals
- reduce cognitive load on engineers
- enable teams to work autonomously and take ownership of components
- this might be a preparation step for rebuilding functionality or extracting separate services

## Enter the modular monolith
[Modular monoliths](http://www.kamilgrzybek.com/design/modular-monolith-primer/) are applications that are deployed as one service but whose code is split into [loosely coupled](https://en.wikipedia.org/wiki/Loose_coupling) components. This allows you to reduce levels of coupling at coding time, without having to change runtime behaviour, infrastructure etc.

## Domain driven design helps establish boundaries
You can start introducing new structure by [modelling the different business contexts and reflecting that in your code](https://martinfowler.com/bliki/BoundedContext.html). Each context comes with its own special terminology. If you get together with domain experts, you can agree shared terms (a.k.a a [ubiquitous language](https://martinfowler.com/bliki/UbiquitousLanguage.html)) and a shared model that both the software engineers and the domain experts understand. You can then use this to decide where to split the application.

## Decide where to start
If you are starting from a monolith, then you can gradually evolve the system towards a modular monolith by
1. organising code into logical components or packages
2. strengthening boundaries and decoupling the components from each other

There are different ways of going about this.

### Option 1: Depth first
One way is to pick out one or two candidate components to focus on, follow the steps below to decouple them from the rest of the application, and then continue component-by-component. 

The advantage of this option is it doesn't require a huge up-front commitment, and you can earn buy-in from stakeholders over time. A small number of people can work on it and then present the results to the rest of the engineering team. You then have good examples for other engineers and teams to follow.

### Option 2: Breadth first
An alternative is to do a very rough pass over the whole codebase, that introduces the desired structure, but accepts that the interfaces are poor and components are still tightly coupled together. You then refine the components.

The advantage of this approach is that engineers can start to benefit straight away from code being easier to navigate. But there's a risk that you never finish the work. You can mitigate this by implementing tooling to inform engineers of issues that need to be corrected, and showing them why it is important.

## Steps to introduce a component
1. Move the existing code, including models
2. Establish a public API for the component
3. Make sure all calls to the component use the public API
4. Introduce an "anti-corruption layer"
	1. Introduce value objects for the component
	2. The anti-corruption layer transforms from ActiveRecord models into value objects of the component
	3. The public API can now be changed to accept and return value objects, not ActiveRecord models
	4. [Prevent outside code from instantiating component models directly](https://kellysutton.com/2019/10/29/taming-large-rails-codebases-with-private-activerecord-models.html)
5. Modify calling code so you no longer need the anti-corruption layer (by using the modified, ActiveRecord-free API directly)
5. Remove associations between component models and outside models
	- If you depend on data outside of the component, then access it through a method call that returns a value object (avoid ActiveRecord)
	- Remove foreign keys that cross the component boundary
	- Prefer polymorphic associations for linkages to things outside of the component. This inverts the dependency
	- (optional) This isolation can be enforced by [migrating data to a separate schema](https://learnitnow.medium.com/working-with-multi-schema-database-in-rails-7e60aef8ff86)
6. Remove cyclical dependencies between components ([Acyclic dependencies principle](https://en.wikipedia.org/wiki/Acyclic_dependencies_principle))

## Useful tools
- https://github.com/Shopify/packwerk allows you to make dependencies between components explicit
- [Rails engines](https://guides.rubyonrails.org/engines.html) are another way of organising a rails app into components

## Additional resources on this topic
- [RubyHack 2019: Taming Monoliths Without Microservices by Kelly Sutton]
(https://www.youtube.com/watch?v=-ovkkvvTiRM&list=PL9_jjLrTYxc3dTbvb8fIuzDFGTCaEdO3a&index=8)
- [The Modular Monolith: Rails Architecture](https://medium.com/@dan_manges/the-modular-monolith-rails-architecture-fb1023826fc4)
- [Taming large rails applications with private ActiveRecord models](https://kellysutton.com/2019/10/29/taming-large-rails-codebases-with-private-activerecord-models.html)
- [Under deconstruction: the state of shopify's monolith](https://shopify.engineering/shopify-monolith)
- [Painless Rails without Overengineeriug](https://painlessrails.com/)
- [Growing rails applications in practice](https://pragprog.com/titles/d-kegrap/growing-rails-applications-in-practice/)
- [Sustainable web development with Ruby on Rails](https://sustainable-rails.com/)
- [Gradual modularization for Ruby and Rails](https://leanpub.com/package-based-rails-applications)
- [Migrating to microservices](https://link.springer.com/chapter/10.1007/978-3-030-31646-4_3)
- [Between monoliths and microservices](https://noti.st/palkan/VWPOSd/between-monoliths-and-microservices)