CQRS
읽기와 쓰기 작업을 별도 데이터 모델로 분리하는 패턴
구조와 동작 원리
화살표를 단계별로 확인하세요.
읽기와 쓰기의 책임을 나눈다. 서로 다른 DB나 이벤트 소싱은 필수 조건이 아니다.
기대 효과
읽기/쓰기 모델 독립 최적화로 성능·확장성·보안 개선
트레이드오프
데이터스토어 간 최종 일관성과 읽기 지연을 유발
은행 잔액·재고·안전 필수 제어처럼 쓰기 직후 동일 데이터가 즉시 보여야 하는 경우 부적합
운영 및 적용 가이드
종합 설명 · 원문 인용으로 검증된 항목과 구분됩니다. 실제 적용 전 확인이 필요합니다.
언제 사용하는가
읽기/쓰기 부하 특성이 크게 다르고 고처리량·저지연, 복잡한 도메인, 감사·규제 요구가 있는 시스템에 적합하다. 예: 주식 거래, 주문·재고 조회가 분리된 이커머스.
시나리오
중형 온라인 쇼핑몰이 초당 수천 건 상품 조회와 상대적으로 적은 주문 쓰기를 처리해야 하고, 주문 직후 재고·주문 상태를 빠르게 보여주되 수 초 지연은 허용한다고 가정한다. 쓰기 측은 주문·재고 명령을 검증해 write DB에 저장하고 이벤트를 발행하며, 읽기 측은 별도 read model을 갱신해 상품 목록·주문 조회를 서비스한다. 결제 후 주문 완료 화면은 read-your-writes로 방금 쓴 내용을 우선 보여주고, 읽기 모델 지연을 모니터링한다.
배치 원칙
- 쓰기 모델/DB는 명령 검증과 트랜잭션의 원본으로 배치
- 읽기 모델/DB는 조회 전용으로 분리하고 필요 시 복제본·캐시 사용
- 도메인 이벤트는 메시지 브로커를 통해 쓰기에서 읽기로 전달
- 프로젝션/인덱서는 읽기 모델을 갱신하도록 별도 배포
- Kubernetes에서는 write/read 서비스와 Redis master/replica, Helm 차트를 분리 배치
온프레미스
온프레미스에서는 컨테이너화한 명령·조회 서비스, 메시지 브로커, 읽기·쓰기 저장소를 사내 Kubernetes나 VM에 배치하고 네트워크·스토리지 격리, 보안·데이터 프라이버시, TCO, 백업·복구를 검토한다. 읽기 복제 지연과 브로커 적체를 모니터링하고 배포·업그레이드 절차를 표준화한다.
클라우드
클라우드에서는 관리형 Kubernetes, 관리형 DB·캐시, 관리형 메시지 브로커를 활용해 읽기·쓰기를 각각 독립 확장하고, 배포 전 로컬·스테이징에서 컨테이너와 오케스트레이션을 테스트한다. 오토스케일링, 다중 AZ, 읽기 복제, 관측성, 비용 태깅을 적용한다.
Kubernetes
Kubernetes에서는 명령·조회 마이크로서비스, 프로젝션 워커, write/read 데이터스토어, 메시지 브로커를 별도 Deployment·StatefulSet과 Service로 배치하고 Helm/Kustomize로 관리한다. Redis master/replica나 Kafka/Pulsar 등은 StatefulSet·Operator로 운영하며 HPA, PDB, NetworkPolicy, 리소스 요청·제한, 롤링 업데이트와 프로젝션 재처리 멱등성을 점검한다.
장애 대응
대표 장애는 읽기 모델 지연·불일치로 사용자가 방금 저장한 내용을 못 보거나 재제출하는 UX 장애, 브로커 적체·프로젝션 중단, 재배포·재처리 중 중복 적용으로 인한 데이터 손상이다. 읽기 모델 오프셋·지연·갭을 대시보드로 추적하고, read-your-writes·캐시로 완화하며, 프로젝션은 멱등하게 만들어 동일 로그 재처리 테스트를 CI에 넣고, 장애 시 브로커 적체 해소→프로젝션 재시작→읽기 모델 재구축 순으로 대응한다.
14개 관점별 근거
조사 당시 근거 보유 여부입니다. 출처의 정확성과 최신성을 별도로 판단하세요.
정의·개요근거 있음
읽기와 쓰기 작업을 별도 데이터 모델로 분리하는 패턴
Segregate the read and write operations for a data store into separate data models.해결하는 문제근거 있음
단일 모델로 읽기·쓰기의 상이한 성능·확장 요구를 최적화하기 어려운 문제를 해결
As applications grow, it can become increasingly difficult to optimize read and write operations on a single data model.주요 구성 컴포넌트·리소스근거 있음
웹 앱(마이크로서비스), 쓰기/읽기 데이터스토어 등으로 구성 가능
It has basically three components: * A web app written in NodeJS (which can be thought of a microservice)데이터·에셋 배치 원칙근거 있음
쓰기용 마스터 저장소와 읽기용 복제본/읽기 모델을 분리 배치
A 'write' datastore (implemented using Redis master instance) * A 'read' datastore (implemented using Redis replica instances )대표 유스케이스근거 있음
고성능·확장성, 복잡한 도메인, 감사·규제 요구가 큰 시스템(예: 주식 거래 플랫폼)
CQRS is great for a stock trading platform where performance, scalability, and regulatory compliance are paramount.시나리오 예시근거 있음
사용자가 폼 제출 후 읽기 모델 지연으로 이전 데이터를 보고 재제출하는 시나리오
the command succeeds, the read model has not caught up, the user's next page load shows old data부적합 상황·주의근거 있음
은행 잔액·재고·안전 필수 제어처럼 쓰기 직후 동일 데이터가 즉시 보여야 하는 경우 부적합
banking balances, inventory counts at checkout, safety-critical controls- don't tolerate eventual consistency well.온프레미스 관점(개발·배포·운영)근거 있음
컨테이너 기반 마이크로서비스를 온프레미스 Kubernetes에 배포 가능
You can take a containerized microservices-based application and deploy it into an on-premises Kubernetes cluster.클라우드 관점(개발·배포·운영)근거 있음
컨테이너화 후 관리형 Kubernetes(예: OCI Kubernetes Engine)에 배포·테스트
After you containerize your microservices, you can deploy them to OCI Kubernetes Engine.Kubernetes 관점(개발·배포·운영)근거 있음
Kubernetes에서 Node.js, Redis, Helm 등으로 CQRS 구성요소를 배포·운영
implementation of CQRS (Command Query Responsibility Segregation) architectural pattern on Kubernetes using NodeJS, Redis and Helm.장애 대응 관점근거 있음
읽기 모델이 쓰기 후 갱신되어 짧은 구간 쿼리가 최신 변경을 반영하지 못함
the read models are updated after the write, so for a short interval a query may not reflect a change장점근거 있음
읽기/쓰기 모델 독립 최적화로 성능·확장성·보안 개선
This approach allows you to optimize each model independently and can improve the performance, scalability, and security of an application.단점·트레이드오프근거 있음
데이터스토어 간 최종 일관성과 읽기 지연을 유발
The CQRS pattern typically results in eventual consistency between the data stores.대표 기술·도구근거 있음
Kubernetes, Node.js, Redis, Helm 등
on Kubernetes using NodeJS, Redis and Helm.추가 참고 문헌 (21)
- CQRS Pattern - Azure Architecture Center | Microsoft Learn ↗
- bliki: CQRS ↗
- Untitled result ↗
- An introduction to command query responsibility segregation ↗
- Command Query Responsibility Segregation (CQRS) ↗
- CQRS Pattern Explained: When To Use It And When Not To ↗
- CQRS pitfalls and patterns • Particular Software ↗
- Applying simplified CQRS and DDD patterns in a microservice - .NET | Microsoft Learn ↗
- Follow these 8 best practices for cloud-native app development | TechTarget ↗
- Design a Microservices-Based Application ↗
- Building Kubernetes Native SaaS applications: iterating quickly by deploying in-cluster data planes | Pixie Labs Blog ↗
- Command-Query Responsibility Segregation (CQRS) ↗
- EZF-006. Should You Use Kubernetes?; CQRS. Command Query Responsibility Segregation; gRPC API Architecture; API Design: 51 ✔️DO's and ❌DON'Ts; Functional Requirements (FR) vs Non-functional (NFR) ↗
- code2k13/cqrs-example: An opiniated sample implementation of CQRS architectural pattern on Kubernetes using NodeJS, Redis and Helm ↗
- An illustrated guide to CQRS data patterns ↗
- CQRS Journey | Microsoft Learn ↗
- CQRS: Separating Reads from Writes Without Losing Your Mind - The HLD Handbook ↗
- CQRS and Projection Architecture | System Design Patterns ↗
- chapter_12_cqrs.asciidoc at master · cosmicpython/book ↗
- 2.20 Error handling and resilience patterns — Software Engineering Guide ↗
- System Design, Plainly — Season 3, Part 3: Event Sourcing & CQRS - DEV Community ↗