Moderne backends mit dem aktor programmiermodell
-
Upload
damir-dobric -
Category
Technology
-
view
259 -
download
4
Transcript of Moderne backends mit dem aktor programmiermodell
![Page 1: Moderne backends mit dem aktor programmiermodell](https://reader038.fdocument.pub/reader038/viewer/2022103010/58a7b1fc1a28ab70368b4fd7/html5/thumbnails/1.jpg)
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](https://reader038.fdocument.pub/reader038/viewer/2022103010/58a7b1fc1a28ab70368b4fd7/html5/thumbnails/2.jpg)
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](https://reader038.fdocument.pub/reader038/viewer/2022103010/58a7b1fc1a28ab70368b4fd7/html5/thumbnails/3.jpg)
“The Backend”
![Page 4: Moderne backends mit dem aktor programmiermodell](https://reader038.fdocument.pub/reader038/viewer/2022103010/58a7b1fc1a28ab70368b4fd7/html5/thumbnails/4.jpg)
Limit of vertical scale ?
![Page 5: Moderne backends mit dem aktor programmiermodell](https://reader038.fdocument.pub/reader038/viewer/2022103010/58a7b1fc1a28ab70368b4fd7/html5/thumbnails/5.jpg)
256 Core Processor
![Page 6: Moderne backends mit dem aktor programmiermodell](https://reader038.fdocument.pub/reader038/viewer/2022103010/58a7b1fc1a28ab70368b4fd7/html5/thumbnails/6.jpg)
Backend Today
![Page 7: Moderne backends mit dem aktor programmiermodell](https://reader038.fdocument.pub/reader038/viewer/2022103010/58a7b1fc1a28ab70368b4fd7/html5/thumbnails/7.jpg)
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](https://reader038.fdocument.pub/reader038/viewer/2022103010/58a7b1fc1a28ab70368b4fd7/html5/thumbnails/8.jpg)
ServiceService
Service
DBClient
Client
Client ServiceService
Service
NLB => Multiple Nodes
![Page 9: Moderne backends mit dem aktor programmiermodell](https://reader038.fdocument.pub/reader038/viewer/2022103010/58a7b1fc1a28ab70368b4fd7/html5/thumbnails/9.jpg)
ServiceService
NLB + Multiple DBs
Service DB
Client
Client
Client ServiceService
ServiceDB
![Page 10: Moderne backends mit dem aktor programmiermodell](https://reader038.fdocument.pub/reader038/viewer/2022103010/58a7b1fc1a28ab70368b4fd7/html5/thumbnails/10.jpg)
ServiceService
Caching helps, but..
Service
DBClient
Client
Client ServiceService
Service
CACHE
![Page 11: Moderne backends mit dem aktor programmiermodell](https://reader038.fdocument.pub/reader038/viewer/2022103010/58a7b1fc1a28ab70368b4fd7/html5/thumbnails/11.jpg)
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](https://reader038.fdocument.pub/reader038/viewer/2022103010/58a7b1fc1a28ab70368b4fd7/html5/thumbnails/12.jpg)
“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](https://reader038.fdocument.pub/reader038/viewer/2022103010/58a7b1fc1a28ab70368b4fd7/html5/thumbnails/13.jpg)
Scale Cube
![Page 14: Moderne backends mit dem aktor programmiermodell](https://reader038.fdocument.pub/reader038/viewer/2022103010/58a7b1fc1a28ab70368b4fd7/html5/thumbnails/14.jpg)
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](https://reader038.fdocument.pub/reader038/viewer/2022103010/58a7b1fc1a28ab70368b4fd7/html5/thumbnails/15.jpg)
ServiceService
Service FabricSupports scale in all 3 dimensions
ServiceDATA
Client
Client
Client ServiceService
ServiceDATA
![Page 16: Moderne backends mit dem aktor programmiermodell](https://reader038.fdocument.pub/reader038/viewer/2022103010/58a7b1fc1a28ab70368b4fd7/html5/thumbnails/16.jpg)
High Level Architecture
![Page 17: Moderne backends mit dem aktor programmiermodell](https://reader038.fdocument.pub/reader038/viewer/2022103010/58a7b1fc1a28ab70368b4fd7/html5/thumbnails/17.jpg)
Service TypesStatelessState fullActor StatelessActor State fullAnything Else
Console AppJAVAASP.NET…
![Page 18: Moderne backends mit dem aktor programmiermodell](https://reader038.fdocument.pub/reader038/viewer/2022103010/58a7b1fc1a28ab70368b4fd7/html5/thumbnails/18.jpg)
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](https://reader038.fdocument.pub/reader038/viewer/2022103010/58a7b1fc1a28ab70368b4fd7/html5/thumbnails/19.jpg)
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](https://reader038.fdocument.pub/reader038/viewer/2022103010/58a7b1fc1a28ab70368b4fd7/html5/thumbnails/20.jpg)
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](https://reader038.fdocument.pub/reader038/viewer/2022103010/58a7b1fc1a28ab70368b4fd7/html5/thumbnails/21.jpg)
Service Fabric Demo
![Page 22: Moderne backends mit dem aktor programmiermodell](https://reader038.fdocument.pub/reader038/viewer/2022103010/58a7b1fc1a28ab70368b4fd7/html5/thumbnails/22.jpg)
Actor Model
![Page 23: Moderne backends mit dem aktor programmiermodell](https://reader038.fdocument.pub/reader038/viewer/2022103010/58a7b1fc1a28ab70368b4fd7/html5/thumbnails/23.jpg)
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](https://reader038.fdocument.pub/reader038/viewer/2022103010/58a7b1fc1a28ab70368b4fd7/html5/thumbnails/24.jpg)
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](https://reader038.fdocument.pub/reader038/viewer/2022103010/58a7b1fc1a28ab70368b4fd7/html5/thumbnails/25.jpg)
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](https://reader038.fdocument.pub/reader038/viewer/2022103010/58a7b1fc1a28ab70368b4fd7/html5/thumbnails/26.jpg)
The “Actor”
ActorASYNC ONLY
STATE PERSISTENCE
SINGLE THREAD EXECUTION
![Page 27: Moderne backends mit dem aktor programmiermodell](https://reader038.fdocument.pub/reader038/viewer/2022103010/58a7b1fc1a28ab70368b4fd7/html5/thumbnails/27.jpg)
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](https://reader038.fdocument.pub/reader038/viewer/2022103010/58a7b1fc1a28ab70368b4fd7/html5/thumbnails/28.jpg)
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](https://reader038.fdocument.pub/reader038/viewer/2022103010/58a7b1fc1a28ab70368b4fd7/html5/thumbnails/29.jpg)
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](https://reader038.fdocument.pub/reader038/viewer/2022103010/58a7b1fc1a28ab70368b4fd7/html5/thumbnails/30.jpg)
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](https://reader038.fdocument.pub/reader038/viewer/2022103010/58a7b1fc1a28ab70368b4fd7/html5/thumbnails/31.jpg)
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](https://reader038.fdocument.pub/reader038/viewer/2022103010/58a7b1fc1a28ab70368b4fd7/html5/thumbnails/32.jpg)
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](https://reader038.fdocument.pub/reader038/viewer/2022103010/58a7b1fc1a28ab70368b4fd7/html5/thumbnails/33.jpg)
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](https://reader038.fdocument.pub/reader038/viewer/2022103010/58a7b1fc1a28ab70368b4fd7/html5/thumbnails/34.jpg)
State persistence
Actor
State
Persistence Provider
SQL
Persistence Provider
?
![Page 35: Moderne backends mit dem aktor programmiermodell](https://reader038.fdocument.pub/reader038/viewer/2022103010/58a7b1fc1a28ab70368b4fd7/html5/thumbnails/35.jpg)
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 36: Moderne backends mit dem aktor programmiermodell](https://reader038.fdocument.pub/reader038/viewer/2022103010/58a7b1fc1a28ab70368b4fd7/html5/thumbnails/36.jpg)
ReferencesHewitt about actor model:
https://www.youtube.com/watch?v=7erJ1DV_TloActor Model popular videos
https://www.youtube.com/watch?v=7erJ1DV_Tlo&list=PLhECcZb9tBx-gPymZfWjRGwnqsqmE1F2cState Persistence
http://developers.de/blogs/damir_dobric/archive/2014/06/17/what-is-state-persistence-in-orleans-and-how-to-use-it.aspx
Service Fabrichttps://azure.microsoft.com/en-gb/documentation/services/service-fabric/
![Page 37: Moderne backends mit dem aktor programmiermodell](https://reader038.fdocument.pub/reader038/viewer/2022103010/58a7b1fc1a28ab70368b4fd7/html5/thumbnails/37.jpg)
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](https://reader038.fdocument.pub/reader038/viewer/2022103010/58a7b1fc1a28ab70368b4fd7/html5/thumbnails/38.jpg)
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](https://reader038.fdocument.pub/reader038/viewer/2022103010/58a7b1fc1a28ab70368b4fd7/html5/thumbnails/39.jpg)
Questions & Answers
![Page 40: Moderne backends mit dem aktor programmiermodell](https://reader038.fdocument.pub/reader038/viewer/2022103010/58a7b1fc1a28ab70368b4fd7/html5/thumbnails/40.jpg)
BACKUP SLIDES
![Page 41: Moderne backends mit dem aktor programmiermodell](https://reader038.fdocument.pub/reader038/viewer/2022103010/58a7b1fc1a28ab70368b4fd7/html5/thumbnails/41.jpg)
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](https://reader038.fdocument.pub/reader038/viewer/2022103010/58a7b1fc1a28ab70368b4fd7/html5/thumbnails/42.jpg)
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](https://reader038.fdocument.pub/reader038/viewer/2022103010/58a7b1fc1a28ab70368b4fd7/html5/thumbnails/43.jpg)
Actor Container
Actor
Actor
Actor
Actor
Actor
Actor
Actor
![Page 44: Moderne backends mit dem aktor programmiermodell](https://reader038.fdocument.pub/reader038/viewer/2022103010/58a7b1fc1a28ab70368b4fd7/html5/thumbnails/44.jpg)
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](https://reader038.fdocument.pub/reader038/viewer/2022103010/58a7b1fc1a28ab70368b4fd7/html5/thumbnails/45.jpg)
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](https://reader038.fdocument.pub/reader038/viewer/2022103010/58a7b1fc1a28ab70368b4fd7/html5/thumbnails/46.jpg)
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](https://reader038.fdocument.pub/reader038/viewer/2022103010/58a7b1fc1a28ab70368b4fd7/html5/thumbnails/47.jpg)
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](https://reader038.fdocument.pub/reader038/viewer/2022103010/58a7b1fc1a28ab70368b4fd7/html5/thumbnails/48.jpg)
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](https://reader038.fdocument.pub/reader038/viewer/2022103010/58a7b1fc1a28ab70368b4fd7/html5/thumbnails/49.jpg)
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](https://reader038.fdocument.pub/reader038/viewer/2022103010/58a7b1fc1a28ab70368b4fd7/html5/thumbnails/50.jpg)
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.