_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

Event Sourcing — stav jako historie událostí

12. 11. 2023 1 min read advanced

Architecture Expert

Event Sourcing — stav jako historie událostí

Event SourcingEventsCQRS 3 min read

Ukládejte stav jako sekvenci událostí. Event store, projekce, snapshoty a implementace.

Principle

Místo aktuálního stavu ukládáte sekvenci událostí. Stav získáte přehráním všech eventů.

# Tradiční: Account { id: 1, balance: 150 }
# Event Sourcing:
AccountCreated  { id: 1, balance: 0 }
MoneyDeposited  { amount: 200 }
MoneyWithdrawn  { amount: 50 }
# → Replay: 0 + 200 - 50 = 150

Event Store

class EventStore {
    async append(aggregateId, events, expectedVersion) {
        const current = await this.getVersion(aggregateId);
        if (current !== expectedVersion) throw new Error('Concurrency conflict');
        for (const event of events) {
            await this.db.query(
                'INSERT INTO events (aggregate_id, version, type, data) VALUES ($1, $2, $3, $4)',
                [aggregateId, ++current, event.type, JSON.stringify(event.data)]
            );
        }
    }
}

Projekce

Eventy nejsou efektivní pro dotazy. Projekce = read modely aktualizované při každém novém eventu. Snapshoty ukládají stav po N eventech.

Summary

Event Sourcing dává kompletní audit trail a časové cestování. Ale přidává komplexitu. Používejte kde je historie klíčová (finance, compliance).

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.