O slideshow foi denunciado.
Seu SlideShare está sendo baixado. ×

Integration-Monday-Stateful-Programming-Models-Serverless-Functions

Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Próximos SlideShares
Azure Durable Functions
Azure Durable Functions
Carregando em…3
×

Confira estes a seguir

1 de 34 Anúncio

Integration-Monday-Stateful-Programming-Models-Serverless-Functions

Baixar para ler offline

Get to know the two stateful programming models of Azure Serverless compute: workflows and actors and how these models can simplify development and how they enable stateful and long-running application patterns within Azure’s compute environments.

Get to know the two stateful programming models of Azure Serverless compute: workflows and actors and how these models can simplify development and how they enable stateful and long-running application patterns within Azure’s compute environments.

Anúncio
Anúncio

Mais Conteúdo rRelacionado

Diapositivos para si (20)

Semelhante a Integration-Monday-Stateful-Programming-Models-Serverless-Functions (20)

Anúncio

Mais de BizTalk360 (20)

Mais recentes (20)

Anúncio

Integration-Monday-Stateful-Programming-Models-Serverless-Functions

  1. 1. Stateful Programming Models in Serverless Functions @SteefJan
  2. 2. Me Azure Technology Consultant Microsoft MVP – Azure InfoQ Cloud Editor WAZUG NL board member Azure Lowlands Organizer Writer
  3. 3. Agenda • Serverless • Azure (Durable) Functions • Workflows • Actors • Key takeaways • Call to action
  4. 4. Serverless FUNCTIONS-AS-A-SERVICE (FAAS) SCALABLE, EVENT-DRIVEN PROGRAMMING MODELS GENERALLY LOW-COST; PAY ONLY FOR WHAT YOU USE
  5. 5. Azure Functions serverless programming model Author functions in C#, F#, JavaScript, TypeScript, Java, Python, PowerShell, and more CodeEvents React to timers, HTTP, or events from your favorite Azure services, with more on the way Outputs Send results to an ever- growing collection of services
  6. 6. Bindings programming model [FunctionName("QueueTriggerTableOutput")] [return: Table("outTable")] public static Person Create([QueueTrigger("myqueue-items")] JObject order) { return new Person { PartitionKey = "Orders", RowKey = Guid.NewGuid().ToString(), Name = order["Name"].ToString(), MobileNumber = order["MobileNumber"].ToString() }; } Trigger binding Output binding
  7. 7. “Functions must be stateless” “Functions must not call other functions” “Functions should do only one thing” FaaS principles and best practices?
  8. 8. Stateful pattern #1: Function chaining F1 F2 F3 Problems: • Relationship between functions and queues is unclear. • Operation context cannot be easily captured without a database. • Middle queues are an implementation detail (conceptual overhead). • Error handling adds a lot more complexity.
  9. 9. Declarative workflow solutions
  10. 10. Durable Functions Durable Task Framework (DTFx) Durable Functions runtime extension Durable storage / messaging
  11. 11. Writing flexible workflows Azure Function Extension Based up on the Durable Task Framework – OSS library since 2014. Persistence on Azure Storage Implementation of stateful workflow-as-code
  12. 12. What “chaining” could look like in code // calls functions in sequence public static async Task<object> Run(IDurableOrchestrationContext ctx) { try { var x = await ctx.CallActivityAsync("F1"); var y = await ctx.CallActivityAsync("F2", x); return await ctx.CallActivityAsync("F3", y); } catch (Exception) { // error handling/compensation can go here (or anywhere) } } Orchestrator Function Activity Functions
  13. 13. A. Memory snapshots B. Compiler hooks C. Event sourcing D. All the above E. None of the above How do we preserve local variable state?
  14. 14. Behind the scenes 1. var x = await ctx.CallActivityAsync("F1"); 2. var y = await ctx.CallActivityAsync("F2", x); 3. return await ctx.CallActivityAsync("F3", y); Orchestrator Function F1 => return 42; F2 => return n + 1; F3 => return n + 2; Execution started Task scheduled, F1 Task completed, F1 => 42 Task scheduled, F2 Task scheduled, F3 Task completed, F3 => 45 Orchestrator completed => 45 Task completed, F2 => 43
  15. 15. Orchestrator code MUST be deterministic! • Rule #1: Never write logic that depends on random numbers, the current date/time, etc. • Rule #2: Never do I/O or custom thread scheduling directly in the orchestrator function. • Rule #3: Do not write infinite loops • Rule #4: Use the built-in workarounds for rules #1, #2, and #3 When in doubt, use the Durable source code analyzer!
  16. 16. Stateful pattern #2: Fan-out & fan-in Problems: • Fanning-out is easy, but fanning-in is significantly more complicated • Stateful agent is required to track progress of all work • All the same problems of the previous pattern
  17. 17. Fan-out & fan-in as code public static async Task Run(IDurableOrchestrationContext context) { var parallelTasks = new List<Task<int>>(); // Get a list of N work items to process in parallel. object[] workBatch = await context.CallActivityAsync<object[]>("F1"); for (int i = 0; i < workBatch.Length; i++) { Task<int> task = context.CallActivityAsync<int>("F2", workBatch[i]); parallelTasks.Add(task); } await Task.WhenAll(parallelTasks); // Aggregate all N outputs and send the result to F3. int sum = parallelTasks.Sum(t => t.Result); await context.CallActivityAsync("F3", sum); }
  18. 18. https://www.microsoft.com/en-us/microsoft-365/customer-stories/fujifilm-manufacturing-azure-ai-functions-japan
  19. 19. Demo!
  20. 20. Ingest Data With Logic Apps you can automate a scheduled process of retrieving data and storing in a database (SQL or NoSQL) using a graphical interface (GUI) – Visual Designer. Convert Epoch to DateTime Ingest Pull Push Pull Push Function A Store in Collection
  21. 21. Ingest Data With Durable Functions you can automate a scheduled process of retrieving data and storing in a database using a Integrated Development Environment – Visual Studio or Visual Code. Orchestrator Client Orchestration Function Convert to TimeStamp Store In Database GetRates
  22. 22. Durable Function Types Orchestrator (Stateful) Activity (Stateless) Entity (Stateful) New! External / Client (Stateless) https://docs.microsoft.com/azure/azure-functions/durable/durable-functions-types-features-overview
  23. 23. Counter entity as a class (C#) public class Counter { [JsonProperty] public int value; public void Add(int amount) => this.value += amount; public void Reset() => this.value = 0; public int Get() => this.value; // Required boilerplate code [FunctionName(nameof(Counter))] public static Task Run([EntityTrigger] IDurableEntityContext ctx) => ctx.DispatchAsync<Counter>(); }
  24. 24. The entities concept Message EntityID: @Counter@Qux Operation: add Input: 1 Entity Class Name: Counter Entity Key: Foo State: 2 Entity Key: Bar State: 4 Entity Key: Baz State: 8 Entity Key: Qux State: 0 Function invocation Context(ID: @Counter@Qux, State: 0, Operation: add, Input: 1)
  25. 25. Demo!
  26. 26. Entities are addressable via an entity ID. Entity operations execute serially, one at a time. Entities are created implicitly when called or signaled. When not executing operations, entities are silently unloaded from memory Similarities with virtual actors
  27. 27. Serverless! Durability > Latency Reliable, in-order messaging No message timeouts One-way messaging* No deadlocks* Integration with orchestrations Differences from other virtual actor frameworks
  28. 28. Other applications • Serverless distributed circuit breaker • Polly announcement: https://github.com/App-vNext/Polly/issues/687 • Example implementation: https://dev.to/azure/serverless-circuit-breakers-with-durable-entities-3l2f • IoT device management • Device offline detection: http://case.schollaart.net/2019/07/01/device-offline-detection-with- durable-entities.html • Stream analysis and alerting: https://thecloudblog.net/post/iot-edge-device-monitoring-and- management-with-azure-durable-entities-functions-part-1/ • API cache • https://www.aaron-powell.com/posts/2019-09-27-using-durable-entities-and-orchestrators-to- create-an-api-cache/ • Ride sharing sample application • https://github.com/Azure/azure-functions-durable-extension/tree/master/samples/entitites- csharp/RideSharing
  29. 29. Key takeaways MODERATE LEARNING CURVE DURABLE FUNCTIONS EXCELLENT FOR BACKEND PROCESSES NOT A COMPETING TECHNOLOGY OF LOGIC APPS OPTION IN AZURE AS ALTERNATIVE TO AKKA.NET OR ORLEANS
  30. 30. Call to action https://aka.ms/DurableFunctions https://aka.ms/DFGitHub @AzureFunctions, @steefjan
  31. 31. @SteefJan https://github.com/steefjan Steef-Jan.Wiggers@codit.eu

×