Cqrs but different

Post on 30-Jul-2015

88 views 0 download

transcript

CQRSbut different

Udi Dahan@udidahan in Particular

Agenda

Background

Architecture

Approach

CQRS Background

Why people think they need it

Scalability

Simpler code

Everybody’s talking about it

Slave N

Scalability – DB Master/Slave

MasterClient Library

Slave 2

Slave 1

async master to slave replication

Scalability – Partitioning

Dividing data across multiple DB instances

Requires some level of application knowledge

Scalability – Sharding

Like partitioning, but not on a natural key

The logical deployment architecture

Client

DB

Server

… or even

Client

DB

Simpler code?

Simpler by comparison?

Everybody’s doin’ it!

Background

Architecture

Approach

Architecture

UI (MVC)

BL

DAL (ORM)

DB

Services

Domain-Driven Design

Value Objects

Anti-corruption layers components

Subdomains & Bounded Contexts

Starting up in the UI

… and going all the way down

UI

BL

DAL

DB

API

… and finally beyond layers

Lightweight ServiceBus (pub/sub)

BC1 BC2 BC3

NoSqlSql Shards

SOA

EDA MVC

Pipes &Filters

REST

DDD

LayersCQRS

What to use when?

Background

Architecture

Approach

Where things can go wrong

High contention domains

… on the most popular items

Regular logic starts to choke

begin transactionvar quantity = select Q from

Inventory where Id = @ProdId

if (quantity >= quantityRequested)update Inventory set Q =quantity – quantityRequestedwhere Id = @ProdId

commit transaction

… and the connection pool to dry up

Resulting in terrible wait timesfor everybody

Regular CQRS might fall behind too

What’s great about Event Sourcingnon-blocking, append-only data model

Business needs to get flexible

Append-only data modelswith simple querying too

ProductID

Delta TimeStamp

5 -1 08:35:22 6-3-2015

5 -1 08:35:27 6-3-2015

5 -3 08:42:12 6-3-2015

5 +250 08:43:06 6-3-2015

5 -2 10:36:22 6-3-2015

5 -5 17:25:54 6-3-2015

5 -3 18:02:37 6-3-2015

select sum(Delta) where …

Add snapshotting for compaction

ProductID

Delta TimeStamp

5 +235 18:03:06 6-3-2015begin transaction

select @q = sum(Delta), TimeStamp <= 5.min.ago

from Inventory where Id = @ProdIddelete from Inventory where Id =

@ProdIdand TimeStamp <= 5.min.agoinsert into Inventory @ProdId, @q,

now()commit transaction

The logical deployment architecture

Client

DB

Server

Simple – isn’t it?

Summary

Background

Architecture

Approach

Want to learn more?

Thank you

Udi Dahan@udidahan in Particular