69
Reactive summit - 22/10/18 Reactive Software with elegance Reactive design patterns for microservices on multicore [email protected]

Reactive Software with elegance - Lightbend · Reactive summit - 22/10/18 Reactive Software with elegance Reactive design patterns for microservices on multicore [email protected]

  • Upload
    others

  • View
    23

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Reactive Software with elegance - Lightbend · Reactive summit - 22/10/18 Reactive Software with elegance Reactive design patterns for microservices on multicore charly.bechara@tredzone.com

Reactive summit - 22/10/18

Reactive Software with elegance

Reactive design patterns for microserviceson multicore

[email protected]

Page 2: Reactive Software with elegance - Lightbend · Reactive summit - 22/10/18 Reactive Software with elegance Reactive design patterns for microservices on multicore charly.bechara@tredzone.com

Outline

Microservices on multicore

Reactive Multicore Patterns

Modern Software Roadmap

2

Page 3: Reactive Software with elegance - Lightbend · Reactive summit - 22/10/18 Reactive Software with elegance Reactive design patterns for microservices on multicore charly.bechara@tredzone.com

MICROSERVICES ON MULTICORE

1

3

Page 4: Reactive Software with elegance - Lightbend · Reactive summit - 22/10/18 Reactive Software with elegance Reactive design patterns for microservices on multicore charly.bechara@tredzone.com

Microservices on MulticoreMicroservice architecture with actor model

ActorµService Message passing

µService µService

4

Page 5: Reactive Software with elegance - Lightbend · Reactive summit - 22/10/18 Reactive Software with elegance Reactive design patterns for microservices on multicore charly.bechara@tredzone.com

Microservices on MulticoreFast data means more inter-communication

Stream

Real time event processing

Communications

Co

mp

uta

tio

ns

Batch

Highly Interconnected Workflows

Fast Data

5

Page 6: Reactive Software with elegance - Lightbend · Reactive summit - 22/10/18 Reactive Software with elegance Reactive design patterns for microservices on multicore charly.bechara@tredzone.com

Microservices on MulticoreMicroservice architecture

µService µService

6

ActorµService Message passing

Page 7: Reactive Software with elegance - Lightbend · Reactive summit - 22/10/18 Reactive Software with elegance Reactive design patterns for microservices on multicore charly.bechara@tredzone.com

Microservices on MulticoreMicroservice architecture + Fast Data

µService µService

7

ActorµService Message passing New interactions

Page 8: Reactive Software with elegance - Lightbend · Reactive summit - 22/10/18 Reactive Software with elegance Reactive design patterns for microservices on multicore charly.bechara@tredzone.com

Microservices on MulticoreMicroservice architecture + Fast Data

µService µService

8

ActorµService Message passing New interactions More interactions

Page 9: Reactive Software with elegance - Lightbend · Reactive summit - 22/10/18 Reactive Software with elegance Reactive design patterns for microservices on multicore charly.bechara@tredzone.com

Microservices on MulticoreMore microservices should run on the same muticore machine

machine

9

Page 10: Reactive Software with elegance - Lightbend · Reactive summit - 22/10/18 Reactive Software with elegance Reactive design patterns for microservices on multicore charly.bechara@tredzone.com

Microservices on MulticoreMicroservice architecture + Fast Data + Multicore

+

machine

Core10

µService µService

Page 11: Reactive Software with elegance - Lightbend · Reactive summit - 22/10/18 Reactive Software with elegance Reactive design patterns for microservices on multicore charly.bechara@tredzone.com

Microservices on MulticoreMicroservice architecture + Fast Data + Multicore

+

machine

σ=0, κ=0

Perfect scalability (N)

σ>>0, κ=0

Contention impact (σ)

σ>>0, κ>0

Coherency impact (κ)

Universal Law of Scalability (Gunther law)Performance model of a system based on queueing theory

11

Page 12: Reactive Software with elegance - Lightbend · Reactive summit - 22/10/18 Reactive Software with elegance Reactive design patterns for microservices on multicore charly.bechara@tredzone.com

Microservices on MulticoreFrom inter-thread communications...

Core12

