EngineeringSeries · Part 4 of 6

One repository for everything: our monorepo and its build order

Why every service, app and website lives in one repository, the order we build them in, and the checks that run across all of it

All of GrowMagik lives in one Git repository: every service, web app, phone app and website, and even this blog. This post is about why we chose one repository, the order we build it in, and the checks that run across all of it.

In the last post we looked at the cloud bill behind GrowMagik. This one is about the code that runs on it, and where that code lives.

The problem: one small change, many places

Say a jeweller asks for one small thing. When a customer's savings plan matures, show the date it matured, on the customer's app and on the staff screen.

That one line of request touches five places. The savings service on the server works out the date and sends it. The shared types that the web and the phone use to read the server's answer need the new field. The staff console shows it. The customer web app shows it. And the shared core of our phone apps shows it, for jewellers and travel agencies alike.

One small request, five places to change. A jeweller asks to show the date a savings plan matured. In five separate repositories that means five changes, five reviews and five releases in the right order, with a gap in between where the phone may ask for a field the server does not send yet. In one repository it is one change, one review and one set of checks, and every part sees the new field together

If each of those lived in its own repository, that is five changes, five reviews, and five releases that must go out in the right order. Between them, the phone could ask for a field the server does not send yet, or the server could stop sending one that a screen still reads. Somebody has to remember the order, and remembering is the part that fails.

We have many such pieces: 27 services on the server, three SDKs, more than fifteen web consoles, the phone apps, the websites and the infrastructure that runs them. And as we said in post 2, most of our code is now written with AI, which does its best work when it can see the whole change at once.

The options we weighed

There are three common ways to organise this much code. Each one is good at something, and each one comes with a risk.

Way A change across server and screens The risk we take on
A repository for every service and app One change in each repository, released in order Apps drift onto different versions of the shared SDKs, and nobody sees the whole change
A few repositories, one per layer (server, web, mobile, infrastructure) Fewer changes, but the important ones still cross repositories The server and its screens still change apart
One repository for everything (a monorepo) One change and one review Tools must know the build order, and a confused build hurts everyone at once

A monorepo is a single repository that holds many projects, along with the links between them. We chose it. But the risk in its row is real, and it is what the rest of this post is about. A monorepo pays off only when two things hold: the build has a strict order that is written down, and the checks run across the whole tree, not one folder at a time.

What we chose, and how we keep it working

1. Everything in one repository

The repository holds the server SDK and the 27 services built on it, the adapters for outside services such as payment gateways and WhatsApp providers, the web shell and its consoles, the web SDK, the native kit and the phone apps, the websites (this blog included), the Terraform and Helm setup for every environment, the API gateway's route list, the end-to-end and API tests, and the design notes. In numbers, that is close to 90 JavaScript packages, about 270 Java modules and more than 5,500 commits so far.

The most important part is how the SDKs are used. Every service and app uses them straight from the same repository. They are never published as versioned packages. So when the web SDK changes, every console and app that uses it is built and tested against the new code in the same change. Nobody has to ask which app is still on an old version of the SDK, because there is only one version.

The same goes for things that usually live somewhere else. The gateway's routes are a file next to the services, so a new route on a service and its entry on the gateway go in together. The Terraform for the event bus sits next to the list of which service listens to which event.

We change it when a part needs a different set of people to see it, such as an outside partner building on one of our SDKs, or when checking out and building the whole tree costs more than keeping the pieces in step. That part would then move out, and its SDK would be published with version numbers.

2. One repository, many releases

One repository does not mean one release. The server ships as the one deployment described in the last post. The web shell and its consoles go out as static files. The phone apps go out as store builds. Each website, this blog included, has its own deploy. A change to a website builds and deploys only that website.

Because the parts ship separately, a change to what one part sends is always made in two steps. First the server adds the new field, while the old one stays. Then the screens start reading the new field. Only after the last reader has moved does the old field go. An app that a customer has not updated keeps working through all of it.

We change it when one part keeps needing another part released at the same moment. That is a sign the line between them is drawn in the wrong place.

3. A build order that is written down

The build order. On the server, three steps always run in the same order: the server SDK, then the adapters, then the 27 services and the one deployment that carries them. On the web and phone side, eight layers build in order, from design tokens and small helpers first to the web shell and the two customer phone apps last. If a task reads what another task built, the build file says so

