Mahesh Krishnan Creating highly available and resilient Microservices on Microsoft Azure Service Fabric CLD33 4

  • Published on

  • View

  • Download

Embed Size (px)


Spark the future.Mahesh KrishnanCreating highly available and resilient Microservices on Microsoft Azure Service FabricCLD334AgendaWhy use microservices?Azure Service Fabric Platform An introductionService Fabric Programming modelReliable ActorsReliable ServicesWrap upTraditional 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 isolationMicrosoft Azure Service FabricA platform for reliable, hyperscale, microservice-based applicationsAzure WindowsServerLinux

Hosted CloudsWindowsServerLinux

Service Fabric

Private CloudsWindowsServerLinuxHigh AvailabilityHyper-ScaleHybrid OperationsHigh DensitymicroservicesRolling UpgradesStateful servicesLow LatencyFast startup & shutdownContainer Orchestration & lifecycle managementReplication & FailoverSimple programming modelsLoad balancingSelf-healingData PartitioningAutomated RollbackHealth MonitoringPlacement ConstraintsApp1App2Service Fabric cluster with microservicesTypes of MicroservicesStateless microserviceStateful microservicesStateless 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, etcCloud Services vs Service FabricAzure Cloud servicesService FabricOne service instance/VMMultiple microservices/VMLower compute densityHigh microservice densitySlow in deployment and upgradesFasterSlower scaling and DRFast scaling across the clusterProgramming models

Service fabric applicationsReliable Actors APIActor programming modelIntroduced in 1973!An actor is the fundamental unit of computationDoes some processingHolds stateCommunicates with other actorsSimilar to objects in OOPReliable 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 DoWorkAsync();}Server side code - Implementationpublic class MyActor : Actor, IMyActor{ public async Task DoWorkAsync() { return await Task.FromResult("result"); }}Client side codevar app = "fabric:/HelloWorld";

var proxy = ActorProxy.Create (ActorId.NewId(), app);

var result = proxy.DoWorkAsync().Result;DemoReliable ActorsMicrosoft Ignite 2015 2015 Microsoft Corporation. All rights reserved. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.11/20/2015 11:27 AM23Actor LifecycleActor 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 APIReliable 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)AsynchrounousTransactionalGetting startedOnly 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 WorkerRoles 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 ("myDictionary");

using (var tx = this.StateManager.CreateTransaction()){ await myDictionary.AddOrUpdateAsync(tx, "counter", 0, (k, v) => ++v); await tx.CommitAsync();}

ScalabilityCan be partitioned for scalability you choose scheme

Node 5Node 4Node 3Node 6Node 2Node 1P2SSSP4SP1SP3SSSDemoReliable ServicesDemoService Fabric in the portalWrap upService Fabric is the next generation of PaaS that canbe used to create microservicesHas 2 programming models:Reliable Actor APIReliable Service APICan 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.comVisit Channel 9 to access a wide range of Microsoft training and event recordings to the TechNet Eval Centre to download trials of the latest Microsoft products

Microsoft Ignite 2015 2015 Microsoft Corporation. All rights reserved. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.11/20/2015 11:27 AM39 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.