Download - Spring Boot Microservices vs Akka Actor Cluster

Transcript
Page 1: Spring Boot Microservices vs Akka Actor Cluster

Two Reactive approaches to scale

1

Comparing two approaches for scalable, message-driven, backend applications

in Java

Spring Boot μServices and Akka

Lorenzo Nicora [email protected]

Page 2: Spring Boot Microservices vs Akka Actor Cluster

2

“Reactive” is…

Reactive

Semantically overloaded term

adj. “Readily responsive to stimulus” [Merrian-Webster dictionary]

From Latin “reagere”: act in return

Page 3: Spring Boot Microservices vs Akka Actor Cluster

• Reactive as ResponsiveQuickly prompt to user actions

Reactive as…

• Reactive as Data bindingDeclarative, as opposed to Imperativea=b+3

e.g. Spreadsheets

• Reactive streams as Asynchronous Data Streams

• Reactive as Reactive Manifesto

Page 4: Spring Boot Microservices vs Akka Actor Cluster

ü Responsive à Low latency

ü Resilient à Stay responsive on failure

ü Elastic à Scale as needed

ü Message-Driven• Async messages as only communication between components

Reactive Manifesto

Page 5: Spring Boot Microservices vs Akka Actor Cluster

✗ No Blocking operation✗ No Synchronization✗ No Resource hogging

Decoupling and isolation in…ü Time à Allow concurrencyü Space à Components location decoupling

Reactive Manifesto promotes

Page 6: Spring Boot Microservices vs Akka Actor Cluster

Top-down approach: from Macro(at μService boundaries)

o Message based communication between serviceso Isolation at service level

Bottom-Up approach: from Micro(within the service)

o Message based communication between componentso Non-blocking processingo Isolation at component level

Macro and Micro approaches

Page 7: Spring Boot Microservices vs Akka Actor Cluster

Two real world projects

Spring BootμService

Akka

Page 8: Spring Boot Microservices vs Akka Actor Cluster

Spring Boot μService applicationü Message-based communication between servicesü Non-blocking (where possible)

o Java 8 CompletableFutureso Http non-blocking Servletso Fast-returning API endpoints

ü Event sourcing persistenceo Concursus forerunner

ü Spring Boot and Spring Cloud Config

Spring Boot μService

Page 9: Spring Boot Microservices vs Akka Actor Cluster

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

Page 10: Spring Boot Microservices vs Akka Actor Cluster

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)

Page 11: Spring Boot Microservices vs Akka Actor Cluster

A different way to concurrency Actors, !Threads

Actor is the primary computational entityo In Java, still a mixed world of Objects and Actors

Actors interact exclusively via asynchronous messageso As opposed to Objects interacting through method callso Actors REACT on receiving a messageo No synchronisation; no lock

Actor model for dummies

Actor

Actor

Actor

Mailbox Mailbox

Mailbox

Page 12: Spring Boot Microservices vs Akka Actor Cluster

Actor handles one message a timeo Message handling code is intrinsically thread safeo May have simple state: Instance properties in Java

Actor is lightweighto Little overhead over creating an object o May be used to hold request context

• Per-request Actors: a common pattern

Actor model

Page 13: Spring Boot Microservices vs Akka Actor Cluster

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

Page 14: Spring Boot Microservices vs Akka Actor Cluster

Implementing Reactive principles

Implementing Reactive principles

from Macro and from MicroPlain Java/Spring or Akka

Page 15: Spring Boot Microservices vs Akka Actor Cluster

Never block threads waitingMacro

o Asynchronous messaging protocols between services

MicroPlain Java

o CompletableFutureo Non-blocking Servletso Low level NIO (!)

Akkao 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

Page 16: Spring Boot Microservices vs Akka Actor Cluster

Handle timeouts, for resiliency

Macro (at service boundaries)o Outbound connection timeouts (client)o Inbound request/response (ack) handling timeouts (server)

MicroPlain Java

o Not easy to handle timeouts consistently à ...unhandled or use default Akka

o Everything has an explicit timeout à impossible to forgeto Actor message receiving timeout handler

Timeouts handlings

Page 17: Spring Boot Microservices vs Akka Actor Cluster

Asynchronous Failure handlingJava CompletableFuture à handle exceptional completion

o Error prone; easily forgotteno No way to separate Error and Failure handling

Actors à failure handled by SupervisorAkka: Supervisor is notified when an Actor throws an Exception• Failure : handled externally from message flow• Error: part of message handling behaviour

Handling Failure, Asynchronously

Page 18: Spring Boot Microservices vs Akka Actor Cluster

Event/Command sourcing natural persistence patterns

for distributed, asynchronous applications

Plain Java/Springo Write your own Event Sourcing à Concursus

Event Sourcing naturally fits Actorso Actors may be statefulo 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

Page 19: Spring Boot Microservices vs Akka Actor Cluster

Conclusions

Page 20: Spring Boot Microservices vs Akka Actor Cluster

Keep in mind Reactive principles…even when not using “reactive” technologies

à Scalable and resilient applications

Many implications behind the generic principles

Reactive principles

Page 21: Spring Boot Microservices vs Akka Actor Cluster

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?

Page 22: Spring Boot Microservices vs Akka Actor Cluster

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?

Page 23: Spring Boot Microservices vs Akka Actor Cluster

Akka/Actor model μServices:

NOT mutually exclusive

Consider adopting Akka for some services• The most business-critical (+resilient)• Highest concurrency

Akka and μServices

Page 24: Spring Boot Microservices vs Akka Actor Cluster

Reactive Manifesto http://www.reactivemanifesto.org/à Glossary: http://www.reactivemanifesto.org/glossary

Concurrency in Erlang & Scala: The Actor Modelhttps://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…

Page 25: Spring Boot Microservices vs Akka Actor Cluster

Thanks!

Questions?