Top Banner
CQRS & Event Sourcing @jankronquist
28
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: CQRS and Event Sourcing

CQRS & Event Sourcing

@jankronquist

Page 2: CQRS and Event Sourcing

Who am I? @jankronquist

Page 3: CQRS and Event Sourcing
Page 4: CQRS and Event Sourcing

Outline

CQRS!

Event sourcing!

Design & Code!!

Functional programming

Page 5: CQRS and Event Sourcing

CQRS

Page 6: CQRS and Event Sourcing

Consistency

User A

User B

Get

Get

Buy!

Buy!!

One ticket left!

One ticket left!

User C Get Buy!!

One ticket left!

User D Get

No tickets!

Page 7: CQRS and Event Sourcing

Consistency

User A

User B

Get

Get

Buy!

Buy!!

One ticket left!

One ticket left!

User C Get Buy!!

One ticket left!

User D Get

No tickets!

Page 8: CQRS and Event Sourcing

Eventual Consistency

User A

User B

Get

Get

Buy!

Buy!!

One ticket left!

One ticket left!

User C Get Buy!!

One ticket left!

User D Get

No tickets!

Buy!!

One ticket left!

Page 9: CQRS and Event Sourcing

CQRS

Command Query Responsibility Segregation!

Commands - action that will modify state!

Queries - anything that view state!

The handling of commands and queries are separated

Page 10: CQRS and Event Sourcing

ProjectionProjection

Common CQRS Architecture

API

Aggregate

Message Bus

Projection

Commands Queries

EventsEventsNot possible to view!

Page 11: CQRS and Event Sourcing

Micro Service Architecture

API Gateway

Service A

Message Bus

Service B

Request Request

MessageMessage

Page 12: CQRS and Event Sourcing

Validation vs Business rules

Validation!Correct structure!

Ranges, lengths, types!

Rules!Should we do this?!

What should happen

When creating the command

When executing the command

Page 13: CQRS and Event Sourcing

CQRS scalability

Queries!Add more nodes for linear scalability!!

Commands!Partitioning!

Process asynchronously

Page 14: CQRS and Event Sourcing

Event sourcing

Page 15: CQRS and Event Sourcing

Event vs Command

Event - Something that has happened!UserCreatedEvent!

FriendRequestReceivedEvent!

TicketSoldEvent!

Command - Something we want to happened!

CreateUserCommand!

SendFriendRequestCommand!

BuyTicketCommand

Page 16: CQRS and Event Sourcing

Event sourcing

All changes recorded as Events!

Order (time) is preserved!

The event log is THE persistent source

Page 17: CQRS and Event Sourcing

Aggregates

Unit of consistency!

Example: user, meeting, order, product, game

MeetingCreated

AttendeeAdded A

AttendeeAdded B

AttendeeRemoved A

AttendeeAdded C

Attendees: A

Attendees: A & B

Attendees: B

Attendees: B & C

Event Stream

Page 18: CQRS and Event Sourcing

Application Service

Aggregate

Command Handler

Event Handler

Inside the aggregate

Command HandlerCommand

Old eventsEvent Handler

New events

Event Store

Page 19: CQRS and Event Sourcing

Event Sourcing Benefits

History of changes!

Reset & replay!

Persisting changes is simple & fast

Page 20: CQRS and Event Sourcing

Example domain

Page 21: CQRS and Event Sourcing

Rock - Paper - Scissors

Page 22: CQRS and Event Sourcing

Opening gambit

http://www.worldrps.com/gambit-play

Page 23: CQRS and Event Sourcing

Opening gambit

http://www.worldrps.com/gambit-play

Page 24: CQRS and Event Sourcing

Lizard - Spock?

Page 25: CQRS and Event Sourcing

http://rps.com

The future Facebook of Rock Paper Scissors!

Millions of users!

Many games per user

Page 26: CQRS and Event Sourcing

Lets design!

Page 27: CQRS and Event Sourcing

Playing the game

Player A

Player B

Server

rock

paperPlayer B: paper

Player A: rock

Game 123

Game 123 winner: Player B loser: Player A

CREATED WAITING

GAME WON

GAME TIED

any move

other move (victory)

other move (tie)

T

Page 28: CQRS and Event Sourcing