Azure Functions Durable Entities: Stateful Serverless

Durable Entities bring actor-model programming to Azure Functions. Store state reliably without a database.

Entity Example

[FunctionName(nameof(Counter))]
public static void Counter([EntityTrigger] IDurableEntityContext ctx)
{
    int current = ctx.GetState<int>();
    switch (ctx.OperationName.ToLower())
    {
        case "add": current += ctx.GetInput<int>(); break;
        case "reset": current = 0; break;
        case "get": ctx.Return(current); break;
    }
    ctx.SetState(current);
}

Calling Entities

var entityId = new EntityId(nameof(Counter), "myCounter");
await client.SignalEntityAsync(entityId, "add", 5);
var state = await client.ReadEntityStateAsync<int>(entityId);

Use Cases

  • Shopping carts
  • Game state
  • Rate limiting counters
  • Aggregations

Discover more from C4: Container, Code, Cloud & Context

Subscribe to get the latest posts sent to your email.

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.