Page 13: Reactive Software with elegance - Lightbend · Reactive summit - 22/10/18 Reactive Software with elegance Reactive design patterns for microservices on multicore charly.bechara@tredzone.com

Microservices on MulticoreFrom inter-thread communications...

Core13

Page 14: Reactive Software with elegance - Lightbend · Reactive summit - 22/10/18 Reactive Software with elegance Reactive design patterns for microservices on multicore charly.bechara@tredzone.com

Microservices on Multicore…to inter-core communications

Core14

Page 15: Reactive Software with elegance - Lightbend · Reactive summit - 22/10/18 Reactive Software with elegance Reactive design patterns for microservices on multicore charly.bechara@tredzone.com

Microservices on MulticoreInter-core communication => cache coherency

machine

> 30 cycles (10 ns)

12 cycles (4 ns)

4 cycles

(1.3 ns)

1 cycle

(0.3 ns)

Assuming Freq = 3 GHz

MESI > 600 cycles

(200 ns)

Shared L3$ or LLC

L1 I$

L2$

L1D$

Registers

core 1

L1 I$

L2$

L1D$

Registers

core N

15

Page 16: Reactive Software with elegance - Lightbend · Reactive summit - 22/10/18 Reactive Software with elegance Reactive design patterns for microservices on multicore charly.bechara@tredzone.com

Microservices on MulticoreExchange software are pushing performance to hardware limits

machine

Stability

Volume Velocity

50%ile 99.99%ile

16

msec µseck.msg/s M. msg/s

Page 17: Reactive Software with elegance - Lightbend · Reactive summit - 22/10/18 Reactive Software with elegance Reactive design patterns for microservices on multicore charly.bechara@tredzone.com

Simplx: one thread per coreNo context switching

17

Page 18: Reactive Software with elegance - Lightbend · Reactive summit - 22/10/18 Reactive Software with elegance Reactive design patterns for microservices on multicore charly.bechara@tredzone.com

Simplx: actors multitasking per thread High core utilization

18

Page 19: Reactive Software with elegance - Lightbend · Reactive summit - 22/10/18 Reactive Software with elegance Reactive design patterns for microservices on multicore charly.bechara@tredzone.com

Simplx: one event loop per core for communicationsLock free

19

Simplx runs on all cores

Event loop = ~300 ns Event loop = ~300 ns

Page 20: Reactive Software with elegance - Lightbend · Reactive summit - 22/10/18 Reactive Software with elegance Reactive design patterns for microservices on multicore charly.bechara@tredzone.com

20

Multicore WITHOUT multithreadedprogramming ?

Page 21: Reactive Software with elegance - Lightbend · Reactive summit - 22/10/18 Reactive Software with elegance Reactive design patterns for microservices on multicore charly.bechara@tredzone.com

Microservices on MuticoreVery good resources, but no multicore-related patterns

machine

21

Page 22: Reactive Software with elegance - Lightbend · Reactive summit - 22/10/18 Reactive Software with elegance Reactive design patterns for microservices on multicore charly.bechara@tredzone.com

REACTIVE MULTICORE PATTERNS

2

22

Page 23: Reactive Software with elegance - Lightbend · Reactive summit - 22/10/18 Reactive Software with elegance Reactive design patterns for microservices on multicore charly.bechara@tredzone.com

23

Page 24: Reactive Software with elegance - Lightbend · Reactive summit - 22/10/18 Reactive Software with elegance Reactive design patterns for microservices on multicore charly.bechara@tredzone.com

Reactive multicore Patterns7 patterns to unleash multicore reactivity

machine

24

Core-to-core messaging (2 patterns)

Core monitoring (2 patterns)

Core-to-core flow control (1 pattern)

Core-to-cache management (2 patterns)

Page 25: Reactive Software with elegance - Lightbend · Reactive summit - 22/10/18 Reactive Software with elegance Reactive design patterns for microservices on multicore charly.bechara@tredzone.com

Core-to-core messagingpatterns

25

Page 26: Reactive Software with elegance - Lightbend · Reactive summit - 22/10/18 Reactive Software with elegance Reactive design patterns for microservices on multicore charly.bechara@tredzone.com

Pattern #1: the core-aware messaging pattern

Inter-core communication: push message

26

destination core socket serversender

Push m

essage

