+ All Categories
Home > Technology > CQRS and Event Sourcing

CQRS and Event Sourcing

Date post: 15-Jul-2015
Category:
Upload: jan-kronquist
View: 137 times
Download: 0 times
Share this document with a friend
Popular Tags:
28
CQRS & Event Sourcing @jankronquist
Transcript

CQRS & Event Sourcing

@jankronquist

Who am I? @jankronquist

Outline

CQRS!

Event sourcing!

Design & Code!!

Functional programming

CQRS

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!

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!

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!

CQRS

Command Query Responsibility Segregation!

Commands - action that will modify state!

Queries - anything that view state!

The handling of commands and queries are separated

ProjectionProjection

Common CQRS Architecture

API

Aggregate

Message Bus

Projection

Commands Queries

EventsEventsNot possible to view!

Micro Service Architecture

API Gateway

Service A

Message Bus

Service B

Request Request

MessageMessage

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

CQRS scalability

Queries!Add more nodes for linear scalability!!

Commands!Partitioning!

Process asynchronously

Event sourcing

Event vs Command

Event - Something that has happened!UserCreatedEvent!

FriendRequestReceivedEvent!

TicketSoldEvent!

Command - Something we want to happened!

CreateUserCommand!

SendFriendRequestCommand!

BuyTicketCommand

Event sourcing

All changes recorded as Events!

Order (time) is preserved!

The event log is THE persistent source

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

Application Service

Aggregate

Command Handler

Event Handler

Inside the aggregate

Command HandlerCommand

Old eventsEvent Handler

New events

Event Store

Event Sourcing Benefits

History of changes!

Reset & replay!

Persisting changes is simple & fast

Example domain

Rock - Paper - Scissors

Opening gambit

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

Opening gambit

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

Lizard - Spock?

http://rps.com

The future Facebook of Rock Paper Scissors!

Millions of users!

Many games per user

Lets design!

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


Recommended