Skip to content
Back to Blog
1 min read

Azure Functions Durable Entities for Stateful Serverless

I wrote “Azure Functions Durable Entities for Stateful Serverless” to share practical, production-minded guidance on this topic.

The Use Case

We needed to track real-time inventory across multiple warehouses. Traditional approaches:

  • Database with locking (too slow)
  • Redis cache (operational overhead)
  • Event sourcing (complex)

Durable Entities offer a middle ground.

Implementation

[FunctionName("InventoryEntity")]
public static void InventoryEntity(
    [EntityTrigger] IDurableEntityContext ctx)
{
    var state = ctx.GetState<InventoryState>() ?? new InventoryState();

    switch (ctx.OperationName.ToLower())
    {
        case "add":
            state.Quantity += ctx.GetInput<int>();
            break;
        case "remove":
            var removeQty = ctx.GetInput<int>();
            if (state.Quantity >= removeQty)
                state.Quantity -= removeQty;
            else
                throw new InvalidOperationException("Insufficient inventory");
            break;
        case "get":
            ctx.Return(state.Quantity);
            break;
    }

    ctx.SetState(state);
}

Calling the Entity

[FunctionName("UpdateInventory")]
public static async Task UpdateInventory(
    [HttpTrigger(AuthorizationLevel.Function, "post")] HttpRequest req,
    [DurableClient] IDurableEntityClient client)
{
    var entityId = new EntityId("InventoryEntity", "warehouse-001-sku-abc");
    await client.SignalEntityAsync(entityId, "add", 100);
}

State is automatically persisted and scales across multiple instances. For our inventory tracking, response times dropped from 200ms to 15ms while maintaining consistency.\n\n## Takeaways\n\nAdd a concise, personal takeaway and recommended next steps here.\n

Michael John Peña

Michael John Peña

Senior Data Engineer based in Sydney. Writing about data, cloud, and technology.