Building a Reactive System with Akka - Workshop @ O'Reilly SAConf NYC

112
Building a Reactive System with Akka Konrad Malawski (@ktosopl) - Akka Team Henrik Engström (@h3nk3) - Telemetry Team O’Reilly Software Architecture Conference, NYC - April 2016

Transcript of Building a Reactive System with Akka - Workshop @ O'Reilly SAConf NYC

Page 1: Building a Reactive System with Akka - Workshop @ O'Reilly SAConf NYC

Building a Reactive System with Akka

Konrad Malawski (@ktosopl) - Akka Team Henrik Engström (@h3nk3) - Telemetry Team

O’Reilly Software Architecture Conference, NYC - April 2016

Page 2: Building a Reactive System with Akka - Workshop @ O'Reilly SAConf NYC

Free e-book and printed report.bit.ly/why-reactive

Covers what reactive actually is.Implementing in existing architectures.

Thoughts from the team that’s buildingreactive apps since more than 6 years.

Page 3: Building a Reactive System with Akka - Workshop @ O'Reilly SAConf NYC
Page 4: Building a Reactive System with Akka - Workshop @ O'Reilly SAConf NYC

• “TRADITIONAL” AND REACTIVE APPLICATIONS

• ACTOR MODEL AND DISTRIBUTED PROGRAMMING

• INTRODUCTION TO "BRACES"

• AKKA CLUSTERING AND PERSISTENCE

• AKKA STREAMS

• AKKA HTTP

• TESTING AKKA Actors / Streams / HTTP

• WHAT FEATURES ARE COMING SOON?

AGENDA

Page 5: Building a Reactive System with Akka - Workshop @ O'Reilly SAConf NYC

“TRADITIONAL” AND REACTIVE APPLICATIONS

Page 6: Building a Reactive System with Akka - Workshop @ O'Reilly SAConf NYC

IT IS 2017 AND WE STILL USE

• Synchronous local and remote calls • Single machine apps - scaling is an afterthought • Non resilient approaches

Result: brittle, slow, non-scalable applications

Page 7: Building a Reactive System with Akka - Workshop @ O'Reilly SAConf NYC

WHAT CAN WE DO ABOUT IT?Use message driven/asynchronous programming

Page 8: Building a Reactive System with Akka - Workshop @ O'Reilly SAConf NYC

PROBLEM SOLVED!?

Page 9: Building a Reactive System with Akka - Workshop @ O'Reilly SAConf NYC

REACTIVE MANIFESTO

http://www.reactivemanifesto.org/

• Created in September 2014, +18k signatures • Consists of four traits

• Responsive

• Resilient

• Elastic

• Message Driven

Page 10: Building a Reactive System with Akka - Workshop @ O'Reilly SAConf NYC

RESPONSIVE

Page 11: Building a Reactive System with Akka - Workshop @ O'Reilly SAConf NYC

RESILIENT

Page 12: Building a Reactive System with Akka - Workshop @ O'Reilly SAConf NYC

E L A S T I C

Page 13: Building a Reactive System with Akka - Workshop @ O'Reilly SAConf NYC

MESSAGE DRIVEN

Page 14: Building a Reactive System with Akka - Workshop @ O'Reilly SAConf NYC

The many meanings of Reactive

reactivemanifesto.org

Page 15: Building a Reactive System with Akka - Workshop @ O'Reilly SAConf NYC
Page 16: Building a Reactive System with Akka - Workshop @ O'Reilly SAConf NYC
Page 17: Building a Reactive System with Akka - Workshop @ O'Reilly SAConf NYC

How to think about these techniques?

bit.ly/why-reactive

Page 18: Building a Reactive System with Akka - Workshop @ O'Reilly SAConf NYC

Akka

Page 19: Building a Reactive System with Akka - Workshop @ O'Reilly SAConf NYC

Akka is the Enabler of Reactive Systems

• Individual entities, actors, that can contain state

• Communication done by message passing

• Lock-free concurrency

• Loosely coupled and distributable

• Fault tolerance via Supervision

Page 20: Building a Reactive System with Akka - Workshop @ O'Reilly SAConf NYC

Akka is a Toolkit

• A Toolkit, not a Framework