Pipe pipe = new Pipe(greenActorId);

pipe.push<HelloEvent>();

~1 µs – 10 µs

~ 500 ns

~ 300 ns

Page 27: Reactive Software with elegance - Lightbend · Reactive summit - 22/10/18 Reactive Software with elegance Reactive design patterns for microservices on multicore charly.bechara@tredzone.com

Pattern #1: the core-aware messaging patternIntra-core communication: push message

27

Push a message

asynchronous

Pipe pipe = new Pipe(greenActorId);

pipe.push<HelloEvent>();

~300 ns

Page 28: Reactive Software with elegance - Lightbend · Reactive summit - 22/10/18 Reactive Software with elegance Reactive design patterns for microservices on multicore charly.bechara@tredzone.com

Intra-core communication: x150 speedup with direct call over push

28

Push a message

asynchronous

Direct call

synchronous

ActorReference<GreenActor> target = getLocalReference(greenActorId);

[...]

target->hello();

Pipe pipe = new Pipe(greenActorId);

pipe.push<HelloEvent>();

Pattern #1: the core-aware messaging pattern

Optimize calls according to the deployment

~300 ns

~2 ns

Page 29: Reactive Software with elegance - Lightbend · Reactive summit - 22/10/18 Reactive Software with elegance Reactive design patterns for microservices on multicore charly.bechara@tredzone.com

Pattern #2: the message mutualization patternNetwork optimizations, core optimizations. Same fight

29

core actor push data

In this use case, the 3 red consumers

process the same data

Page 30: Reactive Software with elegance - Lightbend · Reactive summit - 22/10/18 Reactive Software with elegance Reactive design patterns for microservices on multicore charly.bechara@tredzone.com

Pattern #2: the message mutualization patternCommunication has a cost

30

Many events means

high cache coherence usage (L3)

core actor push data

3 events

Page 31: Reactive Software with elegance - Lightbend · Reactive summit - 22/10/18 Reactive Software with elegance Reactive design patterns for microservices on multicore charly.bechara@tredzone.com

Pattern #2: the message mutualization patternLet’s mutualize inter-core communications

31

1 event

Local router

3 direct calls3 events

Page 32: Reactive Software with elegance - Lightbend · Reactive summit - 22/10/18 Reactive Software with elegance Reactive design patterns for microservices on multicore charly.bechara@tredzone.com

Pattern #2: the message mutualization patternWITH pattern vs WITHOUT pattern: Linear improvement

32

Page 33: Reactive Software with elegance - Lightbend · Reactive summit - 22/10/18 Reactive Software with elegance Reactive design patterns for microservices on multicore charly.bechara@tredzone.com

Core monitoringpatterns

33

@ real-time

Page 34: Reactive Software with elegance - Lightbend · Reactive summit - 22/10/18 Reactive Software with elegance Reactive design patterns for microservices on multicore charly.bechara@tredzone.com

Pattern #3: the core stats patternUse case: monitoring the data distribution throughput

34

core actorcore actor data

We want to know in real-time the number of

messages received per second,

globally, and per core.

StartSequence startSequence;

startSequence.addActor<RedActor>(0); // core 0

startSequence.addActor<RedActor>(0); // core 0

startSequence.addActor<RedActor>(1); // core 1

startSequence.addActor<RedActor>(1); // core 1

Simplx simplx(startSequence);

Page 35: Reactive Software with elegance - Lightbend · Reactive summit - 22/10/18 Reactive Software with elegance Reactive design patterns for microservices on multicore charly.bechara@tredzone.com

Pattern #3: the core stats patternUse case: monitoring the data distribution throughput

35

1

1

struct LocalMonitorActor:Actor {[…]

void newMessage() {

++count;

}

}

struct RedActor:Actor {[…]

ReferenceActor monitor;

RedActor () {

monitor = newSingletonActor<LocalMonitorActor>();

}

void onEvent() {

monitor-> newMessage();

}

}

Local monitoringSingleton

1Increasemessage counter

Page 36: Reactive Software with elegance - Lightbend · Reactive summit - 22/10/18 Reactive Software with elegance Reactive design patterns for microservices on multicore charly.bechara@tredzone.com

