_CORE
AI & Agentic Systems Core Information Systems Cloud & Platform Engineering Data Platform & Integration Security & Compliance QA, Testing & Observability IoT, Automation & Robotics Mobile & Digital Banking & Finance Insurance Public Administration Defense & Security Healthcare Energy & Utilities Telco & Media Manufacturing Logistics & E-commerce Retail & Loyalty
References Technologies Blog Know-how Tools
About Collaboration Careers
CS EN
Let's talk

CQRS — oddělení čtení a zápisu

08. 10. 2024 1 min read advanced

Architecture Expert

CQRS — oddělení čtení a zápisu

CQRSArchitekturaPatterns 3 min read

Command Query Responsibility Segregation pro lepší škálovatelnost. Implementace s praktickými příklady.

Co je CQRS?

CQRS odděluje zápis (Command) od čtení (Query). Čtení potřebuje rychlé dotazy přes denormalizovaná data, zápis potřebuje validaci a konzistenci.

Example

// Command — zápis
class OrderCommandHandler {
    async createOrder(cmd) {
        const order = new Order(cmd.customerId, cmd.items);
        await this.orderRepo.save(order);
        await this.eventBus.publish('OrderCreated', { orderId: order.id, total: order.total });
    }
}
// Query — čtení z denormalizované view
class OrderQueryHandler {
    async getOrderSummary(orderId) {
        return this.readDb.query('SELECT * FROM order_views WHERE id = $1', [orderId]);
    }
}

When to Use

  • Ano: Vysoký poměr čtení vs zápis (90:10)
  • Ano: Složité dotazy přes více agregátů
  • Ne: Jednoduchý CRUD
  • Ne: Silná konzistence je kritická

Summary

CQRS je mocný, ale zvyšuje komplexitu. Použijte tam, kde čtecí a zápisové požadavky jsou výrazně odlišné.

Need Help with Implementation?

Our team has experience designing and implementing modern architectures. We’re happy to help.

Free Consultation

Share:

CORE SYSTEMS tým

Stavíme core systémy a AI agenty, které drží provoz. 15 let zkušeností s enterprise IT.