barcamp presentation
TRANSCRIPT
![Page 1: Barcamp presentation](https://reader035.vdocuments.site/reader035/viewer/2022062407/55d2c664bb61eb60088b4728/html5/thumbnails/1.jpg)
● Event Sourcing● CQRS● LMAX Disruptor
High Performance Bulletproof Architecture
![Page 2: Barcamp presentation](https://reader035.vdocuments.site/reader035/viewer/2022062407/55d2c664bb61eb60088b4728/html5/thumbnails/2.jpg)
Event Sourcing
![Page 3: Barcamp presentation](https://reader035.vdocuments.site/reader035/viewer/2022062407/55d2c664bb61eb60088b4728/html5/thumbnails/3.jpg)
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](https://reader035.vdocuments.site/reader035/viewer/2022062407/55d2c664bb61eb60088b4728/html5/thumbnails/4.jpg)
CartItems
Shipping Information
Traditional Data Structure (DTO or a STATE!)
![Page 5: Barcamp presentation](https://reader035.vdocuments.site/reader035/viewer/2022062407/55d2c664bb61eb60088b4728/html5/thumbnails/5.jpg)
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](https://reader035.vdocuments.site/reader035/viewer/2022062407/55d2c664bb61eb60088b4728/html5/thumbnails/6.jpg)
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](https://reader035.vdocuments.site/reader035/viewer/2022062407/55d2c664bb61eb60088b4728/html5/thumbnails/7.jpg)
Current State - Aggregates (Domain Model)
![Page 8: Barcamp presentation](https://reader035.vdocuments.site/reader035/viewer/2022062407/55d2c664bb61eb60088b4728/html5/thumbnails/8.jpg)
Typical test (any behaviour can be tested like this!)
![Page 9: Barcamp presentation](https://reader035.vdocuments.site/reader035/viewer/2022062407/55d2c664bb61eb60088b4728/html5/thumbnails/9.jpg)
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](https://reader035.vdocuments.site/reader035/viewer/2022062407/55d2c664bb61eb60088b4728/html5/thumbnails/10.jpg)
Java 8 Stream (uppercase sort and print)
![Page 11: Barcamp presentation](https://reader035.vdocuments.site/reader035/viewer/2022062407/55d2c664bb61eb60088b4728/html5/thumbnails/11.jpg)
Java 8 Stream (count all strings starting with “b”)
![Page 12: Barcamp presentation](https://reader035.vdocuments.site/reader035/viewer/2022062407/55d2c664bb61eb60088b4728/html5/thumbnails/12.jpg)
Streams in Reactive Programming
In Rx every data source is a Stream!
![Page 13: Barcamp presentation](https://reader035.vdocuments.site/reader035/viewer/2022062407/55d2c664bb61eb60088b4728/html5/thumbnails/13.jpg)
Streams in Reactive Programming
![Page 14: Barcamp presentation](https://reader035.vdocuments.site/reader035/viewer/2022062407/55d2c664bb61eb60088b4728/html5/thumbnails/14.jpg)
Streams in Reactive Programming
![Page 15: Barcamp presentation](https://reader035.vdocuments.site/reader035/viewer/2022062407/55d2c664bb61eb60088b4728/html5/thumbnails/15.jpg)
CQRS
![Page 16: Barcamp presentation](https://reader035.vdocuments.site/reader035/viewer/2022062407/55d2c664bb61eb60088b4728/html5/thumbnails/16.jpg)
Tipical architecture
UIBUSINESS
LOGIC Storage
Write
Read
![Page 17: Barcamp presentation](https://reader035.vdocuments.site/reader035/viewer/2022062407/55d2c664bb61eb60088b4728/html5/thumbnails/17.jpg)
Tipical architecture
![Page 18: Barcamp presentation](https://reader035.vdocuments.site/reader035/viewer/2022062407/55d2c664bb61eb60088b4728/html5/thumbnails/18.jpg)
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](https://reader035.vdocuments.site/reader035/viewer/2022062407/55d2c664bb61eb60088b4728/html5/thumbnails/19.jpg)
CQRS (Cons)
● Makes you choose between command or queryo You either write or read (but you can workaround it)
![Page 20: Barcamp presentation](https://reader035.vdocuments.site/reader035/viewer/2022062407/55d2c664bb61eb60088b4728/html5/thumbnails/20.jpg)
Questions?
![Page 21: Barcamp presentation](https://reader035.vdocuments.site/reader035/viewer/2022062407/55d2c664bb61eb60088b4728/html5/thumbnails/21.jpg)
Disruptor by LMAX
![Page 22: Barcamp presentation](https://reader035.vdocuments.site/reader035/viewer/2022062407/55d2c664bb61eb60088b4728/html5/thumbnails/22.jpg)
Why Concurrency is HARD?
![Page 23: Barcamp presentation](https://reader035.vdocuments.site/reader035/viewer/2022062407/55d2c664bb61eb60088b4728/html5/thumbnails/23.jpg)
CPU structure
![Page 24: Barcamp presentation](https://reader035.vdocuments.site/reader035/viewer/2022062407/55d2c664bb61eb60088b4728/html5/thumbnails/24.jpg)
False sharing (hidden contention)
![Page 25: Barcamp presentation](https://reader035.vdocuments.site/reader035/viewer/2022062407/55d2c664bb61eb60088b4728/html5/thumbnails/25.jpg)
Java Concurrency
● Locks● Queues● Synchronization● Contention
and other scary things
![Page 26: Barcamp presentation](https://reader035.vdocuments.site/reader035/viewer/2022062407/55d2c664bb61eb60088b4728/html5/thumbnails/26.jpg)
Disruptor Concurrency
● Non● BLOCKING● Contention Free● CONCURRENCY !!!
![Page 27: Barcamp presentation](https://reader035.vdocuments.site/reader035/viewer/2022062407/55d2c664bb61eb60088b4728/html5/thumbnails/27.jpg)
Disruptor Structure
![Page 28: Barcamp presentation](https://reader035.vdocuments.site/reader035/viewer/2022062407/55d2c664bb61eb60088b4728/html5/thumbnails/28.jpg)
LMAX Architecture
![Page 29: Barcamp presentation](https://reader035.vdocuments.site/reader035/viewer/2022062407/55d2c664bb61eb60088b4728/html5/thumbnails/29.jpg)
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](https://reader035.vdocuments.site/reader035/viewer/2022062407/55d2c664bb61eb60088b4728/html5/thumbnails/30.jpg)
Disruptor (Pros)
● Thread affinity (for more performance/throughput) ● Different strategies for Consumers (busy spin, sleep)● Single/Multiple producer strategy
![Page 31: Barcamp presentation](https://reader035.vdocuments.site/reader035/viewer/2022062407/55d2c664bb61eb60088b4728/html5/thumbnails/31.jpg)
Disruptor (Cons)
● Weird at first (again) ● Different approach to concurrency● Hard to understand how the performance is achieved
![Page 32: Barcamp presentation](https://reader035.vdocuments.site/reader035/viewer/2022062407/55d2c664bb61eb60088b4728/html5/thumbnails/32.jpg)
Avoid useless processing (disrupter can batch)
![Page 33: Barcamp presentation](https://reader035.vdocuments.site/reader035/viewer/2022062407/55d2c664bb61eb60088b4728/html5/thumbnails/33.jpg)
Disruptor Implementation (simplified)
![Page 34: Barcamp presentation](https://reader035.vdocuments.site/reader035/viewer/2022062407/55d2c664bb61eb60088b4728/html5/thumbnails/34.jpg)
Why power of 2?
![Page 35: Barcamp presentation](https://reader035.vdocuments.site/reader035/viewer/2022062407/55d2c664bb61eb60088b4728/html5/thumbnails/35.jpg)
lets look at some assembly code
![Page 36: Barcamp presentation](https://reader035.vdocuments.site/reader035/viewer/2022062407/55d2c664bb61eb60088b4728/html5/thumbnails/36.jpg)
Assembly of publish(..) the memory barrier
![Page 37: Barcamp presentation](https://reader035.vdocuments.site/reader035/viewer/2022062407/55d2c664bb61eb60088b4728/html5/thumbnails/37.jpg)
No locks at all ( Atomic.lazySet )
![Page 38: Barcamp presentation](https://reader035.vdocuments.site/reader035/viewer/2022062407/55d2c664bb61eb60088b4728/html5/thumbnails/38.jpg)
Ring Buffer customizations
● Producer strategies○ Single producer○ Multiple producer
● Wait Strategies○ Sleeping Wait○ Yielding Wait○ Busy Spin
![Page 39: Barcamp presentation](https://reader035.vdocuments.site/reader035/viewer/2022062407/55d2c664bb61eb60088b4728/html5/thumbnails/39.jpg)
ES + CQRS + DISRUPTOR + EVENTSTORE
The (almost) perfect architecture
![Page 40: Barcamp presentation](https://reader035.vdocuments.site/reader035/viewer/2022062407/55d2c664bb61eb60088b4728/html5/thumbnails/40.jpg)
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](https://reader035.vdocuments.site/reader035/viewer/2022062407/55d2c664bb61eb60088b4728/html5/thumbnails/41.jpg)
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](https://reader035.vdocuments.site/reader035/viewer/2022062407/55d2c664bb61eb60088b4728/html5/thumbnails/42.jpg)
With pleasure develop colleagues!!!
The END!
![Page 43: Barcamp presentation](https://reader035.vdocuments.site/reader035/viewer/2022062407/55d2c664bb61eb60088b4728/html5/thumbnails/43.jpg)
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](https://reader035.vdocuments.site/reader035/viewer/2022062407/55d2c664bb61eb60088b4728/html5/thumbnails/44.jpg)
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](https://reader035.vdocuments.site/reader035/viewer/2022062407/55d2c664bb61eb60088b4728/html5/thumbnails/45.jpg)
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