Pattern #3: the core stats patternUse case: monitoring the data distribution throughput

36

1

1

1 sec

Service monitoringInform monitoring ofthe last second statistics

Timer

struct LocalMonitorActor:Actor,TimerProxy{ […]

LocalMonitorActor:TimerProxy(*this) {

setRepeat(1000);

}

virtual void onTimeout() {

serviceMonitoringPipe.push<StatsEvent>(count);

count=0;

}

}

Page 37: Reactive Software with elegance - Lightbend · Reactive summit - 22/10/18 Reactive Software with elegance Reactive design patterns for microservices on multicore charly.bechara@tredzone.com

Pattern #4: the core usage patternCore utilization

37

Detect overloading cores before it is too late

Relying on the CPU usage provided by the OS is not enough

100% does not mean the runtime is overloaded

10% does not tell how much data you can really process

Page 38: Reactive Software with elegance - Lightbend · Reactive summit - 22/10/18 Reactive Software with elegance Reactive design patterns for microservices on multicore charly.bechara@tredzone.com

Pattern #4: the core usage patternNo push, no event, no work

38

1 sec

Idle loop

Reality is more about

3 millions loops per second

20 loops in a second

0% core usage

Page 39: Reactive Software with elegance - Lightbend · Reactive summit - 22/10/18 Reactive Software with elegance Reactive design patterns for microservices on multicore charly.bechara@tredzone.com

Pattern #4: the core usage patternEfficient core usage

39

20 loops in a second

0% core usage

11 loops

3 working loops

60% core usage=

1 sec

Working loopIdle loop

Page 40: Reactive Software with elegance - Lightbend · Reactive summit - 22/10/18 Reactive Software with elegance Reactive design patterns for microservices on multicore charly.bechara@tredzone.com

Pattern #4: the core usage patternRuntime performance counters help measurement

40

11 loops

8 idle loops

3 working loops

60% core usage

1 sec

Working loopIdle loop

0 0 0 0 0 0 0 01 11

Duration(IdleLoop) = 0.05 s

Core usage actor

idleLoop= 0|1

CoreUsage = 1 – ∑(idleLoop)*0.05

100

Reality is more about

Duration Idle loop ~300 ns

Page 41: Reactive Software with elegance - Lightbend · Reactive summit - 22/10/18 Reactive Software with elegance Reactive design patterns for microservices on multicore charly.bechara@tredzone.com

Demo: Real-time core monitoringA typical trading workflow

41

Data stream Data processing

Page 42: Reactive Software with elegance - Lightbend · Reactive summit - 22/10/18 Reactive Software with elegance Reactive design patterns for microservices on multicore charly.bechara@tredzone.com

Core-to-core flow controlpatterns

42

Page 43: Reactive Software with elegance - Lightbend · Reactive summit - 22/10/18 Reactive Software with elegance Reactive design patterns for microservices on multicore charly.bechara@tredzone.com

Pattern #5: the queuing prevention patternWhat if producers overflow a consumer ?

43

Your software cannot be more optimized ?

Still, the incoming throughput could be too high,

implying strong queuing.

Continue ?

Stop the flow ?

Merge data ?

Throttling ?

Whatever the decision,

we need to detect the issue

Page 44: Reactive Software with elegance - Lightbend · Reactive summit - 22/10/18 Reactive Software with elegance Reactive design patterns for microservices on multicore charly.bechara@tredzone.com

Pattern #5: the queuing prevention patternWhat’s happening behind a push ?

44

Page 45: Reactive Software with elegance - Lightbend · Reactive summit - 22/10/18 Reactive Software with elegance Reactive design patterns for microservices on multicore charly.bechara@tredzone.com

Pattern #5: the queuing prevention patternLocal Simplx loops handle the inter-core communication

45

Batch ID = 145

Page 46: Reactive Software with elegance - Lightbend · Reactive summit - 22/10/18 Reactive Software with elegance Reactive design patterns for microservices on multicore charly.bechara@tredzone.com

Pattern #5: the queuing prevention patternOnce the destination reads the data, the BatchID is incremented

46

Batch ID = 145

Batch ID = 146

Page 47: Reactive Software with elegance - Lightbend · Reactive summit - 22/10/18 Reactive Software with elegance Reactive design patterns for microservices on multicore charly.bechara@tredzone.com

