Slide 1

Slide 1 text

Jonah Andersson Developer Fundamentals of Serverless .NET Development with Azure Durable Functions 2021-12-01

Slide 2

Slide 2 text

Hi, I’m Jonah Andersson! ☺ ▪ Software Engineer/Developer ▪ Full stack .NET ▪ Certified Azure Developer ▪ Microsoft MVP – Azure ▪ Microsoft Technical Trainer ▪ Founder/Community Leader of Azure User Group Sweden ▪ I mentor to inspire others into tech!

Slide 3

Slide 3 text

Jonah – Offline Version

Slide 4

Slide 4 text

Serverless Azure Functions Durable Functions Application Patterns Demo/Examples Learning Resources

Slide 5

Slide 5 text

5 Scenario: 5 Philosophers, 5 Chopsticks A philosopher needs both Chopsticks - on left and right to eat. Otherwise, he/she can’t The Dining Philosophers Problem PROBLEMS: • Deadlock • Starvation

Slide 6

Slide 6 text

6 Have you been deadlocked? A deadlock is a condition where a program cannot access a resource it needs to continue. When an active application hits a deadlock, it may "hang" or become unresponsive. https://techterms.com/definition/deadlock

Slide 7

Slide 7 text

7 CONCURRENCY DEADLOCK RACING CONDITIONS

Slide 8

Slide 8 text

What is really Serverless? Abstraction of servers and infrastructures Break logic into functions Focus on developer productivity Autoscaling Great integration Consumption model (Pay-as-You-Go) Automation of processes

Slide 9

Slide 9 text

Serverless Sweet Swedish “Fika” Comparison CONTROL PRODUCTIVITY MONOLITH SERVERLESS MICROSERVICES CONTAINERIZATION

Slide 10

Slide 10 text

Azure Functions Azure Serverless Compute Service On-Demand , Pay-as-you-Go Event-Driven Focus on logic = Developer productivity Integration with other services & APIs Azure Functions

Slide 11

Slide 11 text

Azure Durable Functions Extension of Azure Functions Stateful Workflows in Serverless Environments Manages, Checkpoints and Restarts State for you Durable Task Framework https://github.com/Azure/durabletask C#, Javascript, Python, F#, Powershell

Slide 12

Slide 12 text

Azure Functions Azure Durable Functions STATELESS STATEFUL

Slide 13

Slide 13 text

Orchestrator Functions Client Functions Activity Functions Durable Function Types Entity Functions

Slide 14

Slide 14 text

The Orchestrator is our FRIEND but STRICT! More info about Deterministic APIs: https://docs.microsoft.com/en-us/azure/azure- functions/durable/durable-functions-code- constraints#using-deterministic-apis DETERMINISTIC!

Slide 15

Slide 15 text

With Determined Orchestrator No Orchestrator

Slide 16

Slide 16 text

Orchestrator Code Constraints – DONTs & DOs Dont generate random numbers or GUIDs NewGuid() method Dont access data stores like databases and configurations Blocking APIs - Threads like Sleep, Run or Delay Dont create Infinite Loops! CreateAsNew() Pass configurations into Activity function Use DurableOrchestrationContext Dont use CurrentDateTime Use.CurrentUtcDateTime() https://docs.microsoft.com/en-us/azure/azure-functions/durable/durable-functions-code-constraints

Slide 17

Slide 17 text

Application Patterns of Azure Durable Functions Function Chaining Fan out / Fan in Async HTTP API Monitor Human Interaction Aggregator

Slide 18

Slide 18 text

Function Chaining Pattern F1 F2 F4 F3 Execute a sequence of functions in a particular order like a chain! Output of F1 is required input of F2, so on!

Slide 19

Slide 19 text

Orchestrator STARTED Activity 1 SCHEDULED Orchestrator SLEEPS! Activity 1 DONE Orchestrator WAKES UP! Activity 2 SCHEDULED Activity 2 DONE Orchestration COMPLETED Orchestrator SLEEPS! Function Chaining in ACTION!

Slide 20

Slide 20 text

The Orchestrator can be a sleepyhead but it is GOOD!

Slide 21

Slide 21 text

No content

Slide 22

Slide 22 text

Fan Out / Fan In Pattern Execute multiple functions in parallel and then wait for all functions to finish and aggregate the results when done.

Slide 23

Slide 23 text

E2_BackupSiteContent orchestrator function

Slide 24

Slide 24 text

Async HTTP APIs Pattern Start DoWork GetStatus Solves the problem of coordinating the state of long-running operations with external clients Use HTTP Endpoint trigger the long running action Redirect a client to a status endpoint

Slide 25

Slide 25 text

Async HTTP APIs Pattern

Slide 26

Slide 26 text

Monitor Pattern ▪ Flexible, recurring process in a workflow ▪ Polling until specific conditions are met ▪ Use Case: Periodic job clean up

