In microservices, services often keep copies of data owned by other services (for example the Orders service stores a customer name and shipping address). You cannot use one distributed ACID transaction across them, so consistency is managed through ownership rules and reliable event propagation — accepting eventual consistency where the business allows it. 1. Clear data ownership - Each piece of data has exactly one owning service (the source of truth). Others hold read-only copies and never update them directly. 2. Reliable propagation of changes - The owner publishes domain events (CustomerAddressChanged) whenever data changes. - Use the transactional outbox pattern: the state change and the event are saved in the same database transaction; a relay publishes the event to the broker. This avoids "saved but never published" or "published but not saved" inconsistencies. Change data capture (Debezium) is an alternative. 3. Idempotent, ordered consumers - Consumers update their local copies idempotently (inbox table or unique event IDs), so duplicates do not corrupt data. - Handle ordering with version numbers or sequence numbers per entity: ignore events older than the version already stored. Partition by entity ID (Kafka key, Service Bus sessions) when order matters. 4. Sagas for multi-service business transactions - For workflows spanning services (order, payment, inventory), use sagas: each step is a local transaction, and failures trigger compensating actions (refund payment, release stock). Orchestration (a coordinator) or choreography (event reactions) both work. 5. Decide what really needs strong consistency - Some data must be correct at the moment of use (account balance for a payment): get it synchronously from the owner, or keep that logic inside one service. - Other data can be slightly stale (product name on an old order — often deliberately a snapshot at order time). 6. Detect and repair drift - Reconciliation jobs compare copies with the source and fix differences. - Ability to replay events or rebuild read models from the source. - Monitoring of consumer lag and dead-letter queues, with alerts. 7. Contracts and versioning - Versioned event schemas (schema registry, backward-compatible changes), so consumers do not break and silently stop updating copies. 8. User experience - Design the UI for eventual consistency: show "processing" states, refresh after a short delay, or read your own writes from the owning service right after an update.
Ravindra Wadile · · 0 likes