What is Command Query Responsibility Segregation (CQRS) in PHP?
Why Interviewers Ask This
Senior PHP engineers are expected to reason about architecture, performance, and edge cases. This question separates mid-level from senior candidates by testing deep system-level understanding.
Answer
CQRS is an architectural pattern that separates read operations (Queries) from write operations (Commands). A Command changes state and returns nothing (or just an ID). A Query returns data and changes nothing. This separation allows you to optimize reads and writes independently — for example, using Elasticsearch for complex reads while MySQL handles writes. In PHP, implement with a command bus: $commandBus->dispatch(new CreateUserCommand($dto)). Libraries: Broadway, Tactician, Laravel's command bus. CQRS pairs well with Event Sourcing (storing state as a sequence of events instead of current state). It adds complexity and is best suited for domains with complex business logic, high read/write ratio differences, or scalability requirements.
Common Mistake
Candidates often give textbook answers here. Interviewers are more impressed when you relate the concept to a specific problem you solved in a real PHP project.
Previous
What is event-driven programming in PHP?
Next
What is PHP async programming with ReactPHP or Amp?