Skip to main content
PreparationZone Logo
PrepZone

Ordering Guarantees Across Services

Per-partition ordering, total order costs, and when ShardPay accepts out-of-order settlement.

Pass checkpoint below

Why this matters

  • Global ordering requires a single Kafka partition — throughput ceiling unusable for ShardPay's millions of events per minute.
  • Key-based partitioning gives order where the business needs it: all events for accountId X on one partition.
  • Cross-partition operations (A pays B across two account shards) still need saga or application coordination — partition order does not solve everything.
  • Interviewers ask how you guarantee debit-before-credit visibility for one account while scaling horizontally.
No orderIndependent events
Per-partitionSame key ordered
Global totalSingle sequencer
Global order is expensive; partition-key order is the Kafka sweet spot.

Ordering scopes

Total order: one sequence for all events cluster-wide — simple mental model, single bottleneck. Partition order: Kafka preserves order within one partition; no guarantee across partitions. Per-key ordering: hash accountId (or transferId) to partition so all events for that key share one ordered stream.

ShardPay publishes TransferCompleted with key = merchantAccountId so settlement, notification, and analytics consumers see consistent per-merchant ordering without a global sequencer.

Walkthrough: same account, two transfers

Two transfers debit merchant account M-42 in quick succession. Both events hash to partition 17. Consumers process T1 before T2 matching commit order on the ledger shard. Merchant running balance projection never shows T2 before T1 for the same account.

Key points

  • Total order — every consumer sees identical global sequence; impractical at ShardPay scale except for low-volume control topics (Raft metadata changelog with single partition by design).
  • Partition order — broker guarantees order of records in one partition; reordering never happens inside partition. ShardPay relies on this for per-account settlement streams keyed by merchantAccountId.
  • Per-key ordering — choose partition key to match consistency boundary. ShardPay keys by creditor account for settlement; keys by transferId for saga orchestrator steps so all lifecycle events for one transfer co-locate.
  • Cross-partition coordination — operations touching two keys (payer on partition 3, payee on partition 9) have independent order; saga state machine reconciles with idempotent steps and explicit state transitions.
  • Reorder buffer — consumer-side window sorting when upstream cannot guarantee key discipline (legacy producers). ShardPay maintains 500ms reorder buffer on analytics only; financial path rejects out-of-order keys at schema validation.

The rest of this guide is part of Pro

Free covers every fresher guide in full. Pro unlocks Senior, Staff and Architect guides across every track.

Still to come · 2 sections

  • Cross-partition transfers
  • When to widen or narrow the key

Test yourself

Answer these before moving on — recall is what makes it stick.