45
Event Sourcing CQRS LMAX Disruptor High Performance Bulletproof Architecture

Barcamp presentation

Embed Size (px)

Citation preview

Page 1: Barcamp presentation

● Event Sourcing● CQRS● LMAX Disruptor

High Performance Bulletproof Architecture

Page 2: Barcamp presentation

Event Sourcing

Page 3: Barcamp presentation

Event Sourcing (Pros)● ONLY model that never loses data

o You never know how you'd use your data tomorrowo You can add/remove features like there were out there all

this timeo Perfect for Big Data

● Events (facts) are never ever removed or updatedo Easily proves your audit log is correcto Protects from superuser attack

● Business asks for it!

Page 4: Barcamp presentation

CartItems

Shipping Information

Traditional Data Structure (DTO or a STATE!)

Page 5: Barcamp presentation

Cart Created

Event Stream

2 Items Added

1 Item Removed

Shipping Information

Added

● Events are FACTS! They are never deleted from the system● Events are what creates the current state● Depending on your business requirement you might have different

view of the same event stream (different states)● Any possible data can be projected from event stream

Page 6: Barcamp presentation

Event Sourcing (Pros) II● Best traceability and debuggability (time machine)● Move to the past and get the state as it was● Easily evolve domain● No more mapping objects to tables● Smoke testing● Super easy to test● Scale Easily● High Availability● Cost!

Page 7: Barcamp presentation

Current State - Aggregates (Domain Model)

Page 8: Barcamp presentation

Typical test (any behaviour can be tested like this!)

Page 9: Barcamp presentation

Event Sourcing (Cons)

● Weird at first● Event / Command confusion● Side effects on event reply● You either do it all or not doing at all

Page 10: Barcamp presentation

Java 8 Stream (uppercase sort and print)

Page 11: Barcamp presentation

Java 8 Stream (count all strings starting with “b”)

Page 12: Barcamp presentation

Streams in Reactive Programming

In Rx every data source is a Stream!

Page 13: Barcamp presentation

Streams in Reactive Programming

Page 14: Barcamp presentation

Streams in Reactive Programming

Page 15: Barcamp presentation

CQRS

Page 16: Barcamp presentation

Tipical architecture

UIBUSINESS

LOGIC Storage

Write

Read

Page 17: Barcamp presentation

Tipical architecture

Page 18: Barcamp presentation

CQRS (Pros)● PERFORMANCE!!!

o Command side is optimized for writes keep the current state in memory you only really need ACTUAL CONSISTENCY in write side

o Query side is optimized for reads Takes advantage of EVENTUAL CONSISTENCY

● Naturally easy to scale linearlyo Scale only what you need

● Polyglot persistenceo Forget SQL queries, forget joins, forget mapping objects to tables

● Efficiency o Simple commodity hardware might take a huge load

Page 19: Barcamp presentation

CQRS (Cons)

● Makes you choose between command or queryo You either write or read (but you can workaround it)

Page 20: Barcamp presentation

Questions?

Page 21: Barcamp presentation

Disruptor by LMAX

Page 22: Barcamp presentation

Why Concurrency is HARD?

Page 23: Barcamp presentation

CPU structure

Page 24: Barcamp presentation

False sharing (hidden contention)

Page 25: Barcamp presentation

Java Concurrency

● Locks● Queues● Synchronization● Contention

and other scary things

Page 26: Barcamp presentation

Disruptor Concurrency

● Non● BLOCKING● Contention Free● CONCURRENCY !!!

Page 27: Barcamp presentation

Disruptor Structure

Page 28: Barcamp presentation

LMAX Architecture

Page 29: Barcamp presentation

Disruptor (Pros)● Performance of course● Holly BATCHING!!!● Mechanical Sympathy● Optionally GC Free● Prevents False Sharing● Easy to compose dependant consumers (concurrency)● Synchronization free code in consumers● Data Structure (not a frickin framework!!!)● Fits werry well with CQRS and ES

Page 30: Barcamp presentation

Disruptor (Pros)

● Thread affinity (for more performance/throughput) ● Different strategies for Consumers (busy spin, sleep)● Single/Multiple producer strategy

Page 31: Barcamp presentation

Disruptor (Cons)

● Weird at first (again) ● Different approach to concurrency● Hard to understand how the performance is achieved

Page 32: Barcamp presentation

Avoid useless processing (disrupter can batch)

Page 33: Barcamp presentation

Disruptor Implementation (simplified)

Page 34: Barcamp presentation

Why power of 2?

Page 35: Barcamp presentation

lets look at some assembly code

Page 36: Barcamp presentation

Assembly of publish(..) the memory barrier

Page 37: Barcamp presentation

No locks at all ( Atomic.lazySet )

Page 38: Barcamp presentation

Ring Buffer customizations

● Producer strategies○ Single producer○ Multiple producer

● Wait Strategies○ Sleeping Wait○ Yielding Wait○ Busy Spin

Page 39: Barcamp presentation

ES + CQRS + DISRUPTOR + EVENTSTORE

The (almost) perfect architecture

Page 40: Barcamp presentation

Command Bus

Command side

commands *

(1) Replication

(2) Journalling

(3) Unmarshalling

Command Business Logic [1,2,3]

COMMAND DOMAIN MODEL

events *

Command Store

Page 41: Barcamp presentation

Event Bus

Query side

events * (1)(2) Journalling

(3) Unmarshalling

Query Business Logic [1,2]

QUERY DOMAIN MODEL

(1) Replication

Denormalized Data Storage

queries * (n)*

Event Store

Page 42: Barcamp presentation

With pleasure develop colleagues!!!

The END!

Page 43: Barcamp presentation

Links for Event Sourcing and CQRS

● First video you need to watch about ES and CQRS https://www.youtube.com/watch?v=JHGkaShoyNs

● a good webinar about microservice architecture and CQRS http://plainoldobjects.com/presentations/building-and-deploying-microservices-with-event-sourcing-cqrs-and-docker/

● eventstore is a database designed by Greg himself (the guy in first video), he also puts a lot of information about ES and CQRS in docs http://docs.geteventstore.com/introduction/event-sourcing-basics/

● practical video showcase (8 hours but worth it) http://www.viddler.com/v/dc528842

Page 44: Barcamp presentation

Links for LMAX Disruptor

● https://www.youtube.com/watch?v=DCdGlxBbKU4● https://www.youtube.com/watch?v=KrWxle6U10M● https://www.youtube.com/watch?v=IsGBA9KEtTM● http://martinfowler.com/articles/lmax.html● https://www.youtube.com/watch?v=eTeWxZvlCZ8

Page 45: Barcamp presentation

And some stuff about high performance Java code

● https://www.youtube.com/watch?v=NEG8tMn36VQ● https://www.youtube.com/watch?v=t49bfPLp0B0● http://www.slideshare.net/PeterLawrey/writing-and-testing-high-frequency-trading-engines-in-java● https://www.youtube.com/watch?v=ih-IZHpxFkY