Managing Distributed Data

1. Implementing Database per Service

AspectDetail
GoalIndependent schema and scale per service
ImplementationSeparate schema, instance, or engine
No cross-DB joinsUse APIs / events / materialized views
Schema ownershipService owns migrations, no external writes

2. Understanding Shared Database Anti-Pattern

SymptomConsequence
Multiple services write same tableHidden coupling, race conditions
Coordinated migrationsLock-step releases
Shared ORM modelLibrary bumps force mass redeploy
FixCarve ownership; integrate via APIs/events

3. Implementing Data Replication

TypeDetail
SynchronousStrong consistency; higher latency
AsynchronousEventual; replica lag
Logical (CDC)Stream WAL/binlog into Kafka
Cross-regionMulti-master or single-writer + readers

4. Handling Data Consistency

PatternDetail
Local transactionWithin a single service
SagaAcross services with compensations
OutboxAtomic write + event for reliable propagation
IdempotencyTolerate retries safely

5. Using Eventual Consistency

AspectDetail
ConvergenceState agrees after replication
UXMask via optimistic UI
Conflict resolutionLast-write-wins, vector clocks, CRDTs
MeasurementTrack replication lag SLO

6. Implementing Change Data Capture

ToolSource
DebeziumPostgres, MySQL, Mongo, SQL Server
Maxwell's daemonMySQL binlog
AWS DMSMany sources
Postgres logical decodingNative via wal2json/pgoutput
Note: CDC enables outbox-like patterns without app code changes.

7. Managing Data Ownership

PrincipleDetail
Single writerOne service is source of truth per dataset
Read replicasOther services read via API or CDC
Schema authorityOwner approves schema changes
CatalogData catalog (DataHub, Atlan) lists ownership

8. Handling Data Duplication

Trade-offDetail
AcceptableRead-optimized copies, denormalized views
RiskyMultiple writers to same logical entity
SyncEvent-driven; track source-of-truth
ReconciliationPeriodic batch compare

9. Implementing Polyglot Persistence

NeedDatabase
Transactional/relationalPostgres, MySQL
DocumentMongoDB, DynamoDB
Wide-columnCassandra, ScyllaDB
Key-value / cacheRedis, Memcached
SearchElasticsearch, OpenSearch
GraphNeo4j, Neptune
Time seriesInfluxDB, TimescaleDB
Vectorpgvector, Pinecone, Weaviate

10. Implementing Database Sharding

StrategyDetail
Hash-basedEven spread; hard to rebalance
Range-basedGood for range queries; risk of hotspots
DirectoryLookup table; flexible, single point
Geo-shardingRegion-local data
Cross-shard opsAvoid; or use scatter-gather

11. Managing Data Migration

StepDetail
Versioned migrationsFlyway, Liquibase, Alembic
Expand/contractAdd new col → backfill → switch reads → drop old
Dual-writeOld + new during transition
BackfillBatch jobs; track progress
RollbackForward-only with reverts

12. Understanding Transaction Boundaries

BoundaryMechanism
Within serviceACID DB transaction
Across servicesSaga (orchestration / choreography)
Within aggregateSingle transaction guaranteed
Across aggregatesEventual consistency via events