🏗️ Architecture

CQRS

Command Query Responsibility Segregation

An architectural pattern that separates the command (write) and query (read) models. In complex domains, read and write requirements can be optimized independently, and it is often used together with Event Sourcing.

📖 Detailed Explanation

The core concept of CQRS evolved from Bertrand Meyer's CQS (Command Query Separation) principle. CQS is the principle that a method should do only one of two things: either change state (Command) or return a value (Query). CQRS extends this to the architectural level, completely separating the read and write models.

A Command is an operation that changes the state of the system. Examples include "create order," "change product quantity," and "process payment." A Command returns void or only a success/failure result. The Command Handler performs the business logic and updates the domain model.

A Query retrieves data without changing the system state. Examples include "retrieve order list" and "retrieve product details." A Query uses a separate Read Model optimized for reading. It can use denormalized views in which complex joins or aggregation operations have been precomputed.

Synchronization of the two models is mainly done through events. When a Command is processed in the Write Model, a domain event is published, and the Read Model is updated by subscribing to this event. As a result, Eventual Consistency occurs between reads and writes.

The advantages of CQRS are that read/write workloads can be scaled independently, complex queries can be optimized, and the domain logic becomes clearer. Its disadvantages include increased system complexity, design difficulty due to eventual consistency, and higher infrastructure costs.

💻 Code Example

# CQRS 패턴 구현 예제 - Python
from abc import ABC, abstractmethod
from dataclasses import dataclass, field
from typing import List, Optional, Dict, Any
from datetime import datetime
from uuid import uuid4
import asyncio

# ============================================
# Commands (명령) - 상태를 변경하는 작업
# ============================================
@dataclass
class Command(ABC):
    """명령 기본 클래스"""
    command_id: str = field(default_factory=lambda: str(uuid4()))
    timestamp: datetime = field(default_factory=datetime.now)


@dataclass
class CreateOrderCommand(Command):
    """주문 생성 명령"""
    user_id: str
    items: List[Dict[str, Any]]
    shipping_address: str


@dataclass
class UpdateOrderStatusCommand(Command):
    """주문 상태 변경 명령"""
    order_id: str
    new_status: str


# ============================================
# Command Handlers (명령 핸들러)
# ============================================
class CommandHandler(ABC):
    @abstractmethod
    async def handle(self, command: Command) -> Any:
        pass


class CreateOrderCommandHandler(CommandHandler):
    def __init__(self, order_repository, event_bus):
        self.order_repository = order_repository
        self.event_bus = event_bus

    async def handle(self, command: CreateOrderCommand) -> str:
        """주문 생성 - Write Model에 저장"""
        # 1. 도메인 로직 수행
        order_id = str(uuid4())
        total_amount = sum(item["price"] * item["quantity"] for item in command.items)

        order = Order(
            id=order_id,
            user_id=command.user_id,
            items=command.items,
            total_amount=total_amount,
            status="PENDING",
            shipping_address=command.shipping_address,
            created_at=datetime.now()
        )

        # 2. Write Model(DB)에 저장
        await self.order_repository.save(order)

        # 3. 도메인 이벤트 발행 (Read Model 업데이트용)
        event = OrderCreatedEvent(
            order_id=order_id,
            user_id=command.user_id,
            total_amount=total_amount,
            status="PENDING"
        )
        await self.event_bus.publish(event)

        return order_id


# ============================================
# Queries (조회) - 데이터를 읽는 작업
# ============================================
@dataclass
class Query(ABC):
    """조회 기본 클래스"""
    pass


@dataclass
class GetOrderByIdQuery(Query):
    """주문 단건 조회"""
    order_id: str


@dataclass
class GetOrdersByUserQuery(Query):
    """사용자별 주문 목록 조회"""
    user_id: str
    page: int = 1
    page_size: int = 20


# ============================================
# Query Handlers (조회 핸들러)
# ============================================
class QueryHandler(ABC):
    @abstractmethod
    async def handle(self, query: Query) -> Any:
        pass


class GetOrdersByUserQueryHandler(QueryHandler):
    def __init__(self, read_model_repository):
        # Read Model 전용 저장소 (비정규화된 뷰)
        self.read_model = read_model_repository

    async def handle(self, query: GetOrdersByUserQuery) -> List[Dict]:
        """
        Read Model에서 조회 - 읽기에 최적화된 비정규화 데이터
        복잡한 조인 없이 바로 반환 가능
        """
        orders = await self.read_model.find_by_user(
            user_id=query.user_id,
            skip=(query.page - 1) * query.page_size,
            limit=query.page_size
        )
        return orders


