Acta. Durable execution for .NET

learn actastart jobs

How to keep a repeated request from creating a second job

With an idempotency key, a second enqueue with the same key answers the job that is already there.

A how-to for Acta, durable execution for .NET. New to Acta? Run your first durable job in five minutes.

Checked every release learn/000-fundamentals/010-idempotency-key
Read as

Point at anything with a dotted underline, or tap it, to see what it means.

1Give the enqueue a key that names the request

IdempotencyKeyFor(businessKey) on the raw builder stores the key with the job's name in front, so one business key used by two jobs never collides.

idempotency-key.csexcerpt
var jobs = host.Services.GetRequiredService<IJobs>();

var businessKey = $"welcome:{Guid.NewGuid():N}";
var finalKey = IdempotencyKey.ForDefinition("send-welcome-email", businessKey);

var request = JobRequestBuilder
    .Create("idempotency-key", "send-welcome-email")
    .Json(new SendWelcomeEmail("[email protected]", "Sam"))
    .IdempotencyKeyFor(businessKey)
    .Build();
 
var first = await jobs.EnqueueAsync(request, CancellationToken.None);

var second = await jobs.EnqueueAsync(new SendWelcomeEmail("[email protected]", "Sam"), o => o.IdempotencyKey(finalKey));

2Repeat the enqueue and get the same job back

A typed enqueue stores its key exactly as given, so to meet the first job it passes the same composed key. Two typed enqueues need only pass the same string to IdempotencyKey(...). This second enqueue answers the first job, deduplicated.

idempotency-key.csexcerpt
var first = await jobs.EnqueueAsync(request, CancellationToken.None);

var second = await jobs.EnqueueAsync(new SendWelcomeEmail("[email protected]", "Sam"), o => o.IdempotencyKey(finalKey));

3The job it acts on

idempotency-key.csexcerpt
public static class WelcomeJob
{
    [Job("send-welcome-email")]
    public static void Handle(SendWelcomeEmail input) => Console.WriteLine($"Sent welcome email to {input.Name} <{input.Email}>");
}

4Run it and check the output

From a clone of the Acta repository, with the .NET 10 SDK:

Terminal
dotnet run --project learn/000-fundamentals/010-idempotency-key
Its output must contain
Same job - deduplicated.
Sent welcome email to Sam <[email protected]>

Checked every release. Before each release, Acta Verify runs this program and checks that its output contains these lines.

Remember

  • handle.Action says which happened: EnqueueJobAction.Deduplicated when the key matched a job already there, Inserted when this enqueue created it.
  • The key prevents a second job row, not a second side effect. Execution stays at-least-once.
  • Keys keep their case: Order-1 and order-1 are two keys, so lowercase an id yourself when its source ignores case.
  • The name in [Job("send-welcome-email")] and the namespace in Run<IdempotencyKeyJobs>("idempotency-key") are stored with every job. Treat both as permanent once jobs exist under them: a job stored under a name no handler carries stays Ready, and nothing runs it.
  • SendWelcomeEmail is stored with each job as its input. Add a field as optional or with a default. A renamed, removed or retyped field needs a new job name such as send-welcome-email-v2, and the old handler stays registered until its jobs have finished: Change a job's input without breaking the jobs already stored.

The whole program

Every file of learn/000-fundamentals/010-idempotency-key, as it is compiled.

UseLocalDatabase is a helper of the Acta repository, not of the packages. In your own app, register the database with UseSqlite, UsePostgres or UseSqlServer, as in how to run your first job.

idempotency-key.cs64 lines
idempotency-key.cs
// How to keep a repeated request from creating a second job
// With an idempotency key, a second enqueue with the same key answers the job that is already there.
using Acta;
using Acta.Learn.IdempotencyKey;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

var builder = Host.CreateApplicationBuilder(args);

builder.Services.UseActa(j =>
{
    j.UseLocalDatabase(builder.Configuration);
    j.Run<IdempotencyKeyJobs>("idempotency-key");
});

using var host = builder.Build();
await host.StartAsync();

var jobs = host.Services.GetRequiredService<IJobs>();

var businessKey = $"welcome:{Guid.NewGuid():N}";
var finalKey = IdempotencyKey.ForDefinition("send-welcome-email", businessKey);

// learn: 1. Give the enqueue a key that names the request
// `IdempotencyKeyFor(businessKey)` on the raw builder stores the key with the job's name in front, so one business
// key used by two jobs never collides.
var request = JobRequestBuilder
    .Create("idempotency-key", "send-welcome-email")
    .Json(new SendWelcomeEmail("[email protected]", "Sam"))
    .IdempotencyKeyFor(businessKey)
    .Build();
 
var first = await jobs.EnqueueAsync(request, CancellationToken.None);

// learn: end

// learn: 2. Repeat the enqueue and get the same job back
// A typed enqueue stores its key exactly as given, so to meet the first job it passes the same composed key. Two
// typed enqueues need only pass the same string to `IdempotencyKey(...)`. This second enqueue answers the first
// job, deduplicated.
var second = await jobs.EnqueueAsync(new SendWelcomeEmail("[email protected]", "Sam"), o => o.IdempotencyKey(finalKey));

// learn: end

Console.WriteLine($"first:  job {first.JobRef} ({first.Action})");
Console.WriteLine($"second: job {second.JobRef} ({second.Action})");
Console.WriteLine($"business key: {businessKey}");
Console.WriteLine($"final key:    {finalKey}");
Console.WriteLine(first.JobRef == second.JobRef ? "Same job - deduplicated." : "Different jobs.");
Console.WriteLine("Worker is running - press Ctrl+C to stop.");

await host.WaitForShutdownAsync();
await host.StopAsync();

namespace Acta.Learn.IdempotencyKey
{
    public sealed record SendWelcomeEmail(string Email, string Name);

    public static class WelcomeJob
    {
        [Job("send-welcome-email")]
        public static void Handle(SendWelcomeEmail input) => Console.WriteLine($"Sent welcome email to {input.Name} <{input.Email}>");
    }
}

New to Acta?

Acta is durable execution for .NET: workflows and jobs that survive restarts, as a library inside your application with its durable state in your SQL database.