Ariel Onoriaga

Field notes

Splitting a 543k-line Rust monolith into 30 Cargo crates (5x–8.7x faster builds)

Ariel Onoriaga ·

Measured incremental compile-time benchmarks and architectural lessons from splitting a production 543k-line Rust/Axum/PostgreSQL SaaS monolith into a 30-crate Cargo workspace.

Single-crate Rust backends feel effortless up to about 50,000 lines. Around half a million lines of Axum handlers, SQLx queries, domain logic, and migrations, every tiny one-line edit turns into a 35-to-48-second wait while rustc recompiles the entire world.

That was the exact bottleneck in Ez-Commerce — my multi-tenant retail commerce SaaS (stock, offline-capable POS, electronic tax invoicing, storefront, and in-app AI assistant) running in production across 14 live shops with 481 PostgreSQL migrations and over 62,000 production sales in 60 days.

Because much of the codebase grew rapidly through AI-augmented engineering (915 releases since March 2026), compile latency quickly became the primary throttle on both human review and automated test loops. Splitting the 543k-line single crate into a 30-crate Cargo workspace across 44 bounded contexts cut incremental compile times by 5.0× to 8.7×.

Measured incremental build benchmarks

All timings below were measured on 2026-11-24 (at 29 crates on commit 443687d9c, median of 5 alternating warm-cache pairs on the same laptop; the workspace is now 30 crates after adding ez-subscriptions):

Edit scenario Single crate (before) Workspace (after) Speedup
Sales module edit (ez-sales) 35.79s 5.06s 7.1×
Catalog module edit (ez-catalog) 48.04s 5.55s 8.7×
Shared core edit (core lib) 47.67s 9.62s 5.0×
Core edit + full server binary 26.19s 14.90s 1.8×

Cutting an edit-check cycle from 48s down to 5.5s does more than save minutes — it changes how often engineers and test agents run verification before pushing.

The crate topology that actually broke compile coupling

Splitting files arbitrarily into crates does not speed up Rust builds if every crate still depends on a bloated common crate that changes every hour. Three structural rules made the speedup stick:

1. Keep the shared kernel boring and stable

Only true cross-cutting primitives live in the core library: tenant context, typed IDs, money/decimal types, auth claims, error envelopes, and database pool traits. Business workflows stay strictly inside leaf domain crates (ez-sales, ez-catalog, ez-invoicing, ez-subscriptions).

2. Enforce one-way dependencies between bounded contexts

When two modules needed each other’s internals, they could not be split cleanly. Instead of introducing circular traits, we decoupled cross-context side effects through a PostgreSQL transactional outbox with idempotency keys and leased SKIP LOCKED claims (detailed in Payments that survive retries).

3. Keep the HTTP composition binary thin

The top-level server binary wires routers, middleware, and dependency ports from the 28 module crates — holding almost zero business logic of its own. Editing a single module crate only recompiles that leaf crate during focused test runs, never the other 27 modules.

Guarding the boundaries after the split

A workspace split decays fast if contributors quietly pull heavy dependencies back into core or bypass port traits during a rush. To prevent regression:

  • Every port required in production stays non-optional in constructors (no Option<Port> that a production caller could accidentally pass as None).
  • Test stand-ins fail closed and live behind explicit test-support features only.
  • Architecture and dependency-direction rules stay enforced in CI alongside entropy-hunter and real-PostgreSQL integration tests.

The same hexagonal pattern powers my open-source 7-crate Rust workspace lazyfetch (an IO-free core enforced in CI with 146 tests).

Need help untangling a slow Rust or TypeScript backend?

Whether you are fighting slow Cargo compile loops, consolidating microservices into a clean modular monolith (Three services, one Rust binary), or hardening payment and webhook reliability, I work with US startups on fixed-scope 5-day sprints ($2,000) and 15–20 h/week fractional contracts. See full offers at /freelance/.