Learning pathsPricingChecking sign-in…
Key Technologies

Kafka

Kafka is a partitioned durable log.

What, why, when and how

What is it?

Kafka is a partitioned retained event log. Producers append records and consumer groups maintain independent progress through offsets.

Why is it needed?

Multiple downstream systems can process and replay the same event history without sharing one progress cursor.

When should I use it?

Use it for retained streams, independent consumers and per-partition ordering when replay and operational capacity justify a log.

When should I choose something else?

A simple work queue can be easier for independent one-off jobs without retained replay. Kafka transactions do not include arbitrary external provider effects.

How do I use it?

Choose keys and partition count, configure durability and retention, define safe offset commits, and make external sink effects idempotent across replay.

Can you show a concrete example?

A worker applies event e42 to a database and crashes before committing its next offset. The replacement reads e42 again; an atomic event-ID check with the sink mutation prevents a second aggregate increment.

Apply event e42 to sink; commit is not recorded; Worker crashes; replacement reads e42 again; Atomic sink dedupe prevents a second increment
Kafka: one concrete example
Read the diagram

Apply event e42 to sink; commit is not recorded; Worker crashes; replacement reads e42 again; Atomic sink dedupe prevents a second increment

Open original
Kafka: worked example
Apply event e42 to sink; commit is not recorded; Worker crashes; replacement reads e42 again; Atomic sink dedupe prevents a second increment

Kafka: one concrete example

Follow the detailed diagrams below to trace the additional state changes and failure cases behind these answers.

Kafka is a partitioned durable log. Producers append records, and consumer groups track their progress through partition offsets. This model is useful when multiple systems need independent replay of the same events. Its guarantees depend on partitioning, acknowledgment, transaction, and consumer behavior; the word Kafka alone does not make every downstream effect exactly once.

Learning goals

Topics; partitions; keys; consumer groups; ordering scope; offsets; replay; retention; delivery semantics; external effects; skew.

Inspect offsets, assignments and acknowledgement scope

A topic has partition 0 records at offsets 40,41,42 and partition 1 at 10,11. Group A has two consumers and assigns one partition to each; group B can replay both independently. Adding a third consumer to group A does not create a third active partition reader under ordinary assignment. There is order within each partition, not one order across the topic.

Contract / pseudocode
group A, partition 0 committed next offset = 42
read record 42 -> apply sink using event_id=e42
crash before progress commit
replacement reads 42 -> sink detects prior e42
commit next offset = 43
group A, partition 0 committed next offset = 42; read record 42 -> apply sink using event_id=e42; crash before progress commit; replacement reads 42 -> sink detects prior e42; commit next offset = 43
Inspect offsets, assignments and acknowledgement scope
Read the diagram

group A, partition 0 committed next offset = 42; read record 42 -> apply sink using event_id=e42; crash before progress commit; replacement reads 42 -> sink detects prior e42; commit next offset = 43

Open original
Inspect offsets, assignments and acknowledgement scope
group A, partition 0 committed next offset = 42; read record 42 -> apply sink using event_id=e42; crash before progress commit; replacement reads 42 -> sink detects prior e42; commit next offset = 43

Inspect offsets, assignments and acknowledgement scope

The committed offset convention is the next record to consume. Distinguish it from the last record whose effect completed. Parallel processing must not commit beyond an unfinished earlier record if that would skip it after restart. Rebalance transfers partition ownership; bound processing and stop or fence stale work according to the sink contract.

The leader accepts appends and followers replicate them. Producer acknowledgements, in-sync replica membership and minimum replication settings affect the accepted-write loss boundary; replication factor alone is not enough. Idempotent production addresses supported producer retry duplicates. Transactions can couple supported Kafka operations, while an arbitrary external database or payment API remains outside that transaction.

Retention bounds replay. Compaction preserves an applicable latest keyed history under its policy, not every intermediate event forever; tombstones also have retention semantics. A hot partition key concentrates order and throughput; changing the key changes the ordering promise. Track time lag, retained horizon, replica health, skew and bounded retry/DLQ policies.

Show answer and explanation

Answer: Committing 43 before the effect of record 42 succeeds can lose that effect on restart. Committing afterward can replay it; event identity or a coordinated sink protocol protects the replay.

Primary documentation: Kafka design: delivery semantics, replication, log compaction and consumer position. Apply guarantees to the declared producer, broker and sink configuration rather than treating the product name as a guarantee.

Step-by-step reasoning

1. Choose ordering scope

A topic contains partitions. Records within a partition have an order, while different partitions progress independently. A key commonly determines partition placement. Use a conversation ID when per-conversation order matters, but recognize that one extremely active conversation can become a hot partition.

2. Understand consumer groups

Within a group, a partition is assigned to one consumer at a time under ordinary group consumption. More consumers than partitions do not add parallelism for that topic. A separate group can rebuild analytics without advancing the production group’s offsets. Rebalances change ownership and require careful handling of in-flight work.

