Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Event Sourcing avec Azure, quelle base de données choisir

Event Sourcing avec Azure, quelle base de données choisir

Global Azure Bootcamp 2023 (Rennes)

[email protected]

May 12, 2023
Tweet

Other Decks in Programming

Transcript

  1. 🔮
    Event Sourcing in Azure
    Which database to use?

    View full-size slide

  2. 🖖 Hello!
    @spontoreau
    Sylvain Pontoreau
    🎪 Co-organizer
    📕 Book writer
    Microsoft
    󰳕 Work with

    View full-size slide

  3. 📖 Time for self-promotion

    View full-size slide

  4. 🕺 Why I am talking about this topic ?
    I have been diving deep in it for a long time!

    View full-size slide

  5. ❓ What is Event Sourcing

    View full-size slide

  6. 💁 The Event Sourcing pattern
    “Every change to the state of an application
    is captured in an event”
    https://martinfowler.com/eaaDev/EventSourcing.html

    View full-size slide

  7. ✨ Event sourcing benefits
    Rebuild
    Temporal
    queries
    Replay

    View full-size slide

  8. 🎭 Event sourcing use cases
    Audit Debug Consistency

    View full-size slide

  9. 🗄 Event Store
    Sequence of
    events
    Event
    immutability
    Single point
    of Truth

    View full-size slide

  10. 📨 Event Stream
    Sequence of events = Event Stream
    Event Stream
    Event
    Event
    Event
    Event
    Event
    Event
    Event
    Event
    Event
    Timeline

    View full-size slide

  11. 📨 Event Stream
    Load from stream
    Event
    Event
    Event
    Event
    Event
    Event
    Event
    Event
    Event
    Load Reduce State

    View full-size slide

  12. 📨 Event Stream
    Append event
    Event
    Event
    Event
    Event
    Event
    Event
    Event
    Event
    Event
    Append
    Event

    View full-size slide

  13. 📨 Event Stream
    Subscribe
    Event
    Event
    Event
    Event
    Event
    Event
    Event
    Event
    Event
    Append
    Event
    Subscribe

    View full-size slide

  14. 📰 Read operations
    Load Reduce State
    A little bit expensive for read operations…
    … and not really great for querying :(

    View full-size slide

  15. 📽 Projections
    Using subscribe to build a queryable representation of the
    application state (aka Projections)
    Subscribe Projections
    Projector
    ℹ A good database for event sourcing must have a data streaming mechanism

    View full-size slide

  16. ✋ Keep that in mind
    App DB Projections
    Append Subscribe
    App
    Projections
    Append
    Publish


    DB
    ❗ Even if you’re using a Message Broker or an Event Bus

    View full-size slide

  17. ⚠ Eventually consistent
    CAP Theorem (Eric Brewer)
    Consistency
    Availability
    Partition tolerance
    All system nodes needs to manipulate
    exactly the same data at the same time
    Requests must succeed
    Node failure must not create a complete
    system failure
    A distributed system can only guarantee 2 constraints at the same time, not 3…

    View full-size slide

  18. 🔃 CQRS pattern
    What is CQRS?
    • A pattern designed by Greg Young
    • Command Query Responsibility Segregation
    • Read != Write (different needs)
    Query
    A read operation
    Command
    Validation
    Business logic
    Persistence
    CQRS suit perfectly well with Event Sourcing!

    View full-size slide

  19. 🔃 CQRS pattern
    GetBalance
    Query
    Deposite
    Command
    GetAllOperation
    Query
    Withdraw
    Command
    … …
    Dispatcher
    Read
    DB
    Dispatcher
    QueryHandler CommandHandler
    Event
    Store
    Projection Projection
    Aggregate
    State
    Events Behaviors

    View full-size slide

  20. ☁ Azure
    Flexibility Hyper scale Out of the box

    View full-size slide

  21. 🗃 PostgreSQL - Table
    CREATE TABLE "EVENT_STREAM" (
    "streamId" VARCHAR NOT NULL,
    "createdAt" VARCHAR NOT NULL,
    "type" VARCHAR NOT NULL,
    "version" INTEGER NOT NULL,
    "payload" JSONB,
    PRIMARY KEY ("streamId", "createdAt", "version")
    );
    CREATE INDEX ON "EVENT_STREAM" ("streamId");

    View full-size slide

  22. 🗃 PostgreSQL - Notify
    CREATE FUNCTION notify_new_event() RETURNS TRIGGER AS
    $trigger$
    BEGIN
    PERFORM pg_notify('new_event', row_to_json(NEW)::text);
    RETURN NULL;
    END;
    $trigger$
    LANGUAGE plpgsql VOLATILE
    COST 100;

    View full-size slide

  23. 🗃 PostgreSQL - Trigger
    CREATE TRIGGER notify_new_event
    AFTER INSERT
    ON "EVENT_STREAM"
    FOR EACH ROW
    EXECUTE PROCEDURE notify_new_event();

    View full-size slide

  24. 🗃 PostgreSQL - Rules
    CREATE FUNCTION THROW_WHEN_TYRING_TO_UPDATE_EVENT_STREAM()
    RETURNS VOID AS
    $$
    BEGIN
    RAISE EXCEPTION 'Event Stream can not be updated';
    END;
    $$ LANGUAGE plpgsql VOLATILE;
    CREATE RULE "PREVENT_DELETE_ON_EVENT_STREAM" AS
    ON DELETE TO "EVENT_STREAM" DO INSTEAD
    SELECT THROW_WHEN_TYRING_TO_UPDATE_EVENT_STREAM();
    CREATE RULE "PREVENT_UPDATE_ON_EVENT_STREAM" AS
    ON UPDATE TO "EVENT_STREAM" DO INSTEAD
    SELECT THROW_WHEN_TYRING_TO_UPDATE_EVENT_STREAM();

    View full-size slide

  25. 🗃 PostgreSQL - Load events
    SELECT
    "type",
    "payload",
    "version"
    FROM "EVENT_STREAM"
    WHERE "streamId" = $1 ORDER BY "createdAt", "version"

    View full-size slide

  26. 🗃 PostgreSQL - Append events
    INSERT INTO "EVENT_STREAM" (
    "streamId",
    "createdAt",
    "type",
    "version",
    "payload"
    )
    VALUES ($1, $2, $3, (
    SELECT COUNT("streamId") + 1
    FROM "EVENT_STREAM"
    WHERE "streamId" = $5
    ), $4
    )

    View full-size slide

  27. 🗃 PostgreSQL - Conclusion
    ✅ ❌
    • Simple to implement
    • One impl for all languages
    • On premise if needed
    • DB Tuning
    • You must know how to do it
    • Distributed
    • No acknowledgement

    View full-size slide

  28. 🗃 SQL Server
    The same thing but with “Service Broker” 😅

    View full-size slide

  29. 🌐 Cosmos DB
    Change feed for the win!
    Change Feed
    Event
    Stream
    Data

    View full-size slide

  30. 🌐 Cosmos DB
    Table, Trigger, Notify ➡ Nothing to do !!!

    View full-size slide

  31. 🌐 Cosmos DB - Load stream
    const endpoint = "https://your-account.documents.azure.com";
    const key = "";
    const client = new CosmosClient({ endpoint, key });
    const container = client.database("EventStore").container("EventStream");
    const querySpec: SqlQuerySpec = {
    query: `SELECT eventStream.eventType, eventStream.payload, eventStream.version,
    FROM eventStream
    WHERE eventStream.streamId = @streamId
    ORDER BY eventStream.createdAt, eventStream.version`,
    parameters: [{
    name: "@streamId",
    value: ""
    }
    ]
    };
    const { resources } = await container.items.query(querySpec).fetchAll();

    View full-size slide

  32. 🌐 Cosmos DB - Append events
    const endpoint = "https://your-account.documents.azure.com";
    const key = "";
    const client = new CosmosClient({ endpoint, key });
    const container = client.database("EventStore").container("EventStream");
    await container.items.create({
    streamId: "",
    createdAt: "type: "",
    version: ,
    payload: {
    //…
    }
    });

    View full-size slide

  33. 🌐 Cosmos DB - Conclusion
    ✅ ❌
    • Straightforward
    • Serverless
    • Schemaless
    • No rules
    • Version computed in code

    View full-size slide

  34. 🌀 Event Store DB
    Install the SDK and enjoy 🥳

    View full-size slide

  35. 🌀 Event Store DB - Load stream
    const client = new EventStoreDBClient({
    endpoint: "",
    });
    const streamName = "";
    const events = client.readStream(streamName, {
    fromRevision: START,
    direction: FORWARDS,
    maxCount: 10,
    });

    View full-size slide

  36. 🌀 Event Store DB - Append events
    const client = new EventStoreDBClient({
    endpoint: "",
    });
    const streamName = "";
    const event = jsonEvent({
    type: "",
    data: {
    //…
    },
    });
    await client.appendToStream(streamName, [event]);

    View full-size slide

  37. 🌀 Event Store DB

    • Out of the box
    • State of the art
    • Built in projection
    • On premise if needed
    • Manage outside of Azure…
    • Not too much customization
    • Only gRPC client

    View full-size slide

  38. Questions❓

    View full-size slide

  39. 🔮 Thanks!

    View full-size slide