# ============================================
# Events (이벤트) - Write→Read 동기화
# ============================================
@dataclass
class DomainEvent:
    event_id: str = field(default_factory=lambda: str(uuid4()))
    timestamp: datetime = field(default_factory=datetime.now)


@dataclass
class OrderCreatedEvent(DomainEvent):
    order_id: str = ""
    user_id: str = ""
    total_amount: float = 0.0
    status: str = ""


# Event Handler - Read Model 업데이트
class OrderCreatedEventHandler:
    def __init__(self, read_model_repository):
        self.read_model = read_model_repository

    async def handle(self, event: OrderCreatedEvent):
        """이벤트를 받아 Read Model 업데이트"""
        # 읽기에 최적화된 형태로 저장
        read_model_data = {
            "order_id": event.order_id,
            "user_id": event.user_id,
            "total_amount": event.total_amount,
            "status": event.status,
            "status_display": "주문 접수",  # 미리 계산
            "created_at": event.timestamp.isoformat()
        }
        await self.read_model.upsert(event.order_id, read_model_data)


# ============================================
# CQRS Mediator (명령/조회 라우터)
# ============================================
class CQRSMediator:
    """명령과 조회를 적절한 핸들러로 라우팅"""

    def __init__(self):
        self.command_handlers: Dict[type, CommandHandler] = {}
        self.query_handlers: Dict[type, QueryHandler] = {}

    def register_command(self, command_type: type, handler: CommandHandler):
        self.command_handlers[command_type] = handler

    def register_query(self, query_type: type, handler: QueryHandler):
        self.query_handlers[query_type] = handler

    async def send_command(self, command: Command) -> Any:
        """명령 전송 - Write 작업"""
        handler = self.command_handlers.get(type(command))
        if not handler:
            raise ValueError(f"No handler for command: {type(command)}")
        return await handler.handle(command)

    async def send_query(self, query: Query) -> Any:
        """조회 전송 - Read 작업"""
        handler = self.query_handlers.get(type(query))
        if not handler:
            raise ValueError(f"No handler for query: {type(query)}")
        return await handler.handle(query)


# ============================================
# 사용 예시
# ============================================
async def main():
    mediator = CQRSMediator()

    # 핸들러 등록 (의존성 주입)
    # mediator.register_command(CreateOrderCommand, CreateOrderCommandHandler(...))
    # mediator.register_query(GetOrdersByUserQuery, GetOrdersByUserQueryHandler(...))

    # Command: 주문 생성 (Write)
    command = CreateOrderCommand(
        user_id="user-123",
        items=[{"product_id": "prod-1", "price": 10000, "quantity": 2}],
        shipping_address="서울시 강남구"
    )
    order_id = await mediator.send_command(command)

    # Query: 주문 목록 조회 (Read)
    query = GetOrdersByUserQuery(user_id="user-123", page=1)
    orders = await mediator.send_query(query)
// CQRS 패턴 구현 예제 - Node.js/TypeScript
const { v4: uuidv4 } = require('uuid');

// ============================================
// Commands (명령)
// ============================================
class CreateOrderCommand {
    constructor(userId, items, shippingAddress) {
        this.commandId = uuidv4();
        this.timestamp = new Date();
        this.userId = userId;
        this.items = items;
        this.shippingAddress = shippingAddress;
    }
}

class CancelOrderCommand {
    constructor(orderId, reason) {
        this.commandId = uuidv4();
        this.orderId = orderId;
        this.reason = reason;
    }
}

// ============================================
// Command Handlers
// ============================================
class CreateOrderCommandHandler {
    constructor(writeRepository, eventBus) {
        this.writeRepository = writeRepository;
        this.eventBus = eventBus;
    }

    async handle(command) {
        // 1. 비즈니스 검증
        const totalAmount = command.items.reduce(
            (sum, item) => sum + item.price * item.quantity, 0
        );

        // 2. Write Model (정규화된 도메인 모델) 저장
        const order = {
            id: uuidv4(),
            userId: command.userId,
            items: command.items,
            totalAmount,
            status: 'PENDING',
            shippingAddress: command.shippingAddress,
            createdAt: new Date(),
            version: 1  // Optimistic Locking
        };

        await this.writeRepository.save(order);

        // 3. 도메인 이벤트 발행 → Read Model 업데이트
        await this.eventBus.publish('OrderCreated', {
            orderId: order.id,
            userId: order.userId,
            totalAmount: order.totalAmount,
            status: order.status,
            itemCount: command.items.length
        });

        return { orderId: order.id };
    }
}

