Skip to Content

Retries

Learn how to configure retries for actions and steps

Duron provides built-in retry functionality for handling transient failures. Retries apply to steps: you set defaults for all steps of an action and override them per step. A job itself is not retried automatically: when a step error reaches the handler and the handler throws, the job fails. Use retryJob to run a failed job again.

Retry Configuration

Retry configuration uses exponential backoff with configurable limits and timeouts.

{
  retry: {
    limit: 4,        // Maximum number of retry attempts
    factor: 2,       // Exponential backoff factor
    minTimeout: 1000, // Minimum delay before first retry (ms)
    maxTimeout: 30000, // Maximum delay between retries (ms)
  },
}

Retry Options

limit

Maximum number of retry attempts. Defaults to 4.

{
  retry: {
    limit: 3, // Retry up to 3 times
  },
}

factor

Exponential backoff factor. The delay before retry N is minTimeout * factor^(N-1), capped at maxTimeout. Defaults to 2.

{
  retry: {
    factor: 2, // Double the delay each retry
  },
}

minTimeout

Minimum delay in milliseconds before the first retry. Defaults to 1000.

{
  retry: {
    minTimeout: 2000, // Wait at least 2 seconds before retrying
  },
}

maxTimeout

Maximum delay in milliseconds between retries. The calculated delay will be capped at this value. Defaults to 30000.

{
  retry: {
    maxTimeout: 60000, // Cap retry delays at 60 seconds
  },
}

Action-Level Retries

Configure retries for all steps in an action:

const processOrder = defineAction()({
  name: 'processOrder',
  steps: {
    retry: {
      limit: 3,
      factor: 2,
      minTimeout: 1000,
      maxTimeout: 10000,
    },
  },
  handler: async (ctx) => {
    // All steps will use the retry configuration above
    await ctx.step('fetchUser', async () => {
      return await fetchUser(ctx.input.userId)
    })
  },
})

Step-Level Retries

Override retry configuration for specific steps:

handler: async (ctx) => {
  // Use default retry configuration
  await ctx.step('fetchUser', async () => {
    return await fetchUser(ctx.input.userId)
  })

  // Override retry configuration for this step
  await ctx.step(
    'riskyOperation',
    async () => {
      return await riskyOperation()
    },
    {
      retry: {
        limit: 5,
        factor: 3,
        minTimeout: 2000,
        maxTimeout: 30000,
      },
    },
  )
}

Retry Behavior

When Retries Happen

Retries are triggered when a step throws an error. The step will be retried up to limit times before failing.

await ctx.step(
  'fetchData',
  async () => {
    const response = await fetch(url)
    if (!response.ok) {
      throw new Error('Failed to fetch') // This will trigger a retry
    }
    return response.json()
  },
  {
    retry: {
      limit: 3,
    },
  },
)

Retry Delays

The delay between retries follows exponential backoff:

  • Attempt 1: minTimeout ms
  • Attempt 2: minTimeout * factor ms
  • Attempt 3: minTimeout * factor^2 ms
  • Attempt N: minTimeout * factor^(N-1) ms (capped at maxTimeout)

Example with minTimeout: 1000, factor: 2:

  • Attempt 1: 1000ms
  • Attempt 2: 2000ms
  • Attempt 3: 4000ms
  • Attempt 4: 8000ms

With maxTimeout: 5000, the fourth delay would be capped at 5000ms.

The retry count is stored with the step. When a graceful stop returns a job to the queue, a step that was running or waiting for a retry runs again with its count back at 0.

Non-Retriable Errors

Some errors should not be retried. Use NonRetriableError to skip retries:

import { NonRetriableError } from 'duron/errors'

await ctx.step(
  'validateInput',
  async () => {
    if (!isValid(ctx.input)) {
      throw new NonRetriableError('Invalid input') // Won't retry
    }
  },
  {
    retry: {
      limit: 3,
    },
  },
)

Complete Example

import { defineAction } from 'duron/action'
import { NonRetriableError } from 'duron/errors'
import { z } from 'zod'

const sendEmail = defineAction()({
  name: 'sendEmail',
  input: z.object({
    to: z.string().email(),
    subject: z.string(),
    body: z.string(),
  }),
  steps: {
    retry: {
      limit: 3,
      factor: 2,
      minTimeout: 1000,
      maxTimeout: 10000,
    },
  },
  handler: async (ctx) => {
    // Validate input (don't retry validation errors)
    await ctx.step(
      'validateInput',
      async () => {
        if (!ctx.input.to.includes('@')) {
          throw new NonRetriableError('Invalid email')
        }
      },
      {
        retry: {
          limit: 0, // Don't retry validation
        },
      },
    )

    // Fetch user (retry on transient failures)
    const user = await ctx.step('fetchUser', async () => {
      return await fetchUser(ctx.input.to)
    })

    // Send email (use default retry config)
    await ctx.step('sendEmail', async () => {
      return await sendEmailToUser(user, ctx.input.subject, ctx.input.body)
    })

    return { success: true }
  },
})

Best Practices

Retry Transient Failures

Only retry errors that might succeed on retry:

await ctx.step('fetchData', async () => {
  try {
    return await fetch(url)
  } catch (error) {
    // Retry network errors
    if (error instanceof NetworkError) {
      throw error
    }
    // Don't retry validation errors
    throw new NonRetriableError('Invalid request', { cause: error })
  }
})

Use Appropriate Limits

Set retry limits based on the operation:

// Quick operations: fewer retries
{
  retry: {
    limit: 2,
    minTimeout: 500,
  },
}

// Slow operations: more retries
{
  retry: {
    limit: 5,
    minTimeout: 2000,
    maxTimeout: 60000,
  },
}

Consider Timeouts

Ensure retry configuration works with step timeouts:

// If step timeout is 10 seconds and retry limit is 5,
// make sure total retry time doesn't exceed timeout
{
  expire: 60 * 1000, // 60 seconds
  retry: {
    limit: 3,
    minTimeout: 1000,
    maxTimeout: 10000,
  },
}

On this page