On the server, the build is three steps, always in the same order: the server SDK, then the adapters, then the services. Each step stores what it built in the local Maven repository (a cache of built Java libraries on the machine), and the next step reads it from there. Inside each step, Maven works out the order of the modules from what each one uses. Run the third step without the first two, and the services quietly build against whatever older copy of the SDK is sitting in that cache.

On the web and phone side, npm workspaces link the packages to each other, and Turborepo runs the tasks. Every package lists the packages it uses, and one rule covers the whole tree: build what I use before me. Our longest chain is eight steps. Design tokens and small helpers come first. Then sign-in, themes and languages. Then the API client and the shared components, then the shared store, then a typed client for each business area. Then the consoles and the shared feature packages, and after them the phone apps. Last come the web shell, which loads every console, and the two customer phone apps. Turborepo also remembers results, so a package whose inputs have not changed is not built again.

We learned to write down every link in that chain the hard way. On 20 September, a clean type check of the web code reported 45 errors across 7 packages. Every one of them was false. The apps were checking themselves against old built copies of our SDKs, made before the SDKs had changed. Once the SDKs were built first, all 61 tasks passed. A check whose answer depends on what happens to be on the disk is worse than no check at all, because you cannot trust a red or a green from it.

So type checks and lint now wait for the packages they read to be built. Two days later, lint crashed while reading a file that an SDK build had not finished writing: the build reports "done" a few seconds before it writes its type files. And the web shell reads files that its own build writes, so its type check now waits for its own build too. The rule behind all three fixes fits in one line: if a task reads what another task built, the build file names that task.

Two more rules came the same way. On the server, we test a whole service at once, never one module alone, because a module tested alone picks up its neighbours from the cache, and they may be old. And we never run two server builds at the same time in the same folder. They overwrite each other's generated files and fail with errors that look exactly like mistakes in the code.

We change it when the full build gets too slow for everyday work. The next step then is to build and test only what a change can affect. Turborepo can already work that out for the web, and the server would need the same.

4. Checks that read the whole tree

The checks we value most are the ones no single folder can run. Each one reads two parts of the repository and confirms that they agree.

Checks that read the whole tree. Each check reads two parts and fails when they disagree: the screens' types against the API the server publishes, the event routes against the list of who listens to what, the permissions a screen checks against the ones the server grants, a new required database column against the rows already in the table, shared code against words that belong to one trade, and the buttons our screen tests press against the screens themselves. Each check answers pass, fail or unknown, and a run counts only with 0 failures and 0 unknown

Why one repository helps when AI writes the code

Most of our code is written with AI, with engineers deciding what to build and checking what comes out. In one repository, an AI session can follow a field from the database to the phone screen and change every place in one go, instead of guessing what the other side looks like. A short notes file at the top of the repository tells every session how to build and test, and in which order. And when a change still misses a place, the checks above say so before any person has to spot it.

The take-away

A monorepo pays off only when the build order is written down and the checks read the whole tree. Without those two, it is just many projects sharing one folder.

The give-away: our repository layout and build-order rules

Here is our layout, top level only, with what each folder holds. Your names will differ, and the shape is what matters.

text
REPOSITORY LAYOUT sdks/backend     The server SDK: what every service needs, written onceadapters         One folder per kind of outside service, each with a sandboxapis             One folder per business area, four layers in eachsdks/frontend    The web SDK: components, sign-in, the API client, one typed                 client per business areaapps             The web shell, and one folder per consolesdks/native      The native kit, and the customer apps' shared coremobile           One folder per phone app, plus the customer web appwebsites         One shared website kit, and a thin folder per siteinfra            Terraform layers and the Helm chartgateways         The API gateway's routes, kept as datatests            End-to-end and API tests, and shared test datascripts          Deploy scripts and the cross-tree checksdocs             Designs, runbooks and house rules

Coming up next

On Tuesday, 1 December, we look at how one codebase serves both jewellers and travel agencies: what a shop sells, the words it uses and the screens it shows are settings on the shop, never a copy of the code.

Do you keep your code in one repository, or in many? We'd love to hear what has worked for you, and what hasn't.

Discuss this post on LinkedIn →

Written by

Linjith Kunnon

Co-Founder and Chief Architect, QuickBee

Follow along

New posts appear here first. Follow QuickBee to see the rest of what we are building.