Moderne backends mit dem aktor programmiermodell

50
Modern Backends With Service Fabric & Actor Model Damir Dobric [email protected] [email protected] Microsoft PTSP (Partner Technical Solution Specialist) Microsoft Most Valuable Professional Blog Twitter

Transcript of Moderne backends mit dem aktor programmiermodell

Page 1: Moderne backends mit dem aktor programmiermodell

Modern BackendsWith Service Fabric & Actor Model

Damir Dobric

[email protected]@microsoft.com

Microsoft PTSP (Partner Technical Solution Specialist)Microsoft Most Valuable ProfessionalBlog Twitter

Page 2: Moderne backends mit dem aktor programmiermodell

AgendaBackends Today

Multitier Architecture Scaling fictionBackend Limits

“Right” Scaling ApproachLoadbalancingPartitioningMicroservices

Azure Service Fabric Actor ModelMathematical ModelActivation, States,..

Page 3: Moderne backends mit dem aktor programmiermodell

“The Backend”

Page 4: Moderne backends mit dem aktor programmiermodell

Limit of vertical scale ?

Page 5: Moderne backends mit dem aktor programmiermodell

256 Core Processor

Page 6: Moderne backends mit dem aktor programmiermodell

Backend Today

Page 7: Moderne backends mit dem aktor programmiermodell

ServiceService

Client Server / three tire Model Architecture

Service DBClient

Client

Client

Focus Solutions for EnterprisesSOA, RPC, mostly sync invocationWCF, DCOM, CORBA,..

Page 8: Moderne backends mit dem aktor programmiermodell

ServiceService

Service

DBClient

Client

Client ServiceService

Service

NLB => Multiple Nodes

Page 9: Moderne backends mit dem aktor programmiermodell

ServiceService

NLB + Multiple DBs

Service DB

Client

Client

Client ServiceService

ServiceDB

Page 10: Moderne backends mit dem aktor programmiermodell

ServiceService

Caching helps, but..

Service

DBClient

Client

Client ServiceService

Service

CACHE

Page 11: Moderne backends mit dem aktor programmiermodell

Linear Scale is the goal

Number of nodesSpee

d up

The goal is

“linear scale”

Speed up = k * number of nodes

Page 12: Moderne backends mit dem aktor programmiermodell

“speed-up” of traditional approach?

NonlinearAdditional power does not scale wellSpeed-up can even fail

Number of nodesSpee

d up

5 10

Page 13: Moderne backends mit dem aktor programmiermodell

Scale Cube

Page 14: Moderne backends mit dem aktor programmiermodell

Scale Cube X Horizontal scale More nodes NLB

Y Functional Scale Microservices

Z Data Partitioning Separate Tenants Separate by regions …

Page 15: Moderne backends mit dem aktor programmiermodell

ServiceService

Service FabricSupports scale in all 3 dimensions

ServiceDATA

Client

Client

Client ServiceService

ServiceDATA

Page 16: Moderne backends mit dem aktor programmiermodell

High Level Architecture

Page 17: Moderne backends mit dem aktor programmiermodell

Service TypesStatelessState fullActor StatelessActor State fullAnything Else

Console AppJAVAASP.NET…

Page 18: Moderne backends mit dem aktor programmiermodell

IReliableDictionary<K,V> Dict; public Task DoIt(double x) { this.Dict[“A”]++; return TaskDone.Done;}

t2

Backstage: Reliable Dictionary

IReliableDictionary<K,V> Dict; public Task DoIt(double x) { this.Dict[“A”]++; return TaskDone.Done;}

t1

34

“A”lock

Page 19: Moderne backends mit dem aktor programmiermodell

Partitions and Replicas

P6

P3

P7

P2

P5

P42

P1

P6

P3

P7

P2

P5

P4

P1

P6P3

P7

P12

P5

P15

P1

P61

P3

P7

P2

P5

P42

P1

Node

Services are deployed in partitions.

Primary of P1

Secondary of P1

Secondary of P1State full services

are replicated

A1

A1

Multiple services can be deployed in a single

partition

Page 20: Moderne backends mit dem aktor programmiermodell

P6

P3

P7

P2

P5

P4

P1

Partitions and Replicas

P6

P3

P7

P2

P5