• Multitude modules and components – “pick-and-choose”

• Performance and distribution always a goal

• Resilient and asynchronous from its very core

Page 21: Building a Reactive System with Akka - Workshop @ O'Reilly SAConf NYC

ACTOR MODEL AND

DISTRIBUTED PROGRAMMING

Page 22: Building a Reactive System with Akka - Workshop @ O'Reilly SAConf NYC

What is an Actor?

• Behavior (processing) - An actor reacts on messages it receives

• State (storage) - An actor is shielded from the rest of the world - no need for synchronization!

• Communication - An actor interacts with other actors exclusively via messages

• "One actor is no actor" - they come in systems

Page 23: Building a Reactive System with Akka - Workshop @ O'Reilly SAConf NYC

Anatomy of an Akka Actor

Page 24: Building a Reactive System with Akka - Workshop @ O'Reilly SAConf NYC

Anatomy of an ActorSystem

Page 25: Building a Reactive System with Akka - Workshop @ O'Reilly SAConf NYC

INTRODUCTION TO BRACES

Page 26: Building a Reactive System with Akka - Workshop @ O'Reilly SAConf NYC

VOCABULARY

• Drone - autonomous, field-deployed UAV, sends metrics/position to backend system

• DroneShadow - backend “mirror” of field-deployed Drone,keeps metrics and represents drone in backend model

• Backend - single MicroService,backed by Akka Cluster for resilience/load-balancing

Page 27: Building a Reactive System with Akka - Workshop @ O'Reilly SAConf NYC

VOCABULARY

• Micro-service - has a single responsibility, it absolutely does not mean “one node”!

• Distributed Journal - backing datastore of the single service, often Cassandra, SQL or similar. Service should “own your data.”

Page 28: Building a Reactive System with Akka - Workshop @ O'Reilly SAConf NYC

HIGH-LEVEL ARCHITECTURE OVERVIEW

Page 29: Building a Reactive System with Akka - Workshop @ O'Reilly SAConf NYC

HIGH-LEVEL ARCHITECTURE OVERVIEW

Page 30: Building a Reactive System with Akka - Workshop @ O'Reilly SAConf NYC

AKKA ACTOR BASICS

Page 31: Building a Reactive System with Akka - Workshop @ O'Reilly SAConf NYC

LET’S CODE!

Page 32: Building a Reactive System with Akka - Workshop @ O'Reilly SAConf NYC

• Message passing (events are a kind of message)

• Distribution via Location Transparency

• Lock-free & simple Concurrency

• Very powerful abstraction underneath everything we’ll talk about

Akka Actors:

Page 33: Building a Reactive System with Akka - Workshop @ O'Reilly SAConf NYC

AKKA CLUSTERING AND PERSISTENCE

Page 34: Building a Reactive System with Akka - Workshop @ O'Reilly SAConf NYC

HIGH-LEVEL ARCHITECTURE OVERVIEW

Page 35: Building a Reactive System with Akka - Workshop @ O'Reilly SAConf NYC

LET’S CODE!

Page 36: Building a Reactive System with Akka - Workshop @ O'Reilly SAConf NYC

FAILURE SCENARIO: Node Failure

Page 37: Building a Reactive System with Akka - Workshop @ O'Reilly SAConf NYC

HIGH-LEVEL ARCHITECTURE OVERVIEW

Page 38: Building a Reactive System with Akka - Workshop @ O'Reilly SAConf NYC
Page 39: Building a Reactive System with Akka - Workshop @ O'Reilly SAConf NYC

FAILURE SCENARIO: State Recovery

(Akka Persistence)

Page 40: Building a Reactive System with Akka - Workshop @ O'Reilly SAConf NYC
Page 41: Building a Reactive System with Akka - Workshop @ O'Reilly SAConf NYC

http://developer.lightbend.com/docs/akka-commercial-addons/1.0.1/split-brain-resolver.html

//application.confakka.cluster.downing-provider-class="com.lightbend.akka.sbr.SplitBrainResolverProvider"

• Static Quorum - static, very predictable (like zk)

• Keep Majority - dynamic, supports dynamic growth

• Keep Oldest - keep “most stable” members

• Keep Referee - keep “precious side”

