Apache Kafka
medium 34 minApache 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 hear | Say | Because |
|---|---|---|
| Several teams need every order event | A topic, one group per team | Writers stay unaware of readers |
| Rebuild the index, or fix a bug in last week's data | Replay from a position | The log keeps records |
| Millions of clicks a minute | Partitions by user or session | Appends and parallel readers |
| Keep the search index in step with the database | Change data capture into a compacted topic | Latest value per row |
| Send a receipt e-mail | A simple queue is enough | One reader, no replay |
| Show the price now | Not Kafka: a call or a cache | The 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.