2. Konrad `ktoso` Malawski @ Scala Days CPH 2017
Disclaimer:
Parts of this talk is about
under-development “experimental” APIs
which may change slightly.
This is not a strict roadmap,
it is a general outline where we’re headed.
12. ”The actor model in computer science is a
mathematical model of concurrent
computation that treats actors as the universal
primitives of concurrent computation. ”
Wikipedia
The Actor Model
13. and acts on them by:
• Sending messages
• Changing its state / behaviour
• Creating more actors
receives messages
An Actor
14. A concurrency and distribution construct.
an addressable, location-transparent, entity.
An Actor
Actors talk directly to each other.
An ActorSystem is truly peer-to-peer, not client-server.
17. A simple Actor interaction
Could be different threads
or different nodes in cluster.
API remains the same - and always async.
18. Why does it matter?
Could be different threads
or different nodes in cluster.
API remains the same - and always async.
http://www.anandtech.com/show/11464/intel-announces-skylakex-bringing-18core-hcc-silicon-to-consumers-for-1999
21. Binary > Textual Protocols
“The Internet is running in debug mode.”
— Rüdiger Möller
http://java-is-the-new-c.blogspot.de/2014/10/why-protocols-are-messy-concept.html
22. MediaContent {
media = Media {
uri = “http://conference.com/key.mpg"
title = "Keynote"
width = 640
height = 480
format = "video/mpg4"
duration = 18000000
size = 58982400
bitrate = 262144
persons = ["Bill Gates", "Steve Jobs"]
player = JAVA
copyright = null
}
}
images = [
Image {
uri = “http://conference.com/key_large.jpg"
title = "Keynote"
width = 1024
height = 768
size = LARGE
}
Image {
uri = “http://conference.com/key_small.jpg"
title = "Keynote"
width = 320
height = 240
size = SMALL
}
]
Not only JSON: Example data
27. Avoid Java Serialization
Akka uses ProtocolBuffers
for (most*) it’s messages by default.
To completely disable Java Serialization do:
akka.actor.allow-java-serialization = false
(which switches Akka to protobuf completely)
User messages you define your own serializers.
most* – due to wire compatibility some messages, where
some messages did use JavSer in the past
28. Avoid Java Serialization
Good serializers include (but are not limited to):
Kryo, Google Protocol Buffers, SBE,Thrift, JSON if you really want
// dependencies
"com.github.romix.akka" %% "akka-kryo-serialization" % "0.4.0"
// application.conf
extensions = [“com.romix.akka.serialization.kryo.KryoSerializationExtension$"]
serializers {
java = "akka.serialization.JavaSerializer"
kryo = "com.romix.akka.serialization.kryo.KryoSerializer"
}
akka.actor.serialization-bindings {
“com.mycompany.Example”: kryo
. . .
}
[info] ForkJoinActorBenchmark.pingPong java avgt 10 25.464 ± 1.175 us/op
[info] ForkJoinActorBenchmark.pingPong kryo avgt 10 4.348 ± 4.346 us/op
[info] ForkJoinActorBenchmark.pingPong off avgt 10 0.967 ± 0.657 us/op
30. Our binary compatibility story
http://doc.akka.io/docs/akka/current/scala/common/binary-compatibility-rules.html
31. Our binary compatibility story
http://doc.akka.io/docs/akka/current/scala/common/binary-compatibility-rules.html
2.3.1x [2015-09] ->
2.4.x [2015-08] ->
2.5.x [2017-04] -> ...
2.7.x [???] ->
2.8.x [???] ->
3.x [far out still, no need to break APIs]
32. Our binary compatibility story
http://doc.akka.io/docs/akka/current/scala/common/binary-compatibility-rules.html
Binary compatibility != Wire compatibility
/* but we’ll get to that! (hint: Artery) */
33.
34. History of Futures
In Days before Futures got standardised in Scala (~2012).
Their design was heavily influenced by: Akka, Finagle & Scalaz & more…
Archival version @ 2012
http://doc.akka.io/docs/akka/2.0/scala/futures.html
SIP-14 - Futures and Promises
By: Philipp Haller, Aleksandar Prokopec, Heather Miller, Viktor Klang, Roland Kuhn, and Vojin Jovanovic
http://docs.scala-lang.org/sips/completed/futures-promises.html
35.
36. “Best practices are solutions
to yesterdays problems.”
https://twitter.com/FrankBuytendijk/status/795555578592555008
Circuit breaking as substitute of flow-control
37. See also, Nitesh Kant, Netflix @ Reactive Summit
https://www.youtube.com/watch?v=5FE6xnH5Lak
38. See also, Nitesh Kant, Netflix @ Reactive Summit
https://www.youtube.com/watch?v=5FE6xnH5Lak
39. HTTP/1.1 503 Service Unavailable
HTTP/1.1 503 Service Unavailable
Throttling as represented by 503 responses. Client will back-off… but how?
What if most of the fleet is throttling?
42. See also, Nitesh Kant, Netflix @ Reactive Summit
https://www.youtube.com/watch?v=5FE6xnH5Lak
“slamming the breaks”
43. See also, Nitesh Kant, Netflix @ Reactive Summit
https://www.youtube.com/watch?v=5FE6xnH5Lak
“slamming the breaks”
44. See also, Nitesh Kant, Netflix @ Reactive Summit
https://www.youtube.com/watch?v=5FE6xnH5Lak
“slamming the breaks”
45. See also, Nitesh Kant, Netflix @ Reactive Summit
https://www.youtube.com/watch?v=5FE6xnH5Lak
“slamming the breaks”
46. See also, Nitesh Kant, Netflix @ Reactive Summit
https://www.youtube.com/watch?v=5FE6xnH5Lak
“slamming the breaks”
47. See also, Nitesh Kant, Netflix @ Reactive Summit
https://www.youtube.com/watch?v=5FE6xnH5Lak
We’ll re-visit this specific case in a bit :-)
“slamming the breaks”
50. This will lead to the creation
of Akka Streams and Reactive Streams!
We can do better.
51.
52. The heart of Distributed Systems,
built using Akka.
Akka Cluster
53. Akka cluster provides membership
and fault-tolerance for distributed Actors.
- Membership is implemented as epidemic gossip.
- No single point of failure, “Leader” can move to
any of the nodes (deterministically)
- Battle hardened since many years
- Known to scale to 2400 nodes.
Akka Cluster
https://cloudplatform.googleblog.com/2014/01/large-akka-cluster-on-google-compute.html
56. Failure detection using simple heartbeats
often not good enough for production. You can:
- Use Akka Split Brain Resolver (commercial),
multiple split brain scenario resolution strategies
- “Keep majority”, “Keep oldest”, “Static Quorum”
- Perform manual downing
(a safe bet, good if OPS or automated via Nagios etc)
- Roll your own, all required APIs are public
Failure detection is pluggable
https://cloudplatform.googleblog.com/2014/01/large-akka-cluster-on-google-compute.html
59. Back then known as “Spray”,
we joined up and started working
on a streaming-first HTTP server.
Akka HTTP
60. - Fully Typed HTTP model
- So good, other projects use it instead of rolling their own!
(http4s uses Spray’s model.)
- Streaming-focused HTTP server
- Built from the ground up on Akka Streams
- Full Java API (unlike Spray)
- Streaming with WebSockets!
Key features of Akka HTTP
61. Streaming in Akka HTTP
http://doc.akka.io/docs/akka/2.4/scala/stream/stream-customize.html#graphstage-scala
“Framed entity streaming”
http://doc.akka.io/docs/akka/2.4/java/http/routing-dsl/source-streaming-support.html
HttpServer as a:
Flow[HttpRequest, HttpResponse]
62. Streaming in Akka HTTP
HttpServer as a:
Flow[HttpRequest, HttpResponse]
HTTP Entity as a:
Source[ByteString, _]
http://doc.akka.io/docs/akka/2.4/scala/stream/stream-customize.html#graphstage-scala
“Framed entity streaming”
http://doc.akka.io/docs/akka/2.4/java/http/routing-dsl/source-streaming-support.html
63. Streaming in Akka HTTP
HttpServer as a:
Flow[HttpRequest, HttpResponse]
HTTP Entity as a:
Source[ByteString, _]
Websocket connection as a:
Flow[ws.Message, ws.Message]
http://doc.akka.io/docs/akka/2.4/scala/stream/stream-customize.html#graphstage-scala
“Framed entity streaming”
http://doc.akka.io/docs/akka/2.4/java/http/routing-dsl/source-streaming-support.html
77. Using Distributed Data
The focus is on “spreading the data”,
not on the “single entity” like it is in Persistence.
78. Distributed Data visualised
You supply a write consistency level which has the following meaning:
• WriteLocal the value will immediately only be written to the local replica, and
later disseminated with gossip
• WriteTo(n) the value will immediately be written to at least n replicas, including
the local replica
• WriteMajority the value will immediately be written to a majority of replicas, i.e.
at least N/2 + 1 replicas, where N is the number of nodes in the cluster (or cluster
role group)
• WriteAll the value will immediately be written to all nodes in the cluster (or all
nodes in the cluster role group)
86. Suddenly everyone jumped on the word “Stream”.
Akka Streams / Reactive Streams started end-of-2013.
“Streams”
* when put in “” the word does not appear in project name, but is present in examples / style of APIs / wording.
87. Suddenly everyone jumped on the word “Stream”.
Akka Streams / Reactive Streams started end-of-2013.
The word “Stream” is used in many contexts/meanings
Akka Streams
Reactive Streams
RxJava “streams”*
Spark Streaming
Apache Storm “streams”*
Java Steams (JDK8)
Reactor “streams”*
Kafka Streams
ztellman / Manifold (Clojure)
* when put in “” the word does not appear in project name, but is present in examples / style of APIs / wording.
Apache GearPump “streams”
Apache [I] Streams (!)
Apache [I] Beam “streams”
Apache [I] Quarks “streams”
Apache [I] Airflow “streams” (dead?)
Apache [I] Samza
Scala Stream
Scalaz Streams, now known as FS2
Swave.io
Java InputStream / OutputStream / … :-)
2017年: 安定版。リアクティブストリーム付きの JDK9。
88. “Stream”
What does it mean?!
• Possibly infinite datasets (“streams”)
• “Streams are NOT collections.”
• Processed element-by-element
• Element could mean “byte”
• More usefully though it means a specific type “T”
• Asynchronous processing
• Asynchronous boundaries (between threads)
• Network boundaries (between machines)
2017年: 安定版。リアクティブストリーム付きの JDK9。
89. Where does Akka Stream fit?
Akka Streams specifically fits,
if you answer yes to any of these:
• Should it take on public traffic?
• Processing in hot path for requests?
• Integrate various technologies?
• Protect services from over-load?
• Introspection, debugging, excellent Akka integration?
• (vs. other reactive-stream impls.)
90. How do I pick which “streaming” I need?
Kafka serves best as a transport
for pub-sub across services.
• Note that Kafka Streams (db ops are on the node
is rather, different than the Reactive Kafka client
• Great for cross-service communication
instead of HTTP Request / Reply
Kafka はサービス間の pub-sub 通信に向いている
HTTP の代わりにサービス間の通信に使う
91. How do I pick which “streaming” I need?
Spark has vast libraries for ML or join etc ops.
• It’s the “hadoop replacement”.
• Spark Streaming is windowed-batches
• Latency anywhere up from 0.5~1second
• Great for cross-service communication
instead of HTTP Req/Reply
Spark は機械学習系が充実している
92. Oh yeah, there’s JDK8 “Stream” too!
Terrible naming decision IMHO, since Java’s .stream()
• Geared for collections
• Best for finite and known-up-front data
• Lazy, sync/async (async rarely used)
• Very (!) hard to extend
It’s the opposite what we talk about in Streaming
systems!
It’s more:“bulk collection operations”
Also known as… Scala collections API (i.e. Iterator
JDK8 の Stream はイテレータ的なもの
93. What about JDK9 “Flow”?
JDK9 introduces java.util.concurrent.Flow
• Is a 1:1 copy of the Reactive Streams interfaces
• On purpose, for people to be able to impl. it
• Does not provide useful implementations
• Is only the inter-op interfaces
• Libraries like Akka Streams implement RS,
and expose useful APIs for you to use.
JDK9 の Flow はリアクティブ・ストリーム
94. A fundamental building block.
Not end-user API by itself.
reactive-streams.org
Reactive Streams
97. Reactive Streams - story: 2013’s impls
2014–2015:
Reactive Streams Spec & TCK
development, and implementations.
1.0 released on April 28th 2015,
with 5+ accompanying implementations.
2015
Included in JDK9 via JEP-266 “More Concurrency Updates”
download.java.net/java/jdk9/docs/api/java/util/concurrent/Flow.html
104. Fast Publisher will send at-most 3
elements. This is pull-based-
backpressure.
Reactive Streams: “dynamic push/pull”
105. 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> {
}
}
106. Reactive Streams: goals
1) Avoiding unbounded buffering across async boundaries
2) Inter-op interfaces between various libraries
107. 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!
108. 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!
109. 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
110. Akka Streams in 20 seconds:
val firstString: Future[String] =
Source.single(1)
.map(_.toString())
.runWith(Sink.head)
123. Akka HTTP as default backend for Play
Goal is not to “beat Netty*”
but to keep perf
while adding features.
Future:
- Shared Typed HTTP Model
- Shared Monitoring
- Shared performance work
TL;DR; == Shared efforts
* We <3 Netty.
http://playframework.github.io/prune/
Ofc: Netty backend remains available.
127. The journey to Akka Typed
Ancient API, deprecated“Typed Actor” API
Goal was to expose what Java developers knew.
128. The journey to Akka Typed
Old “TypedActor” experimental in 2.3, removed
Upsides:
- Easily bridge to “non-Akka” / “non-Reactive” apps
- type-safe
- “easy” (not necessarily a good thing)
Downsides:
- Reflection, 10x slow-down compared to UntypedActor
- “RPC”-ish, not true to the core messaging
- Not true to Akka’s core principle: Messaging
130. The journey to Akka Typed
“Typed Channels” experimental in 2.3, removed
131. The journey to Akka Typed
“Typed Channels” experimental in 2.3, removed
Upsides:
- completely type-safe
- very expressive
Downsides:
- Too complex, many new operators
- Had to rely on scala macros
- “sender” difficult to solve
135. Akka Typed
try it now, 2.5.2
from repo.akka.io/snapshots
2 styles, 100% awesome.
Full Java & Scala API, as usual.
Actor.mutable – similar to current Actors, Behavior is a class
Actor.immutable – more functional style, recommended
136. Akka Typed
Main user-facing changes:
ActorRef[T] typed ActorRefs.
Core concept is Behavior[T]
which can be freely composed.
You always “become(Behavior)”, by returning Behavior.
sender() is gone,
not possible to type it well.
sender was trouble anyway, so that’s good!
141. Akka Typed
Actor.immutable (Scala)
Don’t worry, Java will eventually get pattern matching:
http://mail.openjdk.java.net/pipermail/amber-spec-experts/2017-April/000033.html
Java adopting Scala features confirms Scala’s design.
…but, until then we provide you with helpers and DSLs:
143. Akka Typed
try it now, 2.5.99-TYPED-M1
from repo.akka.io/snapshots
Learn more:
from the docs:
http://doc.akka.io/docs/akka/snapshot/scala/typed.html
and the blog:
1. Akka Typed: Hello World in the new API
2. Akka Typed: Coexistence
3. Akka Typed: Mutable vs. Immutable
4. Akka Typed: Protocols
5. Akka Typed: Supervision
6. Akka Typed: Lifecycle and Watch
7. Akka Typed: Timers
144.
145. A community for Streams connectors
Alpakka – a community for Stream connectors
Alp
146. Alpakka – a community for Stream connectors
http://developer.lightbend.com/docs/alpakka/current/
147. Alpakka – a community for Stream connectors
http://developer.lightbend.com/docs/alpakka/current/
148. Alpakka – a community for Stream connectors
http://developer.lightbend.com/docs/alpakka/current/
153. Ecosystem that solves problems
> (is greater than)
solving all the problems ourselves
154.
155. Way more than just “we changed the transport.”
New Remoting:
Artery
156. Artery
Next generation remoting layer for Akka.
• Aeron (UDP) based instead of TCP,
• Advanced automatic ActorRef Compression
• Dedicated “lanes” for certain messages / destinations
• Almost alloc-free in steady-state (except deserialization)
158. How to use Artery?
single option,
no new artifacts
159. “Steady state” operation almost alloc-free
Serialize Deserialize
compression compression
package readpackage write
AkkaStreams
(allocationfree)
Pooled envelopes
Pooled ByteBuffers
Deserialize allocates
Pooled ByteBuffers
no allocations
Caches for ActorRefs etc
no allocations in steady state
162. Artery: ActorRef / Manifest Compression
In this case ActorRef compression reduced the size of a small
envelope size by 74% - from 162 to 42 bytes (sic!).
163.
164. Multi Data Center
Customers increasingly have global-scale apps,
so we’re looking into advanced Multi-DataCenter scenarios.
165. Multi Data Center
These are just ideas.
Talk to me, we’re gathering use cases.
- Active + Active ???
- Locality aware Cluster Sharding ???
- Entity “owner” Datacenter ???
- Talk to us about your use cases :)
- …?
168. Lightbend Paradox - docs engine
We know, we know:“Yet another docs engine”
Built-in scala-fiddle support
Akka.js => run Akka docs examples in browser
169. Lightbend Paradox - docs engine
Much much easier to contribute now.
Zero dependencies just type “paradox”
Markdown instead of restructured text!
Built in capabilities to link github / scaladoc
Simple way to build multi-prog-lang docs @scala/@java
177. Tracing Akka with Jaeger or Zipkin
Lightbend Monitoring
https://developer.lightbend.com/docs/cinnamon/latest/extensions/opentracing.html
178. Tracing across nodes
Lightbend Monitoring
https://developer.lightbend.com/docs/cinnamon/latest/extensions/opentracing.html
Already tracing across network transparently,
Akka HTTP coming soon, as will Futures.
182. Ports to other platforms
Not supported by Lightbend, community projects.
http://getakka.net/ http://akka-js.org/
A sign that Akka is interesting and worth porting:
188. Thanks everyone
Committers from the Community!
Jan Pustelnik
Krzysiek Ciesielski,
Alexey Romanchuk,
Heiko Seeberger,
Josep Prat,
Jan Ypma,
André Rüdiger,
Jonas Fonseca
…
and hundreds of contributors
Thanks!
190. 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.
Obligatory “read my book!” slide :-)
191. Metal Gear Solid illustrations
by Lap Pun Cheung
http://www.lpcheung.com/metal-gear-solid/
Hand drawn illustrations:
by myself, CC-BY-NC
Artwork links