Overview

13 Measuring data consistency and transactions

Distributed systems rarely exhibit perfect, instantaneous agreement on data, so this chapter focuses on recognizing, diagnosing, and preventing the kinds of inconsistencies that emerge when services communicate asynchronously and persist state independently. It outlines a practical toolkit for detecting symptoms across boundaries, reconstructing multi-step transactions that span services, and continuously measuring consistency in production. Core themes include spotting coordination breakdowns, using trace IDs and audit logs to connect the dots, and turning business rules (invariants) into guardrails that surface invalid states early.

The chapter first shows how inconsistencies surface in real systems: time-based anomalies, missing or duplicated records, out-of-order workflows, and silent failures. It advocates combining distributed tracing, message-bus inspection, and direct database queries to follow an event end-to-end, illustrating how issues like deadlocks and missing retries can cause “successful” requests to vanish downstream. It then introduces domain invariants—nonnegotiable business rules such as “a refund must reference a valid order”—and demonstrates how to codify them via queries, correlation, and reconciliation checks. For multi-step transactions, it emphasizes high-quality audit logs (immutable, structured, centralized, long-retained) to reconstruct the story, and, in event-driven designs, using event logs, consumer offsets, and safe event replays to validate hypotheses, confirm fixes, and avert repeat failures.

Finally, the chapter turns to measuring and monitoring consistency over time. It explains using checksums and cryptographic hashes to verify that replicated, cached, or transferred data remains intact without moving full payloads, and shows how embedding hashes in audit metadata helps detect drift and rapidly assess a problem’s blast radius. It concludes with reconciliation jobs that continuously compare expected versus actual state across systems—often accelerated by hierarchical hashing—to flag, alert on, or even automatically correct mismatches. Together, these practices create a layered, proactive approach to consistency: detect early, trace rigorously, and measure continuously.

The payment system. The POS initiates a transaction which is audited and sent to execution to another system.
The engineer checked the POS logs followed by the Kafka topic. The clues they found revealed the possibility that the issue is in the auditing service side.
Single process transaction vs. a distributed transaction. A distributed transaction may lead to inconsistencies and domain invariants violations.
Steps for troubleshooting through story reconstruction.
An event-driven design. A message broker manages the messages which are exchanged by several services.
A developer can choose to replay events to troubleshoot a certain scenario.
A consistency problem. Account balances do not reflect the correct money amounts.
The blast radius is a way to see the impact of an issue throughout the system.

Summary

  • Time-based inconsistencies, like delays, out-of-order operations, or missing downstream actions, are early signs of broken coordination. By analyzing traces and timestamps, you can identify where a request silently failed or didn’t propagate as expected.
  • Since no single tool provides a complete picture, using multiple sources (trace spans, logs, and Kafka topics) helps confirm whether messages were successfully emitted, consumed, or lost, allowing for full event reconstruction.
  • Domain invariants are business rules that must always hold true, like "every refund must be linked to an existing order." Encoding these into your monitoring or validation layers lets you catch inconsistencies even when individual services behave "correctly."
  • By automating invariant checks (e.g., as batch jobs), you create an early warning system that flags data inconsistencies in production, helping to prevent minor bugs from turning into customer-facing issues.
  • Audit logs act as the black box of your system, recording key business events and outcomes. They help reconstruct what happened across services, especially valuable when trying to explain partial failures or verify state transitions.
  • Good audit logs include transaction IDs, clear action labels, timestamps, and outcomes. When centralized and consistently formatted (e.g., JSON), they become a powerful tool for incident analysis and compliance.
  • While audit logs show what was recorded, traces show what was executed, and event logs show what was communicated. Correlating these sources helps build a full timeline and detect mismatches between intention and effect.
  • Kafka topics act as durable ledgers of what happened in event-driven architectures. Examining these logs helps you confirm whether an event was sent, when, and by which producer, even if consumers missed it.
  • Replaying events (e.g., with kafka-console-producer) lets you test services under controlled conditions. It’s especially useful for verifying that services consume and react to events properly, or to reproduce bugs in staging.
  • Hashing critical fields lets you verify that the same data exists across systems without transmitting full records. This is ideal for checking data integrity between replicas, caches, or integration points.
  • Instead of comparing every record, you can hash batches or partitions to detect mismatches efficiently. This divide-and-conquer approach keeps reconciliation jobs performant, even at large scale.

