In a 60-engineer mobile super-app (ride-hailing, food delivery and a wallet in one binary), a one-line change takes minutes to build incrementally: how do you diagnose the module graph, and which dependency rules do you enforce?
answer
- measure what rebuilds per change
- hubs fan out, leaves do not
- a grab-bag common module is a hub
- acyclic, API-only between features
- a failing build check, not a reviewer
basics
~20 sMeasure which modules rebuild for typical changes and find the hubs: a grab-bag common module, features wired to each other's implementations, re-exported dependencies. Split them, put API modules between features, and enforce acyclic, API-only edges with a failing build check.
solid answer
~50 sFirst measure: for a handful of typical one-line changes, record which modules rebuild, and count how many modules sit downstream of each module. Slow builds usually trace to **hub modules**: a grab-bag `common` module almost everything depends on, a feature others depend on through its implementation, a module that re-exports its own dependencies. Any edit there puts a large slice of the graph in the rebuild set. Then restructure: split the grab-bag by purpose, give cross-feature needs a small API module, keep implementations as **leaves** that only the app module depends on, and keep each module's own dependencies off its public surface. Finally, write the rules down and make the build fail on them: the graph is acyclic, features depend on other features' API modules only, core never depends on a feature. Rules enforced only in review erode as teams grow; a check in the build does not.
go deeper
Recall that an incremental build redoes what changed and what depends on it, so a module many others depend on is expensive to edit.
Explain fan-out and hubs, why an edit to a leaf is cheap, and why an API module between features cuts the rebuild set.
Run the diagnosis with measurements rather than guesses, name the culprits from the graph, and enforce the rules with a check that fails the build so they survive deadlines.
Weigh restructuring cost against the hours the team loses daily, sequence the migration so new edges cannot regress, and assign the rules and the hubs to an owning platform team.
## What an incremental build actually redoes An **incremental build** recompiles the modules whose inputs changed, and then the modules that depend on them, as far as the change propagates. So the cost of a one-line change is set by the **graph**, not by the size of the line: an edit to a module with many modules downstream of it can put many of them in the rebuild set, while an edit to a **leaf**, a module nothing else depends on, rebuilds that leaf and repackages the app. A change to a module's **public surface**, what other modules compile against, forces its dependants to recompile. A change hidden inside the module may or may not, depending on how well the build tool can tell that nothing its dependants see has changed. Design the graph so the common case is cheap whatever the tool does. ## Diagnosing: find the hubs In the super-app, about 60 engineers in eight feature teams, the complaint is that an incremental build after a one-line change takes minutes. Work it like a production incident: 1. **Pick representative changes**: a line in a ride screen, a line in a wallet data source, a line in the shared UI components. 2. **Record the rebuild set** for each: which modules actually recompiled, and how long each took. 3. **Compute fan-out**: for every module, count how many modules sit downstream of it. The ones with large counts are the **hubs**. 4. **Cross-reference churn**: a hub that also changes daily is the expensive one. A large, stable hub is far less of a problem. As an illustration (numbers invented for the example): if a `common` module has 50 modules downstream of it and gets edited a dozen times a day, it alone can account for most of the team's waiting, while a wallet implementation behind an API module rebuilds itself and repackages the app. ## The usual culprits | Shape in the graph | Why it is slow | Fix | |---|---|---| | A grab-bag common module everything depends on | Every edit to it reaches most of the graph | Split by purpose: UI components, networking, formatting, analytics | | A feature depending on another feature's implementation | Edits to one team's screens rebuild another team's modules | Introduce an API module; depend on that | | A module re-exporting its own dependencies | Consumers compile against things they never use, so unrelated changes reach them | Keep dependencies private unless they appear in the contract | | Logic piled into the app module | The module that depends on everything also changes most | Move logic down into features and core | | Modules far too coarse | One module holds several teams' code, so every edit rebuilds all of it | Split along what changes together | The opposite failure exists too: a graph **too fine-grained** pays fixed overhead per module, in configuration and build-step cost, and can end up slower and harder to maintain. Consolidate modules that always change together. ## The rules to enforce - **The graph is acyclic.** Two modules that each need the other cannot be ordered; if they must cooperate, a third module below both carries the shared contract. - **Features depend on other features' API modules only**, never on their implementations. - **Core modules never depend on feature modules.** - **Only the app module depends on implementation modules** (tests aside), and it is where implementations are bound. - **Modules keep their own dependencies private** unless a type is part of their contract. ## Enforce in the build, not in review A rule that lives in a wiki or in reviewers' memory erodes as the team grows: one deadline, one "just this once" edge, and the next author copies it. Encode the rules as a **check that runs on every change**, a test or build step that reads the declared module dependencies and fails on a forbidden edge. Track hub fan-out over time so a growing hub is visible before it hurts. Exceptions go through the team that owns the rule, in writing. ## What not to expect Restructuring the graph shortens the common incremental build; it does not make a clean build fast, and caching or faster machines help both but cannot rescue a graph where every change reaches everything. How a feature opens another feature's screen without a dependency on it is a separate question about route contracts.
- A completed wallet top-up should refresh the balance shown in the ride feature. How does that work without a feature-to-feature implementation edge?Both features depend on a contract below them, the wallet's API module or a core data module that owns the balance as its single source of truth, and the ride feature observes it. Neither imports the other's implementation, and the app module binds the real one. How the ride feature opens a wallet screen is a separate route-contract question.
- Why can adding modules make the build slower rather than faster?Every module carries fixed overhead: its own configuration, its own build step, more graph for the tool to evaluate. Past some granularity that overhead outweighs what incremental builds save. Granularity should follow the code's size and change patterns, so modules that always change together are better merged.
- How do you stop the graph from eroding after the restructure?Add the dependency check before the restructure finishes so new edges cannot regress while old code is still moving, run it on every change, and chart each module's downstream count over time. Exceptions are written down and owned by the platform team rather than waved through review.
- Only total lines of code drive incremental build time; the graph's shape does not matter.
- Putting all shared code in one common module minimises rebuilds.
- Code review alone is enough to keep features off each other's implementations.
- More, smaller modules always make builds faster.
- A feature may depend on another feature's implementation if it needs only one class.