Command Palette

Search for a command to run...

PHASE 10Intermediate ~12 min· topic 11 of 16

Topic 10.11

Kafka Deep Dive

In one line

The distributed commit log: topic, partition, offset, consumer group, producer, consumer, replication — the full mental model.

0/16 · 0%

Think of it like this

A very durable, replayable news ticker tape that a company keeps forever. Once a headline is printed on the tape, any number of readers can walk up and read it (even hours later), and the tape itself never forgets what was printed.

Key ideas

  1. 01

    The chain to internalize: Producer → Topic → Partition → Broker → Consumer Group → Offset (see the Blog for the full essay).

  2. 02

    Append-only, ordered log per partition; replication factor N → each partition has N copies across brokers.

  3. 03

    Why partitions: parallel throughput — N partitions allow N concurrent writers and N consumers per group.

  4. 04

    Ordering: strictly per-partition; keys hash to partitions (same key = same partition = ordered).

  5. 05

    Consumer groups split partitions — max active consumers = partition count.

  6. 06

    Consumer failure → rebalance → some partitions move to survivors → reprocessing from old offsets → DUPLICATES (design consumers idempotent).

  7. 07

    Producer acks: 0 (fire), 1 (leader), all (ISR quorum) — the durability dial.

  8. 08

    Retention: Kafka keeps the log (configurable days); consumers replay from any offset — the property NoSQL queues don't have.

Java / Spring map

  • →

    Spring Kafka: @KafkaListener(groupId=...), ProducerFactory config acks=all + retries; ConsumerSeekAware to replay.

Code & diagrams

topics, partitions, consumer groupsdiagram
Rendering diagram…
SpringKafka.javajava

Producer + consumer skeleton — the shapes you'll be drawing in interviews.

// --- producer ---
@Configuration
public class KafkaProducerConfig {
  @Bean
  public ProducerFactory<String, String> producerFactory() {
    Map<String, Object> p = new HashMap<>();
    p.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "broker:9092");
    p.put(ProducerConfig.ACKS_CONFIG, "all");        // durability dial
    p.put(ProducerConfig.RETRIES_CONFIG, 5);         // at-least-once + dupes possible
    p.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
    p.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
    return new DefaultKafkaProducerFactory<>(p);
  }
  @Bean public KafkaTemplate<String, String> kafkaTemplate() { return new KafkaTemplate<>(producerFactory()); }
}

// --- consumer (idempotent by design) ---
@Service
public class OrderEventConsumer {
  @KafkaListener(topics = "order-events", groupId = "payment-service")
  public void on(String message) {
    var event = OrderEvent.parse(message);
    if (paymentRepo.existsByEventId(event.eventId)) return;   // idempotency
    paymentRepo.process(event);                                // upsert by event id
  }
}

Explain without notes

01

Consumer dies mid-batch → explain rebalance, offset reset, and the duplicate that follows.

02

Broker dies → ISR, leader election, and the durability the producer asked for with acks=all.

Practice

01

Sketch the full chain with 3 partitions, 4 consumers (one idle) and offsets.

02

Design the retry/DLQ topic layout for an event that always fails validation.

Trade-offs

  • ↔

    Exactly-once is possible (transactions + idempotent producer) but the standard production answer is at-least-once + idempotent consumers.

Run it in production

Completion checklist

  • I can reproduce the full Kafka mental model and the four failure Q&A from the PDF (consumer dies, broker dies, ordering, duplicates).

Back to phase