Split Brain / Failure Detection

Page 42: Building a Reactive System with Akka - Workshop @ O'Reilly SAConf NYC

http://developer.lightbend.com/docs/akka-commercial-addons/1.0.1/split-brain-resolver.html

• Simple timeout - known as “auto down”

• very naive,

• not really intended for real-world usage.

Naive Failure Detection

Page 43: Building a Reactive System with Akka - Workshop @ O'Reilly SAConf NYC

• Battle-tested Membership Protocol

• Powerful distribution mechanism

• Application aware Sharding

• Scale-out & Resilience

Akka Cluster:

Page 44: Building a Reactive System with Akka - Workshop @ O'Reilly SAConf NYC

• EventSourcing style persistence

• Pluggable Journals (Cassandra, SQL, Mongo, Dynamo, and more …)

• Trivial to plug in

• Enables CQRS-style architectures

• Powers AtLeastOnceDelivery

Akka Persistence:

Page 45: Building a Reactive System with Akka - Workshop @ O'Reilly SAConf NYC

AKKA STREAMS

Page 46: Building a Reactive System with Akka - Workshop @ O'Reilly SAConf NYC

Fast Publisher Slow Subscriber

Akka Streams - BackPressure

Page 47: Building a Reactive System with Akka - Workshop @ O'Reilly SAConf NYC

Subscriber usually has some kind of buffer.

Push model

Page 48: Building a Reactive System with Akka - Workshop @ O'Reilly SAConf NYC

Push model

Page 49: Building a Reactive System with Akka - Workshop @ O'Reilly SAConf NYC

Push model

Page 50: Building a Reactive System with Akka - Workshop @ O'Reilly SAConf NYC

Push model

Page 51: Building a Reactive System with Akka - Workshop @ O'Reilly SAConf NYC

What if the buffer overflows?

Push model

Page 52: Building a Reactive System with Akka - Workshop @ O'Reilly SAConf NYC

Use bounded buffer, drop messages + require re-sending

Push model

Page 53: Building a Reactive System with Akka - Workshop @ O'Reilly SAConf NYC

Kernel does this!Routers do this!

(TCP)

Use bounded buffer, drop messages + require re-sending

Push model

Page 54: Building a Reactive System with Akka - Workshop @ O'Reilly SAConf NYC

Increase buffer size… Well, while you have memory available!

Push model

Page 55: Building a Reactive System with Akka - Workshop @ O'Reilly SAConf NYC

Reactive Streams explained

Reactive Streams explained in 1 slide

Page 56: Building a Reactive System with Akka - Workshop @ O'Reilly SAConf NYC

Fast Publisher will send at-most 3 elements. This is pull-based-backpressure.

Reactive Streams: “dynamic push/pull”

Page 57: Building a Reactive System with Akka - Workshop @ O'Reilly SAConf NYC

JEP-266 – soon…!public final class Flow { private Flow() {} // uninstantiable

@FunctionalInterface public static interface Publisher<T> { public void subscribe(Subscriber<? super T> subscriber); }

public static interface Subscriber<T> { public void onSubscribe(Subscription subscription); public void onNext(T item); public void onError(Throwable throwable); public void onComplete(); }

public static interface Subscription { public void request(long n); public void cancel(); }

public static interface Processor<T,R> extends Subscriber<T>, Publisher<R> { }}

Page 58: Building a Reactive System with Akka - Workshop @ O'Reilly SAConf NYC

Reactive Streams: goals

1) Avoiding unbounded buffering across async boundaries

2)Inter-op interfaces between various libraries

Page 59: Building a Reactive System with Akka - Workshop @ O'Reilly SAConf NYC

Reactive Streams: goals1) Avoiding unbounded buffering across async boundaries

2)Inter-op interfaces between various libraries

Argh, implementing a correct RS Publisher or Subscriber is so hard!

Page 60: Building a Reactive System with Akka - Workshop @ O'Reilly SAConf NYC

1) Avoiding unbounded buffering across async boundaries

2)Inter-op interfaces between various libraries

Reactive Streams: goals

Argh, implementing a correct RS Publisher or Subscriber is so hard!

