Azure Service Fabric and the Actor Model: when did we forget Object Orientation?João Pedro “jota” Martins03.September.2016
João Pedro “jota” MartinsCloud Solution Architect @ Microsoft UKCo-founder - GASP (architecture UG) + APPU (usability prof. assoc.)TechEd 2006 – “Iron Architect” competition winnerBizTalk Server MVP 2006-2011Former CTO @ |create|it| - Premium S.I./MSFT Partner @ Lisbon, Portugal
Ground Rules and ExpectationsMurphy’s law:there will [probably] be problems in the
demos
This is a different way of architecting software: there will be disagreements
There is a lot to say: there won’t be enough time
What’s this session about?Microsoft Azure Service Fabric= A platform for reliable, hyperscale, microservice-based applications= “PaaS v2” + Stateless+Stateful Services/Actors= Platform for applications BORN FOR THE CLOUD, not Lift&Shift’ed
Actor Model @ Service Fabric= model of concurrent computation that treats "actors" as the primitives of concurrent computation. Actors can implement logic, create more actors, send messages, and determine how to respond to a message received. Actors only modify private state.= use Service Fabric for hosting~= Object Orientation with state persistence + communication through messaging passing
the platform
Public Cloud Other CloudsOn PremisesPrivate cloud
LifecycleMgmt
Independent Scaling
Independent Updates
Always On
Availability
ResourceEfficient
Stateless/Stateful
Your laptop
Microsoft Azure Service FabricA platform for reliable, hyperscale, microservice-based applications
Microservices
Service Fabric
Battle-hardened for over 5 yearsAzure Core
Infrastructure
thousands of machines
Power BI
Intune
800k devices
Azure SQL Database
1.4 million databases
Bing Cortana
500m evals/sec
Azure Document
DB
billions transactions/wee
k
Skype for Business
Hybrid Ops
Event Hubs
20bn events/day
IoT Suit
e
Halo 4 MP
as “Project Orleans”
Windows OS
Windows OS Windows OS
Windows OS
Windows OS
Windows OS
FabricNode
FabricNode
FabricNode
FabricNode
FabricNode
FabricNode
Set of OS instances grouped to form a pool of resourcesCluster can scale to 1000s of machines, is self repairing, and scales-up or downActs as environment-independent abstraction layer [note: can spread geographic regions]
Service Fabric Cluster
= VM scale set running special services in it
Azure Service Fabric CapabilitiesHigh density service hostingPartitioning supportLoad balancing Placement constraintsConsistent state replication frameworkReliable distributed key/value store, collections and queuesActor Model
Application deployment services: Rolling update with rollback Strong versioning Side-by-side support
Leadership electionName service for discovery
= enterprise-ready computational environment to run things at scale
Service Fabric MicroservicesA microservice is whatever you want it to be:
– ASP.NET, node.js, Java VMs, an arbitrary .exe (* Linux version coming soon)– What we deploy to Service Fabric
Stateless microservice– Has either no state or it can be retrieved from an external store – There can be N instances in multiple nodes– e.g. web frontends, protocol gateways, load balancers, etc.– Most of what we’ve been doing in the last 10 years
Stateful microservice– Maintain hard, authoritative state– N consistent /reliablecopies achieved through replication and local persistence– Reduces the complexity and number of components in traditional three-tier architecture – e.g. web user session , documents, workflow, user profile, shopping cart, IoT devices,
multiplayer games, etc.
App1 App2
Handling Machine Failures
App Type Packages Service Fabric Cluster VMs
#FAIL
Upgrading Services with zero downtime
Application Package FD0/UD0
FD0/UD1
FD1/UD6
FD1/UD5
FD2/UD4
FD2/UD3
FD – Failure Domain; UD – Upgrade Domain
Node 5Node 4Node 3 Node 6Node 2Node 1
Service partitioning
P2
S
SS
P4SP1
SP3SS
S
• Services can be partitioned for scale-out.• You can choose your own partitioning scheme.• Service partitions are striped across machines in the
cluster.• Replicas automatically scale out & in on cluster changes
Basics for devsDownload from WebPI
Sets up local dev cluster with 5 nodes by default
Includes service fabric explorer + powershell cmdlets
Same code that runs on Azure
the programming model
What is a microservice in Service Fabric?Is (logic + state) that is independently versioned, deployed, and scaledHas a unique name that can be resolved (e.g. fabric:/myapplication/myservice)Interacts with other microservices over well defined interfaces and protocols like RESTRemains always logically consistent in the presence of failuresHosted inside a “container” nodeCan be written in any language/frameworkDeveloped by a small engineering team
Reliable Services APIBuild stateless services using existing technologies such as ASP.NET WebAPIBuild stateful services using reliable collections• ReliableDictionary<T>, ReliableQueue<T>• Data Contract SerializerManage the concurrency and granularity of state changes using transactionsCommunicate with services using the technology of your choice • e.g. WebAPI , WCF
Collections• Single machine• Single threaded
Concurrent Collections• Single machine• Multi threaded
Reliable Collections• Multi machine• Multi threaded• Replicated (HA)• Persistence• Asynchronous• Transactional
Queues Storage
“Classical” 3-Tier service patternFront End(StatelessWeb)
StatelessMiddle-tierCompute
Cache
Scale with partitioned storage
Increase reliability with queues
Reduce read latency with caches
Manage your own transactions for state consistency
… Many moving parts each managed differently
Load Balancer
StatefulMiddle-tierCompute
Stateful services: Simplify design, reduce latency
Front End(StatelessWeb)
data stores used for analytics and disaster recovery
Application state lives in the compute tier
Low Latency reads and writes
Partitions are first class for scale-out
Built in transactions
Fewer moving parts
Load Balancer
Stateful service + Automatic Failoverdemo
Reliable Actors
Public Cloud Other CloudsOn PremisesPrivate cloud
LifecycleMgmt
Independent Scaling
Independent Updates
Always On
Availability
ResourceEfficient
Stateless/Stateful
Guest Executables Reliable Services API
Reliable Actors API
Service Fabric Programming Models
Web Apps - ASP.NET Core
- OWIN
Your laptop
What is the actor model?Independent entities… holding their own attributes… with their own behaviour… making decisions based on their knowledge… sending messages… and responding to messages. can be modelled with actor pattern/model.
Service Fabric Reliable Actors• Asynchronous and single-threaded programming
model• API to build stateless and stateful objects through the
virtual actor programming model• Support polymorphism/inheritance• Proxy for actor-to-actor and client-to-actor
communication• Runtime automatically maintains lifecycle of actors, and
handles failovers, scales, upgrades, partitioning, etc.
Stateful Actors: creating an actor servicedemo
Stateful Actors: managing a conferencedemo
Demo: “class diagram”
Defining an actor class
Creating an actor instance / Sending messages
ActorProxy.Create – creates an Actor with a given Id and returns a proxy to it, or returns a proxy to existing actorAs there are no constructors, initializing or sending messages is the same
Actor initializationJust create a method and call it via a message (there are no constructors), for example:
Actor/Client-to-Actor messagingRemember this requires network communication, including serialization and deserialization of payloads.
ActorProxy retries in case of communication failure or actor failover…Þ At-least-once message delivery
Þ … use idempotenceÞ Order is not guaranteed
Only one method of an actor instance is executed at any given time.
Turn-based concurrency
Only applies to each specific actor instance . Multiple actor instances run in parallel in the same host .
ReentrancyBy default actors allow re-entrancein the same logical call-chain context.
This can be disabled for specific actor types with:ReentrancyMode = ActorReentrancyMode.Disallowedin ActorConcurrencySettings when the actor type is registered with Service Fabric
Reading and updating state inside na actorStateManager instance, inherited from Actor superclassReads and writes that actor instance’s private data:- SetStateAsync(“name”, object) // name = object;- GetStateAsync<T>(“name”) || TryGetState<T> // return
name;- AddStateAsync(“name”, object) || TryAddStateAsync<T>- AddOrUpdateStateAsync(“name”, object, delegate)- RemoveStateAsync(“name”) || TryRemoveStateAsync
Values are persisted at the end of the actor method (all-or-nothing).
Lifetime and garbage collectionActor activation- When a call comes for an actor and one is not already active, a new actor is created- The actor's state is loaded if it's maintaining state.- The OnActivateAsync
method is called.
Activation = instantiate a CLR object in memory + loading its State if it exits
Actor deactivation- When an actor is not used for some period of time (60min), it is removed from the Active Actors table- The OnDeactivateAsync
method is called
Being used = method call or reminder method executed
Garbage CollectionWhen an actor is deactivated, references to the actor object are released and it can be garbage collected normally by the CLR GC (1x/min)This only cleans up the actor object; it does not remove state stored in the actor's State Manager. The next time the actor is activated, a new actor object is created and its state is restored.
«ACTORS ARE MEANT TO ABSTRACT AWAY THE LIFETIME OF AN OBJECT'S INSTANCE. JUST CALL ONE WHEN YOU NEED IT, AND IT WILL BE THERE.»
Timers- Schedule regular execution of a callback on an actor.- Time doesn’t count during execution.- Doesn’t prevent GC.- Respects turn-based concurrency.- Use UnregisterTimer to remove.
RemindersSimilar to Timers, but:- Are persisted with Actor state- Execute even if actor is deactivated/failover- Can prevent GC- Only one callback method- Use UnregisterReminder to remove
EventsDesigned for Actor to Client communication
Recreated in case of failovers
1 - Define an interface that describes the events published by the actor.
4 - On the client, create a proxy to the actor that publishes the event and subscribe to its events.
2 - Declare the events published by the actor in the actor interface.
3 - On the client side, implement the event handler
5 - On the actor, publish the events
In conclusion…Service Fabric Actors…• Have behaviour + state• Bring back the Object Orientation + statefulnes concepts we
had lost• Run in a performant enterprise-ready scalable environment• Especially suited for web session state, shopping cart, iot devices,
or any scenarios with independent objects with own lifetime/state/behaviour
But…• Requires re-architecting existing apps• API can still be improved
ReferencesAzure Service Fabric - http://aka.ms/ServiceFabric Azure Service Fabric Documentation - http://aka.ms/ServiceFabricdocsOrleans: Distributed Virtual Actors for Programmability and Scalability - https://www.microsoft.com/en-us/research/publication/orleans-distributed-virtual-actors-for-programmability-and-scalability/Book: Programming Microsoft Azure Service Fabric by Haishi Bai (Microsoft Press)Azure Service Fabric Samples - http://aka.ms/ServiceFabricSamples Akka.net (alternative .Net implementation of the Actor Model) - http://getakka.net/ Actor Model (1973) - https://en.wikipedia.org/wiki/Actor_modelMartin Fowler on Microservices - http://martinfowler.com/articles/microservices.htmlSignup for Service Fabric on Linux - http://aka.ms/SFlinuxpreview
Try a Party Cluster (it’s free!):
http://aka.ms/TryServiceFabric
João Pedro “jota” [email protected]@lokijotapt.linkedin.com/in/joaopedromartins/
thanks! questions?
Top Related