Sitemap

Command-Query Responsibility Segregation (CQRS) in Rust with Actix

4 min readMay 18, 2025
Press enter or click to view image in full size
Photo by Muha Ajjan on Unsplash

“One model to read, one model to write — CQRS makes this explicit. Rust makes it safe.”

🧭 What Is CQRS?

CQRS (Command Query Responsibility Segregation) is an architectural pattern that separates read operations (queries) from write operations (commands). In Rust, where strong typing and ownership are first-class, this pattern maps cleanly to the language’s strengths.

Use cases include:

  • Auditable systems (event sourcing, logging)
  • Complex domain logic for writes vs performance-optimized reads
  • Decoupling bounded contexts (e.g., microservices)

Let’s implement a simple user registration system using Actix-Web, showcasing how CQRS can be cleanly modeled in idiomatic Rust.

🧱 System Overview

We’ll build:

  • Command Handler: Creates users.
  • Query Handler: Fetches users.
  • State Storage: In-memory DB (mocked with Mutex<HashMap>).

🧩 Dependencies

# Cargo.toml
[dependencies]
actix-web = "4"
serde = { version = "1", features = ["derive"] }
serde_json = "1"
tokio = { version = "1"…

--

--

Adam Szpilewicz

Written by Adam Szpilewicz

Backend software / data engineer working with rust, golang and python. I like writting and reading about code and software engineering.

Responses (1)