Pattern #5: the queuing prevention patternBatchID does not increment if destination core is busy

47

Batch ID = 145

Batch ID = 145

Page 48: Reactive Software with elegance - Lightbend · Reactive summit - 22/10/18 Reactive Software with elegance Reactive design patterns for microservices on multicore charly.bechara@tredzone.com

Pattern #5: the queuing prevention patternCore to core communication at max pace

48

BatchID batchID(pipe);

pipe.push<Event>();

(…)

if(batchID.hasChanged()) {

// push again

} else { //destination is busy

//merging data, start throttling, reject orders…

}

Batch ID = 145

Batch ID = 145

Page 49: Reactive Software with elegance - Lightbend · Reactive summit - 22/10/18 Reactive Software with elegance Reactive design patterns for microservices on multicore charly.bechara@tredzone.com

Pattern #5: the queuing prevention patternDemo: code java

49

Same id=> queuing

Last id=> no queuing

Page 50: Reactive Software with elegance - Lightbend · Reactive summit - 22/10/18 Reactive Software with elegance Reactive design patterns for microservices on multicore charly.bechara@tredzone.com

Core-to-cache managementpatterns

50

Page 51: Reactive Software with elegance - Lightbend · Reactive summit - 22/10/18 Reactive Software with elegance Reactive design patterns for microservices on multicore charly.bechara@tredzone.com

Pattern #6: the cache-aware split pattern FIX + execution engine

51

new order

Page 52: Reactive Software with elegance - Lightbend · Reactive summit - 22/10/18 Reactive Software with elegance Reactive design patterns for microservices on multicore charly.bechara@tredzone.com

Pattern #6: the cache-aware split patternFIX + execution engine

52

Almost all tags sent

in the new order request

need to be sent back in the

acknowledgment

new order

acknowledgment

A FIX order can easily size ~200 Bytes

Page 53: Reactive Software with elegance - Lightbend · Reactive summit - 22/10/18 Reactive Software with elegance Reactive design patterns for microservices on multicore charly.bechara@tredzone.com

Pattern #6: the cache-aware split patternStability depends on the ability to be cache friendly

53

200 Bytesper order:

1 10000 open orders per book

Local storage

order book Local storage

To stay « in-cache » and get stable

performance, one core can store

~1300 open orders.

Page 54: Reactive Software with elegance - Lightbend · Reactive summit - 22/10/18 Reactive Software with elegance Reactive design patterns for microservices on multicore charly.bechara@tredzone.com

Pattern #6: the cache-aware split pattern… but FIX orders are huge

54

FIX orderorder entry

~32 Bytes:idpricequantitytypevalidity …

1 10000 open orders per book

Local storage

order book Local storage

Page 55: Reactive Software with elegance - Lightbend · Reactive summit - 22/10/18 Reactive Software with elegance Reactive design patterns for microservices on multicore charly.bechara@tredzone.com

Pattern #6: the cache-aware split patternLet’s cut it and send only the strict minimum

55

~32 Bytes:idpricequantitytypevalidity …

1 10000 open orders per book

Local storage

order book Local storageFIX orderorder entry

Page 56: Reactive Software with elegance - Lightbend · Reactive summit - 22/10/18 Reactive Software with elegance Reactive design patterns for microservices on multicore charly.bechara@tredzone.com

Pattern #6: the cache-aware split patternand reconciliate both parts later

56

~32 Bytes:idpricequantitytypevalidity …

1 10000 open orders per book

Local storage

order book Local storageFIX orderorder entry order

Page 57: Reactive Software with elegance - Lightbend · Reactive summit - 22/10/18 Reactive Software with elegance Reactive design patterns for microservices on multicore charly.bechara@tredzone.com

Pattern #6: the cache-aware split patternWe have divided by 6 the number of cores to be stable

57

~32 Bytes:idpricequantitytypevalidity …

1 10000 open orders per book

Local storage

order book Local storage

To stay « in-cache » and get stable

performance, one core can store

8000 open orders.

Page 58: Reactive Software with elegance - Lightbend · Reactive summit - 22/10/18 Reactive Software with elegance Reactive design patterns for microservices on multicore charly.bechara@tredzone.com

