Events represent past facts — publish structured events from services and consume them asynchronously.
Event-Driven Architecture
# Events represent things that happened (past tense)
# UserRegistered, OrderPlaced, PaymentFailed
# Event envelope
{
"event_id": "uuid-abc-123",
"event_type": "OrderPlaced",
"timestamp": "2025-01-15T12:00:00Z",
"data": {"order_id":42,"total":99.99}
}
# Python Kafka publisher
from confluent_kafka import Producer
import json, uuid
producer = Producer({"bootstrap.servers":"kafka:9092"})
def publish(event_type: str, data: dict):
event = {
"event_id": str(uuid.uuid4()),
"event_type": event_type,
"data": data
}
producer.produce("events", json.dumps(event).encode())
producer.flush()