Command Query Responsibility Segregation (CQRS) is an architectural pattern where read and write operations are separated, allowing for scalability and optimized data handling.
Core Ideas
- Command Side: Handles writes, updates the state
- Query Side: Handles reads, optimized for queries
- Events: Commands emit events that update the read model
Example Command
class CreateOrderCommand {
public function __construct(public int $userId, public array $items) {}
}
Example Query
class OrderReadModel {
public function getOrdersByUser(int $userId): array {}
}
Benefits
- Optimized read models
- Clear separation of concerns
- Scalable architecture
Conclusion
CQRS with PHP is ideal for systems with complex business logic and high read/write demands.
Comentarios (3)
CQRS finally clicks after seeing the separation of commands and queries.
Useful pattern for high-load PHP apps.
Great post!