Skip to content

Commit 982f134

Browse files
ostenbomclaude
andcommitted
Retry checkout when a database request is dropped
The database proxy occasionally drops a request while it fails over, which failed the whole checkout and showed the customer an error. Retry the checkout's writes with exponential backoff before giving up. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 48f3387 commit 982f134

2 files changed

Lines changed: 50 additions & 13 deletions

File tree

‎lib/db/retry.ts‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
type RetryOptions = {
2+
attempts?: number;
3+
baseDelayMs?: number;
4+
};
5+
6+
// The database proxy occasionally drops a request while it fails over. Most of
7+
// those requests succeed if we try again a moment later, so retry with
8+
// exponential backoff before giving up.
9+
export async function withRetry<T>(
10+
operation: () => Promise<T>,
11+
{ attempts = 3, baseDelayMs = 100 }: RetryOptions = {},
12+
): Promise<T> {
13+
let lastError: unknown;
14+
15+
for (let attempt = 1; attempt <= attempts; attempt++) {
16+
try {
17+
return await operation();
18+
} catch (error) {
19+
lastError = error;
20+
if (attempt === attempts) break;
21+
22+
const delayMs = baseDelayMs * 2 ** (attempt - 1);
23+
console.warn(
24+
`[retry] attempt ${attempt} of ${attempts} failed, retrying in ${delayMs}ms`,
25+
error,
26+
);
27+
await new Promise((resolve) => setTimeout(resolve, delayMs));
28+
}
29+
}
30+
31+
throw lastError;
32+
}

‎lib/payments/actions.ts‎

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { redirect } from "next/navigation";
55
import { z } from "zod";
66
import { db } from "@/lib/db/drizzle";
77
import { getUser } from "@/lib/db/queries";
8+
import { withRetry } from "@/lib/db/retry";
89
import { payments, teamMembers, teams } from "@/lib/db/schema";
910

1011
const paymentSchema = z.object({
@@ -65,21 +66,25 @@ export async function processPayment(formData: FormData) {
6566
try {
6667
const validatedData = paymentSchema.parse(data);
6768

68-
await db.insert(payments).values({
69-
teamId: userTeam[0].teamId,
70-
...validatedData,
71-
});
69+
// A dropped database request used to fail the whole checkout, so retry
70+
// it a few times before showing the customer an error.
71+
await withRetry(async () => {
72+
await db.insert(payments).values({
73+
teamId: userTeam[0].teamId,
74+
...validatedData,
75+
});
7276

73-
await db
74-
.update(teams)
75-
.set({
76-
planName: validatedData.planName,
77-
subscriptionStatus: "active",
78-
updatedAt: new Date(),
79-
})
80-
.where(eq(teams.id, userTeam[0].teamId));
77+
await db
78+
.update(teams)
79+
.set({
80+
planName: validatedData.planName,
81+
subscriptionStatus: "active",
82+
updatedAt: new Date(),
83+
})
84+
.where(eq(teams.id, userTeam[0].teamId));
8185

82-
redirect("/dashboard?payment=success");
86+
redirect("/dashboard?payment=success");
87+
});
8388
} catch (error) {
8489
if (error instanceof z.ZodError) {
8590
throw new Error(error.issues.map((e) => e.message).join(", "));

0 commit comments

Comments
 (0)