Slide 1

Slide 1 text

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

Slide 2

Slide 2 text

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 building reactive apps since more than 6 years.

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

• “TRADITIONAL” AND REACTIVE APPLICATIONS • ACTOR MODEL AND DISTRIBUTED PROGRAMMING • INTRODUCTION • AKKA CLUSTERING AND PERSISTENCE • AKKA STREAMS • AKKA HTTP • TESTING AKKA Actors / Streams / HTTP • WHAT FEATURES ARE COMING SOON? AGENDA

Slide 5

Slide 5 text

“TRADITIONAL” AND REACTIVE APPLICATIONS

Slide 6

Slide 6 text

WANT TO FOLLOW ALONG? https://github.com/henrikengstrom/sa-2017- akka

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

PROBLEM SOLVED!?

Slide 10

Slide 10 text

REACTIVE MANIFESTO http://www.reactivemanifesto.org/ • Created in September 2014, +18k signatures • Consists of four traits • Responsive • Resilient • Elastic • Message Driven

Slide 11

Slide 11 text

RESPONSIVE

Slide 12

Slide 12 text

RESILIENT

Slide 13

Slide 13 text

E L A S T I C

Slide 14

Slide 14 text

MESSAGE DRIVEN

Slide 15

Slide 15 text

The many meanings of Reactive reactivemanifesto.org

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

No content

Slide 18

Slide 18 text

How to think about these techniques? bit.ly/why-reactive

Slide 19

Slide 19 text

AKKA

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

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

Slide 22

Slide 22 text

ACTOR MODEL AND DISTRIBUTED PROGRAMMING

Slide 23

Slide 23 text

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

Slide 24

Slide 24 text

Anatomy of an Akka Actor

Slide 25

Slide 25 text

Anatomy of an ActorSystem

Slide 26

Slide 26 text

INTRODUCTION

Slide 27

Slide 27 text

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

Slide 28

Slide 28 text

VOCABULARY • DroneManager - the (one) thing keeping track of drones • 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.”

Slide 29

Slide 29 text

HIGH-LEVEL ARCHITECTURE OVERVIEW

Slide 30

Slide 30 text

HIGH-LEVEL ARCHITECTURE OVERVIEW

Slide 31

Slide 31 text

AKKA ACTOR BASICS

Slide 32

Slide 32 text

LET’S CODE!

Slide 33

Slide 33 text

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

Slide 34

Slide 34 text

AKKA CLUSTERING AND PERSISTENCE

Slide 35

Slide 35 text

HIGH-LEVEL ARCHITECTURE OVERVIEW

Slide 36

Slide 36 text

LET’S CODE!

Slide 37

Slide 37 text

FAILURE SCENARIO: Node Failure

Slide 38

Slide 38 text

HIGH-LEVEL ARCHITECTURE OVERVIEW

Slide 39

Slide 39 text

No content

Slide 40

Slide 40 text

FAILURE SCENARIO: State Recovery (Akka Persistence)

Slide 41

Slide 41 text

No content

Slide 42

Slide 42 text

http://developer.lightbend.com/docs/akka-commercial-addons/1.0.1/split-brain-resolver.html // application.conf akka.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

Slide 43

Slide 43 text

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

Slide 44

Slide 44 text

• Battle-tested Membership Protocol • Powerful distribution mechanism • Application aware Sharding • Scale-out & Resilience (+2400 nodes) Akka Cluster:

Slide 45

Slide 45 text

• EventSourcing style persistence • Pluggable Journals (Cassandra, SQL, Mongo, Dynamo, and more …) • Trivial to plug in • Enables CQRS-style architectures • Powers AtLeastOnceDelivery Akka Persistence:

Slide 46

Slide 46 text

AKKA STREAMS

Slide 47

Slide 47 text

Fast Publisher Slow Subscriber Akka Streams - BackPressure

Slide 48

Slide 48 text

Subscriber usually has some kind of buffer. Push model

Slide 49

Slide 49 text

Push model

Slide 50

Slide 50 text

Push model

Slide 51

Slide 51 text

Push model

Slide 52

Slide 52 text

What if the buffer overflows? Push model

Slide 53

Slide 53 text

Use bounded buffer, drop messages + require re-sending Push model

Slide 54

Slide 54 text

Kernel does this! Routers do this! (TCP) Use bounded buffer, drop messages + require re-sending Push model

Slide 55

Slide 55 text

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

Slide 56

Slide 56 text

Reactive Streams explained Reactive Streams explained in 1 slide

Slide 57

Slide 57 text

Fast Publisher will send at-most 3 elements. This is pull-based-backpressure. Reactive Streams: “dynamic push/pull”

Slide 58

Slide 58 text

JEP-266 – soon…! public final class Flow { private Flow() {} // uninstantiable @FunctionalInterface public static interface Publisher { public void subscribe(Subscriber subscriber); } public static interface Subscriber { 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 extends Subscriber, Publisher { } }

Slide 59

Slide 59 text

Reactive Streams: goals 1) Avoiding unbounded buffering across async boundaries 2)Inter-op interfaces between various libraries