P4

P1

P6P3

P7

P2

P5

P4

P1

P6

P3

P7

P2

P5

P4

P1

If the node fails a new replica is automatically

created.

Old Primary of P1 fails

This is a new primary n P1

Secondary of P1

New secondary of P1

Page 21: Moderne backends mit dem aktor programmiermodell

Service Fabric Demo

Page 22: Moderne backends mit dem aktor programmiermodell

Actor Model

Page 23: Moderne backends mit dem aktor programmiermodell

The Actor ModelMathematical theory of computationIntroduced 1973 by Hewitt, Bishop, and

SteigerA framework and basis for reasoning about

concurrencyActors as primitives of concurrent

computation

Page 24: Moderne backends mit dem aktor programmiermodell

Definition

The actor model is a mathematical model of concurrent computation.

https://en.wikipedia.org/wiki/Actor_model

Page 25: Moderne backends mit dem aktor programmiermodell

Actor Languages ABCL AmbientTalk[33]

Axum[34]

CAL Actor Language D E Elixir Erlang Fantom Humus[35]

Io Ptolemy Project Rebeca Modeling Langua

ge Reia Rust[citation needed]

SALSA[36]

Scala[37][38]

Scratch TNSDL

Page 26: Moderne backends mit dem aktor programmiermodell

The “Actor”

ActorASYNC ONLY

STATE PERSISTENCE

SINGLE THREAD EXECUTION

Page 27: Moderne backends mit dem aktor programmiermodell

public interface IHelloActor : IActor{ Task<string> SayHello(string greeting);}

Actor is defined by interface

Marker interface to indicate actor interfaces

All actor interface operations must be asynchronous

Page 28: Moderne backends mit dem aktor programmiermodell

public class HelloActor : Actor, IHelloActor{ Task<string> SayHello(string greeting) { var resp = "You said: '" + greeting + "', I say: Hello!"; return Task.SayHello(resp); }}

Implementing the actor type

Page 29: Moderne backends mit dem aktor programmiermodell

Activation and deactivation

public override Task OnActivateAsync() { return base.OnActivateAsync(); }

public override Task OnDeactivateAsync() { return base.OnDeactivateAsync(); }

IHelloActor helloActor =ActorProxy.Create<IHelloActor>(actorId, appName);

ACTO

RCL

IEN

T

Page 30: Moderne backends mit dem aktor programmiermodell

ExampleIdentifier can be of types:

{long , GUID, String}

ActorId actorId = ActorId.NewId();string applicationName = "fabric:/CalculatorActorApp";IHelloActor helloActor =ActorProxy.Create<IHelloActor>(actorId, applicationName);

double result = helloActor .AddAsync(2, 3).Result;

Page 31: Moderne backends mit dem aktor programmiermodell

Single Activation Principle

Actor

Actor

Actor

Actor

Actor

Actor

Actor

Actor

Actor

Actor X

Actor

Actor

Actor

Actor

Page 32: Moderne backends mit dem aktor programmiermodell

New Instance on other Node

Actor

Actor

Actor

Actor

Actor

Actor

Actor

Actor

Actor

Actor X

Actor

Actor

Actor

Actor

Actor X

Page 33: Moderne backends mit dem aktor programmiermodell

State vs. StatelessStateless:

public class HelloActor : Actor, IHelloActor

State full:public class HelloActor : StatefullActor<MyState>, IHello[DataContract]public class State { [DataMember]

public string WhateverSerializable;}

Page 34: Moderne backends mit dem aktor programmiermodell

State persistence

Actor

State

Persistence Provider

SQL

Persistence Provider

?

Page 35: Moderne backends mit dem aktor programmiermodell

State replica + persistence

Actor

State

Persistence Provider

?

Actor

State

1

2

await this.SaveStateAsync();

Call SaveAsync() to explicitly enforce

saving of the state

But State is also saved implicitly,

after method comlettion.

Page 37: Moderne backends mit dem aktor programmiermodell

RecapBackends Today

Multitier Architecture can lead to bottleneckBackend Limits (More nodes can mean less perf.)

Scaling CubeLoadbalancing (As we know it)Partitioning (SF provides it out of the box.)Microservices (SF containers PaaS ready)

