Prince of Code </>

Back to blog
·6 min read

Kafka: building event-driven architectures

Apache Kafka enables decoupled, scalable and resilient communication between microservices through event streaming.

KafkaJavaFull Stack

Why event-driven?

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 concepts in brief

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.

Use case: marketplace order flow

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.

Spring Boot Kafka producer example

Producing an event from a Spring Boot service:

java
@Service
public class OrderEventProducer {

    @Autowired
    private KafkaTemplate<String, OrderEvent> kafkaTemplate;

    public void sendOrderCreated(OrderEvent event) {
        kafkaTemplate.send("order.created", event.getOrderId(), event);
    }
}