Top Banner
Be Reactive!
40

Be Reactive! at Functional and Reactive Programmers Group, Kaiserslautern March 31 2015

Jul 15, 2015

Download

Software

Lutz Hühnken
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Be Reactive! at Functional and Reactive Programmers Group, Kaiserslautern March 31 2015

Be Reactive!

Page 2: Be Reactive! at Functional and Reactive Programmers Group, Kaiserslautern March 31 2015

What’s happening?

Page 3: Be Reactive! at Functional and Reactive Programmers Group, Kaiserslautern March 31 2015
Page 4: Be Reactive! at Functional and Reactive Programmers Group, Kaiserslautern March 31 2015
Page 5: Be Reactive! at Functional and Reactive Programmers Group, Kaiserslautern March 31 2015
Page 6: Be Reactive! at Functional and Reactive Programmers Group, Kaiserslautern March 31 2015
Page 7: Be Reactive! at Functional and Reactive Programmers Group, Kaiserslautern March 31 2015

Be Reactive! SUG KL March 2015

Elastic

• Ability to scale, but also • be efficient, leverage modern multi- / many-core hardware • Scale up and down - Clusters need to support joining and

leaving of nodes • To scale, every part must scale - no single point of failure, no

contention

7

Page 8: Be Reactive! at Functional and Reactive Programmers Group, Kaiserslautern March 31 2015

Be Reactive! SUG KL March 2015

Resilient

• Failure is embraced as a regular state in the system • Resilience is a first-class concept in your software • Failure is detected, isolated, and managed • Applications can recover from failure of parts (self-heal)

8

Page 9: Be Reactive! at Functional and Reactive Programmers Group, Kaiserslautern March 31 2015

Be Reactive! SUG KL March 2015

Before we get to message-driven

• Let’s talk about • Multi-Threading • I/O • Distribution • Deployment

9

Page 10: Be Reactive! at Functional and Reactive Programmers Group, Kaiserslautern March 31 2015

Threads vs. task level concurrency

Page 11: Be Reactive! at Functional and Reactive Programmers Group, Kaiserslautern March 31 2015

Be Reactive! SUG KL March 2015

A supposedly great idea

• Threads are • lightweight processes • easier programming model, no IPC

11

Page 12: Be Reactive! at Functional and Reactive Programmers Group, Kaiserslautern March 31 2015

Be Reactive! SUG KL March 2015

Lightweight?

12

Slide stolen from John Rose, Java VM Architect, JFokus, Stockholm, February 2015

Page 13: Be Reactive! at Functional and Reactive Programmers Group, Kaiserslautern March 31 2015

Be Reactive! SUG KL March 2015

The problem with Threads

13

They discard the most essential and appealing properties of sequential computation: understandability, predictability, and determinism.

Threads, as a model of computation, are wildly nondeterministic, and the job of the programmer becomes one of pruning that nondeterminism.

Page 14: Be Reactive! at Functional and Reactive Programmers Group, Kaiserslautern March 31 2015

+

Let’s talk about Multi-Threading

Page 15: Be Reactive! at Functional and Reactive Programmers Group, Kaiserslautern March 31 2015

Be Reactive! SUG KL March 2015

Threads

• not efficient • memory consumption • cost for context switch • shared memory bad for modern NUMA architectures • locks lead to contention

• not a good programming model • shared mutual state is difficult to reason about

15

Page 16: Be Reactive! at Functional and Reactive Programmers Group, Kaiserslautern March 31 2015

Be Reactive! SUG KL March 2015

Tasks

Better • task level (sub-thread) concurrency • share nothing approach • # threads ~ # of cores

• Example: Actors, Play Actions

16

Page 17: Be Reactive! at Functional and Reactive Programmers Group, Kaiserslautern March 31 2015

Blocking vs. non-blocking I/O

Page 18: Be Reactive! at Functional and Reactive Programmers Group, Kaiserslautern March 31 2015

Be Reactive! SUG KL March 2015

High concurrency matters

18

But there’s one thing we can all agree on: At high levels of concurrency (thousands of connections) your server needs to go to asynchronous non-blocking. [..] any part of your server code blocks you’re going to need a thread. And at these levels of concurrency, you can’t go creating threads for every connection.

From https://strongloop.com/strongblog/node-js-is-faster-than-java/

Page 19: Be Reactive! at Functional and Reactive Programmers Group, Kaiserslautern March 31 2015

Be Reactive! SUG KL March 2015

Blocking I/O, Thread-per-request

19

From https://strongloop.com/strongblog/node-js-is-faster-than-java/

Page 20: Be Reactive! at Functional and Reactive Programmers Group, Kaiserslautern March 31 2015

Be Reactive! SUG KL March 2015

Non-blocking I/O

20

Note: Single Thread is crazy! What about your other cores?

Page 21: Be Reactive! at Functional and Reactive Programmers Group, Kaiserslautern March 31 2015

Be Reactive! SUG KL March 2015

Non-blocking

21

For task level (sub-thread level) concurrency • each thread is responsible for n tasks • and that n might be pretty big

You don’t want to block such a thread with blocking I/O

What if you must? Separate!

