Moderne backends mit dem aktor programmiermodell

Preview:

Citation preview

Modern BackendsWith Service Fabric & Actor Model

Damir Dobric

damir.dobric@daenet.comb-dadobr@microsoft.com

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

AgendaBackends Today

Multitier Architecture Scaling fictionBackend Limits

“Right” Scaling ApproachLoadbalancingPartitioningMicroservices

Azure Service Fabric Actor ModelMathematical ModelActivation, States,..

“The Backend”

Limit of vertical scale ?

256 Core Processor

Backend Today

ServiceService

Client Server / three tire Model Architecture

Service DBClient

Client

Client

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

ServiceService

Service

DBClient

Client

Client ServiceService

Service

NLB => Multiple Nodes

ServiceService

NLB + Multiple DBs

Service DB

Client

Client

Client ServiceService

ServiceDB

ServiceService

Caching helps, but..

Service

DBClient

Client

Client ServiceService

Service

CACHE

Linear Scale is the goal

Number of nodesSpee

d up

The goal is

“linear scale”

Speed up = k * number of nodes

“speed-up” of traditional approach?

NonlinearAdditional power does not scale wellSpeed-up can even fail

Number of nodesSpee

d up

5 10

Scale Cube

Scale Cube X Horizontal scale More nodes NLB

Y Functional Scale Microservices

Z Data Partitioning Separate Tenants Separate by regions …

ServiceService

Service FabricSupports scale in all 3 dimensions

ServiceDATA

Client

Client

Client ServiceService

ServiceDATA

High Level Architecture

Service TypesStatelessState fullActor StatelessActor State fullAnything Else

Console AppJAVAASP.NET…

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

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

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

Service Fabric Demo

Actor Model

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

SteigerA framework and basis for reasoning about

concurrencyActors as primitives of concurrent

computation

Definition

The actor model is a mathematical model of concurrent computation.

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

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

The “Actor”

ActorASYNC ONLY

STATE PERSISTENCE

SINGLE THREAD EXECUTION

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

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

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

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;

Single Activation Principle

Actor

Actor

Actor

Actor

Actor

Actor

Actor

Actor

Actor

Actor X

Actor

Actor

Actor

Actor

New Instance on other Node

Actor

Actor

Actor

Actor

Actor

Actor

Actor

Actor

Actor

Actor X

Actor

Actor

Actor

Actor

Actor X

State vs. StatelessStateless:

public class HelloActor : Actor, IHelloActor

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

public string WhateverSerializable;}

State persistence

Actor

State

Persistence Provider

SQL

Persistence Provider

?

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.

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!

Q & AModern Backends

with Service Fabric and Actor Model

Damir Dobric

damir.dobric@daenet.comb-dadobr@microsoft.com

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

Questions & Answers

BACKUP SLIDES

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

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); }

Actor Container

Actor

Actor

Actor

Actor

Actor

Actor

Actor

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)

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!

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.

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

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);

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

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.

Recommended