Page 61: Building a Reactive System with Akka - Workshop @ O'Reilly SAConf NYC

Reactive Streams: goals

Argh, implementing a correct RS Publisher or Subscriber is so hard!

You should be using Akka Streams instead!

1) Avoiding unbounded buffering across async boundaries

2)Inter-op interfaces between various libraries

Page 62: Building a Reactive System with Akka - Workshop @ O'Reilly SAConf NYC

HIGH-LEVEL ARCHITECTURE OVERVIEW

Page 63: Building a Reactive System with Akka - Workshop @ O'Reilly SAConf NYC

LET’S CODE!

Page 64: Building a Reactive System with Akka - Workshop @ O'Reilly SAConf NYC

• Fully Typed API

• Asynchronous Back-Pressure

• First impl. to pass Reactive Streams standard

• Reactive Streams coming to JDK9

• Powerful composable ScalaDSL and JavaDSL

• Open Materializer API (e.g. Intel GearPump)

Akka Streams:

Page 65: Building a Reactive System with Akka - Workshop @ O'Reilly SAConf NYC

AKKA HTTP

Page 66: Building a Reactive System with Akka - Workshop @ O'Reilly SAConf NYC

HIGH-LEVEL ARCHITECTURE OVERVIEW

Page 67: Building a Reactive System with Akka - Workshop @ O'Reilly SAConf NYC

A core feature not obvious to the untrained eye…!

Quiz time! TCP is a ______ protocol?

Akka HTTP

Page 68: Building a Reactive System with Akka - Workshop @ O'Reilly SAConf NYC

A core feature not obvious to the untrained eye…!

Quiz time! TCP is a STREAMING protocol!

Akka HTTP

Page 69: Building a Reactive System with Akka - Workshop @ O'Reilly SAConf NYC

Streaming in Akka HTTP

http://doc.akka.io/docs/akka/2.4.7/scala/stream/stream-customize.html#graphstage-scala “Framed entity streaming” https://github.com/akka/akka/pull/20778

HttpServer as a: Flow[HttpRequest, HttpResponse]

Page 70: Building a Reactive System with Akka - Workshop @ O'Reilly SAConf NYC

Streaming in Akka HTTP

http://doc.akka.io/docs/akka/2.4.7/scala/stream/stream-customize.html#graphstage-scala “Framed entity streaming” https://github.com/akka/akka/pull/20778

HttpServer as a: Flow[HttpRequest, HttpResponse]

HTTP Entity as a: Source[ByteString, _]

Page 71: Building a Reactive System with Akka - Workshop @ O'Reilly SAConf NYC

Streaming in Akka HTTP

http://doc.akka.io/docs/akka/2.4.7/scala/stream/stream-customize.html#graphstage-scala “Framed entity streaming” https://github.com/akka/akka/pull/20778

HttpServer as a: Flow[HttpRequest, HttpResponse]

HTTP Entity as a: Source[ByteString, _]

Websocket connection as a: Flow[ws.Message, ws.Message]

Page 72: Building a Reactive System with Akka - Workshop @ O'Reilly SAConf NYC

Streaming from Akka HTTP (Java) public static void main(String[] args) { final ActorSystem system = ActorSystem.create(); final Materializer materializer = ActorMaterializer.create(system); final Http http = Http.get(system);

final Source<Tweet, NotUsed> tweets = Source.repeat(new Tweet("Hello world"));

final Route tweetsRoute = path("tweets", () -> completeWithSource(tweets, Jackson.marshaller(), EntityStreamingSupport.json()) );

final Flow<HttpRequest, HttpResponse, NotUsed> handler = tweetsRoute.flow(system, materializer);

http.bindAndHandle(handler, ConnectHttp.toHost("localhost", 8080), materializer ); System.out.println("Running at http://localhost:8080");

}

Page 73: Building a Reactive System with Akka - Workshop @ O'Reilly SAConf NYC

LET’S CODE!

Page 74: Building a Reactive System with Akka - Workshop @ O'Reilly SAConf NYC

It’s turtles buffers all the way down!

Page 75: Building a Reactive System with Akka - Workshop @ O'Reilly SAConf NYC

Streaming from Akka HTTP

