Spark the future.
Mahesh Krishnan
Creating highly available and resilient Microservices on Microsoft Azure Service Fabric
CLD334
AgendaWhy use microservices?Azure Service Fabric Platform – An introductionService Fabric Programming model
Reliable ActorsReliable Services
Wrap up
Traditional Monolithic approachA monolithic application has most of its functionality withina single process that is commonly componentized with libraries
Scales by cloning the app on multiple servers/VMs/containers
Microservices approachSeparates functionality into separate smaller services
Uses a standard communication protocol
Scales out be deploying each service independentlycreating instances of these services across servers/VMscontainers
Why use microservices?Can be versioned, deployed and scaled independentlyAllows use of different technologiesLoose coupling of servicesFaster delivery of featuresFacilitates small, focused teamsImproves fault isolation
Microsoft Azure Service FabricA platform for reliable, hyperscale, microservice-based applications
Azure
WindowsServer Linux
Hosted Clouds
WindowsServer Linux
Service Fabric
Private Clouds
WindowsServer Linux
High Availability
Hyper-Scale
Hybrid Operations
High Density
microservices
Rolling Upgrades Stateful
services
Low LatencyFast startup & shutdown
Container Orchestration & lifecycle management Replication &
Failover
Simple programming models
Load balancing
Self-healing
Data Partitioning
Automated Rollback
Health Monitoring
Placement Constraints
App1
App2
Service Fabric cluster with microservices
Types of MicroservicesStateless microserviceStateful microservices
Stateless MicroservicesHas either no state or state is persisted externally (such as Azure database or Azure storage)
There can be multiple instances of the services
e.g. Existing ASP.NET web apps, Azure cloud services
Stateful MicroservicesState is maintained within the microserviceAllows for hot data to be next to the computeState is partitioned for scaleEach partition state is replicated for reliability
e.g. Database, documents, user profile, shopping cart, etc
Programming models
Service fabric applications
Reliable Actors API
Actor programming modelIntroduced in 1973!An actor is the fundamental unit of computation
Does some processingHolds state
Communicates with other actors
Similar to objects in OOP
Reliable Actors APIBuild reliable stateless and stateful objects using a virtual Actor programming model
Suitable for applications with multiple independent unitsof scale and compute
Automatic state management and turn based concurrency
Reliable ActorsActors have an lD that maps to an object
Service Fabric manages nodes and can transparentlymove actors amongst them (provides HA and scale)
Actor methods run via turn based concurrency
Getting startedDownload the Service Fabric Download
Ensure PowerShell is installed and right execution policies set up
Run the DevClusterSetup.ps1script to start local cluster
Server side code - Interfacepublic interface IMyActor : IActor{ Task<string> DoWorkAsync();}
Server side code - Implementationpublic class MyActor : Actor, IMyActor{ public async Task<string> DoWorkAsync() { … return await Task.FromResult("result"); }}
Client side codevar app = "fabric:/HelloWorld";
var proxy = ActorProxy.Create<IMyActor> (ActorId.NewId(), app);
var result = proxy.DoWorkAsync().Result;
Demo
Reliable Actors
Actor Lifecycle
Actor lifecycle - ActivationIf actor is not active, a new actor is created
Its state is then loaded (for stateful actors)
OnActivateSync method is called
Added to Active Actors table
Actor lifecycle - DeactivationUnused actors removed from the Active Actors table
OnDeactivateSync method is called on Actor
Once deactivated, objects are garbage collected by CLR
Actor lifecycle – more conceptsScan interval – Time interval between successive scansby Actor runtime for inactive actors (Default: 60 seconds)
Idle timeout – Time an actor can be idle before it is deactivated (Default: 60 seconds)
ActorGarbageCollection attribute used to change defaults
Reliable Services API
Reliable Services APIWrite services that are reliable, available, scalable and provide consistency
Use Reliable Collections to persist state
Communicate using tech of your choice (WCF, WebAPI, ..)
Reliable CollectionCan be used to create Stateful services.
Features:Multi-machineReplicated (HA)Persistance (durable)AsynchrounousTransactional
Getting started…Only two methods you need to implement to get started.
CreateCommunicationListener – Method where the communication stack for the service is defined
RunAsync – This is where the Service runs the business logic – similar to WorkerRole’s Run method
Server side code - Implementationpublic class MyStatelessService : StatelessService{ protected override ICommunicationListener CreateCommunicationListener() { …}
protected override async Task RunAsync(CancellationToken cancellationToken) { … }}
Reliable Collectionvar myDictionary = await this.StateManager.GetOrAddAsync <IReliableDictionary<string, long>>("myDictionary");
using (var tx = this.StateManager.CreateTransaction()){ … await myDictionary.AddOrUpdateAsync(tx, "counter", 0, (k, v) => ++v); await tx.CommitAsync();}
Demo
Reliable Services
Demo
Service Fabric in the portal
Wrap up…Service Fabric is the next generation of PaaS that canbe used to create microservicesHas 2 programming models:
Reliable Actor APIReliable Service API
Can create either stateful or stateless servicesAvailability, Scalability, load balancing, etc comes OOB
Complete your session evaluation on My Ignite for your chance to win one of many daily prizes.
Continue your Ignite learning pathVisit Microsoft Virtual Academy for free online training visit https://www.microsoftvirtualacademy.com
Visit Channel 9 to access a wide range of Microsoft training and event recordings https://channel9.msdn.com/
Head to the TechNet Eval Centre to download trials of the latest
Microsoft products http://Microsoft.com/en-us/evalcenter/
© 2015 Microsoft Corporation. All rights reserved.Microsoft, Windows and other product names are or may be registered
trademarks and/or trademarks in the U.S. and/or other countries.MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY,
AS TO THE INFORMATION IN THIS PRESENTATION.