Skip to content
bitzorcas
EN

Concept

Idempotency

Request deduplication — IdempotencyPipelineBehavior with IMemoryCache for automatic duplicate request detection and cached response replay.

Last updated

BitzOrcas provides automatic request idempotency through the IdempotencyPipelineBehavior in the Mediator pipeline. Requests carrying an Idempotency-Key header are automatically deduplicated.

How it works

Request with Idempotency-Key: "req-abc123"
IdempotencyPipelineBehavior
├── Key exists in cache? → Return cached response immediately
└── Key not found? → Execute handler → Cache result → Return response

Interface

Commands opt-in by implementing IIdempotentRequest:

public interface IIdempotentRequest
{
string GetIdempotencyKey();
}
public record CreateNoteCommand(string Title, string Content)
: IRequest<Result<Note>>, IIdempotentRequest
{
public string GetIdempotencyKey() => $"{Title.GetHashCode()}";
}

Idempotency-Key header

Clients send the idempotency key as an HTTP header:

POST /api/notes
Idempotency-Key: req-abc123
Content-Type: application/json
{"title": "Hello", "content": "World"}
  • First request: handler executes, result cached
  • Duplicate request: cached result returned immediately
  • Cache TTL: configurable (default: 5 minutes)

Implementation details

  • Uses IMemoryCache for single-instance deduplication
  • Cache key includes the idempotency key + tenant context
  • Handler results are serialized and cached for replay
  • Works with IUnitOfWork — cached results include committed transaction outcomes

Current limitations

LimitationDescription
Single-instanceIn-memory cache doesn’t share across instances
Memory pressureNo cache size limits — high-volume systems need bounded cache
No expiry notificationCached results persist until TTL expires

Production roadmap

For production multi-instance deployments:

{
"Idempotency": {
"Backend": "Redis",
"DefaultTtlSeconds": 300,
"MaxCacheSize": 10000
}
}

See also