Slide 1

Slide 1 text

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

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

global.azurebootcamp.net 3 Thanks to our international sponsor

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

Azure Cosmos DB

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

They are using Azure Cosmos DB

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

Models

Slide 10

Slide 10 text

• Geo-redundancy Globally distributed

Slide 11

Slide 11 text

Strong Bounded staleness Session Consistent Prefix Eventual Consistency

Slide 12

Slide 12 text

Change Feed Change Feed Event Stream Data

Slide 13

Slide 13 text

APIs

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

Azure Cosmos DB SQL Api

Slide 16

Slide 16 text

SDKs

Slide 17

Slide 17 text

• 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

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

const querySpec: SqlQuerySpec = { query: "SELECT root.firstName root.lastName FROM root WHERE root.email = @email", parameters: [{ name: "@email", value: "[email protected]" }] }; const { result } = await container .items .query(querySpec) .toArray(); const item = await container .item(id); const { body: readDoc } = item.read(); Querying

Slide 20

Slide 20 text

const person = { firstName: "Sylvain", lastName: "PONTOREAU", email: "[email protected]" }; 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 = "[email protected]"; await item.replace(readDoc); await item.delete(); Persistence

Slide 21

Slide 21 text

CQRS & Event Sourcing

Slide 22

Slide 22 text

Data Driven Development Domain Driven Design Domain Driven Development != Data structure MCD CRUD Behavior Bounded Context Event

Slide 23

Slide 23 text

Bounded context Independent Autonomous Loosely coupled

Slide 24

Slide 24 text

Query Just a read operation Command Data validation Business logic Persistence CQRS

Slide 25

Slide 25 text

CQRS GetOrderById Query UpdateProduct Command GetProducts Query DeleteOrder Command … … Dispatcher Dispatcher QueryHandler CommandHandler Validation Plugin Monitoring Plugin Telemetry Plugin … Eventual consistency

Slide 26

Slide 26 text

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?

Slide 27

Slide 27 text

https://martinfowler.com/eaaDev/EventSourcing.html Event Sourcing

Slide 28

Slide 28 text

CQRS & Event Sourcing GetOrderById Query UpdateProduct Command GetProducts Query DeleteOrder Command … … Dispatcher Dispatcher QueryHandler CommandHandler Projection Projection Aggregate State Events

Slide 29

Slide 29 text

Cosmos DB as an Event Store Read Enpoint Write Enpoint Event Store Projector Read DB publish trigger persist query

Slide 30

Slide 30 text

Demo

Slide 31

Slide 31 text

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

Slide 32

Slide 32 text

Questions?

Slide 33

Slide 33 text

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