// ============================================
// Queries (조회)
// ============================================
class GetOrderSummaryQuery {
    constructor(orderId) {
        this.orderId = orderId;
    }
}

class GetUserOrderHistoryQuery {
    constructor(userId, options = {}) {
        this.userId = userId;
        this.page = options.page || 1;
        this.pageSize = options.pageSize || 10;
        this.statusFilter = options.statusFilter;
    }
}

// ============================================
// Query Handlers (Read Model 사용)
// ============================================
class GetUserOrderHistoryQueryHandler {
    constructor(readModelRepository) {
        // Read Model: 비정규화된 조회 전용 저장소
        this.readModel = readModelRepository;
    }

    async handle(query) {
        /**
         * Read Model은 이미 비정규화되어 있어
         * 복잡한 JOIN 없이 빠르게 조회 가능
         */
        const result = await this.readModel.aggregate([
            { $match: { userId: query.userId } },
            { $sort: { createdAt: -1 } },
            { $skip: (query.page - 1) * query.pageSize },
            { $limit: query.pageSize },
            {
                $project: {
                    orderId: 1,
                    totalAmount: 1,
                    status: 1,
                    statusDisplay: 1,  // 미리 계산된 한글 상태
                    itemCount: 1,      // 미리 계산된 상품 수
                    createdAt: 1
                }
            }
        ]);

        return {
            orders: result,
            page: query.page,
            pageSize: query.pageSize
        };
    }
}

// ============================================
// Event Handler (Write → Read 동기화)
// ============================================
class OrderProjectionHandler {
    constructor(readModelRepository) {
        this.readModel = readModelRepository;
    }

    // 이벤트를 받아 Read Model 업데이트
    async handleOrderCreated(event) {
        const statusMap = {
            'PENDING': '주문 접수',
            'PAID': '결제 완료',
            'SHIPPED': '배송 중',
            'DELIVERED': '배송 완료'
        };

        // 읽기에 최적화된 비정규화 데이터 저장
        await this.readModel.insertOne({
            orderId: event.orderId,
            userId: event.userId,
            totalAmount: event.totalAmount,
            status: event.status,
            statusDisplay: statusMap[event.status],
            itemCount: event.itemCount,
            createdAt: new Date(),
            // 조회 시 자주 필요한 파생 데이터 미리 계산
            formattedAmount: `${event.totalAmount.toLocaleString()}원`,
            isRecent: true
        });
    }

    async handleOrderStatusChanged(event) {
        await this.readModel.updateOne(
            { orderId: event.orderId },
            {
                $set: {
                    status: event.newStatus,
                    statusDisplay: this.getStatusDisplay(event.newStatus),
                    updatedAt: new Date()
                }
            }
        );
    }
}

// ============================================
// CQRS Dispatcher
// ============================================
class CQRSDispatcher {
    constructor() {
        this.commandHandlers = new Map();
        this.queryHandlers = new Map();
    }

    registerCommand(CommandClass, handler) {
        this.commandHandlers.set(CommandClass.name, handler);
    }

    registerQuery(QueryClass, handler) {
        this.queryHandlers.set(QueryClass.name, handler);
    }

    async dispatch(commandOrQuery) {
        const name = commandOrQuery.constructor.name;

        if (name.includes('Command')) {
            const handler = this.commandHandlers.get(name);
            if (!handler) throw new Error(`No handler for ${name}`);
            return await handler.handle(commandOrQuery);
        }

        if (name.includes('Query')) {
            const handler = this.queryHandlers.get(name);
            if (!handler) throw new Error(`No handler for ${name}`);
            return await handler.handle(commandOrQuery);
        }

        throw new Error(`Unknown type: ${name}`);
    }
}

// ============================================
// Express API 예시
// ============================================
const express = require('express');
const app = express();

app.post('/orders', async (req, res) => {
    // Command (Write) - POST, PUT, DELETE
    const command = new CreateOrderCommand(
        req.body.userId,
        req.body.items,
        req.body.shippingAddress
    );
    const result = await dispatcher.dispatch(command);
    res.status(201).json(result);
});