Page 76: Building a Reactive System with Akka - Workshop @ O'Reilly SAConf NYC

Streaming from Akka HTTP

Page 77: Building a Reactive System with Akka - Workshop @ O'Reilly SAConf NYC

Streaming from Akka HTTP

No demand from TCP=

No demand upstream=

Source won’t generate tweets

=> Bounded memory stream processing!

Page 78: Building a Reactive System with Akka - Workshop @ O'Reilly SAConf NYC

FAILURE SCENARIO 1

Page 79: Building a Reactive System with Akka - Workshop @ O'Reilly SAConf NYC

HIGH-LEVEL ARCHITECTURE OVERVIEW

Page 80: Building a Reactive System with Akka - Workshop @ O'Reilly SAConf NYC

HIGH-LEVEL ARCHITECTURE OVERVIEW

Page 81: Building a Reactive System with Akka - Workshop @ O'Reilly SAConf NYC

HIGH-LEVEL ARCHITECTURE OVERVIEW

Page 82: Building a Reactive System with Akka - Workshop @ O'Reilly SAConf NYC

FAILURE SCENARIO 2

Page 83: Building a Reactive System with Akka - Workshop @ O'Reilly SAConf NYC

HIGH-LEVEL ARCHITECTURE OVERVIEW

Page 84: Building a Reactive System with Akka - Workshop @ O'Reilly SAConf NYC

HIGH-LEVEL ARCHITECTURE OVERVIEW

Page 85: Building a Reactive System with Akka - Workshop @ O'Reilly SAConf NYC

HIGH-LEVEL ARCHITECTURE OVERVIEW

Page 86: Building a Reactive System with Akka - Workshop @ O'Reilly SAConf NYC

• “Streaming-first” HTTP Server

• Powerful composable ScalaDSL and JavaDSL

• Built completely on Akka Streams

• Trivially exposes TCP level flow control to Akka Streams as backpressure

• Simple inter-op with Actors, Futures, Streams

• HTTP/2 coming very soon

Akka HTTP:

Page 87: Building a Reactive System with Akka - Workshop @ O'Reilly SAConf NYC

TESTING ASYNCHRONOUS CODE WITH AKKA

Page 88: Building a Reactive System with Akka - Workshop @ O'Reilly SAConf NYC

TestKits and tools provided• Asynchronous is hard • Testing asynchonous code is hard

• We’ve prepared plenty tools to make it simple

Page 89: Building a Reactive System with Akka - Workshop @ O'Reilly SAConf NYC

TestKits and tools provided• Akka Actors - TestKit • Akka Streams - TestSource / TestSink

Page 90: Building a Reactive System with Akka - Workshop @ O'Reilly SAConf NYC

LET’S CODE!

Page 91: Building a Reactive System with Akka - Workshop @ O'Reilly SAConf NYC

• All major modules come with dedicated TestKit

• Actors, Streams, MultiJVM testing (!)

• Makes asynchronous code simpler to test

Akka Testing:

Page 92: Building a Reactive System with Akka - Workshop @ O'Reilly SAConf NYC

STREAMING INTEGRATION WITH “ALPAKKA”

Page 93: Building a Reactive System with Akka - Workshop @ O'Reilly SAConf NYC

Alpakka is a community driven project• Community of Akka Streams “Connectors”

• Akka Streams == Reactive Streams impl == everyone profits!

• Similar in goals to Apache Camel • “inter-op all the things!”

• We’ve prepared plenty tools to make it simple

Page 94: Building a Reactive System with Akka - Workshop @ O'Reilly SAConf NYC

Alpakka – a community for Stream connectors

http://developer.lightbend.com/docs/alpakka/current/

Page 95: Building a Reactive System with Akka - Workshop @ O'Reilly SAConf NYC

Alpakka – a community for Stream connectors

http://developer.lightbend.com/docs/alpakka/current/

Google Pub/Sub…

……

Page 96: Building a Reactive System with Akka - Workshop @ O'Reilly SAConf NYC

Alpakka – a community for Stream connectors

http://developer.lightbend.com/docs/alpakka/current/

Page 97: Building a Reactive System with Akka - Workshop @ O'Reilly SAConf NYC

Alpakka – a community for Stream connectors