Slide 27

Slide 27 text

Monitor Pattern

Slide 28

Slide 28 text

Human Interaction Pattern Requests Approval ProcessApproval Escalate Automate business processes that requires human interaction

Slide 29

Slide 29 text

Approval Workflow Example

Slide 30

Slide 30 text

Aggregator (Stateful Entities) Aggregating event data over a period of time into a single addressable entity.

Slide 31

Slide 31 text

Aggregator (Stateful Entities) https://docs.microsoft.com/en-us/azure/azure-functions/durable/durable-functions-entities

Slide 32

Slide 32 text

Sub-Orchestrations in Durable Functions Sub-Orchestration in Orchestration Supported .NET, Javascript, Python

Slide 33

Slide 33 text

Sub-Orchestration in Code

Slide 34

Slide 34 text

Developing Azure Functions Visual Studio or VS Code Azure Portal, Azure CLI Azure Functions Core Tools https://github.com/Azure/azure-functions-core-tools A client for web requests like a web browser or Postman Azure Storage Explorer (Local Dev) Supported Languages C#, Java, Powershell, Python, Typescript, etc.

Slide 35

Slide 35 text

Fullstack Development with Azure Functions Azure Static Web Apps – React, Blazor, Vue.js, etc. API support – Azure Static Web Apps with Azure Functions Bring Your Own Functions

Slide 36

Slide 36 text

Orchestration Details https://docs.microsoft.com/en-us/azure/azure-functions/durable/durable-functions-instance-management

Slide 37

Slide 37 text

Coding Durable Functions – Good to know & have! Azure Application Insights Concious Versioning Side-by-Side Deployment Deployment with Breaking Change Azure Storage Explorer Deployment Slots Azure Key Vault / Configurations https://docs.microsoft.com/en-us/azure/azure-functions/durable/durable-functions-versioning

Slide 38

Slide 38 text

Error Handling – FunctionFailedException() Call another function activity Use Automatic Retry on Failure – CallActivityWithRetryAsync() Try-Catch Blocks https://docs.microsoft.com/en-us/azure/azure-functions/durable/durable-functions-error-handling Durable Timer ( context.CreateTimer()) .NET App Logging / Application Insights Durable Functions Monitor https://github.com/scale-tone/DurableFunctionsMonitor#durable-functions-monitor

Slide 39

Slide 39 text

Automatic Retry

Slide 40

Slide 40 text

Try – Catch

Slide 41

Slide 41 text

https://docs.microsoft.com/en-us/azure/azure-functions/durable/durable-functions-unit-testing Unit Testing

Slide 42

Slide 42 text

Deployment & CI /CD Azure Function App (Windows + Linux) Azure Function App Container Azure Container Registry GitHub Actions/Workflow ARM Templates PubXml

Slide 43

Slide 43 text

Azure Container Apps (Preview) MS Docs: https://docs.microsoft.com/en-us/azure/container-apps/ Fully managed serverless container service Built for Microservices scales dynamically based on HTTP traffic, events, and long- running background jobs Full support for KEDA and DAPR

Slide 44

Slide 44 text

https://azure.microsoft.com/en-us/updates/generally-available-azure-functions-runtime-40/

Slide 45

Slide 45 text

Use Case: Serverless Power for Azure’s Cloud Security https://www.microsoft.com/security/blog/2021/08/19/automating-security-assessments-using-cloud-katana/ https://cloud-katana.com/ By Roberto Rodriguez https://github.com/Azure/Cloud-Katana

Slide 46

Slide 46 text

Demo - Serverless Function Chaining Example F1 F2 F3 Azure BLOB Trigger Orchestrator Function Starts Orchestration SendMessageToServiceBus() SendSMSCallviaTwilio() SendNotificationEmail() https://github.com/jonahandersson/serverlesslab-azure-durable-functionchaining-template

Slide 47

Slide 47 text

Expected Results

Slide 48

Slide 48 text

Learning Resources • http://aka.ms/azurefunctions • Microsoft Learn – Azure Functions • Get updates via Twitter @AzureFunctions • Azure Functions University by Marc Duiker https://bit.ly/az-func-uni-playlist (YouTube) https://bit.ly/az-func-uni (GitHub) • https://github.com/Azure/azure-functions-durable-extension • Azure Serverless Community Library https://www.serverlesslibrary.net/

Slide 49

Slide 49 text

https://www.meetup.com/azureusergroupsundsvallsverige For the community, by the community!

Slide 50

Slide 50 text

As a developer, I Serverless because I can solve complex problems, build modern apps and it makes me productive!

Slide 51

Slide 51 text

Tack så mycket! Thanks! https://www.jonahandersson.tech https://linkedin.com/in/jonahandersson @cjkodare