Advanced Backend Java· 50 min

Kafka, Redis, and Resilient Services

Apply messaging, caching, retries, timeouts, circuit breakers, and idempotency.

Java interview lesson
Question 31 · Not reviewed

Distributed systems · reliability

Can you explain this clearly in an interview?

Use the strong answer as your reference, explain it aloud in your own words, then choose how soon you want to review it again.

Interview prompt

Kafka, Redis, and Resilient Services

Explain it aloud in your own words

Strong answer

Kafka is a durable, partitioned log: ordering is guaranteed only within a partition, consumers in a group share partitions, and delivery is at-least-once unless you engineer otherwise, so consumers must be idempotent. Redis is an in-memory data store used for caching, rate limiting, distributed locks with leases, and short-lived state, and it should be treated as losable unless persistence is configured deliberately. Resilience patterns (timeouts, retries with backoff, circuit breakers, bulkheads, and dead-letter topics) keep partial failures from cascading.

Key points

  • Choose the message key deliberately; it decides partition, ordering, and hot-spotting.
  • Commit offsets after processing succeeds, and deduplicate with a processed-message table or natural idempotency.
  • Retry only transient failures; poison messages go to a dead-letter topic after bounded attempts.
  • A circuit breaker protects the caller and the dependency; it needs a fallback that is acceptable to the business.

Interviewer looks for

Explain the delivery guarantee you actually get, how duplicates and reordering are handled, and how retries, timeouts, and circuit breakers interact without amplifying load.

Java example

1@Component
2class PaymentEventsListener {
3  private final ProcessedEvents processed;
4  private final LedgerService ledger;
5
6  PaymentEventsListener(ProcessedEvents processed, LedgerService ledger) {
7    this.processed = processed;
8    this.ledger = ledger;
9  }
10
11  @KafkaListener(topics = "payments.captured", groupId = "ledger")
12  @Transactional
13  void on(PaymentCaptured event) {
14    if (!processed.markIfNew(event.eventId())) return; // idempotent consumer
15    ledger.record(event.orderId(), event.amount());
16  }
17}
18
19@Service
20class ShippingQuotes {
21  @CircuitBreaker(name = "carrier", fallbackMethod = "flatRate")
22  @Retry(name = "carrier")
23  @TimeLimiter(name = "carrier")
24  CompletableFuture<Quote> quote(Parcel parcel) {
25    return CompletableFuture.supplyAsync(() -> carrierApi.quote(parcel));
26  }
27
28  CompletableFuture<Quote> flatRate(Parcel parcel, Throwable cause) {
29    return CompletableFuture.completedFuture(Quote.flat(parcel));
30  }
31}

Topic interview questions

Revise this topic like an interviewer

These questions are tied to this lesson. Read each question aloud, then compare your explanation with the strong answer.

5 questions
Q1Basic

How does Kafka guarantee ordering?

Strong answer

Only within a partition. Messages with the same key go to the same partition and are consumed in order by one consumer in the group. There is no global ordering across partitions, so the key must group the events that need to stay ordered, such as all events for one order.

Q2Intermediate

What does at-least-once delivery mean for a Kafka consumer?

Strong answer

A message can be redelivered after a crash or rebalance if its offset was not committed, so the same event may be processed twice. Consumers must be idempotent, for example by recording processed event ids in the same transaction as the side effect or using upserts.

Q3Intermediate

How do you handle a poison message in Kafka?

Strong answer

Classify the failure, retry transient errors a bounded number of times with backoff, then publish the message with error metadata to a dead-letter topic and commit the offset so the partition is not blocked. Alert on DLT volume and provide a replay path.

Q4Advanced

How does a circuit breaker work and how is it different from a retry?

Strong answer

A circuit breaker tracks failure rate or slow calls; when a threshold is exceeded it opens and fails fast for a period, then lets a few trial calls through in half-open state. Retries repeat a call hoping a transient error clears. Unbounded retries against a failing service amplify load, which the breaker prevents.

Q5Advanced

What are safe ways to use Redis for a distributed lock?

Strong answer

Acquire with SET key token NX PX ttl so the lock has an owner token and an expiry, release only if the token matches (atomically via a script), and keep the critical section shorter than the lease. For correctness-critical work, add a fencing token checked by the resource, because a paused client can outlive its lease.

How well do you know this?

Your review choice is saved locally and synced to your learner profile when signed in.