Advanced

Partitioning & Reconciliation

Design Kafka topics that preserve per-entity ordering, tame skew, and make late arrivals safe with watermarks and versioning.

The Golden Rule of Kafka Ordering

Kafka guarantees the order of messages within a single partition. It makes no ordering guarantees across different partitions. Therefore, to maintain the correct sequence of changes for any given entity (like a specific customer or a single product), all changes for that entity must be sent to the same partition. This is the fundamental purpose of a partition key.

Partition sizing (rule-of-thumb)

Start with a target throughput per partition your consumers can sustain (1–5k events/s).

How to Choose a Good Partition Key

The quality of your partition key directly impacts both the correctness and performance of your system.

Good Keys (Preserve Order & Distribute Load)

Bad Keys (Lead to Incorrect Order or Skew)

Mitigations for hot keys (keep per-entity order)

Partitioning Simulator: The Impact of Key Choice

Controls
6 1000 5000 Skew stresses single partitions. 40% 2000 1

Max partition rate

Events/s on the hottest partition.

P95 partition rate

Load tail—capacity planning.

Estimated lag

Seconds to drain if max > capacity.

Ordering guarantee

Per-key

Global order is not guaranteed (per-key only).

Partition load (events/s)

Tip: If one bar dominates, you have partition skew. Consider a better key (include a tenant ID or sharding suffix), or move hot tenants to their own topic/cluster.

Making Sinks Bulletproof: Version Guards & Watermarks

Even with perfect partitioning, network retries or broker failures can cause events to arrive out of order. To protect against late-arriving events and guarantee correctness, your target table needs two key columns:

  1. id: The primary key of the entity.
  2. event_timestamp (or version number): A strictly increasing value from the source (a transaction ID or a precise timestamp) that indicates when the change occurred.

The consumer then uses a MERGE (or INSERT...ON CONFLICT) statement with a crucial WHERE clause: only update the target row if the incoming event's timestamp is newer than the timestamp already stored.

Watermark: if your source timestamps can arrive late, advance a per-table watermark (“safe up to T-Δ”) before finalizing aggregates. Late events ≤ Δ update rows; older ones are quarantined or appended to a corrections table.

Production-Ready Idempotent Sink Pattern (Postgres)

-- Assumes target table has a primary key `id` and a column `event_timestamp`
-- This MERGE statement is atomic and safe from race conditions.

MERGE INTO target_table T
USING (
  SELECT
    :entity_id AS id,
    :payload AS payload,
    :event_timestamp AS ts
) AS S
ON (T.id = S.id)
WHEN MATCHED AND T.event_timestamp < S.ts THEN
  -- Only update if the incoming event is newer
  UPDATE SET
    payload = S.payload,
    event_timestamp = S.ts
WHEN NOT MATCHED THEN
  -- If the record doesn't exist, create it
  INSERT (id, payload, event_timestamp)
  VALUES (S.id, S.payload, S.ts);

Tie-break equal timestamps (sequence) — BigQuery example

MERGE `dw.target` T
USING (SELECT @id AS id, @ts AS ts, @seq AS seq, @payload AS p) S
ON T.id = S.id
WHEN MATCHED AND (T.ts < S.ts OR (T.ts = S.ts AND T.seq < S.seq)) THEN
  UPDATE SET payload = S.p, ts = S.ts, seq = S.seq
WHEN NOT MATCHED THEN
  INSERT (id, ts, seq, payload) VALUES (S.id, S.ts, S.seq, S.p);

Version guards + watermarks make consumers idempotent and resilient to duplicates and late arrivals.

Audit loops (detect & repair)

Partitioning Knowledge Check

Test your understanding of partition key selection and ordering guarantees.

Q1

Why is choosing the right partition key critical in CDC?

Q2

What is partition skew, and why is it a problem?

Q3

What is a 'late-arriving' event in CDC?

Q4

How can you mitigate partition skew caused by a single hot key?

Q5

What is an audit loop in the context of CDC partitioning?

0/5 correct
Progress 0% No progress yet
Progress is stored locally in this browser.