Which module should you split out of a monolith first?
Question
A store monolith runs Cart, Pricing, Catalog, and Invoicing on one PostgreSQL database. Checkout currently takes 12 ms in-process, with a 50 ms p95 SLA. Finance ships tax changes daily, while checkout deploys weekly.
Per checkout, Cart calls Pricing 6 times synchronously and joins 4 shared tables (cart_items, promos, prices, catalog_skus). Checkout triggers Invoicing once after payment commits and needs no reply; Invoicing currently joins orders and customers.
Each synchronous cross-service RPC adds 8 ms of network and serialization overhead. Writing a local outbox row adds 2 ms.
- Why is splitting
Pricingout first a trap? - How do you extract
Invoicingcleanly without shared-database coupling?
Show hints (3)
- "COUNT SYNC HOPS"
Multiply the 6
CarttoPricingcalls by8 msand add the12 msbaseline. Does checkout still meet its50 msSLA? - "FIND THE ASYNC SEAM"
Checkoutdoes not wait for an invoice PDF. What happens if it publishes anOrderPaidevent after payment instead of making a sync RPC? - "BREAK SHARED JOINS"
Give
Invoicingits own database, carry the order snapshot inside the event, and publish reliably with a transactional outbox.
Show checked explanation
Extract Invoicing first over an async OrderPaid event, and leave Cart and Pricing together. Splitting Pricing adds 48 ms (6 * 8 ms), pushing checkout to 60 ms (5x baseline) and breaking the 50 ms SLA. Writing a local outbox row keeps checkout at 14 ms, isolates failures, and lets finance deploy daily on its own database.
BASE_MS, SLA_MS, RPC_MS, OUTBOX_MS = 12, 50, 8, 2 options = [ ("monolith", 0, 0, True, False), ("split_pricing_sync", 6, 0, True, True), ("split_invoicing_sync", 1, 0, False, True), ("split_invoicing_async", 0, OUTBOX_MS, False, False), ] for name, rpcs, extra, coupled, blocks in options: ms = BASE_MS + rpcs * RPC_MS + extra sla = "OK" if ms <= SLA_MS else "BREACH" pick = "PICK" if (ms <= SLA_MS and not coupled and not blocks) else "SKIP" print(f"{name:21} {ms:2d}ms {sla:6} coupled={int(coupled)}" f" blocks={int(blocks)} -> {pick}")
monolith 12ms OK coupled=1 blocks=0 -> SKIP split_pricing_sync 60ms BREACH coupled=1 blocks=1 -> SKIP split_invoicing_sync 20ms OK coupled=0 blocks=1 -> SKIP split_invoicing_async 14ms OK coupled=0 blocks=0 -> PICK
Author-checked on CPython 3.12.10: ran the Python 3.12 latency and coupling model, checked every SLA margin, and tested transactional outbox atomicity and duplicate delivery in SQLite 3.49.1. Checked by an editor other than the author.
Walkthrough
Cut along low-chattiness seams where release cadence diverges, not along every domain noun.
| Option | Sync RPCs | Checkout time | Deploy coupling | Outage blocks checkout? |
|---|---|---|---|---|
| Stay monolith | 0 | 12 ms | High (shared release) | Shared process |
Split Pricing (sync) | 6 | 60 ms (breach) | High (4 shared joins) | Yes |
Split Invoicing (sync) | 1 | 20 ms | Low | Yes |
Split Invoicing (async) | 0 (+2 ms outbox) | 14 ms (36 ms spare) | Low | No |
Why splitting Pricing creates a distributed monolith. Cart calls Pricing 6 times per checkout. Over a network, 6 * 8 ms = 48 ms of RPC overhead pushes checkout from 12 ms to 60 ms, breaking the 50 ms SLA by 10 ms. Breaking those 4 shared SQL joins also forces remote lookups and lockstep deploys whenever promo rules change. Keep Cart and Pricing in the same deployable unit.
Why Invoicing is the right first cut. Finance ships tax updates daily while checkout ships weekly. Checkout never needs an invoice back to finish payment. A synchronous call to Invoicing (20 ms) still lets a billing outage block checkout. An async event removes that runtime link and leaves 36 ms of SLA headroom (14 ms total).
How to extract Invoicing safely without shared tables:
- Database per service: Give
Invoicingits own ledger and invoice database. Never let it query the monolith'sordersorcustomerstables directly. - Transactional outbox: Inside the checkout transaction, write the paid order and an
OrderPaidoutbox row together (+2 ms). A relay publishes that row to the broker. The event carries the line items and billing address snapshot, removing cross-database joins. - Strangler Fig and Anti-Corruption Layer: Route
/invoices/*at the gateway to the new service gradually. Place an Anti-Corruption Layer atInvoicing's edge to map legacy order fields into clean invoice concepts. - Idempotent consumer: Deduplicate
OrderPaidbyorder_idso retried broker deliveries never create two invoices.
Common mistakes
- Splitting
CartandPricingbecause they have separate names. High call volume and 4 shared tables turn a fast monolith into a slow distributed monolith. - Extracting
Invoicingcode while letting it keep readingordersandcustomersin the monolith database. Shared tables re-couple both teams' schemas. - Publishing
OrderPaidstraight to Kafka after committing PostgreSQL without an outbox. A crash between commit and publish silently drops invoices. - Calling
Invoicingsynchronously fromCheckouteven though users do not wait for a PDF. A billing deploy or outage then takes down sales.
Follow-up questions
- "How do you migrate existing
/invoicesread traffic without a big-bang rewrite?" Use the Strangler Fig pattern. First shadow-feedOrderPaidevents and backfill historical invoices into the new database. Compare outputs against the monolith, shift/invoices/*reads at the API gateway in slices, and retire the old module once parity holds. - "What if finance needs customer address edits after the order was placed?" Invoices are legal records frozen at purchase time, so storing the checkout snapshot is actually more accurate than joining a mutable
customersrow. For credit notes or updated billing profiles, consume aCustomerBillingUpdatedevent into a small local projection. - "When would extracting
Pricingever make sense?" Only after redesigning the interface into one coarse-grainedPriceCart(items)call (cutting 6 hops to 1,20 mstotal). You would also keep read-only catalog price snapshots locally, or split when pricing needs separate ML compute scaling.
What a strong answer covers
Computes the 60 ms SLA breach from 6 sync RPCs and rejects splitting Pricing. Picks Invoicing for daily deploy isolation and async decoupling (14 ms), and eliminates shared tables with database-per-service, transactional outbox, Strangler Fig routing, and an Anti-Corruption Layer.
Open in interactive practice deck →