3. Place the offset boundary

Committing an offset before an external database update risks losing the effect after a crash. Updating first and committing afterward risks replaying the effect. An idempotent sink or a transactionally coordinated offset-and-effect design resolves this boundary. Kafka transactions can coordinate supported Kafka reads and writes, but arbitrary external APIs need their own protection.

4. Operate retention and replay

Offsets identify log positions, not permanent storage of every event. Retention limits how far back a consumer can replay. Monitor lag in both offsets and time, disk usage, skew, and failure recovery. Schema evolution must keep old retained records readable by the replaying consumer.

Contracts and state

The following sketch makes the decision boundary concrete. Field names and capacity assumptions are illustrative; adapt them to the stated product contract.

Contract / pseudocode
Record: {event_id, campaign_id, occurred_at, schema_version}
Partition key: campaign_id (ordering vs hot-campaign trade-off)
Group A: production aggregates
Group B: rebuild-v2
Sink key: event_id or deterministic aggregate update identity

Worked example

An analytics worker writes a click aggregate and crashes before committing its offset. The replacement reads the same record again. If it simply increments a counter, the click counts twice. If the sink records event_id atomically with the increment, replay can recognize the prior effect. Retaining only a short dedupe window is safe only when it covers the promised replay and redelivery horizon.

Failure walkthrough

A consumer is slow enough that records expire before it reads them. Restarting from its old offset cannot recover data no longer retained. Decide whether to restore from an archive, rebuild from another source, or report an unrecoverable gap. Set retention using recovery objectives and expected outage duration, not just disk convenience.

Read offset 42 → Commit effect to external DB → Crash before offset commit → Offset 42 delivered again → Sink deduplicates event identity
Kafka · scenario walkthrough
Read the diagram

Read offset 42 → Commit effect to external DB → Crash before offset commit → Offset 42 delivered again → Sink deduplicates event identity

Open original
Kafka · scenario walkthrough
Read offset 42 → Commit effect to external DB → Crash before offset commit → Offset 42 delivered again → Sink deduplicates event identity

Kafka · scenario walkthrough

Decisions and trade-offs

ConceptMeaningCommon confusion
Partition orderOrder inside one log partitionGlobal order across topic
Consumer groupIndependent progress and assignmentsA separate copy of every record per worker
Offset commitRecorded read progressAtomic external side effect

Check your understanding

Why can a second consumer group rebuild a view without disturbing the live group?

Show answer and explanation

Answer: Each group tracks its own offsets, so replay progress is independent while retained records are shared. Both groups still compete for broker and sink resources; rate-limit rebuilds and ensure retention covers the needed history.

Transfer to a new scenario

Replay a click stream to rebuild aggregates while preserving a separate production consumer group.

Why does committing an offset not make an external database update exactly once?

Primary documentation

Read the first-party engineering account or official technical reference. Company engineering posts describe the scope and date of that publication; the interview reconstruction and scenarios here are original teaching examples.

Trace one event through log and consumer state

A topic partition is an ordered append log. Producers choose keys that determine which related events share order. A consumer group distributes partitions among consumers; ordering across different partitions is not one global sequence. Increasing consumer count beyond partition count does not create more active readers in that group for that topic.

Producer acknowledgement and replication settings determine when an append is considered accepted and what failures it can tolerate. Idempotent production addresses certain producer retry duplicates, while transactions can coordinate supported Kafka operations. Neither automatically makes an external email or database write atomic with an offset commit.

A consumer can commit an offset before processing, risking loss after a crash, or after processing, risking repetition. Protect the sink with idempotent writes or a transaction that couples the effect to a consumed identity. Retention is a finite replay window; consumer lag approaching retention is a data-loss risk for that consumer. Rebalancing requires safe ownership transfer and bounded processing times.

A decision worksheet for Kafka: read the mechanism and its guarantee together.
A decision worksheet for Kafka: read the mechanism and its guarantee together.
Read the diagram

Concept / Meaning / Common confusion; Partition order / Order inside one log partition / Global order across topic; Consumer group / Independent progress and assignments / A separate copy of every record per worker; Offset commit / Recorded read progress / Atomic external side effect

Open original
Kafka choices for this workload
A decision worksheet for Kafka: read the mechanism and its guarantee together.

A decision worksheet for Kafka: read the mechanism and its guarantee together.

Operational sketch

Contract / pseudocode
partition key = order_id
consume record at offset 420
apply sink effect using event_id
commit safe progress after effect
on replay: same event_id produces no second effect

A tempting mistake

A hot key can bottleneck one partition. Randomly salting it improves distribution only if the application no longer requires a single order or adds a later merge step.

Transfer exercise

Can “exactly-once Kafka” guarantee one SMS?

Show answer and explanation

Answer: No. The SMS provider is outside the Kafka transaction. Use provider idempotency where supported and reconciliation for uncertain outcomes.

Your study notes