Apache Kafka Fundamentals

Use Kafka as a durable event log: topics, partitions, offsets, producers, consumers and consumer groups, replication and retention.

Intermediate · about 8 hours · difficulty 3/5

Learn first: Stream Processing Concepts, Docker for Data Engineers

Apache Kafka is the backbone of real-time data at companies like Flipkart, Swiggy, Paytm and most large banks. It is a distributed, durable, append-only log that many producers write to and many consumers read from independently.

Core concepts

  • Topic: a named stream of records (orders, payments).
  • Partition: a topic is split into ordered partitions for parallelism. Ordering is guaranteed only within a partition.
  • Key: records with the same key (such as order_id) go to the same partition, keeping their order.
  • Offset: each record's position in a partition. Consumers track offsets to know where they are.
  • Broker: a Kafka server; a cluster has several. Newer Kafka uses KRaft instead of ZooKeeper.
  • Replication factor: copies of each partition across brokers for durability.
  • Retention: records are kept for a time or size limit, not deleted when read. Log compaction keeps the latest value per key.

Producers and consumers

  • Producers choose keys, batching, compression and acks (acks=all for durability). Idempotent producers avoid duplicates on retry.
  • Consumer groups share partitions among members for scaling; each partition goes to one consumer in a group. Adding consumers beyond the partition count does nothing.
  • Committing offsets after processing gives at-least-once; committing before gives at-most-once.

Running it

Run Kafka locally with Docker, or try managed services (Confluent Cloud free tier, Amazon MSK, Redpanda as a Kafka-compatible alternative). Use the CLI tools to create topics, produce and consume.

Pitfalls

  • Too few partitions limiting throughput forever (increasing later breaks key ordering).
  • Hot partitions from a skewed key.
  • Consumer lag growing unnoticed; always monitor lag.

You are ready to move on when you can run Kafka in Docker, produce keyed order events from Python, consume them with a two-member consumer group, and explain which consumer received which partitions and why.

Key concepts

  • Topics and partitions
  • Keys and ordering
  • Offsets and commits
  • Consumer groups
  • Brokers, replication, KRaft
  • Retention and log compaction
  • Producer acks and idempotence
  • Consumer lag monitoring
  • Managed Kafka and Redpanda

Mini project

Run Kafka in Docker Compose. Write a Python producer that emits simulated UPI payment events keyed by user_id, and a consumer group of two consumers that aggregates totals per user. Kill one consumer and observe the rebalance and lag.

Check yourself

A topic has 6 partitions and your consumer group has 10 consumers. How many are idle, and how would you increase throughput?

Free resources