Barcamp presentation

Preview:

Citation preview

● Event Sourcing● CQRS● LMAX Disruptor

High Performance Bulletproof Architecture

Event Sourcing

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!

CartItems

Shipping Information

Traditional Data Structure (DTO or a STATE!)

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

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!

Current State - Aggregates (Domain Model)

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

Event Sourcing (Cons)

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

Java 8 Stream (uppercase sort and print)

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

Streams in Reactive Programming

In Rx every data source is a Stream!

Streams in Reactive Programming

Streams in Reactive Programming

CQRS

Tipical architecture

UIBUSINESS

LOGIC Storage

Write

Read

Tipical architecture

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

CQRS (Cons)

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

Questions?

Disruptor by LMAX

Why Concurrency is HARD?

CPU structure

False sharing (hidden contention)

Java Concurrency

● Locks● Queues● Synchronization● Contention

and other scary things

Disruptor Concurrency

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

Disruptor Structure

LMAX Architecture

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

Disruptor (Pros)

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

Disruptor (Cons)

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

Avoid useless processing (disrupter can batch)

Disruptor Implementation (simplified)

Why power of 2?

lets look at some assembly code

Assembly of publish(..) the memory barrier

No locks at all ( Atomic.lazySet )

Ring Buffer customizations

● Producer strategies○ Single producer○ Multiple producer

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

ES + CQRS + DISRUPTOR + EVENTSTORE

The (almost) perfect architecture

Command Bus

Command side

commands *

(1) Replication

(2) Journalling

(3) Unmarshalling

Command Business Logic [1,2,3]

COMMAND DOMAIN MODEL

events *

Command Store

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

With pleasure develop colleagues!!!

The END!

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

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

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