FAQ

What are the most common signs that my system has data consistency problems?
  • Missing or duplicated records between services
  • Out-of-sync states (e.g., paid but not shipped)
  • Delayed effects despite a “success” response
  • Trace trees that stop before a DB write or message publish
  • Gaps in ID sequences or unexpected rollback counts
  • Business-level alerts (e.g., refunds without orders) firing
  • Start by correlating traces, logs, event streams, and direct DB queries
How do I use distributed tracing to find where a multi-step transaction broke?
  • Search by a transaction/trace ID across services
  • Look for missing or incomplete spans downstream of key steps
  • Treat spans that end before persistence or publishing as red flags
  • Correlate spans with service logs and message broker topics
  • Verify that the producing service actually emitted the event
What are time-based anomalies and how can I spot them?
  • Irregularities like delays, out-of-order processing, duplicate retries, clock skew
  • Symptoms: success responses with no effects within expected SLA windows
  • Detect via timestamped traces, queue lag metrics, and aligned service clocks
  • Cross-check by inspecting broker offsets and DB write timestamps
What are domain invariants, and how do they help catch invalid states?
  • Business rules that must always hold (e.g., “refund must reference a valid completed order”)
  • Encode them in code, monitoring, and reconciliation jobs
  • Continuously query for violations (missing relationships, impossible sequences)
  • Use invariants to define business-level alerts and dashboards
How can database deadlocks or aborted transactions silently cause data loss?
  • Deadlocks can abort one of the transactions without obvious surface errors
  • Symptoms: missing rows, gaps in sequences, traces ending before DB writes
  • Confirm via DB deadlock logs and metrics (lock wait, rollbacks)
  • Fix by adding retry-with-backoff on transient failures and idempotency
  • Collaborate with DBAs to tune indexes and transaction scopes
Audit logs vs. debug logs: what’s the difference and what should audit logs contain?
  • Debug logs: internal execution detail; Audit logs: what happened, when, and with which IDs
  • Good audit entries include timestamp, unique business/transaction ID, action, outcome, source
  • Make audit logs immutable, structured, centrally stored, and long-retained
  • Mask sensitive data to meet compliance (e.g., PCI, GDPR)
How do I reconstruct a multi-service transaction using audit logs?
  • Start with the initiating ID (user/transaction/order)
  • Build a timeline of key business events across services
  • Look for missing mandatory steps or contradictory outcomes
  • Account for clock skew; correlate with traces and broker events
  • Identify the earliest divergence and drill into service-specific logs
How do event logs and replay (e.g., with Kafka) help diagnose missing messages?
  • Event logs show what was published, when, and by which producer
  • Check consumer group offsets to verify what was actually processed
  • Detect misconfigurations (wrong topic/partition/group) and idle consumers
  • Replay events in a safe environment to reproduce and validate fixes
  • Use built-in CLI tools to inspect topics and re-feed messages
How can checksums and hashes help verify data integrity across systems?
  • Compute checksums/hashes on payloads at send and receive points
  • Compare digests to detect drift without transferring full datasets
  • Embed hashes of key fields in audit records for later verification
  • Use stronger cryptographic hashes (e.g., SHA-256) for reliability
  • Spot-check hashes to quickly assess incident blast radius
What are reconciliation jobs and how should I design them?
  • Periodic processes that compare expected vs. actual state across systems
  • Detect and surface mismatches (missing, duplicated, out-of-sync entities)
  • Prioritize business invariants and high-value flows
  • Use hierarchical or chunked hashing to scale comparisons
  • Decide response: alert, queue for manual review, or safe auto-correction/retry
  • Track metrics (violation rate, age of inconsistencies) and trend over time

pro $24.99 per month

  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose one free eBook per month to keep
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime

lite $19.99 per month

  • access to all Manning books, including MEAPs!

team

5, 10 or 20 seats+ for your team - learn more


choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Troubleshooting Java, Second Edition ebook for free
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Troubleshooting Java, Second Edition ebook for free
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Troubleshooting Java, Second Edition ebook for free