Kafka Consumer is a crucial component of the Kafka ecosystem, responsible for reading and processing messages that are produced and stored by Kafka producers in brokers.

Notes

Consumers are the engine that drives data consumption in Kafka, enabling real-time analytics and stream processing.

Consumers are designed to efficiently read streams of records from Kafka topics. They maintain their position within each topic-partition offset and can consume records independently or as part of a consumer group for scalable message processing.

flowchart LR
    Producer --> |sends msg to| Broker
    subgraph "Broker"
        direction LR
        TopicA -->|redirects to| PA1(PartitionA1)
        TopicA -->|redirects to| PA2(PartitionA2)
        TopicB -->|redirects to| PB1(PartitionB1)
        TopicB -->|redirects to| PB2(PartitionB2)
        TopicB -->|redirects to| PB3(PartitionB3)
    end
    Broker --> |is read by| CG
    subgraph CG["Consumer Group"]
	    direction LR
	    Consumer1
	    Consumer2
	    Consumer3
	    style Consumer1 fill:#998877,stroke:#333,stroke-width:2px;
	    style Consumer2 fill:#998877,stroke:#333,stroke-width:2px;
	    style Consumer3 fill:#998877,stroke:#333,stroke-width:2px;
    end

TakeAways

  • 📌 Kafka Consumers are responsible for reading records from Kafka topics.
  • 💡 Consumers maintain offsets to track their position in topic partitions and ensure efficient data retrieval.
  • 🔍 By joining a consumer group, multiple consumers can collaboratively process messages, enhancing scalability and fault tolerance.

Process

  • 📄 Data Retrieval: Consumers read records from the Kafka Broker.
  • 🔒 Offset Management: The Consumer maintains offsets to track their position within each topic-partition.
  • 🖥 Group Coordination: If part of a consumer group, the coordinator assigns partitions to consumers for efficient and scalable message processing.
  • 🔄 Rebalancing: When new consumers join or existing ones leave, the consumer group rebalances partitions among members to ensure load distribution.

Thoughts

  • HighAvailability: Consumers within a group can handle failures gracefully by redistributing partitions.
  • Scalability: By joining a consumer group, Kafka supports ↔️ Horizontal scaling of message processing.
  • FaultTolerance: Offsets are periodically committed to ensure that no messages are lost or reprocessed in case of a failure.

References

  1. 🐦 Kafka
  2. Apache Kafka Consumer Official Documentation