Retry with backoff

Something failed during a request. Defer the retry instead of blocking the response.

Use this to
  • A Stripe charge fails at checkout — retry in 1 minute without holding up the response
  • A CRM sync times out during signup — schedule retries over the next hour
  • A webhook delivery returns a 500 — back off and try again over the next day
  • An email send fails — retry with exponential backoff instead of showing the user an error
Code
Something fails in your API route — schedule a retry
// respond to the user now, retry happens later
try {
  await stripe.charges.create({ amount, customer });
} catch (err) {
  await dk.schedule("retry-charge", {
    key: order.id,
    delay: "1m",
  });
}
When the time comes, try again with backoff
dk.handle("retry-charge", {
  handler: async ({ key }) => {
    const order = await db.orders.find(key);
    await stripe.charges.create({
      amount: order.amount,
      customer: order.customerId,
    });
  },
  retry: {
    attempts: 5,
    backoff: "exponential",
  },
});
npm install delaykit