Spring Boot Microservices vs Akka Actor Cluster

download Spring Boot Microservices vs Akka Actor Cluster

of 25

  • date post

    08-Jan-2017
  • Category

    Software

  • view

    450
  • download

    4

Embed Size (px)

Transcript of 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 Lorenzo.nicora@opencredo.com

  • 2

    Reactive is

    Reactive

    Semantically overloaded term

    adj. Readily responsive to stimulus [Merrian-Webster dictionary]From Latin reagere: act in return

  • 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

  • Responsive Low latency

    Resilient Stay responsive on failure

    Elastic Scale as needed

    Message-Driven Async messages as only communication between components

    Reactive Manifesto

  • No Blocking operation No Synchronization No Resource hogging

    Decoupling and isolation in Time Allow concurrency Space Components location decoupling

    Reactive Manifesto promotes

  • 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

  • Two real world projects

    Spring BootService

    Akka

  • 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

  • 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

  • TL;DRThe 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)

  • 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

  • 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

  • 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

  • Implementing Reactive principles

    Implementing Reactive principles

    from Macro and from MicroPlain Java/Spring or Akka

  • 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

  • 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

  • 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

  • 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

  • Conclusions

  • Keep in mind Reactive principleseven when not using reactive technologies

    Scalable and resilient applications

    Many implications behind the generic principles

    Reactive principles

  • 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?

  • 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 doesnt integrate with Spring

    Does Akka help?

  • Akka/Actor model Services:

    NOT mutually exclusive

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

    Akka and Services

  • 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

  • Thanks!

    Questions?