KatInterview

Making a Kafka-to-Postgres Payment Consumer Safe Under Retries

System DesignModel answer
MediumModel answerSystem Designjavaspring-securitykafka

Last updated

Fix lost security context in async tasks, duplicate ledger writes after Kafka redelivery, and unsafe shared metrics maps in a Java 17 / Spring Boot payment consumer.

Scenario: Hardening a payment-event consumer

A Java 17 / Spring Boot 3 service reads card authorization events from a Kafka topic named payment-authorizations. For each event it calls an internal risk-scoring REST API asynchronously, then writes the resulting transaction to PostgreSQL.

Load testing exposed three defects:

  1. Missing credentials downstream. Calls made from async tasks get 401 Unauthorized because the Spring SecurityContext is not present on the worker threads.
  2. Double debits. During network instability and consumer-group rebalances, the same Kafka record is handled more than once and the ledger receives duplicate debit rows.
  3. Corrupt metrics. A shared in-memory metrics store built on a plain HashMap sometimes shows wrong counts or throws ConcurrentModificationException.

Describe how you would diagnose and fix each problem, with concrete implementation choices.

What to cover

A. Security context across threads

  • Why does SecurityContextHolder.getContext() come back empty inside a task started with CompletableFuture.supplyAsync() or a custom ThreadPoolTaskExecutor?
  • Give two sound ways to carry the caller's credentials into worker threads. Explain the specific danger of MODE_INHERITABLETHREADLOCAL when threads are pooled.

B. Kafka delivery and idempotency

  • Compare enable.auto.commit=true with Spring Kafka's AckMode.MANUAL_IMMEDIATE. Why does at-least-once delivery produce duplicates around rebalances?
  • Design an end-to-end approach so redelivered records can never produce a second database write. Name the constraints and the check you would use.
  • How do you deal with an unprocessable "poison pill" record without stalling the partition forever?

C. Concurrency and the database/Kafka boundary

  • Compare ConcurrentHashMap, Collections.synchronizedMap() and an explicit ReentrantLock for a read-heavy, highly concurrent metrics store.
  • Suppose the database transaction commits but the Kafka offset commit then fails. What happens, and how do you handle the inconsistency between the two systems?

Hints

Hint 1

A ThreadLocal belongs to exactly one thread. Ask which thread the authentication was stored on and which thread runs the async task, then ask when an inheritable thread local is copied.

Hint 2

Treat the database as the single referee for duplicates: record a stable business identifier under a uniqueness constraint in the same transaction as the ledger write, and still acknowledge duplicates.

Hint 3

You cannot make a DB commit and a Kafka offset commit atomic. Make redelivery harmless instead, and use an outbox table if the service also needs to publish events after its DB write.