![Page 2: Actors Model, Asynchronous Design with Non Blocking IO and SEDA](https://reader033.vdocument.in/reader033/viewer/2022061218/54b784024a7959db528b467a/html5/thumbnails/2.jpg)
Agenda A Little bit about Concurrency
Dealing with State (Share Mutable, Isolated Mutable, Persistent Data Structure)
Strategies
Concurrency with Intensive IO
Scalability
STM – Software Transaction Memory
Actors Model and SEDA
![Page 3: Actors Model, Asynchronous Design with Non Blocking IO and SEDA](https://reader033.vdocument.in/reader033/viewer/2022061218/54b784024a7959db528b467a/html5/thumbnails/3.jpg)
What´s Concurrency ? In a concurrent program two or more actions take place simultaneously
We often write concurrent programs using threads
Starting threads is easy, but their execution sequence is non- deterministic !!
Coordinate threads and ensure they’re handling data consistently is very difficulty
![Page 4: Actors Model, Asynchronous Design with Non Blocking IO and SEDA](https://reader033.vdocument.in/reader033/viewer/2022061218/54b784024a7959db528b467a/html5/thumbnails/4.jpg)
Three prominent options for concurrency
The “Synchronize and Suffer” model
The Software-Transactional Memory (STM) model
The Actor-based Concurrency model
![Page 5: Actors Model, Asynchronous Design with Non Blocking IO and SEDA](https://reader033.vdocument.in/reader033/viewer/2022061218/54b784024a7959db528b467a/html5/thumbnails/5.jpg)
Exploring Design Options Shared Mutable Design
Isolated Mutable Design
Purely Immutable Design (with functional languages)
![Page 6: Actors Model, Asynchronous Design with Non Blocking IO and SEDA](https://reader033.vdocument.in/reader033/viewer/2022061218/54b784024a7959db528b467a/html5/thumbnails/6.jpg)
Three ways to avoid problems Synchronize Properly
Don’t Share State !!
Don’t Mutable State
“Avoiding mutable state is the secret weapon to winning concurrency battles”
![Page 7: Actors Model, Asynchronous Design with Non Blocking IO and SEDA](https://reader033.vdocument.in/reader033/viewer/2022061218/54b784024a7959db528b467a/html5/thumbnails/7.jpg)
Strategies Sequential to Concurrent
Divide and Conquer
Decide the Number of Threads:
Runtime.getRuntime().availableProcessors();
We can compute the total number of threads we’d need as:
Number of threads = Number of Available Cores / (1 - Blocking Coefficient)
![Page 8: Actors Model, Asynchronous Design with Non Blocking IO and SEDA](https://reader033.vdocument.in/reader033/viewer/2022061218/54b784024a7959db528b467a/html5/thumbnails/8.jpg)
Concurrency with Intensive IO An IO intensive application has a large blocking coefficient and will benefit
from more threads than the number of available cores.
A computation intensive task has a blocking coefficient of 0 and an IO intensive task has a value close to 1—a fully blocked task is doomed so we don’t have to worry about the value reaching 1.
In order to determine the number of threads you need to know two things: The number of available cores The blocking coefficient of your tasks
![Page 9: Actors Model, Asynchronous Design with Non Blocking IO and SEDA](https://reader033.vdocument.in/reader033/viewer/2022061218/54b784024a7959db528b467a/html5/thumbnails/9.jpg)
Speedup for the IO Intensive App
![Page 10: Actors Model, Asynchronous Design with Non Blocking IO and SEDA](https://reader033.vdocument.in/reader033/viewer/2022061218/54b784024a7959db528b467a/html5/thumbnails/10.jpg)
Concurrent Computation of Prime Numbers
![Page 11: Actors Model, Asynchronous Design with Non Blocking IO and SEDA](https://reader033.vdocument.in/reader033/viewer/2022061218/54b784024a7959db528b467a/html5/thumbnails/11.jpg)
Speedup for the Computationally Intensive
![Page 12: Actors Model, Asynchronous Design with Non Blocking IO and SEDA](https://reader033.vdocument.in/reader033/viewer/2022061218/54b784024a7959db528b467a/html5/thumbnails/12.jpg)
Managing Threads with ExecutorService
![Page 13: Actors Model, Asynchronous Design with Non Blocking IO and SEDA](https://reader033.vdocument.in/reader033/viewer/2022061218/54b784024a7959db528b467a/html5/thumbnails/13.jpg)
Software Transactional Memory - STM Separation of Identity and State
![Page 14: Actors Model, Asynchronous Design with Non Blocking IO and SEDA](https://reader033.vdocument.in/reader033/viewer/2022061218/54b784024a7959db528b467a/html5/thumbnails/14.jpg)
Clojure STM
![Page 15: Actors Model, Asynchronous Design with Non Blocking IO and SEDA](https://reader033.vdocument.in/reader033/viewer/2022061218/54b784024a7959db528b467a/html5/thumbnails/15.jpg)
Actos Model – Isolating Mutability
![Page 16: Actors Model, Asynchronous Design with Non Blocking IO and SEDA](https://reader033.vdocument.in/reader033/viewer/2022061218/54b784024a7959db528b467a/html5/thumbnails/16.jpg)
Life Cycle of an Actor
![Page 17: Actors Model, Asynchronous Design with Non Blocking IO and SEDA](https://reader033.vdocument.in/reader033/viewer/2022061218/54b784024a7959db528b467a/html5/thumbnails/17.jpg)
Actors Model Lock free approach to concurrency
No shared state between actors
Asynchronous message passing Mailboxes to buffer incoming messages
![Page 18: Actors Model, Asynchronous Design with Non Blocking IO and SEDA](https://reader033.vdocument.in/reader033/viewer/2022061218/54b784024a7959db528b467a/html5/thumbnails/18.jpg)
SEDA Staged Event Driven Architecture
Decomposes a complex, event-driven application into a set of stages connected by queues.
The most fundamental aspect of the SEDA architecture is the programming model that supports stage-level backpressure and load management.
![Page 19: Actors Model, Asynchronous Design with Non Blocking IO and SEDA](https://reader033.vdocument.in/reader033/viewer/2022061218/54b784024a7959db528b467a/html5/thumbnails/19.jpg)
Stages Stages
One actor class per stage
Shared dispatcher
Individually tunable I/O Bound
CPU Bound Easier to reason about Code reuse
![Page 20: Actors Model, Asynchronous Design with Non Blocking IO and SEDA](https://reader033.vdocument.in/reader033/viewer/2022061218/54b784024a7959db528b467a/html5/thumbnails/20.jpg)
![Page 21: Actors Model, Asynchronous Design with Non Blocking IO and SEDA](https://reader033.vdocument.in/reader033/viewer/2022061218/54b784024a7959db528b467a/html5/thumbnails/21.jpg)
Dispatchers ThreadBasedDispatcher Binds one actor to its own thread
ExecutorBasedEventDrivenDispatcher Must be shared between actors
ExecutorBasedEventDrivenWorkStealingDispatcher Must be shared between actors of the same type
![Page 22: Actors Model, Asynchronous Design with Non Blocking IO and SEDA](https://reader033.vdocument.in/reader033/viewer/2022061218/54b784024a7959db528b467a/html5/thumbnails/22.jpg)
Queues SEDA has a queue per stage model
Akka actors have their own mailbox
How do we evenly distribute work?
![Page 23: Actors Model, Asynchronous Design with Non Blocking IO and SEDA](https://reader033.vdocument.in/reader033/viewer/2022061218/54b784024a7959db528b467a/html5/thumbnails/23.jpg)
Work Stelaling"Actors of the same type can be set up to share this dispatcher and during execution time the different actors will steal messages from other actors if they have less messages to process”
![Page 24: Actors Model, Asynchronous Design with Non Blocking IO and SEDA](https://reader033.vdocument.in/reader033/viewer/2022061218/54b784024a7959db528b467a/html5/thumbnails/24.jpg)
Fault Tolerance Supervisors Restarts actors Stops after x times within y milliseconds
Restart Strategies OneForOne AllForOne
![Page 25: Actors Model, Asynchronous Design with Non Blocking IO and SEDA](https://reader033.vdocument.in/reader033/viewer/2022061218/54b784024a7959db528b467a/html5/thumbnails/25.jpg)
Final Product
![Page 26: Actors Model, Asynchronous Design with Non Blocking IO and SEDA](https://reader033.vdocument.in/reader033/viewer/2022061218/54b784024a7959db528b467a/html5/thumbnails/26.jpg)
I hope that this presentation was useful for open mind to a new model for build scalable APIs
Thanks – Felipe Oliveira @soaexpertbr