http://developer.lightbend.com/docs/alpakka/current/

Page 98: Building a Reactive System with Akka - Workshop @ O'Reilly SAConf NYC

LET’S CODE!

Page 99: Building a Reactive System with Akka - Workshop @ O'Reilly SAConf NYC

• Vibrant community of Akka Streams developers

• Tens of integrations already, more coming as-we-speak

• All fully Streaming and Back-pressure Aware and s

Akka “Alpakka”:

Page 100: Building a Reactive System with Akka - Workshop @ O'Reilly SAConf NYC

MONITORING

Page 101: Building a Reactive System with Akka - Workshop @ O'Reilly SAConf NYC

MONITORING FEATURES (2017-04)• Akka Actors • Akka Remoting and Clustering • Lagom Circuit Breakers • Dispatchers / Thread Pools • Various backend integration (ES, StatsD, …) • Sandbox environment (EKG) for easy exploration

Page 102: Building a Reactive System with Akka - Workshop @ O'Reilly SAConf NYC

Datadog Integration

Page 103: Building a Reactive System with Akka - Workshop @ O'Reilly SAConf NYC

• Commercial monitoring

• Get the full picture about your applications in production

• Highly optimised, fine-tuned by core Lightbend teams

Lightbend Monitoring:

Page 104: Building a Reactive System with Akka - Workshop @ O'Reilly SAConf NYC

SUMMING UP

Page 105: Building a Reactive System with Akka - Workshop @ O'Reilly SAConf NYC

ARCHITECTURE

• Resilient & Elastic from the ground up • High-performance & Asynchronous all-the-way • Highly Self-Healing / Resilient • Well established API endpoints (HTTP/WS/SSE) • Highly reusable & composable code

• See Reactive Streams (j.u.c.Flow in JDK9)

Page 106: Building a Reactive System with Akka - Workshop @ O'Reilly SAConf NYC

CODE

• Full feature parity between Java & Scala DSL (always!) • Asynchronous all-the-way • Understandable and well-defined failure scenarios • Start local, go distributed with no or minimal changes • Highly reusable & composable code

• Brought to you by leaders of Reactive Streams

Page 107: Building a Reactive System with Akka - Workshop @ O'Reilly SAConf NYC

MONITORING

• Awesome monitoring of asynchronous code • Coming soon:

• Tracing across Actors, Futures, HTTP • Smart Cluster Sharding insights

Page 108: Building a Reactive System with Akka - Workshop @ O'Reilly SAConf NYC

WHAT FEATURES ARE COMING SOON?

Page 109: Building a Reactive System with Akka - Workshop @ O'Reilly SAConf NYC

Next steps for Akka

New Akka Remoting (benchmarked 1,000,000+ msg/s (!)), (built using Akka Streams, Aeron)

More integrations for Akka Streams stages, project Alpakka.

Akka Typed actively progressing (!).

Akka HTTP/2 Proof of Concept in progress. Akka HTTP as default backend of Play Framework.

Highly Available CRDTs with Akka Distributed Data.

Page 110: Building a Reactive System with Akka - Workshop @ O'Reilly SAConf NYC

We <3 contributions• Easy to contribute:

• https://github.com/akka/akka/issues?q=is%3Aissue+is%3Aopen+label%3Aeasy-to-contribute

• https://github.com/akka/akka/issues?q=is%3Aissue+is%3Aopen+label%3A%22nice-to-have+%28low-prio%29%22

• Akka: akka.io && github.com/akka • Reactive Streams: reactive-streams.org • Mailing list:

• https://groups.google.com/group/akka-user • Public chat rooms:

• http://gitter.im/akka/dev developing Akka • http://gitter.im/akka/akka using Akka

Page 111: Building a Reactive System with Akka - Workshop @ O'Reilly SAConf NYC

Lightbend booth / sponsor area

O’Reilly’s “Ask the Experts”

Henrik’s talk on Wednesday @ 4.50 pm…

Catch us here:Catch us here

Page 112: Building a Reactive System with Akka - Workshop @ O'Reilly SAConf NYC

EOF Q/A

Konrad Malawski (@ktosopl) - Akka Team Henrik Engström (@h3nk3) - Telemetry Team