Path: redb-route/redb.Route/Abstractions/IIdempotentRepository.cs
Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages.
License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route
Page: https://redbase.app/src/dev-2026-12-06/redb-route/redb.Route/Abstractions/IIdempotentRepository.cs
----
namespace redb.Route.Abstractions;
///
/// Repository that tracks message identifiers for idempotent consumption.
/// Implementations may use in-memory stores, Redis, or a database.
///
public interface IIdempotentRepository
{
///
/// Attempts to add the key. Returns true if the key was new (not a duplicate).
/// Returns false if the key already exists (duplicate).
///
/// Unique message identifier.
/// Cancellation token.
/// True if the message should be processed; false if it is a duplicate.
Task Add(string key, CancellationToken ct = default);
///
/// Confirms the key after successful processing.
/// For implementations that use two-phase commit (add → process → confirm).
///
/// Unique message identifier.
/// Cancellation token.
Task Confirm(string key, CancellationToken ct = default);
///
/// Removes the key, allowing it to be reprocessed.
/// Called on rollback or when retrying.
///
/// Unique message identifier.
/// Cancellation token.
Task Remove(string key, CancellationToken ct = default);
///
/// Checks if the key already exists in the repository.
///
/// Unique message identifier.
/// Cancellation token.
/// True if the key exists.
Task Contains(string key, CancellationToken ct = default);
///
/// Removes all keys from the repository.
/// Primarily for testing.
///
/// Cancellation token.
Task Clear(CancellationToken ct = default);
///
/// Whether writes through an ambient , so that its
/// rollback takes the key back. The idempotent consumer relies on it inside a .Transacted() block: a key that
/// is part of the transaction goes with its rollback and stays with its commit, and the consumer must not remove it
/// afterwards — another node may already hold the same key again. A repository that keeps its keys outside the
/// transaction (in memory, say) returns , the default, and the consumer removes the key itself
/// when the transaction does not commit.
///
bool JoinsAmbientTransaction => false;
///
/// Claims the key as does, and when it is held already tells why: the message is still being
/// processed under an earlier claim () or was processed and confirmed
/// (). An unconfirmed claim older than is
/// taken to be abandoned (its holder stopped between and ) and is taken over
/// (): the repository replaces exactly that claim, so when two callers try it at
/// once one wins and the other gets .
///
/// The default claims with and takes any held key for confirmed, which is what a caller of
/// assumes. A repository that keeps the confirmed state and the time of the claim overrides it, so
/// that a receiver can tell a resend of a message still in flight from a duplicate, and so that a claim left behind
/// by a stopped node does not block its message for good.
///
///
/// Unique message identifier.
/// Age past which an unconfirmed claim is taken over; null never takes one over.
/// Cancellation token.
async Task Claim(string key, TimeSpan? abandonedAfter, CancellationToken ct = default) =>
await Add(key, ct).ConfigureAwait(false) ? IdempotentClaim.Claimed : IdempotentClaim.Confirmed;
///
/// that also names the claim it took: identifies this claim of
/// the key, so that and
/// act on it only while it is still the one held. A holder
/// that outlived and was taken over then confirms or removes nothing of the new
/// holder's claim.
///
/// The default answers without a token, and the token methods then act on the key as
/// and do.
///
///
/// Unique message identifier.
/// Age past which an unconfirmed claim is taken over; null never takes one over.
/// Cancellation token.
async Task Lease(string key, TimeSpan? abandonedAfter, CancellationToken ct = default) =>
new(await Claim(key, abandonedAfter, ct).ConfigureAwait(false), null);
///
/// Confirms the claim names (from ) after successful processing. Returns
/// false, and confirms nothing, when the key is no longer held by that claim: it was taken over or removed. A null
/// token confirms the key as does.
///
/// Unique message identifier.
/// The of the claim.
/// Cancellation token.
async Task Confirm(string key, string? token, CancellationToken ct = default)
{
await Confirm(key, ct).ConfigureAwait(false);
return true;
}
///
/// Removes the claim names (from ), so the message can be processed again.
/// Returns false, and removes nothing, when the key is no longer held by that claim: another holder's claim stays. A
/// null token removes the key as does.
///
/// Unique message identifier.
/// The of the claim.
/// Cancellation token.
async Task Release(string key, string? token, CancellationToken ct = default)
{
await Remove(key, ct).ConfigureAwait(false);
return true;
}
}
/// The outcome of : the claim's outcome and, when the caller holds it, its token.
/// As answers.
///
/// The id of the claim the caller holds ( or ),
/// for and
/// ; null otherwise, and from a repository
/// that keeps no claim ids.
///
public readonly record struct IdempotentLease(IdempotentClaim Outcome, string? Token)
{
/// Whether the caller holds the claim and processes the message.
public bool Holds => Outcome is IdempotentClaim.Claimed or IdempotentClaim.TakenOver;
}
/// The outcome of .
public enum IdempotentClaim
{
/// The key was new: the caller holds the claim and processes the message.
Claimed,
/// An abandoned, unconfirmed claim was replaced: the caller holds the claim and processes the message.
TakenOver,
/// Another claim of the key is not confirmed yet: its message is still being processed.
InFlight,
/// The message was processed and its claim confirmed: a duplicate.
Confirmed,
}