Module A-10·23 min read

When to decouple — the outbox pattern, datastore split strategies, and transitioning a write-heavy module out of the monolith without data loss.

Module 9 — Pragmatic Microservice Deconstruction: Splitting Ingestion from Analytics

What this module covers: The Modulith from Module 8 is the right starting architecture. But eventually a specific module genuinely needs independent scaling, geographic distribution, or a different runtime. Extracting a module incorrectly causes data loss, split-brain state, and deployment coupling that defeats the purpose of the split. This module covers how to identify the correct seam to split, the outbox pattern for atomically publishing events during extraction, datastore split strategies, and the operational reality of running two services where there was one.


When to Split: The Three Legitimate Signals

The decision to split a module out of the Modulith should be data-driven. There are exactly three legitimate reasons:

Signal 1: Independent scaling requirement

A module needs 10× the compute of others. The analytics module needs 32 CPU cores for aggregation while ingestion needs 4. Scaling the whole Modulith to 32 cores wastes 28 cores of ingestion capacity.

javascript

If analytics consistently uses 15× more CPU than ingestion per transaction, that's a scaling signal.

Signal 2: Different availability requirements

Ingestion must be 99.99% available (data loss on downtime). Analytics can tolerate 99.9% (reports are slightly stale during an outage). Running them together means analytics bugs can take down ingestion — an unacceptable risk profile.

Signal 3: Genuine team autonomy need

A separate team owns analytics and deploys 10 times per day. Coupling their deployment to ingestion (which deploys once a week) creates a deployment bottleneck. Conway's Law applies.

Do NOT split for:

  • "Microservices are the modern way" (cargo cult)
  • The module is large (size is not a reason for distribution)
  • You want to use a different language (use a native addon instead)
  • "It might need to scale later" (YAGNI — split when the signal is real)

Identifying the Split Seam

The correct seam for splitting is where write contention and read contention diverge.

For a blockchain indexer:

text

The seam: separate the write service from the read/analytics service. The write service owns the primary. The analytics service reads from a read replica or a separate projection store.

text

Now writes and reads never compete. The ingestion service can sustain 50K writes/sec at full I/O. Analytics has its own PostgreSQL instance sized for reads.


The Outbox Pattern: Atomic Write + Event Publish

The most dangerous moment in a service split is when a write to the database and a publish to Kafka must be treated as a unit. If you write to the database and then publish to Kafka, a crash between the two leaves the database updated but Kafka unnotified — downstream services miss the event.

javascript

The outbox pattern solves this by writing the event to an outbox table in the same database transaction as the business data. Atomicity is guaranteed by the database. A separate process reads the outbox and publishes to Kafka.

javascript
javascript

Why FOR UPDATE SKIP LOCKED: Multiple outbox publisher instances can run safely. Each grabs a batch of rows that no other publisher is currently processing. No duplicate publishes.

The at-least-once guarantee: if the publisher crashes after publishing to Kafka but before marking the rows, it will re-publish on restart. The Kafka consumer must be idempotent — processing the same event twice must be safe.


Kafka Consumer: Idempotent Processing

The analytics service consumes from the Kafka topic. Because the outbox guarantees at-least-once delivery, the consumer must handle duplicates:

javascript

Datastore Split Strategies

When splitting, you have several options for how to divide the data:

Strategy 1: Primary + Read Replica (simplest)

text

The read replica receives WAL from the primary and stays < 1 second behind. Analytics queries run against it without competing with writes. No separate schema required.

Limitation: analytics read volume still competes with replication I/O on the replica. Works for moderate analytics load.

Strategy 2: CQRS with separate projection store

text

The analytics database is optimized for read patterns: denormalized tables, partial indexes, materialized views. The ingestion database is optimized for write throughput: normalized, minimal indexes.

javascript

Strategy 3: Dedicated analytics store (ClickHouse/TimescaleDB)

For high-cardinality time-series analytics (transaction volume by hour by sender by network), a columnar store like ClickHouse provides 10–100× better query performance than PostgreSQL.

javascript

This is the correct deployment for a production blockchain explorer where analytics queries aggregate billions of rows.


Service Contract: The Anti-Corruption Layer

When splitting, the analytics service must not depend on the ingestion service's internal data model. If ingestion changes its schema, analytics should not break.

javascript

Version the events (eventVersion: 1). When the ingestion event format changes, bump to eventVersion: 2 and have analytics handle both versions during the transition.


Operational Reality: Two Services

After the split, you have doubled the operational surface:

ConcernBefore (Modulith)After (Split)
Deployments1 pipeline2 pipelines
Health checks1 endpoint2 endpoints
Logs1 log stream2 log streams
Distributed tracingOptionalRequired
Schema migrations1 database2 databases
Kafka consumer lagN/AMust monitor
Outbox queue depthN/AMust monitor
Data consistencyGuaranteed (same TX)Eventual (Kafka lag)

Monitoring additions for the split:

javascript
javascript

Production Incident: Split-Brain During Extraction Migration

Context: A blockchain indexer mid-migration. Ingestion writes to PostgreSQL. The outbox publisher was deployed but the analytics Kafka consumer had not yet been deployed.

What happened:

The outbox table accumulated 12 million rows over 2 days. When the analytics consumer was finally deployed, it began processing 12 million events from the beginning of time. The analytics database received 12 million inserts in 4 hours — a 10× spike over normal write rate. The analytics PostgreSQL ran out of IOPS and fell behind. Queries against the analytics API returned stale data for 18 hours while the consumer caught up.

The fix:

javascript

The lesson: migrations that involve a new consumer processing a backlog must account for the resource cost of processing historical events. Pre-seed the downstream datastore before enabling the consumer and starting from the current offset.


Summary

ConceptKey Takeaway
Split signalsIndependent scale requirement, different availability, team autonomy. Not "microservices are modern."
Correct seamWhere write and read contention diverge. Separate write-heavy from read-heavy on different datastores.
Outbox patternWrite event to outbox table in same DB transaction as business data. Atomicity guaranteed.
At-least-once deliveryPublisher may re-publish after crash. Consumer must be idempotent.
FOR UPDATE SKIP LOCKEDMultiple publishers consume outbox without duplicates.
Idempotent consumerINSERT ... ON CONFLICT DO NOTHING. Safe to process same event twice.
CQRS projectionsAnalytics DB is denormalized, read-optimized. Updated via Kafka, never by ingestion service.
Anti-corruption layerVersion your events. Translate between service domains explicitly.
Operational cost2 pipelines, 2 health checks, consumer lag monitoring, outbox depth monitoring.
Historical backfillPre-seed downstream DB before enabling consumer. Never process 12M historical events on a live system.

You can now build and split distributed services. Module 10 covers how those services talk to each other at scale — gRPC vs REST, Kafka consumer group mechanics, and event sourcing for payment ledgers that need replay capability.

Next: Module 10 — High-Performance IPC: gRPC, Kafka & Event Streams →


Knowledge Check

When migrating from a Modulith to Microservices, what is the primary purpose of the "Outbox Pattern"?


Why is the FOR UPDATE SKIP LOCKED clause critical when multiple workers are processing an outbox table?


What role does an Anti-Corruption Layer (ACL) play when splitting the Analytics service from the Ingestion service?

Test your knowledge with more question sets

Sign in to access a wider variety of questions and get notified when new practice sets are added to this module.

Sign in & Register

Discussion

0

Join the discussion

Loading comments...

© 2026 Jatin Jain Saraf (JJS). All rights reserved.