Apache Kafka enables decoupled, scalable and resilient communication between microservices through event streaming.
In a traditional REST architecture, services call each other directly. This creates tight coupling: if the order service is down, the payment service fails too. Event-driven architecture breaks this dependency. Services emit events to Kafka topics and consume them independently.
Kafka organises messages in topics. Producers write events, consumers read them. A consumer group ensures each message is processed once per group. Kafka retains messages for a configurable duration, enabling replay and fault recovery.
In Mboka Market, when a buyer confirms an order, the order service emits an `order.created` event to Kafka. The payment service, notification service and inventory service each consume this event independently. No service blocks another.
Producing an event from a Spring Boot service:
@Service
public class OrderEventProducer {
@Autowired
private KafkaTemplate<String, OrderEvent> kafkaTemplate;
public void sendOrderCreated(OrderEvent event) {
kafkaTemplate.send("order.created", event.getOrderId(), event);
}
}