## Notes **Command and Query Responsibility Segregation** is the principle of separating reads from writes in a distributed architecture. This becomes an important solution when - There are a lot of concurrent requests - Writes are expensive - Reads dominate majority of requests ![[cqrs-diagram.png]] ## Pros - **Code simplification**: Reduces system complexity by separating writes and reads - **Resource optimization**: Optimizes resource usage by having separate databases for writes and reads - **Scalability**: Improves scalability for reads as you can simply add more database replicas - **Reduce number of errors**: By limiting the entities that modify shared data, reduce chances of unexpected modifications ## Cons - **Code complexity**: Developers have to manage reads/writes separately - **Increased development time**: Can increase development time when adopting this pattern - **Additional infrastructure**: Needs additional computing resources for different read/write data sources and components - **Increased Latency**: high-throughput queries can take longer ## Use Cases - **E-commerce**: Writes are only processing and inventory management; reads tend to be more numerous as users browse product catalogs - **Banks**: - *Reads*: balance inquiries and reporting - *Write*: transactions and calculations - **Healthcare**: - *Reads*: are patient searches, medical record retrieval, generating reports - *Writes*: data updates, scheduling, treatment plans - **Social Media**: - *Reads*: are feed generation, content recommendations, and user profile queries - *Writes* are content creation, updates, and engagement tracking