Page 22: Be Reactive! at Functional and Reactive Programmers Group, Kaiserslautern March 31 2015

Distributed Transactions vs. EC

Page 23: Be Reactive! at Functional and Reactive Programmers Group, Kaiserslautern March 31 2015

Be Reactive! SUG KL March 2015

Life beyond Distributed Transactions

23

In general, application developers simply do not implement large scalable applications assuming distributed transactions. When they attempt to use distributed transactions, the projects founder because the performance costs and fragility make them impractical. [..] Instead, applications are built using different techniques which do not provide the same transactional guarantees but still meet the needs of their businesses.

Page 24: Be Reactive! at Functional and Reactive Programmers Group, Kaiserslautern March 31 2015

24

Want Almost-Infinite Scaling• More of everything… Year by year, bigger and bigger• If it fits on your machines, multiply by 10, if that fits, multiply by 1000…• Strive to scale almost linearly (N log N for some big log).

Assumptions(Don’t Have to Prove These… Just Plain Believe Them)

Grown-Ups Don’t Use Distributed Transactions•The apps using distributed transactions become too fragile…• Let’s just consider local transactions. ! Multiple disjoint scopes of serializability

Want Scale-Agnostic Apps • Two layers to the application: scale-agnostic and scale-aware• Consider scale-agnostic API

Scale Agnostic Code

Scale-Aware-Code

Application

Upper Layer

Lower Layer

Scale Agnostic API

Page 25: Be Reactive! at Functional and Reactive Programmers Group, Kaiserslautern March 31 2015

Be Reactive! SUG KL March 2015

Distributed Transactions

25

Distributed Transactions („2PC“) are a source of unnecessary failure and of contention.

It can usually be avoided. In the aforementioned paper by Pat Helland, he shows how to use local transactions and at-least-once delivery instead.

The data storage landscape is changing, moving towards event sourcing and immutability. This is a great match for reactive systems.

Page 26: Be Reactive! at Functional and Reactive Programmers Group, Kaiserslautern March 31 2015

App Servers vs. Containerless

Page 27: Be Reactive! at Functional and Reactive Programmers Group, Kaiserslautern March 31 2015

Be Reactive! SUG KL March 2015 27

Page 28: Be Reactive! at Functional and Reactive Programmers Group, Kaiserslautern March 31 2015

Be Reactive! SUG KL March 2015 28

Page 29: Be Reactive! at Functional and Reactive Programmers Group, Kaiserslautern March 31 2015

Be Reactive! SUG KL March 2015

Java EE Application Servers

29

Servlet API was developed for thread-per-request, synchronous I/O.

Application servers are not of much use anyway, nobody uses them as containers for multiple applications.

Go containerless.

Page 30: Be Reactive! at Functional and Reactive Programmers Group, Kaiserslautern March 31 2015

Summary

Page 31: Be Reactive! at Functional and Reactive Programmers Group, Kaiserslautern March 31 2015

Be Reactive! SUG KL March 2015

Checklist

31

If

• Threads are the smallest unit of concurrency in your code, or

• You use blocking I/O (without clear separation), or • You use 2-phase-commit across systems, or • You run in a Java EE Application Server

Then your application is not reactive.

Page 32: Be Reactive! at Functional and Reactive Programmers Group, Kaiserslautern March 31 2015

Be Reactive! SUG KL March 2015

Want list

32

• Task-level (sub-thread level) concurrency • Non-blocking I/O • Distribution • Containerless

Bonus: A simple, unified programming model

Page 33: Be Reactive! at Functional and Reactive Programmers Group, Kaiserslautern March 31 2015

.. gives you all that

Page 34: Be Reactive! at Functional and Reactive Programmers Group, Kaiserslautern March 31 2015

Be Reactive! SUG KL March 2015

Message-Driven

• Loosely coupled architecture, easier to extend, maintain, evolve • Asynchronous and non-blocking • Concurrent by design, immutable state • Lower latency and higher throughput

34

Page 35: Be Reactive! at Functional and Reactive Programmers Group, Kaiserslautern March 31 2015
Page 36: Be Reactive! at Functional and Reactive Programmers Group, Kaiserslautern March 31 2015
Page 37: Be Reactive! at Functional and Reactive Programmers Group, Kaiserslautern March 31 2015

Go Reactive! Swisscom March 2015 37

Page 38: Be Reactive! at Functional and Reactive Programmers Group, Kaiserslautern March 31 2015

Go Reactive! Swisscom March 2015

• We want to be asynchronous and non-blocking

• We need to ensure that our data is protected without locks

• We want to compose individual tasks into broader business logic

• Functional programming is critical to meeting these needs • Declarative • Immutable • Referentially transparent • Pure functions that only have inputs and outputs

38

Functional programming is key

Page 39: Be Reactive! at Functional and Reactive Programmers Group, Kaiserslautern March 31 2015

Be Reactive! SUG KL March 2015

Where can I find that?

39

Page 40: Be Reactive! at Functional and Reactive Programmers Group, Kaiserslautern March 31 2015

Be Reactive! SUG KL March 2015 40

Want to learn more? http://www.typesafe.com http://go.typesafe.com

Questions? [email protected]

@lutzhuehnken on Twitter