SystemExpertsSystemExperts
Pricing

Patterns

35 items

Horizontal Scaling Pattern

15mbeginner

Retry with Backoff Pattern

15mbeginner

Queue-based Load Leveling Pattern

20mintermediate

Replication Pattern

25mintermediate

Caching Strategies Pattern

25mintermediate

Fan-out Pattern

20mintermediate

Fan-in Pattern

20mintermediate

Persistent Connections Pattern

20mintermediate

Load Balancing Pattern

20mintermediate

Circuit Breaker Pattern

20mintermediate

Bloom Filters Pattern

20mintermediate

Time-Series Storage Pattern

20mintermediate

Bulkhead Pattern

20mintermediate

Batch Processing Pattern

20mintermediate

Write-Ahead Log Pattern

20mintermediate

API Gateway Pattern

20mintermediate

Backend for Frontend Pattern

20mintermediate

Sidecar Pattern

20mintermediate

Idempotency Pattern

20mintermediate

Rate Limiting Pattern

20mintermediate

Backpressure Pattern

20mintermediate

Pub/Sub Pattern

25mintermediate

Eventual Consistency Pattern

25mintermediate

Sharding Pattern

25madvanced

Conflict Resolution Pattern

25madvanced

Strong Consistency Pattern

30madvanced

Leader Election Pattern

25madvanced

Consensus Protocols Pattern

30madvanced

Stream Processing Pattern

25madvanced

Change Data Capture Pattern

25madvanced

Distributed Locking Pattern

25madvanced

Two-Phase Commit Pattern

25madvanced

LSM Trees Pattern

25madvanced

Event Sourcing Pattern

30madvanced

CQRS Pattern

28madvanced
System Design Pattern
Communicationcqrsread-modelwrite-modelseparationoptimizationadvanced

CQRS Pattern

Separating read and write models for scalability

Used in: E-commerce Catalogs, Reporting Systems, Search|28 min read

Summary

Command Query Responsibility Segregation (CQRS) separates read (query) and write (command) operations into different models. Commands mutate state through a write model optimized for consistency. Queries read from read models optimized for specific access patterns. This separation enables massive read scalability and complex queries without impacting writes.

Key Takeaways

Asymmetric Read/Write Patterns

Most applications have vastly different read and write patterns. Writes are infrequent and require strong consistency. Reads are frequent and have diverse access patterns. A single model optimized for one pattern compromises the other.

Write Model: Consistency and Rules

The write model focuses on enforcing business invariants, validating commands, and maintaining consistency. It uses normalized schemas and transactions.

Read Model: Speed and Flexibility

Read models are optimized for specific queries. You can have multiple read models: one denormalized for dashboards, one in Elasticsearch for search, one in Redis for real-time updates.

Traditional applications use a single model for both reads and writes. This forces compromises that hurt both operations. Write optimization (normalization) hurts read performance. Read optimization (denormalization) complicates writes.

CQRS Architecture

Summary

Command Query Responsibility Segregation (CQRS) separates read (query) and write (command) operations into different models. Commands mutate state through a write model optimized for consistency. Queries read from read models optimized for specific access patterns. This separation enables massive read scalability and complex queries without impacting writes.

Key Takeaways

Asymmetric Read/Write Patterns

Most applications have vastly different read and write patterns. Writes are infrequent and require strong consistency. Reads are frequent and have diverse access patterns. A single model optimized for one pattern compromises the other.

Write Model: Consistency and Rules

The write model focuses on enforcing business invariants, validating commands, and maintaining consistency. It uses normalized schemas and transactions.

Read Model: Speed and Flexibility

Read models are optimized for specific queries. You can have multiple read models: one denormalized for dashboards, one in Elasticsearch for search, one in Redis for real-time updates.

Eventual Consistency Between Models

Write and read models are eventually consistent. When a command updates the write model, read models update asynchronously via events.

CQRS Complexity Spectrum

Premium Content

Sign in to access this content or upgrade for full access.

CQRS exists on a spectrum. Simple: same database, separate tables. Medium: separate databases with async replication. Complex: multiple specialized read models with event sourcing.

Natural Fit with Event Sourcing

CQRS pairs well with event sourcing. The write model appends events. Read models subscribe to events and build projections. Events are the synchronization mechanism.

Pattern Details

Traditional applications use a single model for both reads and writes. This forces compromises that hurt both operations. Write optimization (normalization) hurts read performance. Read optimization (denormalization) complicates writes.

CQRS Architecture

Trade-offs

AspectAdvantageDisadvantage