distribute your domain - lightbenddownloads.typesafe.com › website › presentations › ... ·...
TRANSCRIPT
![Page 1: Distribute your domain - Lightbenddownloads.typesafe.com › website › presentations › ... · Like Erlang/Elixir processes, only better Distributed by design, local is an optimization](https://reader033.vdocuments.net/reader033/viewer/2022060210/5f04ae527e708231d40f2ce7/html5/thumbnails/1.jpg)
Easy Scalability with AkkaDistribute your domain
![Page 2: Distribute your domain - Lightbenddownloads.typesafe.com › website › presentations › ... · Like Erlang/Elixir processes, only better Distributed by design, local is an optimization](https://reader033.vdocuments.net/reader033/viewer/2022060210/5f04ae527e708231d40f2ce7/html5/thumbnails/2.jpg)
Who?
● BoldRadius Solutions○ boldradius.com○ Typesafe Partner○ Scala, Akka and Play specialists○ Ottawa, Saskatoon, San Francisco, Boston, Chicago, Montreal, New
York, Toronto
● Michael Nash, VP Capabilities○ @MichaelPNash○ [email protected]
![Page 3: Distribute your domain - Lightbenddownloads.typesafe.com › website › presentations › ... · Like Erlang/Elixir processes, only better Distributed by design, local is an optimization](https://reader033.vdocuments.net/reader033/viewer/2022060210/5f04ae527e708231d40f2ce7/html5/thumbnails/3.jpg)
What?
What will I know in the next 45 minutes?● Distributed Domain-Driven Design
○ What is it, and how does it apply to project?○ What is CQRS and ES, and how to they relate?
● Akka○ How to do DDDD and CQRS/ES with Akka○ What is different in a DDDD Akka project, and why?○ How does this kind of project scaled compared to a
“normal” project, even with Akka?
![Page 4: Distribute your domain - Lightbenddownloads.typesafe.com › website › presentations › ... · Like Erlang/Elixir processes, only better Distributed by design, local is an optimization](https://reader033.vdocuments.net/reader033/viewer/2022060210/5f04ae527e708231d40f2ce7/html5/thumbnails/4.jpg)
Scalability
● Scalability is not performance○ Performance increasing reduces response time○ Scalability increasing improves capacity in relation to
added resources● Scalability can be...
○ Vertical (Scaling “up”): Bigger box (or boxes)○ Horizontal (Scaling “out”): More boxes
![Page 5: Distribute your domain - Lightbenddownloads.typesafe.com › website › presentations › ... · Like Erlang/Elixir processes, only better Distributed by design, local is an optimization](https://reader033.vdocuments.net/reader033/viewer/2022060210/5f04ae527e708231d40f2ce7/html5/thumbnails/5.jpg)
● The Actor model for the JVM○ A share-nothing distributed message-
driven compute model○ Scala and Java APIs
● Like Erlang/Elixir processes, only better○ Distributed by design, local is an optimization
![Page 6: Distribute your domain - Lightbenddownloads.typesafe.com › website › presentations › ... · Like Erlang/Elixir processes, only better Distributed by design, local is an optimization](https://reader033.vdocuments.net/reader033/viewer/2022060210/5f04ae527e708231d40f2ce7/html5/thumbnails/6.jpg)
CQRS and ES
● Command Query Responsibility Segregation○ Separate the read and write paths of the problem
● Event-Sourcing○ State is built from a journal of events
● One doesn’t need the other, but they can be combined very effectively
![Page 7: Distribute your domain - Lightbenddownloads.typesafe.com › website › presentations › ... · Like Erlang/Elixir processes, only better Distributed by design, local is an optimization](https://reader033.vdocuments.net/reader033/viewer/2022060210/5f04ae527e708231d40f2ce7/html5/thumbnails/7.jpg)
Event Sourcing
![Page 8: Distribute your domain - Lightbenddownloads.typesafe.com › website › presentations › ... · Like Erlang/Elixir processes, only better Distributed by design, local is an optimization](https://reader033.vdocuments.net/reader033/viewer/2022060210/5f04ae527e708231d40f2ce7/html5/thumbnails/8.jpg)
CQRS
![Page 9: Distribute your domain - Lightbenddownloads.typesafe.com › website › presentations › ... · Like Erlang/Elixir processes, only better Distributed by design, local is an optimization](https://reader033.vdocuments.net/reader033/viewer/2022060210/5f04ae527e708231d40f2ce7/html5/thumbnails/9.jpg)
Domain
● Your application’s domain is the part that relates to the problem
● The rest of your application is infrastructure, basically
● If you have a Payroll app, then Employees, Benefits, Pay Stubs are in your domain
![Page 10: Distribute your domain - Lightbenddownloads.typesafe.com › website › presentations › ... · Like Erlang/Elixir processes, only better Distributed by design, local is an optimization](https://reader033.vdocuments.net/reader033/viewer/2022060210/5f04ae527e708231d40f2ce7/html5/thumbnails/10.jpg)
DDDD
● Domain-Driven Design (DDD) is not only for Object-Oriented Systems
● Object-Oriented and Functional are not an either-or choice, or at odds with each other
● Distributed Domain Driven Design is a natural fit with the actor model
![Page 11: Distribute your domain - Lightbenddownloads.typesafe.com › website › presentations › ... · Like Erlang/Elixir processes, only better Distributed by design, local is an optimization](https://reader033.vdocuments.net/reader033/viewer/2022060210/5f04ae527e708231d40f2ce7/html5/thumbnails/11.jpg)
Traditional Domain Instances
![Page 12: Distribute your domain - Lightbenddownloads.typesafe.com › website › presentations › ... · Like Erlang/Elixir processes, only better Distributed by design, local is an optimization](https://reader033.vdocuments.net/reader033/viewer/2022060210/5f04ae527e708231d40f2ce7/html5/thumbnails/12.jpg)
Non-Distributed Domain
● Basic CRUD (Create, Read, Update, Delete)● Writes and reads to same database
○ Always consistent● Scaled via multiple identical replicas
○ and load balancing on HTTP● Bottlenecks on contention
○ reads and writes interfere with each other
![Page 13: Distribute your domain - Lightbenddownloads.typesafe.com › website › presentations › ... · Like Erlang/Elixir processes, only better Distributed by design, local is an optimization](https://reader033.vdocuments.net/reader033/viewer/2022060210/5f04ae527e708231d40f2ce7/html5/thumbnails/13.jpg)
When we scale the traditional model…
● We add multiple servers○ so there’s more than one copy of each domain
instance (potentially)● Each must read state every time
○ from the shared resource (the database)● Each must write (fully) to the same shared
resource to avoid conflict● Scalability is limited
![Page 14: Distribute your domain - Lightbenddownloads.typesafe.com › website › presentations › ... · Like Erlang/Elixir processes, only better Distributed by design, local is an optimization](https://reader033.vdocuments.net/reader033/viewer/2022060210/5f04ae527e708231d40f2ce7/html5/thumbnails/14.jpg)
![Page 15: Distribute your domain - Lightbenddownloads.typesafe.com › website › presentations › ... · Like Erlang/Elixir processes, only better Distributed by design, local is an optimization](https://reader033.vdocuments.net/reader033/viewer/2022060210/5f04ae527e708231d40f2ce7/html5/thumbnails/15.jpg)
With Akka and DDDD
● Wouldn’t it be great if you could just keep your domain instances in memory?
● But how to recover from its volatile nature: an event journal!
![Page 16: Distribute your domain - Lightbenddownloads.typesafe.com › website › presentations › ... · Like Erlang/Elixir processes, only better Distributed by design, local is an optimization](https://reader033.vdocuments.net/reader033/viewer/2022060210/5f04ae527e708231d40f2ce7/html5/thumbnails/16.jpg)
![Page 17: Distribute your domain - Lightbenddownloads.typesafe.com › website › presentations › ... · Like Erlang/Elixir processes, only better Distributed by design, local is an optimization](https://reader033.vdocuments.net/reader033/viewer/2022060210/5f04ae527e708231d40f2ce7/html5/thumbnails/17.jpg)
But I have too many instances!
● Only active domain instances are in memory● Instances activate and passivate as needed
![Page 18: Distribute your domain - Lightbenddownloads.typesafe.com › website › presentations › ... · Like Erlang/Elixir processes, only better Distributed by design, local is an optimization](https://reader033.vdocuments.net/reader033/viewer/2022060210/5f04ae527e708231d40f2ce7/html5/thumbnails/18.jpg)
Activation and Passivation
![Page 19: Distribute your domain - Lightbenddownloads.typesafe.com › website › presentations › ... · Like Erlang/Elixir processes, only better Distributed by design, local is an optimization](https://reader033.vdocuments.net/reader033/viewer/2022060210/5f04ae527e708231d40f2ce7/html5/thumbnails/19.jpg)
Ingredients
● Akka - Scala API○ with Clustering and Persistence modules
● Cassandra○ And the DataStax driver
● Typesafe Activator○ For our template
![Page 20: Distribute your domain - Lightbenddownloads.typesafe.com › website › presentations › ... · Like Erlang/Elixir processes, only better Distributed by design, local is an optimization](https://reader033.vdocuments.net/reader033/viewer/2022060210/5f04ae527e708231d40f2ce7/html5/thumbnails/20.jpg)
What’s Different?
● Domain object instances are transient, and memory-resident when in use
● Reads and writes don’t contend for resources
● Cluster can be grown until domain instances per node = 1
![Page 21: Distribute your domain - Lightbenddownloads.typesafe.com › website › presentations › ... · Like Erlang/Elixir processes, only better Distributed by design, local is an optimization](https://reader033.vdocuments.net/reader033/viewer/2022060210/5f04ae527e708231d40f2ce7/html5/thumbnails/21.jpg)
What do you get?
● Scalability○ Add nodes to handle higher load on the fly
● Failover○ No single point of failure or contention○ Instances can be created anywhere as needed
● Simplicity○ Build your app without having to worry about scalability
later
![Page 22: Distribute your domain - Lightbenddownloads.typesafe.com › website › presentations › ... · Like Erlang/Elixir processes, only better Distributed by design, local is an optimization](https://reader033.vdocuments.net/reader033/viewer/2022060210/5f04ae527e708231d40f2ce7/html5/thumbnails/22.jpg)
Consider your own domain
● Consider writes independently from reads○ Are they really the same even now?
● Deployment structure is critical to get all benefits safely
![Page 23: Distribute your domain - Lightbenddownloads.typesafe.com › website › presentations › ... · Like Erlang/Elixir processes, only better Distributed by design, local is an optimization](https://reader033.vdocuments.net/reader033/viewer/2022060210/5f04ae527e708231d40f2ce7/html5/thumbnails/23.jpg)
Example Applications
Online Auctions: Two implementations● Users place bids for online auctions● Most code shared● Identical APIs● Identical deploy structure, same machines,
same number of nodes● One CRUD, One CQRS/DDDD
![Page 24: Distribute your domain - Lightbenddownloads.typesafe.com › website › presentations › ... · Like Erlang/Elixir processes, only better Distributed by design, local is an optimization](https://reader033.vdocuments.net/reader033/viewer/2022060210/5f04ae527e708231d40f2ce7/html5/thumbnails/24.jpg)
CRUD Actor
● Uses Akka● Receives request● Writes to datastore directly
![Page 25: Distribute your domain - Lightbenddownloads.typesafe.com › website › presentations › ... · Like Erlang/Elixir processes, only better Distributed by design, local is an optimization](https://reader033.vdocuments.net/reader033/viewer/2022060210/5f04ae527e708231d40f2ce7/html5/thumbnails/25.jpg)
CRUD Actor
![Page 26: Distribute your domain - Lightbenddownloads.typesafe.com › website › presentations › ... · Like Erlang/Elixir processes, only better Distributed by design, local is an optimization](https://reader033.vdocuments.net/reader033/viewer/2022060210/5f04ae527e708231d40f2ce7/html5/thumbnails/26.jpg)
CQRS/DDDD Actor
● In-memory state● Recovers state from journal on startup● Uses become to change states● Passivates on timeout
![Page 27: Distribute your domain - Lightbenddownloads.typesafe.com › website › presentations › ... · Like Erlang/Elixir processes, only better Distributed by design, local is an optimization](https://reader033.vdocuments.net/reader033/viewer/2022060210/5f04ae527e708231d40f2ce7/html5/thumbnails/27.jpg)
![Page 28: Distribute your domain - Lightbenddownloads.typesafe.com › website › presentations › ... · Like Erlang/Elixir processes, only better Distributed by design, local is an optimization](https://reader033.vdocuments.net/reader033/viewer/2022060210/5f04ae527e708231d40f2ce7/html5/thumbnails/28.jpg)
![Page 29: Distribute your domain - Lightbenddownloads.typesafe.com › website › presentations › ... · Like Erlang/Elixir processes, only better Distributed by design, local is an optimization](https://reader033.vdocuments.net/reader033/viewer/2022060210/5f04ae527e708231d40f2ce7/html5/thumbnails/29.jpg)
![Page 30: Distribute your domain - Lightbenddownloads.typesafe.com › website › presentations › ... · Like Erlang/Elixir processes, only better Distributed by design, local is an optimization](https://reader033.vdocuments.net/reader033/viewer/2022060210/5f04ae527e708231d40f2ce7/html5/thumbnails/30.jpg)
Test Setup3 Cassandra Nodes (Clustered)3 Akka processing Nodes (Clustered)1 Front-end (Part of the Akka cluster)All Small AWS Instances100 simultaneous users100 auctionsTwo minutes of bidding
![Page 31: Distribute your domain - Lightbenddownloads.typesafe.com › website › presentations › ... · Like Erlang/Elixir processes, only better Distributed by design, local is an optimization](https://reader033.vdocuments.net/reader033/viewer/2022060210/5f04ae527e708231d40f2ce7/html5/thumbnails/31.jpg)
Simulation
![Page 32: Distribute your domain - Lightbenddownloads.typesafe.com › website › presentations › ... · Like Erlang/Elixir processes, only better Distributed by design, local is an optimization](https://reader033.vdocuments.net/reader033/viewer/2022060210/5f04ae527e708231d40f2ce7/html5/thumbnails/32.jpg)
Response Time DistributionCRUD CQRS
Scale is different!
![Page 33: Distribute your domain - Lightbenddownloads.typesafe.com › website › presentations › ... · Like Erlang/Elixir processes, only better Distributed by design, local is an optimization](https://reader033.vdocuments.net/reader033/viewer/2022060210/5f04ae527e708231d40f2ce7/html5/thumbnails/33.jpg)
Response Time Percentiles: CRUD
![Page 34: Distribute your domain - Lightbenddownloads.typesafe.com › website › presentations › ... · Like Erlang/Elixir processes, only better Distributed by design, local is an optimization](https://reader033.vdocuments.net/reader033/viewer/2022060210/5f04ae527e708231d40f2ce7/html5/thumbnails/34.jpg)
Response Time Percentiles: CQRS
Note that the scale is different!
![Page 35: Distribute your domain - Lightbenddownloads.typesafe.com › website › presentations › ... · Like Erlang/Elixir processes, only better Distributed by design, local is an optimization](https://reader033.vdocuments.net/reader033/viewer/2022060210/5f04ae527e708231d40f2ce7/html5/thumbnails/35.jpg)
Requests per Second: CRUD
![Page 36: Distribute your domain - Lightbenddownloads.typesafe.com › website › presentations › ... · Like Erlang/Elixir processes, only better Distributed by design, local is an optimization](https://reader033.vdocuments.net/reader033/viewer/2022060210/5f04ae527e708231d40f2ce7/html5/thumbnails/36.jpg)
Requests per Second: CQRS
Scale is different!
![Page 37: Distribute your domain - Lightbenddownloads.typesafe.com › website › presentations › ... · Like Erlang/Elixir processes, only better Distributed by design, local is an optimization](https://reader033.vdocuments.net/reader033/viewer/2022060210/5f04ae527e708231d40f2ce7/html5/thumbnails/37.jpg)
Total Requests/Responses in 2 mins
CRUD: 75,991CQRS: 199,357
![Page 38: Distribute your domain - Lightbenddownloads.typesafe.com › website › presentations › ... · Like Erlang/Elixir processes, only better Distributed by design, local is an optimization](https://reader033.vdocuments.net/reader033/viewer/2022060210/5f04ae527e708231d40f2ce7/html5/thumbnails/38.jpg)
Adding more nodes...
● CRUD version○ Contention increases○ Diminishing benefit to adding more nodes○ Tuning doesn’t help much
● CQRS version○ No contention○ Near-linear benefit until number of nodes = number
of auctions○ Tuning is very important
![Page 39: Distribute your domain - Lightbenddownloads.typesafe.com › website › presentations › ... · Like Erlang/Elixir processes, only better Distributed by design, local is an optimization](https://reader033.vdocuments.net/reader033/viewer/2022060210/5f04ae527e708231d40f2ce7/html5/thumbnails/39.jpg)
Activator Templatehttps://github.com/boldradius/akka-dddd-template
Add your own domain!
![Page 41: Distribute your domain - Lightbenddownloads.typesafe.com › website › presentations › ... · Like Erlang/Elixir processes, only better Distributed by design, local is an optimization](https://reader033.vdocuments.net/reader033/viewer/2022060210/5f04ae527e708231d40f2ce7/html5/thumbnails/41.jpg)
Further Reading