Microservices: Azure Cosmos DB to rule them all!

Microservices: Azure Cosmos DB to rule them all!

A presentation about Azure Cosmos DB and how to build Microservices with this service and CQRS/ES patterns.

Talk made during the Global Azure Bootcamp 2019 in Rennes.

Video (French) - 1h22 to 2h10 : https://www.youtube.com/watch?v=PLi-XmvtPVs

9298dcce5b2b0e88d8ffbb837192ca98?s=128

Sylvain Pontoreau

April 27, 2019
Tweet

Transcript

  1. Microservices: Azure Cosmos DB to rule them all! Sylvain Pontoreau

  2. global.azurebootcamp.net 2 Thanks to our local sponsor William BORDES Yoann

    GUILLO
  3. global.azurebootcamp.net 3 Thanks to our international sponsor

  4. 4 http://sylvain.pontoreau.com Meet the Team Microsoft Engineer Paris TypeScript co-organizer

    Sylvain PONTOREAU Azure App Engineer @spontoreau https://www.linkedin.com/in/spontoreau
  5. Azure Cosmos DB

  6. What is Azure Cosmos DB • Cloud Database: https://azure.microsoft.com/en-us/services/cosmos-db/ •

    Released in may 2017 • Based on Document DB • Multi-model • Globally distributed • High availability • Mathematically viable: https://azure.microsoft.com/en-us/blog/azure-cosmos-db-pushing-the-frontier-of-globally-distributed-databases/ • JavaScript engine embedded
  7. They are using Azure Cosmos DB

  8. https://azure.microsoft.com/en-us/blog/how-halo-5-guardians-implemented-social-gameplay-using-azure-documentdb/ Halo & Azure Cosmos DB

  9. Models

  10. • Geo-redundancy Globally distributed

  11. Strong Bounded staleness Session Consistent Prefix Eventual Consistency

  12. Change Feed Change Feed Event Stream Data

  13. APIs

  14. Azure Cosmos DB Account Databases Collections Documents Documents in Azure

    Cosmos DB
  15. Azure Cosmos DB SQL Api

  16. SDKs

  17. • Open source https://github.com/Azure/azure-cosmos-js • Written in TypeScript • NPM

    Package • Documentation https://docs.microsoft.com/en-us/javascript/api/@azure/cosmos $ npm install @azure/cosmos Node.js SDK
  18. const { database} = await client .databases .create({ id: databaseId

    }); const { container } = await database .container .create({ id: containerId }); const client = new CosmosClient({ endpoint: myEndpoint, auth: { masterKey: myKey } }); CosmosClient, Database & Container
  19. const querySpec: SqlQuerySpec = { query: "SELECT root.firstName root.lastName FROM

    root WHERE root.email = @email", parameters: [{ name: "@email", value: "sypontor@microsoft.com" }] }; const { result } = await container .items .query(querySpec) .toArray(); const item = await container .item(id); const { body: readDoc } = item.read(); Querying
  20. const person = { firstName: "Sylvain", lastName: "PONTOREAU", email: "sypontor@microsoft.com"

    }; const { body } = await container .items .create(person); if(body !== undefined) { console.log(body.id); } const item = await container .item(body.id); const { body: readDoc } = item.read(); readDoc.email = "sylvain@microsoft.com"; await item.replace(readDoc); await item.delete(); Persistence
  21. CQRS & Event Sourcing

  22. Data Driven Development Domain Driven Design Domain Driven Development !=

    Data structure MCD CRUD Behavior Bounded Context Event
  23. Bounded context Independent Autonomous Loosely coupled

  24. Query Just a read operation Command Data validation Business logic

    Persistence CQRS
  25. CQRS GetOrderById Query UpdateProduct Command GetProducts Query DeleteOrder Command …

    … Dispatcher Dispatcher QueryHandler CommandHandler Validation Plugin Monitoring Plugin Telemetry Plugin … Eventual consistency
  26. Consistency • All system nodes needs to manipulate exactly the

    same data at the same time. Availability Partition tolerance • Requests must succeed. • Node failure must not create a complete system failure. A distributed system can only guarantee 2 constrains at the same time, not 3 Eventual consistency?
  27. https://martinfowler.com/eaaDev/EventSourcing.html Event Sourcing

  28. CQRS & Event Sourcing GetOrderById Query UpdateProduct Command GetProducts Query

    DeleteOrder Command … … Dispatcher Dispatcher QueryHandler CommandHandler Projection Projection Aggregate State Events
  29. Cosmos DB as an Event Store Read Enpoint Write Enpoint

    Event Store Projector Read DB publish trigger persist query
  30. Demo

  31. Resources https://docs.microsoft.com/en-us/azure/cosmos-db/introduction https://docs.microsoft.com/en-us/azure/cosmos-db/create-sql-api-nodejs https://docs.microsoft.com/en-us/azure/cosmos-db/change-feed https://medium.com/@thomasweiss_io/planet-scale-event-sourcing-with-azure- cosmos-db-48a557757c8d https://www.youtube.com/watch?v=5YNJpGwj_Zs https://docs.microsoft.com/en-US/azure/architecture/patterns/cqrs https://docs.microsoft.com/en-us/azure/architecture/patterns/event-sourcing https://docs.microsoft.com/en-

    us/dotnet/standard/microservices-architecture/microservice-ddd-cqrs-patterns/ https://docs.nestjs.com/recipes/cqrs https://gitpitch.com/jrouaix/cqrs-es-presentation/master#/ https://www.youtube.com/watch?v=qBLtZN3p3FU https://www.youtube.com/watch?v=LDW0QWie21s
  32. Questions?

  33. 33 Merci d’être venus A bientôt !