69
What is Reactive Programming? An Overview of the Reactive Programming Paradigm

What is Reactive programming?

Embed Size (px)

DESCRIPTION

A general overview of Reactive programming, including the history of Reactive application development and why this paradigm is the right fit for developing modern software. Explores examples from JavaScript, Scala, Erlang, and Akka.

Citation preview

Page 1: What is Reactive programming?

What is Reactive Programming?An Overview of the Reactive Programming Paradigm

Page 2: What is Reactive programming?

A Pre-Reactive World.

Page 3: What is Reactive programming?

–Dan Kegel: The C10k Problem, 1999

“It’s time for web servers to handle ten thousand clients simultaneously.”

Page 4: What is Reactive programming?

Expectations in the year 2000

4

• Limited connectivity

Page 5: What is Reactive programming?

Expectations in the year 2000

5

• Limited connectivity

• Snapshots of data on hard drives,

centralized databases, etc

Page 6: What is Reactive programming?

Expectations in the year 2000

6

• Limited connectivity

• Snapshots of data on hard drives,

centralized databases, etc

• Response times in seconds

Page 7: What is Reactive programming?

Expectations in the year 2000

7

• Limited connectivity

• Snapshots of data on hard drives,

centralized databases, etc

• Response times in seconds

• Downtime all the time

Page 8: What is Reactive programming?

Hardware in the year 2000

8

• A single machine with a single core

Page 9: What is Reactive programming?

Hardware in the year 2000

9

• A single machine with a single core

• Limited networking — e.g, a mix of broadband, dialup internet, FidoNet

Page 10: What is Reactive programming?

Hardware in the year 2000

10

• A single machine with a single core

• Limited networking — e.g, a mix of broadband, dialup internet, FidoNet

• Expensive memory, CPU, hard drives

Page 11: What is Reactive programming?

Hardware in the year 2000

11

• A single machine with a single core

• Limited networking — e.g, a mix of broadband, dialup internet, FidoNet

• Expensive memory, CPU, hard drives

• Small data

Page 12: What is Reactive programming?

–Dan Kegel: The C10k Problem, 1999

“In 1999, one of the busiest FTP sites, cdrom.com, handled 10,000 clients simultaneously through a Gigabit Ethernet pipe.”

Page 13: What is Reactive programming?

Fast forward 14 years.

Page 14: What is Reactive programming?

Expectations have changed. Hardware has changed.

Page 15: What is Reactive programming?

Expectations in the year 2014

15

• Limited connectivity ⇨ always connected

Page 16: What is Reactive programming?

Expectations in the year 2014

16

• Limited connectivity ⇨ always connected

• Snapshots of small data ⇨ real-time streams, distributed big data

Page 17: What is Reactive programming?

Expectations in the year 2014

17

• Limited connectivity ⇨ always connected

• Snapshots of small data ⇨ real-time streams, distributed big data

• Response times in seconds ⇨ response times in milli/microseconds

Page 18: What is Reactive programming?

Expectations in the year 2014

18

• Limited connectivity ⇨ always connected

• Snapshots of small data ⇨ real-time streams, distributed big data

• Response times in seconds ⇨ response times in milli/microseconds

• Downtime all the time ⇨ downtime measured in seconds

Page 19: What is Reactive programming?

Hardware in the year 2014

19

• A single machine ⇨ clusters of machines

Page 20: What is Reactive programming?

Hardware in the year 2014

20

• A single machine ⇨ clusters of machines

• A single core ⇨ multiple cores

Page 21: What is Reactive programming?

Hardware in the year 2014

21

• A single machine ⇨ clusters of machines

• A single core ⇨ multiple cores

• Slow networks ⇨ fast networks

Page 22: What is Reactive programming?

Hardware in the year 2014

22

• A single machine ⇨ clusters of machines

• A single core ⇨ multiple cores

• Slow networks ⇨ fast networks

• Small data snapshots ⇨ big data streams

Page 23: What is Reactive programming?

–Scott Andreas, Urban Airship

“If one node can only serve 10,000 clients, the venture is cost-prohibitive from the start – just one million devices would require a fleet of 100

servers.”

Page 24: What is Reactive programming?

In 2010, Urban Airship used Java NIO to successfully test 512,000+ concurrent connections on a single 2.5 GB EC2 instance.

Page 25: What is Reactive programming?

Realities of modern software development

25

• Applications are distributed by default

Page 26: What is Reactive programming?

Realities of modern software development

26

• Applications are distributed by default

• Applications are composed from other applications

Page 27: What is Reactive programming?

Realities of modern software development

