Skip to content

Apache Kafka

TL;DR

  • What: a distributed, partitioned, replicated append-only commit log. Producers append records; consumers read at their own pace by tracking offsets.
  • Why an architect cares: it decouples producers from consumers in time and scale, and allows replay. It's the backbone for event-driven integration and streaming.
  • Verdict: a strong default for high-volume event streams and integration. Overkill as a simple task queue or for request/response.

Problem and forces

Point-to-point integrations (N×M connections), brokers that delete messages once consumed (no replay), and batch ETL latency. Kafka balances:

  • Throughput against ordering (ordering is guaranteed only within a partition)
  • Durability against latency (acks=all plus min.insync.replicas)
  • Retention / replay against storage cost
  • Consumer independence: every consumer group reads the full stream

Diagram

flowchart LR
  P1[Payments svc] -->|key = accountId| T
  P2[Cards svc] --> T
  subgraph T[Topic: payments.events]
    direction TB
    p0[Partition 0]
    p1[Partition 1]
    p2[Partition 2]
  end
  T --> G1[Consumer group: fraud]
  T --> G2[Consumer group: ledger]
  T --> G3[Consumer group: analytics]

Each consumer group gets the full stream. Within a group, each partition goes to exactly one consumer. Partitions are the unit of parallelism and of ordering.

Building blocks

Concept What it is Architectural role
Topic Named stream of records Contract boundary between producer and consumers
Partition Ordered, immutable log segment of a topic Unit of ordering, parallelism and scaling
Offset Position of a record in a partition Consumer state; enables replay
Consumer group Set of consumers sharing the work Load balancing; independent views of one stream
Replication (leader/ISR) Copies of each partition across brokers Durability and availability
KRaft Built-in Raft metadata quorum (ZooKeeper removed in 4.x) Simpler operations; control plane

Guarantees

  • Ordering: per partition only. Use a business key (e.g. accountId) as the record key so related events stay ordered.
  • Delivery: at-least-once by default. Exactly-once requires the idempotent producer plus transactions, and only holds within Kafka (read-process-write). External side effects still need idempotent consumers.
  • Durability: acks=all, replication.factor=3, min.insync.replicas=2.

Trade-offs

Attribute Impact Notes
Scalability + Horizontal via partitions; the partition count is hard to reduce later
Availability + Replication plus leader election
Latency ± Milliseconds, but batching trades latency for throughput
Consistency ± Ordered per partition; no cross-partition ordering
Operability − Partition planning, rebalances, consumer lag, schema evolution
Cost ± Cheap per message at scale; high fixed operating cost for small loads

Alternatives

Option Best at Weak at
RabbitMQ / IBM MQ Task queues, routing, per-message acknowledgement Replay, very high throughput
Cloud Pub/Sub, Event Hubs, Kinesis Managed, little to operate Portability; feature gaps
Apache Pulsar Multi-tenancy, tiered storage Smaller ecosystem
Database plus CDC Simple, transactional Not a general event backbone

Practitioner insights

  • Most "Kafka lost my message" incidents are producer misconfiguration (acks=1) or consumers committing the offset before processing.
  • Hot partitions from skewed keys quietly cap throughput. Check key cardinality early.
  • Use a transactional outbox to publish events consistently with a database write. Avoid dual writes.
  • A schema registry with compatibility rules is not optional at enterprise scale.
  • Kafka 4.x share groups add queue-like consumption (many consumers per partition). They are useful for work queues, but there is no ordering.

Takeaway

  • One-liner: a replayable, partitioned event log that decouples producers and consumers in time and scale.
  • Heuristic: if you need replay or many independent consumers, use Kafka. If you need per-message work distribution only, use a queue.
  • Review question: "What is the partition key, and what breaks if two events for the same key land out of order?"