Lorenzo Nicora introduces reactive principles and compares two different approaches to them: a microservice architecture based on Spring Boot and a clustered application using Akka, based on lessons learned from real-world projects. Lorenzo also briefly introduces the Actor programming model and how it differs from other approaches for tackling concurrent and non-blocking programming in Java.
1. Two Reactive approaches to scale
1
Comparing two approaches
for scalable, message-driven, backend applications
in Java
Spring Boot ÎŒServices and Akka
Lorenzo Nicora Lorenzo.nicora@opencredo.com
3. âą Reactive as Responsive
Quickly prompt to user actions
Reactive asâŠ
âą Reactive as Data binding
Declarative, as opposed to Imperative
a=b+3
e.g. Spreadsheets
âą Reactive streams as Asynchronous Data Streams
âą Reactive as Reactive Manifesto
4. ĂŒ Responsive Ă Low latency
ĂŒ Resilient Ă Stay responsive on failure
ĂŒ Elastic Ă Scale as needed
ĂŒ Message-Driven
âą Async messages as only communication between components
Reactive Manifesto
5. â No Blocking operation
â No Synchronization
â No Resource hogging
Decoupling and isolation inâŠ
ĂŒ Time Ă Allow concurrency
ĂŒ Space Ă Components location decoupling
Reactive Manifesto promotes
6. Top-down approach: from Macro
(at ÎŒService boundaries)
o Message based communication between services
o Isolation at service level
Bottom-Up approach: from Micro
(within the service)
o Message based communication between components
o Non-blocking processing
o Isolation at component level
Macro and Micro approaches
8. Spring Boot ÎŒService application
ĂŒ Message-based communication between services
ĂŒ Non-blocking (where possible)
o Java 8 CompletableFutures
o Http non-blocking Servlets
o Fast-returning API endpoints
ĂŒ Event sourcing persistence
o Concursus forerunner
ĂŒ Spring Boot and Spring Cloud Config
Spring Boot ÎŒService
9. Java application using Akka, Cluster
ĂŒ Akka Actor programming model
ĂŒ Event-sourcing by Akka Persistence
ĂŒ Akka Cluster
ĂŒ Deployed using ConductR [Commercial]
ĂŒ Akka Http server (no container), non blocking
ĂŒ TypeSafe Config
â not Lagom framework
â not Akka Streams
Akka Cluster
10. TL;DR
âThe actor model in computer science is a mathematical model of
concurrent computation that treats "actors" as the universal primitives of
concurrent computation.
In response to a message that it receives, an actor can: make local
decisions, create more actors, send more messages, and determine how to
respond to the next message received. Actors may modify private state, but
can only affect each other through messages (avoiding the need for any
locks).â
[Wikipedia]
Actor model
Actor
Actor
Actor
Mailbox Mailbox
Mailbox
ĂŒ Article by Carl Hewitt (1973)
ĂŒ Erlang (1986)
ĂŒ Akka framework (2009)
11. A different way to concurrency
Actors, !Threads
Actor is the primary computational entity
o In Java, still a mixed world of Objects and Actors
Actors interact exclusively via asynchronous messages
o As opposed to Objects interacting through method calls
o Actors REACT on receiving a message
o No synchronisation; no lock
Actor model for dummies
Actor
Actor
Actor
Mailbox Mailbox
Mailbox
12. Actor handles one message a time
o Message handling code is intrinsically thread safe
o May have simple state: Instance properties in Java
Actor is lightweight
o Little overhead over creating an object
o May be used to hold request context
âą Per-request Actors: a common pattern
Actor model
13. Collaborating Actors
â Dependency Injection
o Akka: Actor Refs (location/lifecycle transparent) â object refs
Actors are created by Actors (âŠthen may be passed around)
Parent Ă Children
Supervision hierarchy
Failure handling
Supervision
15. Never block threads waiting
Macro
o Asynchronous messaging protocols between services
Micro
Plain Java
o CompletableFuture
o Non-blocking Servlets
o Low level NIO (!)
Akka
o Actors REACT to messages: never blocks waiting.
o Have to use some discipline not to block
e.g. waiting for a response from a synchronous resource
Non-blocking
16. Handle timeouts,
for resiliency
Macro (at service boundaries)
o Outbound connection timeouts (client)
o Inbound request/response (ack) handling timeouts (server)
Micro
Plain Java
o Not easy to handle timeouts consistently Ă ...unhandled or use default
Akka
o Everything has an explicit timeout Ă impossible to forget
o Actor message receiving timeout handler
Timeouts handlings
17. Asynchronous Failure handling
Java CompletableFuture Ă handle exceptional completion
o Error prone; easily forgotten
o No way to separate Error and Failure handling
Actors Ă failure handled by Supervisor
Akka: Supervisor is notified when an Actor throws an Exception
âą Failure : handled externally from message flow
âą Error: part of message handling behaviour
Handling Failure, Asynchronously
18. Event/Command sourcing
natural persistence patterns
for distributed, asynchronous applications
Plain Java/Spring
o Write your own Event Sourcing Ă Concursus
Event Sourcing naturally fits Actors
o Actors may be stateful
o Events and Commands are messages
âą When received Ă Actor change its state
âą May be saved and replayed
o Actors may represent Aggregate Root (DDD) or Command Processor
ĂŒ Akka Persistence: Command and Event Sourcing out of the box
Persistence: Event-sourcing
20. Keep in mind Reactive principles
âŠeven when not using âreactiveâ technologies
Ă Scalable and resilient applications
Many implications behind the generic principles
Reactive principles
21. Does a âReactiveâ technology like Akka help?
at Micro
ĂŒ Simpler, more testable concurrent code
ĂŒ Requires less discipline then plain Java 8
â A new programming model to learn
Does Akka help?
22. Does a âReactiveâ technology like Akka help?
at Macro
âą Still requires discipline at architectural level
â Akka forces to reinvent a lot of wheels on integration
â Akka doesnât integrate with Spring
Does Akka help?
23. Akka/Actor model
ÎŒServices:
NOT mutually exclusive
Consider adopting Akka for some services
âą The most business-critical (+resilient)
âą Highest concurrency
Akka and ÎŒServices
24. Reactive Manifesto
http://www.reactivemanifesto.org/
Ă Glossary: http://www.reactivemanifesto.org/glossary
Concurrency in Erlang & Scala: The Actor Model
https://rocketeer.be/articles/concurrency-in-erlang-scala/
Introducing Actors (Akka)
http://rerun.me/2014/09/11/introducing-actors-akka-notes-part-1/
DDD and Actor model (Vaughn Vernon, DDD eXchange 2013)
https://skillsmatter.com/skillscasts/4185-vaughn-vernon
Akka project
https://github.com/akka/akka
MoreâŠ