1 min read
Azure Functions Durable Entities for Stateful Serverless
Durable Entities bring actor-model patterns to Azure Functions. Think of them as stateful objects in a serverless world.
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.