Interview question
What is Apache Kafka and how does Spring Boot integrate with it? Apache Kafka क्या है और Spring Boot इससे कैसे integrate करता है?
Answer
Apache Kafka is a distributed event streaming platform for building real-time data pipelines. Spring Boot integrates via Spring Cloud Stream or spring-kafka, enabling producer/consumer patterns for high-throughput, fault-tolerant message processing.
// Maven dependency
// spring-kafka or spring-cloud-stream-kafka
// Kafka Producer
@Service
public class KafkaProducer {
@Autowired
private KafkaTemplate<String, String> kafkaTemplate;
public void sendMessage(String topic, String message) {
kafkaTemplate.send(topic, message)
.addCallback(
result -> System.out.println('Sent: ' + message),
ex -> System.err.println('Error: ' + ex.getMessage())
);
}
}
// Kafka Consumer
@Service
public class KafkaConsumer {
@KafkaListener(topics = 'orders', groupId = 'order-service')
public void consumeMessage(String message) {
System.out.println('Received: ' + message);
// Process message
}
}
// With Spring Cloud Stream
@Configuration
public class KafkaStreamConfig {
@Bean
public Function<String, String> process() {
return input -> {
System.out.println('Processing: ' + input);
return input.toUpperCase();
};
}
}
// Kafka concepts:
// Topic - category of events
// Partition - parallel processing unit
// Consumer Group - multiple consumers for same topic
// Offset - position in partition
// Replication - fault tolerance
// Key features:
// 1. High throughput - millions of messages/sec
// 2. Durability - persisted to disk
// 3. Scalability - add brokers dynamically
// 4. Stream processing - Kafka Streams API
// 5. Exactly-once semantics - no duplicate processingApache Kafka:
Kafka Producer:
kafkaTemplate.send(topic, message);
Kafka Consumer:
@KafkaListener(topics = 'name', groupId = 'id')
public void consume(String msg) { }
Key Concepts:
1. Topic - event categories
2. Partition - parallelism
3. Consumer Group - multiple consumers
4. Offset - message position
5. Replication - fault tolerance
Features:
- High throughput
- Persistent storage
- Distributed
- Stream processing
- Exactly-once deliveryWas this answer clear?