Design and implement event-driven cloud architectures with Kafka topics, Dapr pub/sub, and scalable microservice patterns. Use when designing distributed systems, implementing async messaging, creating event-based workflows, or building loosely coupled services. Covers fault-tolerant, cloud-portable architectures.
Design scalable, loosely coupled event-driven architectures for cloud-native applications.
┌─────────────────────────────────────────────────────────────┐
│ Services │
│ ┌─────────┐ ┌─────────┐ ┌─────────┐ ┌─────────┐ │
│ │ Todo │ │ User │ │ Notify │ │Analytics│ │
│ │ Service │ │ Service │ │ Service │ │ Service │ │
│ └────┬────┘ └────┬────┘ └────┬────┘ └────┬────┘ │
│ │ │ │ │ │
│ └────────────┴────────────┴────────────┘ │
│ │ │
│ ┌────┴────┐ │
│ │ Dapr │ ← Cloud-agnostic abstraction│
│ │ Sidecar │ │
│ └────┬────┘ │
└─────────────────────────┼───────────────────────────────────┘
│
┌─────┴─────┐
│Event Bus │ ← Kafka / Redis / Cloud PubSub
└───────────┘
<domain>.<entity>.<action>
Examples:
todos.task.created
todos.task.completed
users.account.registered
{
"specversion": "1.0",
"id": "uuid",
"source": "/todos/api",
"type": "todos.task.created",
"time": "2024-01-15T10:30:00Z",
"data": {
"todoId": "123",
"userId": "456",
"title": "Buy groceries"
}
}
User creates todo
│
▼
┌──────────────┐ todos.task.created
│ Todo Service │─────────────────────────┐
└──────────────┘ │
▼
┌────────────────────────────────────────┐
│ Event Bus │
└────────────────────────────────────────┘
│ │
▼ ▼
┌──────────────┐ ┌──────────────┐
│ Notification │ │ Analytics │
│ Service │ │ Service │
└──────────────┘ └──────────────┘
from dapr.clients import DaprClient
async def publish_todo_created(todo: Todo):
with DaprClient() as client:
client.publish_event(
pubsub_name="pubsub",
topic_name="todos.task.created",
data=json.dumps({"todoId": str(todo.id), "title": todo.title})
)
@app.get("/dapr/subscribe")
async def subscribe():
return [
{"pubsubname": "pubsub", "topic": "todos.task.created", "route": "/events/todo-created"}
]
@app.post("/events/todo-created")
async def handle_todo_created(request: Request):
event = await request.json()
await send_notification(event["data"]["userId"], "Todo created!")
return {"status": "SUCCESS"}
# components/pubsub.yaml
apiVersion: dapr.io/v1alpha1