Pattern #7: the $-friendly actor directory patternRouting a message needs an Actor directory

58

Message initiator

A

Send message to A

Where is A ?

What’s its address ?

Send message to A

router destination

Page 59: Reactive Software with elegance - Lightbend · Reactive summit - 22/10/18 Reactive Software with elegance Reactive design patterns for microservices on multicore charly.bechara@tredzone.com

Pattern #7: the $-friendly actor directory patternRegular routing design is simple

59

router destinationcore

incoming key @destination

Page 60: Reactive Software with elegance - Lightbend · Reactive summit - 22/10/18 Reactive Software with elegance Reactive design patterns for microservices on multicore charly.bechara@tredzone.com

Pattern #7: the $-friendly actor directory pattern

Multi-scaling communications impacts the actor address size

60

sender destination core process server

ActorID

ActorID + CoreID

ActorID + CoreID + EngineID

ActorID + CoreID + EngineID + MachineID

Page 61: Reactive Software with elegance - Lightbend · Reactive summit - 22/10/18 Reactive Software with elegance Reactive design patterns for microservices on multicore charly.bechara@tredzone.com

Pattern #7: the $-friendly actor directory patternThe local directory can be huge

61

incoming key @destination

K D

12 bytes* 10 bytes

N r

ecord

s size = N(K + D)

size = 50 000(12 + 10) ~ 1.1 MB

*ISIN code = 12 characters

Page 62: Reactive Software with elegance - Lightbend · Reactive summit - 22/10/18 Reactive Software with elegance Reactive design patterns for microservices on multicore charly.bechara@tredzone.com

Pattern #7: the $-friendly actor directory patternLet’s take advantage of core awareness

62

router destinationcore

core 1

cores n

incoming key @destination

Page 63: Reactive Software with elegance - Lightbend · Reactive summit - 22/10/18 Reactive Software with elegance Reactive design patterns for microservices on multicore charly.bechara@tredzone.com

Pattern #7: the $-friendly actor directory patternLet’s take advantage of core awareness

63

core 1

cores n

sender destination corelocalrouter

incoming key coreID

coreID @localrouter destination index

index

Page 64: Reactive Software with elegance - Lightbend · Reactive summit - 22/10/18 Reactive Software with elegance Reactive design patterns for microservices on multicore charly.bechara@tredzone.com

Pattern #7: the $-friendly actor directory patternWe save about 40% cache memory

64

incoming key coreID

12 byte 1 byte

N r

ecord

s

coreID @localrouter destination index

1 byte 10 byte 1 byte

256 r

ecord

s

size = 50 000(12 + 1) + 256(1+10 +1) ~ 0,65MB+

Page 65: Reactive Software with elegance - Lightbend · Reactive summit - 22/10/18 Reactive Software with elegance Reactive design patterns for microservices on multicore charly.bechara@tredzone.com

MODERN SOFTWARE ROADMAP

3

65

Page 66: Reactive Software with elegance - Lightbend · Reactive summit - 22/10/18 Reactive Software with elegance Reactive design patterns for microservices on multicore charly.bechara@tredzone.com

"The most amazing achievement of the computer

software industry is its continuing cancellation of

the steady and staggering gains made by the

computer hardware industry."

Henry Peteroski

Page 67: Reactive Software with elegance - Lightbend · Reactive summit - 22/10/18 Reactive Software with elegance Reactive design patterns for microservices on multicore charly.bechara@tredzone.com

Multicore software roadmap to success

67

DesignConcurrent

Develop Monothreaded

Run Parallel

Execute Reactive

Current Multi-core Software(with Multithreading)

Next Generation

Multi-core Software(with actor model)

Page 68: Reactive Software with elegance - Lightbend · Reactive summit - 22/10/18 Reactive Software with elegance Reactive design patterns for microservices on multicore charly.bechara@tredzone.com

Simplx is now open source Apache 2.0

https://github.com/Tredzone/simplx

68

[email protected]

Page 69: Reactive Software with elegance - Lightbend · Reactive summit - 22/10/18 Reactive Software with elegance Reactive design patterns for microservices on multicore charly.bechara@tredzone.com

69