Back to blog

CQRS: read and write separation in architectures

·2 min read

CQRS (Command Query Responsibility Segregation) is an architectural pattern that separates read operations (queries) from write operations (commands). Instead of using the same model for both, each operation has its own model and, optionally, its own database.

When to apply CQRS

CQRS shines in applications where read and write patterns are asymmetric. For example, an e-commerce system: writes are simple inserts (create order), but reads involve complex aggregations (order history, recommendations, reports).

Benefits

  • Independent scalability: scale reads and writes separately.
  • Use-case optimization: read databases can be denormalized for fast queries (Elasticsearch, Redis), while writes use normalized databases (PostgreSQL).
  • Cleaner domain models: commands model user intentions, not CRUD operations.

Considerations

CQRS introduces complexity: eventual consistency between models, data synchronization, and more code to maintain. Not recommended for simple CRUD applications.

CQRS without Event Sourcing

CQRS can be implemented without Event Sourcing. The write model persists in PostgreSQL, and a worker updates the read model (materialized views, caches, Elasticsearch). Sync latency is typically milliseconds.

CQRS with NestJS

NestJS supports CQRS with the @nestjs/cqrs package. Commands and queries are defined as classes, handlers process the logic, and the command/query bus distributes them.

CQRS is a powerful pattern for applications with complex read/write requirements. At Vynta, we design CQRS architectures that scale with your business.

Related articles

Have a project in mind?

Let's talk