AlgoMaster Logo
AlgoMasterRetry and Dead-Letter Outbox Eventsmedium

Retry and Dead-Letter Outbox Events

medium

An outbox relay should retry transient publication failures without retrying forever. After a configured number of failed delivery attempts, a poison event moves to a dead-letter state so later events and operators can make progress.

Design an OutboxRetryScheduler class:

  • OutboxRetryScheduler(int maxAttempts, int baseDelaySeconds) configures one retry policy.
  • String recordFailure(int sequence) records a failed delivery attempt.
  • String recordSuccess(int sequence) records successful publication.
  • int failures(int sequence) returns the stored failure count, or 0 for an unseen event.

For the f-th failure of a nonterminal event:

  • If f < maxAttempts, return "RETRY:d", where d = baseDelaySeconds * 2^(f - 1).
  • If f == maxAttempts, mark the event terminal and return "DEAD_LETTER".

recordSuccess marks any non-dead-lettered event "PUBLISHED". Both PUBLISHED and DEAD_LETTER are terminal: every later success or failure call returns the stored terminal string and does not change the failure count.

Example 1:

Input:

Output:

Explanation: The first two failures schedule exponential delays. The third reaches the cap, and subsequent calls cannot leave the dead-letter state.

Example 2:

Input:

Output:

Explanation: Success makes the event terminal with its one historical failure preserved.

Constraints

  • 1 <= maxAttempts <= 20
  • 1 <= baseDelaySeconds <= 1000
  • 0 <= sequence <= 10^9
  • All computed retry delays fit in a signed 32-bit integer.
  • At most 1000 method calls are made per object.
Hints

Loading...
CallReturns
new OutboxRetryScheduler(3, 5)null
recordFailure(7)"RETRY:5"
recordFailure(7)"RETRY:10"
recordFailure(7)"DEAD_LETTER"
recordFailure(7)"DEAD_LETTER"
failures(7)3

The third failure reaches the attempt cap. The dead-letter state is terminal, so the fourth call changes nothing.

Run checks these cases. Submit also runs a larger hidden set.