Service FabricReliable Cluster. Can host “anything”Actor Model is cool for specific problem domainsIt behaves as OO, but is NOT!

Page 38: Moderne backends mit dem aktor programmiermodell

Q & AModern Backends

with Service Fabric and Actor Model

Damir Dobric

[email protected]@microsoft.com

Microsoft PTSP (Partner Technical Solution Specialist)Microsoft Most Valuable ProfessionalBlog Twitter

Page 39: Moderne backends mit dem aktor programmiermodell

Questions & Answers

Page 40: Moderne backends mit dem aktor programmiermodell

BACKUP SLIDES

Page 41: Moderne backends mit dem aktor programmiermodell

How to set provider?[VolatileActorStateProvider]public class VoicemailBoxActor : Actor<VoicemailBox>, IVoicemailBoxActor{ public Task<List<Voicemail>> GetMessagesAsync() { return Task.FromResult(State.MessageList); } ...}

Page 42: Moderne backends mit dem aktor programmiermodell

How to implement provider?

public interface IActorStateProvider : IStateProviderReplica{ Task DeleteReminderAsync(ActorId actorId, string reminderName); void Initialize(ActorTypeInformation actorTypeInformation); Task<IActorReminderCollection> LoadRemindersAsync(CancellationToken cancelT); Task<T> LoadStateAsync<T>(ActorId actorId); void OnActorActivated(ActorId actorId); void OnActorDeactivated(ActorId actorId); Task SaveReminderAsync(ActorId actorId, IActorReminder state); Task SaveStateAsync<T>(ActorId actorId, T state); }

Page 43: Moderne backends mit dem aktor programmiermodell

Actor Container

Actor

Actor

Actor

Actor

Actor

Actor

Actor

Page 44: Moderne backends mit dem aktor programmiermodell

Actor

Actor

Actor

Actor

Actor

Actor

Actor LocationTransparency

Actor

Actor

Actor

Actor

Actor

Actor

Actor

Actor

Actor

Actor

Actor

Actor

Actor

Actor

Actor

Actor Actor

Actor

Actor

?

A System is “location transparent” if it is ab le to

route my calls automatically to the required resource

(actor)

Page 45: Moderne backends mit dem aktor programmiermodell

Activation ModesSingle Activation (default)

Used when actors have a stateSingle activation prevents state reconciliation

Stateless Worker (automatically)Used to increase throughputIf actor has no the state

Not all frameworks do support this!

Page 46: Moderne backends mit dem aktor programmiermodell

Unordered Message Delivery

FIFO with many actors is hardMessage Order slows down

performanceMost scenarios do not require ordered

deliveryApplication can implement it if

requiredI.E.: Send next message after last was commited or send list of items.

Page 47: Moderne backends mit dem aktor programmiermodell

Facts about ActorsNo new() / Dispose() required.Lifecycle is managed by the runtime. Similar to virtual memoryIf there is no existing activation, a message sent to it triggers instantiationRuntime can create multiple activations of stateless grains (for performance)Transparent recovery from server failuresCode in grain can call methods from another grainGrains can pass their references to one another aroundReferences can be persistedGrains never fail

Page 48: Moderne backends mit dem aktor programmiermodell

Actor Activation

airplane2

airplane2

airplane2

airplane2

airplane2

empty

airplane1

Reference to existing or new instance is returned. Client has no knowledge about it.

ActorId uldId = new ActorId("AKE12345");  string appName = "fabric:/LogisticTrackingEngine";  IAirplaneActor airplane = ActorProxy.Create<IAirplaneActor>(uldId, appName); await airplane.DoSomething(x,y);

Page 49: Moderne backends mit dem aktor programmiermodell

Multiple Instances Possible

Actor

Actor

Actor

Actor

Actor

Actor

Actor

Actor

Actor

Actor

Actor

Actor

Actor

Actor

Actor

Actor Actor

Actor

Actor

• If no state, then multiple instances canbe used to increase performance.

• Not all frameworks support this

Page 50: Moderne backends mit dem aktor programmiermodell

Host Scale ConceptActors are hosted by host called Service

FabricDifferent frameworks use different (but

similar) concepts)For example: ORLEANS is also

framework with host.Service Fabric is general purpose host.It provides scalability, deployment,

replications etc.