Slide 60

Slide 60 text

Reactive Streams: goals 1) Avoiding unbounded buffering across async boundaries 2)Inter-op interfaces between various libraries Argh, implementing a correct RS Publisher or Subscriber is so hard!

Slide 61

Slide 61 text

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!

Slide 62

Slide 62 text

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

Slide 63

Slide 63 text

HIGH-LEVEL ARCHITECTURE OVERVIEW

Slide 64

Slide 64 text

LET’S CODE!

Slide 65

Slide 65 text

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

Slide 66

Slide 66 text

AKKA HTTP

Slide 67

Slide 67 text

HIGH-LEVEL ARCHITECTURE OVERVIEW

Slide 68

Slide 68 text

A core feature not obvious to the untrained eye…! Quiz time! TCP is a ______ protocol? Akka HTTP

Slide 69

Slide 69 text

A core feature not obvious to the untrained eye…! Quiz time! TCP is a STREAMING protocol! Akka HTTP

Slide 70

Slide 70 text

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]

Slide 71

Slide 71 text

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, _]

Slide 72

Slide 72 text

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]

Slide 73

Slide 73 text

LET’S CODE!

Slide 74

Slide 74 text

It’s turtles buffers all the way down!

Slide 75

Slide 75 text

Streaming from Akka HTTP

Slide 76

Slide 76 text

Streaming from Akka HTTP

Slide 77

Slide 77 text

Streaming from Akka HTTP No demand from TCP = No demand upstream = Source won’t generate tweets => Bounded memory stream processing!

Slide 78

Slide 78 text

FAILURE SCENARIO 1

Slide 79

Slide 79 text

HIGH-LEVEL ARCHITECTURE OVERVIEW

Slide 80

Slide 80 text

HIGH-LEVEL ARCHITECTURE OVERVIEW

Slide 81

Slide 81 text

HIGH-LEVEL ARCHITECTURE OVERVIEW

Slide 82

Slide 82 text

FAILURE SCENARIO 2

Slide 83

Slide 83 text

HIGH-LEVEL ARCHITECTURE OVERVIEW

Slide 84

Slide 84 text

HIGH-LEVEL ARCHITECTURE OVERVIEW

Slide 85

Slide 85 text

HIGH-LEVEL ARCHITECTURE OVERVIEW

Slide 86

Slide 86 text

• “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:

Slide 87

Slide 87 text

TESTING ASYNCHRONOUS CODE WITH AKKA

Slide 88

Slide 88 text

TestKits and tools provided • Asynchronous is hard • Testing asynchronous code is hard • We’ve prepared plenty tools to make it simple

Slide 89

Slide 89 text

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

Slide 90

Slide 90 text

LET’S CODE!

Slide 91

Slide 91 text

• All major modules come with dedicated TestKit • Actors, Streams, MultiJVM testing (!) • Makes asynchronous code simpler to test Akka Testing:

Slide 92

Slide 92 text

STREAMING INTEGRATION WITH “ALPAKKA”

Slide 93

Slide 93 text

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

Slide 94

Slide 94 text

Alpakka – a community for Stream connectors http://developer.lightbend.com/docs/alpakka/current/ Google Pub/Sub … … … … … … …

Slide 95

Slide 95 text

Alpakka – a community for Stream connectors http://developer.lightbend.com/docs/alpakka/current/

Slide 96

Slide 96 text

Alpakka – a community for Stream connectors http://developer.lightbend.com/docs/alpakka/current/

Slide 97

Slide 97 text

LET’S CODE!

Slide 98

Slide 98 text

• 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”:

Slide 99

Slide 99 text

MONITORING

Slide 100

Slide 100 text

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

Slide 101

Slide 101 text

Example Integration: Datadog

Slide 102

Slide 102 text

• Commercial monitoring • Get the full picture about your applications in production • Highly optimized, fine-tuned by core Lightbend teams Lightbend Monitoring:

Slide 103

Slide 103 text

SUMMING UP

Slide 104

Slide 104 text

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)

Slide 105

Slide 105 text

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

Slide 106

Slide 106 text

WHAT FEATURES ARE COMING SOON?

Slide 107

Slide 107 text

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.

Slide 108

Slide 108 text

MONITORING • Awesome monitoring of asynchronous code • Coming soon: • Tracing across Actors, Futures, HTTP • Smart Cluster Sharding insights • Akka HTTP metrics and events

Slide 109

Slide 109 text

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

Slide 110

Slide 110 text

Lightbend booth / sponsor area O’Reilly’s “Ask the Experts” Henrik’s talk on Wednesday @ 4.50 pm… Catch us here: Catch us here

Slide 111

Slide 111 text

EOF Q/A Konrad Malawski (@ktosopl) - Akka Team Henrik Engström (@h3nk3) - Telemetry Team