Junior — Middle
How well do you understand Apache Kafka and can you explain its main principles of operation?
sobes.tech AI
Answer from AI
Apache Kafka is a distributed data streaming platform used for building real-time event processing systems. The main principles of Kafka operation:
- Topics — logical channels where messages are published.
- Producers — applications that send messages to topics.
- Consumers — applications that read messages from topics.
- Brokers — servers that store and transmit messages.
- Partitions — topics are divided into partitions for scalability and parallel processing.
- Offsets — unique identifiers for messages within a partition, allowing consumers to track which messages they have already read.
Kafka provides high performance, fault tolerance, and scalability through data replication and a distributed architecture.
Example of a simple producer in Java using Kafka Producer API:
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", "Hello Kafka"));
producer.close();