Building flexible and scalable applications
Allard Buijze – abu@trifork.com – @allardbz
Using CQRS and Axon Framework
Building flexible and scalable applications Using CQRS and Axon - - PowerPoint PPT Presentation
Building flexible and scalable applications Using CQRS and Axon Framework Allard Buijze abu@trifork.com @allardbz Allard Buijze CTO of Trifork Amsterdam ~ 15 years of web development experience Strong believer in DDD and CQRS
Building flexible and scalable applications
Allard Buijze – abu@trifork.com – @allardbz
Using CQRS and Axon Framework
Allard Buijze
CTO of Trifork Amsterdam ~ 15 years of web development experience Strong believer in DDD and CQRS Developer and initiator of Axon Framework
Java Framework for scalability and performance www.axonframework.org
Layered architecture
Domain ¡Model ¡ User ¡Interface ¡ Service ¡Layer ¡ Data ¡Access ¡Layer ¡
Evolution of a Domain Model
Evolution of a Domain Model
A matter of perspective
Source: ¡h;ps://en.wikipedia.org/wiki/Ambiguous_image ¡
A matter of perspective
Evolution of a Domain Model
Big ball of “Mud”
Source: ¡h;p://www.sabisabi.com/images/DungBeetle-‑on-‑dung.JPG ¡
Microservice Architecture
Source: ¡h;p://marInfowler.com/arIcles/microservices.html ¡
Microservices vs Monoliths
Almost ¡ all ¡ the ¡ cases ¡ where ¡ I've ¡ heard ¡ of ¡ a ¡ system ¡ that ¡ was ¡ built ¡ as ¡ a ¡ microservice ¡ system ¡ from ¡ scratch, ¡it ¡has ¡ended ¡up ¡in ¡serious ¡trouble. ¡ Almost ¡ all ¡ the ¡ successful ¡ microservice ¡ stories ¡ have ¡ started ¡ with ¡ a ¡ monolith ¡ that ¡ got ¡ too ¡ big ¡ and ¡ was ¡ broken ¡up ¡
MarIn ¡Fowler ¡
Source: ¡h;p://marInfowler.com/bliki/MonolithFirst.html ¡
Desired evolution
Axon Framework
“CQRS Framework” for Java
Open source (Apache 2 License)
Simplify CQRS based applications
Building blocks common in CQRS-based architectures
More information: www.AxonFramework.org
Axon Framework – Core Principles
Message oriented
Events Commands
Location transparency
Separate infastructure from business logic
Customizable
Configure to match your infrastructure, not vice versa
CQRS
CQRS Based Architecture
Command ¡model ¡ ProjecIons ¡
CQRS Based Architecture
Command ¡model ¡ ProjecIons ¡
T: ¡1 ¡mln ¡/ ¡s ¡ Resp: ¡< ¡10 ¡ms ¡ T: ¡Thr. ¡20 ¡/ ¡s ¡ Resp: ¡< ¡100 ¡ms ¡ T: ¡10 ¡mln ¡/ ¡s ¡
T: ¡1 ¡/ ¡s ¡
Synchronizing models
Command ¡model ¡ ProjecIons ¡
Ev Events
How about....
How about....
How about....
CQRS Based Architecture
Separate infrastructure from business logic
A ¡ D ¡ B ¡ E ¡ C ¡
Spring configuration - Simple
Separate infrastructure from business logic
A ¡ D ¡ B ¡ E ¡ C ¡
Separate infrastructure from business logic
A ¡ D ¡ B ¡ E ¡ C ¡
Spring configuration – Distributed Events
Separate infrastructure from business logic
A ¡ D ¡ B ¡ E ¡ C ¡
Separate infrastructure from business logic
A ¡ D ¡ B ¡ E ¡ C ¡
Spring configuration – Distributed Commands
CQRS Based Architecture
Axon – Command Bus API
commandBus.dispatch(commandMessage, ¡new ¡VoidCallback() ¡{ ¡ ¡ ¡ ¡ ¡@Override ¡ ¡ ¡ ¡ ¡public ¡void ¡onSuccess() ¡{ ¡... ¡} ¡ ¡ ¡ ¡ ¡ ¡@Override ¡ ¡ ¡ ¡ ¡public ¡void ¡onFailure(Throwable ¡cause) ¡{ ¡... ¡} ¡ }); ¡
@CommandHandler ¡ public ¡void ¡handle(CreateToDoItemCommand ¡command) ¡{ ¡ ¡ ¡ ¡ ¡// ¡handle ¡command ¡ } ¡
Axon – Event Bus API
@EventHandler ¡ public ¡void ¡onEvent(ToDoItemCompletedEvent ¡event) ¡ { ¡ ¡ ¡ ¡ ¡// ¡handle ¡event ¡ } ¡ eventBus.publish(asEventMessage(new ¡ToDoItemCompletedEvent(“todo1”))); ¡
CQRS Based Architecture
interface ¡AggregateRoot ¡ abstract ¡class ¡AbstractAggregateRoot ¡ ¡ interface ¡EventSourcedAggregateRoot ¡ abstract ¡class ¡AbstractAnnotatedAggregateRoot ¡
The power of ubiquitous events
Event Sourcing
Event Sourcing
ID ¡ OrderID ¡ Product ¡ Count ¡ 1 ¡ 1 ¡ Deluxe ¡Chair ¡ 1 ¡ 2 ¡ 1 ¡ ... ¡ ... ¡
OrderItems ¡
Seq# ¡ Event ¡ 0 ¡ OrderCreatedEvent ¡ 1 ¡ ItemAddedEvent ¡(2x ¡Deluxe ¡Chair ¡-‑ ¡€ ¡399) ¡ 2 ¡ ItemRemovedEvent ¡(1x ¡Deluxe ¡Chair ¡-‑ ¡€ ¡399) ¡ 3 ¡ OrderConfirmed ¡ 4 ¡ OrderCancelledByUserEvent ¡ 5 ¡ ReturnShipmentReceived ¡ ID ¡ Status ¡ 1 ¡ Return ¡shipment ¡rcvd ¡
Orders ¡
Event Sourcing
Pros
Audit trail Reconstruct query model(s) Management reports since day 1 Data analysis
Cons
Maintain history (upcasters) Ever-growing
CQRS Based Architecture
interface ¡EventStore ¡ ¡ class ¡JpaEventStore ¡ class ¡JdbcEventStore ¡ class ¡MongoEventStore ¡
Axon – Event Sourcing
Make ¡decisions ¡ Apply ¡state ¡
Event Sourcing - Testing
Given-when-then fixtures
Given some past events When I apply a new Command Expect these new Events
fixture.given(new GameStartedEvent(…), new CallMadeEvent(…), new TurnChangedEvent(…)) .when( new MakeCallCommand(…)) .expectEvents( new CallMadeEvent(…), new TurnChangedEvent(…)); ¡
Getting started...
Download: axonframework.org/download
Code: github.com/AxonFramework Help: axonframework@googlegroups.com
Axon Framework – Some cases
Finance
Process automation in a top 50 bank Trading engine for ETF (index trackers) trading Pension fund calculations at a large bank On-line payment processing
Gaming
On-line bridge platform (bridgebig.com) On-line casino (casumo.com)
Healthcare
Electronic Medical Record for the Geriatric Healthcare Tracking and Tracing of equipment for dental implants
Aviation
Optimizing aircraft movement at several large airports
www.axonframework.org
Allard Buijze <abu@trifork.com> @allardbz / @axonframework