Session presented at DDD event in TVP/Microsoft UK HQ. Introduction to Azure Service Fabric, and focus on the actor model (formerly known as Project Orleans), with demos and documentation on how it is supported in Service Fabric. Goal: ask ourselves why did we really replace OO with "stateless services".
Azure Service Fabric and the Actor Model: when did we forget Object Orientation?
1. Azure Service Fabric and the Actor
Model: when did we forget Object
Orientation?
João Pedro “jota”Martins
03.September.2016
2. João Pedro “jota” Martins
Cloud Solution Architect @ Microsoft UK
Co-founder - GASP (architecture UG) + APPU (usability prof. assoc.)
TechEd 2006 – “Iron Architect” competition winner
BizTalk Server MVP 2006-2011
Former CTO @ |create|it| - Premium S.I./MSFT Partner @ Lisbon, Portugal
3. Ground Rules and Expectations
Murphy’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
4. What’s this session about?
MicrosoftAzureServiceFabric
=A platform for reliable, hyperscale, microservice-based applications
=“PaaS v2”+ Stateless+Stateful Services/Actors
=Platform for applications BORNFORTHECLOUD,not Lift&Shift’ed
ActorModel@ ServiceFabric
=model of concurrent computation that treats "actors"astheprimitivesofconcurrentcomputation.
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
6. Public Cloud Other CloudsOn Premises
Private cloud
Your laptop
MicrosoftAzure Service Fabric
A platformfor reliable,hyperscale,microservice-basedapplications
Microservices
Service Fabric
8. Windows OS
Windows OS Windows OS
Windows OS
Windows OS
Windows OS
Fabric
Node
Fabric
Node
Fabric
Node
Fabric
Node
Fabric
Node
Fabric
Node
Set of OS instances grouped to form a pool of resources
Cluster can scale to 1000s of machines, is self repairing, and scales-up or down
Acts as environment-independent abstraction layer [note:canspreadgeographicregions]
Service Fabric Cluster
= VM scale set running special services in it
9. Azure ServiceFabricCapabilities
High density service hosting
Partitioningsupport
Load balancing
Placement constraints
Consistent state replication framework
Reliable distributed key/value store,
collections and queues
Actor Model
Application deployment services:
Rolling update with rollback
Strong versioning
Side-by-side support
Leadership election
Name service for discovery
= enterprise-ready computational environment to run things at scale
13. Node 5Node 4Node 3 Node 6Node 2Node 1
Service partitioning
P2
S
S
S
P4
S
P1
S
P3S
S
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
14. Basics for devs
Download from WebPI
Sets up local dev cluster
with 5 nodes by default
Includes service fabric
explorer + powershell
cmdlets
Same code that runs on
Azure
16. What is a microservice in Service Fabric?
Is (logic + state) that is independently versioned, deployed, and scaled
Has a unique name that can be resolved (e.g. fabric:/myapplication/myservice)
Interacts with other microservices over well defined interfaces and
protocols like REST
Remains always logically consistent in the presence of failures
Hosted inside a “container” node
Can be written in any language/framework
Developed by a small engineering team
17. Reliable Services API
Build stateless services using existing technologies such as ASP.NET WebAPI
Build stateful services using reliable collections
• ReliableDictionary<T>, ReliableQueue<T>
• Data Contract Serializer
Manage the concurrency and granularity of state changes using transactions
Communicate 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
18. Queues Storage
“Classical” 3-Tier service pattern
Front End
(Stateless
Web)
Stateless
Middle-tier
Compute
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
19. Stateful
Middle-tier
Compute
Stateful services: Simplifydesign, reduce latency
Front End
(Stateless
Web)
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
22. Public Cloud Other CloudsOn Premises
Private cloud
ServiceFabric Programming Models
Your laptop
23. 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.
24. 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.
29. 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 actor
As there are no constructors, initializing or sending messages is the same
31. Actor/Client-to-Actor messaging
Remember 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.
33. Reentrancy
By default actors allow re-entrance
in the same logical call-chain context.
This can be disabled for specific actor types with:
ReentrancyMode = ActorReentrancyMode.Disallowed
in ActorConcurrencySettings when the actor type is registered with Service Fabric
34. Reading and updating state inside na actor
StateManager instance, inherited from Actor superclass
Reads 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).
35. Lifetimeand garbage collection
Actoractivation
-When a callcomes for an actor
and one is not already active, a
newactoris created
-The actor's state is loaded ifit's
maintaining state.
- The OnActivateAsync method
is called.
Activation =instantiate aCLR
object in memory +loading its
State if it exits
Actordeactivation
- Whenanactoris notusedfor
someperiodof time(60min),itis
removed from the Active Actors
table
- The OnDeactivateAsync
method is called
Being used =method call or
reminder method executed
GarbageCollection
When an actor is deactivated,
references to the actor object are
released and itcan be garbage
collected normally by the CLR GC
(1x/min)
Thisonlycleansuptheactor
object;itdoes not removestate
storedintheactor'sState
Manager.
The next time the actor is
activated, a newactorobjectis
createdandits stateisrestored.
«ACTORSAREMEANTTOABSTRACTAWAYTHELIFETIMEOFANOBJECT'SINSTANCE.JUSTCALLONEWHENYOUNEEDIT,ANDITWILLBETHERE.»
36. 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.
37. Reminders
Similar 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
38. Events
Designed for
Actor to Client
communication
Recreated incase
of failovers
1-Defineaninterfacethatdescribes the
eventspublished bytheactor.
4-Ontheclient, createaproxytotheactor
thatpublishestheeventandsubscribetoits
events.
2-Declaretheeventspublished bythe
actorintheactorinterface.
3-Ontheclient side,
implement theevent handler
5-Ontheactor,publishtheevents
39. 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
40. References
Azure Service Fabric - http://aka.ms/ServiceFabric
Azure Service Fabric Documentation - http://aka.ms/ServiceFabricdocs
Orleans: 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_model
Martin Fowler on Microservices - http://martinfowler.com/articles/microservices.html
Signup for Service Fabric on Linux - http://aka.ms/SFlinuxpreview
Try a Party Cluster (it’s free!):
http://aka.ms/TryServiceFabric
41. João Pedro “jota” Martins
JoaoPedro.Martins@Microsoft.com
blog.joaopedromartins.net
@lokijota
pt.linkedin.com/in/joaopedromartins/
thanks! questions?
Hinweis der Redaktion
The demos are based in software that is in preview. Problems are to be expected.
The programming model underlying this approach is different from what has been the best practice in the age of SOA.
Questions: [Hands raised]
Who has used Azure Web and Worker Roles?
Who has developed services-driven systems with stateless services?
Service Fabric enables you to build and manage scalable and reliable applications composed of microservices running at very high density on a shared pool of machines (commonly referred to as a Service Fabric cluster).
Powers a huge set of Microsoft Services: Azure SQL Database, Azure DocumentDB, Cortana, Power BI, Microsoft Intune, Azure Event Hubs, Skype for Business and others
It provides a sophisticated runtime for building distributed, scalable stateless and stateful microservices.
Also provides comprehensive application management capabilities for provisioning, deploying, monitoring, upgrading/patching, and deleting deployed applications.
Service Fabric is tailored to creating “born in the cloud” services that can start small, as needed, and grow to massive scale with hundreds or thousands of machines, creating Service Fabric clusters across availability sets in a region or across regions.
Fundamental concepts
The Actor model adopts the philosophy that everything is an actor. This is similar to the everything is an object philosophy used by some object-oriented programming languages.
An actor is a computational entity that, in response to a message it receives, can concurrently:
- send a finite number of messages to other actors;
- create a finite number of new actors;
- designate the behavior to be used for the next message it receives.
There is no assumed sequence to the above actions and they could be carried out in parallel.
Decoupling the sender from communications sent was a fundamental advance of the Actor model enabling asynchronous communication and control structures as patterns ofpassing messages.[8]
Recipients of messages are identified by address, sometimes called "mailing address". Thus an actor can only communicate with actors whose addresses it has. It can obtain those from a message it receives, or if the address is for an actor it has itself created.
The Actor model is characterized by inherent concurrency of computation within and among actors, dynamic creation of actors, inclusion of actor addresses in messages, and interaction only through direct asynchronous message passing with no restriction on message arrival order.
Executables
Any exe, in any language and programming model.
Packaged as Application, it gets versioning, upgrade, monitoring, health, etc.
Reliable Services
Stateless & stateful services
Uses SDK
Reliable Actors
Stateless & stateful actor objects
Uses SDK
https://azure.microsoft.com/en-us/documentation/articles/service-fabric-reliable-services-reliable-collections/
Reliable Collections can be thought of as the natural evolution of the System.Collectionsclasses: a new set of collections that are designed for the cloud and multi-computer applications without increasing complexity for the developer. As such, Reliable Collections are:
Replicated: State changes are replicated for high availability.
Persisted: Data is persisted to disk for durability against large-scale outages (for example, a datacenter power outage).
Asynchronous: APIs are asynchronous to ensure that threads are not blocked when incurring IO.
Transactional: APIs utilize the abstraction of transactions so you can manage multiple Reliable Collections within a service easily.
Reliable Collections provide strong consistency guarantees out of the box in order to make reasoning about application state easier. Strong consistency is achieved by ensuring transaction commits finish only after the entire transaction has been applied on a quorum of replicas, including the primary. To achieve weaker consistency, applications can acknowledge back to the client/requester before the asynchronous commit returns.
The Reliable Collections APIs are an evolution of concurrent collections APIs (found in theSystem.Collections.Concurrent namespace):
Asynchronous: Returns a task since, unlike concurrent collections, the operations are replicated and persisted.
No out parameters: Uses ConditionalResult to return a bool and a value instead of out parameters. ConditionalResult is like Nullable but does not require T to be a struct.
Transactions: Uses a transaction object to enable the user to group actions on multiple Reliable Collections in a transaction.
Executables
Any exe, in any language and programming model.
Packaged as Application, it gets versioning, upgrade, monitoring, health, etc.
Reliable Services
Stateless & stateful services
Uses SDK
Reliable Actors
Stateless & stateful actor objects
Uses SDK
The easiest way to visualize the Actor pattern at work is to look at human society . Each human (actor) is an independent entity . We all hold our own attributes, and we make decisions based on our knowledge (state and behaviour) and in reaction to the surrounding stipulates (messages) . We communicate with (send messages to) each other .
Similarly, if a system can be described by a number of independent, interactive actors, it can be modelled using the Actor pattern . For example, in a multiplayer game, each player can be considered as an actor; in an e-commerce system, each shopping cart can be considered as an actor; in an Internet of Things (IoT) scenario, a sensor can be considered as an actor . A player, a shopping cart, and a sensor don’t seem to have anything in common, yet because they all have states, they all demonstrate some behaviours, and they all represent independent entities, they can be abstracted as actors .
Good for representing discrete, independent entities e.g. IoT devices, workflow actions, shopping carts, domain entities, etc.
- The Actor concept was originated in the early 70 in order to do large scale parallel processing and is becoming more popular with the advent of cloud computing. It’s meant to make writing distributed applications easier by dividing the problem in multiple units of state and computation that can run in parallel. Each unit in a simple single-threaded. Actors are actually very similar to objects in the way they represent state with some logic that operates on that state. These actors communicates with each other using asynchronous messages.
- Running the actors on top of Service Fabric, you get the reliability of code as well as state and all other benefits of Service Fabric platform, like security, placement and resource balancing, zero downtime upgrade, locally replicated stores.
What is an actor?
- An independent unit of compute and [persistent] state with large number of them executing in parallel: an instance of a class
- Communicates with other actors using asynchronous messaging
- Has single threaded execution - turn based concurrency
- Good for representing discrete, independent entities e.g. IoT devices, workflow actions, shopping carts, domain entities, etc.
Actors are virtual, which means logically they always exist . You don’t need to create or destroy an actor . When you need to talk to an actor, you just use the actor proxy to send a message to it, Service Fabric will activate the actor if it hasn’t been activated or if it has been deactivated . After an actor hasn’t been used for a while, Service Fabric automatically will deactivate the actor to reduce resource consumption .