Architecture

Implementing Event-Driven Architecture with Kafka

By Mohd Baquir Qureshi
Server cluster wiring

In a traditional synchronous architecture, when a user places an order, the Order Service makes a blocking HTTP call to the Payment Service, then another to the Inventory Service, and finally to the Notification Service. If the Email API is down, the entire checkout process fails. This is a fragile anti-pattern in distributed systems.

The Event-Driven Paradigm

In an Event-Driven Architecture (EDA) powered by Apache Kafka, services do not talk to each other directly. Instead, they broadcast events to a central immutable log.

When an order is placed, the Order Service simply writes an OrderCreated event to a Kafka topic and immediately returns a 202 Accepted to the client.

Publishing and Subscribing

Other microservices subscribe to that topic and process the event at their own pace.

# Publishing an Event (Python with confluent-kafka)
from confluent_kafka import Producer
import json

p = Producer({'bootstrap.servers': 'localhost:9092'})

def create_order(order_data):
    # Save to local DB first (Outbox pattern recommended)
    db.save(order_data)
    
    # Broadcast the event
    event = {"event_type": "OrderCreated", "payload": order_data}
    p.produce('order_events', key=str(order_data['id']), value=json.dumps(event))
    p.flush()

Handling Eventual Consistency

The primary trade-off of EDA is shifting from strong ACID compliance to Eventual Consistency. You can no longer wrap operations across multiple microservices in a single SQL transaction. If the Payment Service successfully processes the card but the Inventory Service realizes the item is out of stock, you must implement compensating transactions (the Saga Pattern) to publish an OrderCancelled and PaymentRefunded event.

Conclusion

Kafka provides incredible fault tolerance. If the Notification Service crashes for two hours, the messages simply queue up in Kafka. When the service reboots, it picks up exactly where it left off. Decoupling your services via events is the true path to massive horizontal scalability.