app.get('/orders', async (req, res) => {
    // Query (Read) - GET
    const query = new GetUserOrderHistoryQuery(req.query.userId, {
        page: parseInt(req.query.page) || 1
    });
    const result = await dispatcher.dispatch(query);
    res.json(result);
});
┌─────────────────────────────────────────────────────────────────────────┐
│                          CQRS Architecture                               │
└─────────────────────────────────────────────────────────────────────────┘

                              ┌─────────────┐
                              │   Client    │
                              └──────┬──────┘
                                     │
                    ┌────────────────┴────────────────┐
                    │                                  │
             ┌──────▼──────┐                   ┌──────▼──────┐
             │   Command   │                   │    Query    │
             │   (Write)   │                   │   (Read)    │
             │ POST/PUT/DEL│                   │     GET     │
             └──────┬──────┘                   └──────┬──────┘
                    │                                  │
             ┌──────▼──────┐                   ┌──────▼──────┐
             │   Command   │                   │    Query    │
             │   Handler   │                   │   Handler   │
             │ (비즈니스    │                   │ (조회 최적화)│
             │   로직)     │                   │             │
             └──────┬──────┘                   └──────┬──────┘
                    │                                  │
             ┌──────▼──────┐                   ┌──────▼──────┐
             │ Write Model │                   │ Read Model  │
             │ (정규화 DB) │                   │(비정규화 뷰)│
             │             │                   │             │
             │ PostgreSQL  │                   │  MongoDB /  │
             │   MySQL     │                   │   Redis     │
             └──────┬──────┘                   └──────▲──────┘
                    │                                  │
                    │         ┌─────────────┐         │
                    └────────►│   Event     │─────────┘
                              │    Bus      │
                              │  (Kafka)    │
                              └─────────────┘

═══════════════════════════════════════════════════════════════════════════

데이터 흐름:

1. Command Flow (쓰기)
   Client → Command → Handler → Write Model → Event 발행

2. Event Flow (동기화)
   Event Bus → Event Handler → Read Model 업데이트

3. Query Flow (읽기)
   Client → Query → Handler → Read Model → 결과 반환

═══════════════════════════════════════════════════════════════════════════

Write Model (정규화)              Read Model (비정규화)
┌─────────────────────┐          ┌─────────────────────────────┐
│ orders              │          │ order_summaries             │
├─────────────────────┤          ├─────────────────────────────┤
│ id                  │          │ order_id                    │
│ user_id (FK)        │  ───►    │ user_name (복사됨)          │
│ status              │  Event   │ status                      │
│ created_at          │          │ status_display (계산됨)     │
│ shipping_address_id │          │ total_amount                │
└─────────────────────┘          │ formatted_amount (계산됨)   │
                                 │ item_count (계산됨)         │
┌─────────────────────┐          │ shipping_address (복사됨)   │
│ order_items         │          │ created_at                  │
├─────────────────────┤          └─────────────────────────────┘
│ id                  │
│ order_id (FK)       │          장점: 조인 없이 즉시 반환
│ product_id (FK)     │          단점: 데이터 중복, 동기화 지연
│ quantity            │
│ price               │
└─────────────────────┘

🗣️ Say It Like This in Practice

💬 In an architecture design discussion
"The order retrieval API is too slow. It joins 5 tables every time. If we introduce CQRS and create a denormalized view for queries, we can return results directly without joins and significantly reduce response time."
💬 In a data consistency discussion
"In CQRS, eventual consistency applies between the Write and Read Models. An order may not appear in the list right after it is created. If this is a problem for the business, there is also the option of querying directly from the Write Model right after creation."
💬 In a tech stack decision meeting
"Transactions are important for the Write Model, so I'd suggest using PostgreSQL, and since the Read Model is meant for fast queries, using Redis or Elasticsearch would be good. Synchronizing events with Kafka would be stable."

⚠️ Cautions & Best Practices

❌
Over-applying it to simple CRUD

CQRS is over-engineering for a simple CRUD application. Consider adopting it only for complex domains where read/write requirements differ significantly.

❌
Ignoring eventual consistency

The Read Model is not synchronized with the Write Model immediately. Explain to users the issue of "I created it but it's not in the list" in the UI, or handle only the period right after creation as an exception.

❌
Not preparing for event loss

If events are lost, the Read Model becomes permanently inconsistent. Store events in an event store and implement a Replay feature.

✅
Best Practices

Clear Command/Query separation, event-based synchronization, implementing a Read Model Rebuild feature, detecting synchronization lag through monitoring, and gradual adoption (starting with some features).

🔗 Related Terms

📚 Learn More