Sobes.tech
Senior

Tell me about Kafka: its composition, how you work with it, main components.

sobes.tech AI

Answer from AI

Apache Kafka is a distributed data streaming platform designed for processing large volumes of messages in real-time.

Main components:

  • Producer — an application that publishes (sends) messages to Kafka.
  • Broker — a Kafka server that stores and forwards messages.
  • Topic — a category or channel where messages are published.
  • Partition — a section of a topic that allows for scaling and parallel processing.
  • Consumer — an application that reads messages from Kafka.
  • Consumer Group — a group of consumers that ensures load balancing.
  • Zookeeper (in older versions) — a service for coordinating and managing the cluster.

How Kafka works:

  1. The Producer sends messages to a specific topic.
  2. Messages are written to partitions on brokers, in order and with offset preservation.
  3. Consumers read messages, tracking the offset, which allows them to process data sequentially and reliably.

Kafka provides high performance, fault tolerance, and scalability, making it popular for logging systems, monitoring, event processing, and microservices integration.

Example of a simple Producer in Java:

Properties props = new Properties();
props.put("bootstrap.servers", "localhost:9092");
props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");

KafkaProducer<String, String> producer = new KafkaProducer<>(props);
producer.send(new ProducerRecord<>("my-topic", "key1", "value1"));
producer.close();