AlgoViz

Apache Kafka

medium 34 min

Apache Kafka is a distributed log for events.

When it comes up

The moments this idea belongs in a design interview, and the moments it does not.

Signals

When to bring it up

You hearSayBecause
Several teams need every order eventA topic, one group per teamWriters stay unaware of readers
Rebuild the index, or fix a bug in last week's dataReplay from a positionThe log keeps records
Millions of clicks a minutePartitions by user or sessionAppends and parallel readers
Keep the search index in step with the databaseChange data capture into a compacted topicLatest value per row
Send a receipt e-mailA simple queue is enoughOne reader, no replay
Show the price nowNot Kafka: a call or a cacheThe caller waits

In the interview

  • Name the key and the order it gives
  • Say the copies and the minimum
  • Say where duplicates are handled
  • Give a partition count with its sum

Avoid

  • "Kafka is exactly once" with no scope
  • More partitions as a cure for a hot key
  • Kafka between every two services
  • Quoting millions a second without sizes

Options

Explain your choices

Order events feed billing, search and analytics. How do you publish them?

BreaksCall the three services after saving the order
How
Three HTTP calls in the order handler.
Limit
One slow service delays the order, and a crash between calls leaves them disagreeing.
Choose when
Never for three, perhaps for one that must answer.
WorksSave the order, then send to Kafka
How
Write the row, then produce with acks=all.
Limit
A crash between the two loses the event: two systems, no shared transaction.
Choose when
A rare loss is acceptable and repaired by a nightly check.
StrongOutbox or change data capture, then Kafka
How
Write the row and the event in one database transaction. A connector publishes it, keyed by order id. Each group applies events idempotently.
Limit
One more moving part, and a small delay.
Choose when
Orders, payments, stock: whenever the two must agree.

How safe should the writes be?

BreaksThree copies, acks=1
How
Answer after the leader's append.
Limit
A leader that dies at the wrong moment takes acknowledged records with it.
Choose when
Data that may have gaps.
WorksThree copies, acks=all, minimum 1
How
The default minimum.
Limit
If followers fall behind, one copy counts as all.
Choose when
Only if staying writable matters more.
StrongThree copies, acks=all, minimum 2
How
Copies in three zones.
Limit
With one copy left writes stop, so producers need a buffer or a clear error.
Choose when
The usual choice for business events.