Starting fun with Durable Functions

Not so long ago I started developing a simple side project related to game development. Since it's just a proof-of-concept, I've decided, that I'll present here sneak peeks with the work done. There're two main parts of this project:

  • cloud backend based on serverless architecture
  • desktop client built with Electron and HTML5

As I said it's more like a concept than a real product so all bleeding edge technologies will be used just to confirm that "it's possible" :)

Starting with Durable Functions

Orchestrating workflows in Azure Functions is pretty new feature, which is still in the preview stage. The good thing is the fact, that it's currently somehow documented(not very good but still better than nothing) and draws bigger and bigger audience to FaaS built with Azure components. You can find all required details regarding installation and concepts of this functionality here. We'll go straight to the implementation.

Consider following diagram:

If you're familiar with Durable Task Framework, those names should be familiar in some way. We're telling the framework to start an orchestration, which will schedule activities in the proper order and perform some work. Note that communication between Client and Orchestration is not bidirectional. The idea here is to orchestrate work and don't wait for it. What we're given however is a link to the orchestration itself, where we can check the current status and result. 

The important thing here is that Orchestration aggregates data from Activity 1 and Activity 2 before going further. This allows for easy decoupling of responsibilities, so there's no problem when it comes to ordering tasks.

Let's check the real world example.


I converted the above diagram to the corresponding functions:

public static class Create
	public static async Task<HttpResponseMessage> StartOrchestration(
		[HttpTrigger(AuthorizationLevel.Function, methods: "post", Route = "orchestrators/run")] HttpRequestMessage req,
		[OrchestrationClient] DurableOrchestrationClient starter,
		TraceWriter log)
		// Function input comes from the request content.
		dynamic eventData = await req.Content.ReadAsAsync<object>();
		string instanceId = await starter.StartNewAsync("Galaxy_Create", eventData);

		log.Info($"Started orchestration with ID = '{instanceId}'.");

		return starter.CreateCheckStatusResponse(req, instanceId);

	public static async Task<string> RunImpl([OrchestrationTrigger] DurableOrchestrationContext context)
		var result = await Task.WhenAll(context.CallActivityAsync<string>("Utility_Coords"),

		await context.CallActivityAsync("Galaxy_Create_Impl", new CreateGalaxyContext(result[1], result[0]));

		return "Galaxy created!";

	public static async Task CreateGalaxy(
		[ActivityTrigger] CreateGalaxyContext context,
		[Table("galaxies")] IAsyncCollector<GalaxyDataEntity> galaxies)
			await galaxies.AddAsync(new GalaxyDataEntity(context.Name, context.Coords));                

If I start an orchestration, I'll get a new galaxy added to my table in Table Storage like so:

Let's analyze each function one by one.


It's our Orchestration Client. It acts as an interface to the orchestration, se we can start it. As you can see it works as a HttpTrigger - you can use whichever trigger you like like Event Hub or Event Grid. It returns a simple payload, which allow you to interact with the working orchestration:

    "id": "8d5c8fdf0cf54d87839d9d17223268bf",
    "statusQueryGetUri": "http://localhost:7071http://localhos/admin/extensions/DurableTaskExtension/instances/8d5c8fdf0cf54d87839d9d17223268bf?taskHub=SampleHubVS&connection=Storage&code=YgC1NtQXBBRJGO9sGbNWVA03nVjedvjPwPUWZ/lH3vIBVcngQIDYjg==",
    "sendEventPostUri": "http://localhost:7071/admin/extensions/DurableTaskExtension/instances/8d5c8fdf0cf54d87839d9d17223268bf/raiseEvent/{eventName}?taskHub=SampleHubVS&connection=Storage&code=YgC1NtQXBBRJGO9sGbNWVA03nVjedvjPwPUWZ/lH3vIBVcngQIDYjg==",
    "terminatePostUri": "http://localhost:7071/admin/extensions/DurableTaskExtension/instances/8d5c8fdf0cf54d87839d9d17223268bf/terminate?reason={text}&taskHub=SampleHubVS&connection=Storage&code=YgC1NtQXBBRJGO9sGbNWVA03nVjedvjPwPUWZ/lH3vIBVcngQIDYjg=="


The actual orchestration. It holds logic responsible for scheduling all tasks as needed.


The actual implementation of a worker, which is responsible for inserting data into a storage table. It's unaware of other functions - it just expects a particular context passed to it, which hold the necessary data.

There're other 2 functions in this solution(Utility_Coords and Utility_Name), but they're just a standard ActivityTriggers, which generate random data. 


As you can see Durable Functions ease developing organized flows greatly. Without this feature we'd have to use e.g. queues or perform HTTP requests to gather all the data. What is more it moves a solution towards event driven architectures and simplifies overall structure. In the next post we'll convert this solution to a passive application with Event Grid taking the role of the commander. Stay tuned!

Tips & tricks - finding invocation identifier in Azure Function

It's Friday so it's time for something easy and simple yet useful. You may wondering what is an identifier you often see when running a function:

Well, it's not a function instance identifier since it changes each time a function runs. In fact it's an invocation identifier and, while it's not so obvious initially, you can easy inject it into your function.

To do so you need a class, which is called ExecutionContext. Normally it's not available by default, but by injecting it you'll gain some information about a job function invocation like:

  • invocation identifier
  • function name
  • function directory

Now when you prepare a function similar to mine:

public static void Run(
	[TimerTrigger("*/15 * * * * *")]TimerInfo timer,
	ExecutionContext context,
	TraceWriter log)
	log.Info($"Function called with `InvocationId` {context.InvocationId}");
	log.Info($"Function called with `FunctionName` {context.FunctionName}");
	log.Info($"Function called with `FunctionDirectory` {context.FunctionDirectory}");

You'll get following result: