📡 You're offline — showing cached content
New version available!
Quick Access
Tutorials Microservices CQRS

CQRS

5 min read
CQRS separates read and write models — each can use the optimal DB and scale independently.

CQRS — Command Query Responsibility Segregation

# Separate read and write models
# Write side: enforce business rules
class PlaceOrderHandler:
    def handle(self, cmd: PlaceOrderCommand) -> str:
        order = Order.create(cmd.user_id, cmd.items)
        db.save(order)
        event_bus.publish("OrderPlaced", order.to_dict())
        return order.id

# Read side: optimised denormalised views
class OrderSummaryProjection:
    def on_order_placed(self, event: dict):
        read_db.upsert("order_summaries", {
            "order_id":  event["order_id"],
            "user_name": lookup_user(event["user_id"]),
            "total":     event["total"]
        })

# Benefits: read/write scale independently
# Trade-off: eventual consistency between read and write sides