27

• Applications are distributed by default

• Applications are composed from other applications

• Performance expectations — a slow application is a dead application

Page 28: What is Reactive programming?

Realities of modern software development

28

• Applications are distributed by default

• Applications are composed from other applications

• Performance expectations — a slow application is a dead application

• UIs must adapt to multiple screen sizes and types of input

Page 29: What is Reactive programming?

Realities of modern software development

29

• Applications are distributed by default

• Applications are composed from other applications

• Performance expectations — a slow application is a dead application

• UIs must adapt to multiple screen sizes and types of input

• Developers can no longer avoid parallelism and concurrency

Page 30: What is Reactive programming?

C10k ⇨ C500k

Page 31: What is Reactive programming?

Not so fast…

Page 32: What is Reactive programming?

–Mihai Rotaru, 2013, discussing MigratoryData WebSocket server

“MigratoryData scales up to 12 million concurrent users from a single Dell PowerEdge R610 server while pushing up to 1.015 Gbps live data

(each user receives a 512-byte message every minute).”

Page 33: What is Reactive programming?

C10k ⇨ C12m

Page 34: What is Reactive programming?

What is Reactive Programming?

Page 35: What is Reactive programming?

–Jonas Bonér

“Instead of the latest framework of the day or the latest buzzword, reactive brings the talk back to core principles; the design, the

architecture, the way we think about building software.”

Page 36: What is Reactive programming?

Core principles of reactive software

36

• React to events ⇨ Event-driven

• React to load ⇨ Scalable

• React to failure ⇨ Resilient

• React to users ⇨ Responsive

• Reactive ⇨ Event-driven, Scalable, Resilient, Responsive

Page 37: What is Reactive programming?

Reactive

37

Page 38: What is Reactive programming?

re·spon·sive !

“Reacting quickly and positively.” !

synonyms: quick to react to, reactive to, receptive to, open to suggestions about, amenable to, flexible to, sensitive to, sympathetic to

Page 39: What is Reactive programming?

Responsive — flexibility

39

Progressive UI enhancement

• Never allow slow external dependencies to degrade the user experience

Blue skies

• Never assume blue skies, assume grey skies

Page 40: What is Reactive programming?

Responsive — quick to react

40

Beware of blocking IO!

• Asynchronous — non-blocking, blocking

• Synchronous — non-blocking, blocking

• Do you know what type your web framework implements?

Perceived quality • Perception of responsiveness is key, feel counts!

• Never, ever perform blocking IO on the UI thread!

Page 41: What is Reactive programming?

Responsiveness !

Being quick to react to all users of a system — under blue skies and grey skies — in order to ensure a consistent experience.

Page 42: What is Reactive programming?

scal·able !

“Capable of being easily expanded or upgraded on demand.” !

synonyms: ascendable, climbable, extensible, expandable, expansible, adaptable, elastic, flexible

Page 43: What is Reactive programming?

Scalable

43

Important terms • Performance — a measure of your response time

• Load — impacts performance if not handled properly

Dealing with load • Scale up — faster CPU, more memory, bigger hard drive

• Scale out — distribute work across nodes and clusters

Page 44: What is Reactive programming?

Scalability !

The capability of your system to be easily upgraded on demand in order to ensure responsiveness under various load conditions.

Page 45: What is Reactive programming?

re·sil·ient !

“Able to withstand or recover quickly from difficult conditions.” !

synonyms: strong, tough, hardy

Page 46: What is Reactive programming?

Resilient

46

Bulkheads • Partitioning — partition thread groups for different functionality

Backpressure • Throttling — wait for a consumer to demand more work

Circuit Breakers • Stability — Failing external dependencies shouldn’t bring down an entire app

Page 47: What is Reactive programming?

Resiliency !

The proper application of fundamental design and architecture principles to ensure responsiveness under grey skies.

Page 48: What is Reactive programming?

e·vent !

“Something that happens or is regarded as happening; an occurrence, especially one of some importance.”

!

driv·en !

“Propelled or motivated by something.”

Page 49: What is Reactive programming?

Traditional concurrency models

49

Thread based concurrency with objects and shared state • Imperative, sequential execution

• Most common programming paradigm

• Threads for concurrency

• Difficult to reason about, error-prone

• Implementations — most OO based web frameworks, languages, and toolkits

Page 50: What is Reactive programming?

Traditional concurrency models

50

Software transactional memory (STM) • Immutable values

• Atomicity, consistency, isolation and durability

• Cannot perform any operation that cannot be undone, including (most) I/O

• Think command pattern/interface

