38
Spark the future.

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

Embed Size (px)

Citation preview

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

Spark the future.

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

Mahesh Krishnan

Creating highly available and resilient Microservices on Microsoft Azure Service Fabric

CLD334

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

AgendaWhy use microservices?Azure Service Fabric Platform – An introductionService Fabric Programming model

Reliable ActorsReliable Services

Wrap up

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

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

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

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

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

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

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

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

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

App1

App2

Service Fabric cluster with microservices

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

Types of MicroservicesStateless microserviceStateful microservices

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

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

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

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

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

Programming models

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

Service fabric applications

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

Reliable Actors API

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

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

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

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

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

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

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

Getting startedDownload the Service Fabric Download

Ensure PowerShell is installed and right execution policies set up

Run the DevClusterSetup.ps1script to start local cluster

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

Server side code - Interfacepublic interface IMyActor : IActor{ Task<string> DoWorkAsync();}

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

Server side code - Implementationpublic class MyActor : Actor, IMyActor{ public async Task<string> DoWorkAsync() { … return await Task.FromResult("result"); }}

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

Client side codevar app = "fabric:/HelloWorld";

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

var result = proxy.DoWorkAsync().Result;

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

Demo

Reliable Actors

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

Actor Lifecycle

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

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

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

Actor lifecycle - DeactivationUnused actors removed from the Active Actors table

OnDeactivateSync method is called on Actor

Once deactivated, objects are garbage collected by CLR

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

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

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

Reliable Services API

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

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, ..)

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

Reliable CollectionCan be used to create Stateful services.

Features:Multi-machineReplicated (HA)Persistance (durable)AsynchrounousTransactional

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

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

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

Server side code - Implementationpublic class MyStatelessService : StatelessService{ protected override ICommunicationListener CreateCommunicationListener() { …}

protected override async Task RunAsync(CancellationToken cancellationToken) { … }}

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

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();}

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

Demo

Reliable Services

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

Demo

Service Fabric in the portal

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

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

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

Complete your session evaluation on My Ignite for your chance to win one of many daily prizes.

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

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/

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

© 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.