Exercise — Eventual Consistency: make a consumer idempotent and order-tolerant.

Topic: microservices, Data Management. Level: Intermediate.

Answer

Combine a processed-id set (idempotency) with a version check (ordering). Duplicates are dropped by id; out-of-order updates are dropped by version. Together they make the projection converge to the latest state regardless of delivery quirks. ```java @KafkaListener(topics = "inventory") void handle(StockChanged e) { if (!processed.add(e.eventId())) return; // idempotent: dup dropped Integer current = readDb.versionOf(e.sku()); if (current != null && e.version() <= current) return; // order-tolerant readDb.setStock(e.sku(), e.qty(), e.version()); } ```