• Implementations — Haskell STM (native), libs for Scala, Java, OCaml, Clojure, etc

Page 51: What is Reactive programming?

Share nothing concurrency

51

Event driven concurrency • Thread based event loops

• Often single threaded

• Implementations — Node.js, Twisted Python, EventMachine (Ruby)

Page 52: What is Reactive programming?

Share nothing concurrency

52

Actor based concurrency • Mutable state, but completely isolated

• State only mutated by the actor

• Asynchronous message passing between actors using mailboxes

• Thread based actors or event based actors

• Implementations — Erlang, Akka

Page 53: What is Reactive programming?

Event driven !

An event driven architecture forces programmers to deal with scalability and resilience at a code level in order to facilitate responsiveness.

Page 54: What is Reactive programming?

re·ac·tive !

“Responsive to stimulus.” !

synonyms: active, aware, conscious, receptive, sensitive, acknowledging, alive, answering

Page 55: What is Reactive programming?

Reactive

55

Page 56: What is Reactive programming?

A quick code example.

Page 57: What is Reactive programming?

–@davidlohr

A programmer had a problem. They thought to themselves, "I know, I'll solve it with threads". have Now problems two they.

Page 58: What is Reactive programming?

Non-deterministic — mutable state, threads, locks

58

var a = foo.getB() + bar.getC()!System.out.println(a) // “42”!a = foo.getB() + bar.getC()!System.out.println(a) // Who knows?

Page 59: What is Reactive programming?

Non-deterministic — mutable state, threads, locks

59

var a = foo.getB() + bar.getC()!System.out.println(a) // “42”!a = foo.getB() + bar.getC()!System.out.println(a) // Who knows?!!

• The value of a may or may not still be 42

• Is another thread is mutating foo or bar?

• Pull-based — getB() and getC() must be explicitly invoked

• Later mutation to foo and bar doesn’t effect a

• Based on time and space

Page 60: What is Reactive programming?

Why is non-determinism the root of all evil?

60

Key terms • Parallelism — when at least two threads are executing simultaneously

• Concurrency — when at least two threads are making progress

Mutable state + threads + locks • Code that is error prone and difficult to reason about

• Extremely difficult to implement parallelism and concurrency with MuSTL

Page 61: What is Reactive programming?

Akka is a toolkit and runtime for building highly concurrent, distributed, and fault tolerant event-driven applications on the JVM.

Page 62: What is Reactive programming?

A simple Akka code example

62

class GameActor extends Actor {! def receive = {! case handshakeRequest: HandshakeRequest => ! sender ! computeHandshakeResponse(handshakeRequest)! case turnMessage: TurnMessage => ! sender ! computeTurnResponse(turnMessage) ! }!}!!

• Time, space, mutation ⇨ flow, events, messages

Page 63: What is Reactive programming?

Akka Actors

63

• Process messages asynchronously

• Pattern match against message types

• Focus on flow and values — not objects, threads, and locks

• Higher-level of abstraction — easier to write concurrent, distributed systems

• Think observable/observer and pub/sub

• Influenced by the Erlang actor model

Page 64: What is Reactive programming?

Erlang has embraced the tenets of reactive programming since 1985.

Page 65: What is Reactive programming?

Erlang characteristics and use cases

65

• Share nothing architecture — processes cannot share data

• Pure message passing — copy all the data you need in the messages

• Crash detection and recovery — things will crash, so let them crash then recover

Page 66: What is Reactive programming?

Erlang characteristics and use cases

66

Popular Erlang Software • Wings 3D — 3D modelling • Ejabberd — instant messaging server • CouchDB — document database • RabbitMQ — enterprise-y messaging

Erlang in Production • Ericsson — 3G mobile networks

• Amazon — SimpleDB, EC2

• Facebook — back-end chat server

• T-Mobile — SMS and authentication

Page 67: What is Reactive programming?

–Jonas Bonér

“Reactive looks back in history. It doesn't ignore the goldmine of knowledge that's been around for 30 to 40 years, but rather tries to bring

it into new context.”

Page 68: What is Reactive programming?

Sources and credits

68

• Jonas Bonér — Reactive Supply to Changing Demand

• Joe Armstrong — K things I know about building Resilient Reactive Systems

• Benjamin Erb — Concurrent Programming for Scalable Web Architectures

• Rich Hickey — The Value of Values

• Plus various talks, papers, books, blog posts, and tweets by…

• Dean Wampler, Erik Meijer, Martin Odersky, Martin Thompson, etc

Page 69: What is Reactive programming?

©Typesafe 2014 – All Rights Reserved