Commit 7c05dfb
fix(prisma): add retry for Aurora Serverless v2 connection errors (#121)
## Issue
close #104
close #105
## Problem
The starter kit has three issues with Prisma + Aurora Serverless v2
(auto-pause enabled with `minCapacity: 0`):
1. **Credential leak**: `console.log(process.env.DATABASE_URL)` in
`prisma.ts` outputs the full connection string including password to
CloudWatch Logs.
2. **No runtime retry**: Aurora drops idle connections after
`idle_session_timeout` (60s) and takes ~15s to resume from auto-pause
([docs](https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/aurora-serverless-v2-auto-pause.html)).
Without retry, queries fail with transient errors (P1017, ECONNRESET)
and do not recover.
3. **No migration retry**: `migration-runner.ts` runs `prisma db push`
without retry. During `cdk deploy`, Aurora may still be resuming,
causing P1001 ("Can't reach database server") and failing the entire
deployment.
## Solution
- **Remove `console.log(DATABASE_URL)`** to fix the credential leak.
- **Add a Prisma client extension** (`Prisma.defineExtension` with
`$allModels.$allOperations`) that retries transient connection errors
with exponential backoff. Retryable errors: P2024, P1001, P1017,
idle-session timeout, ECONNRESET. Non-retryable errors (auth failures,
schema errors) are thrown immediately.
- **Add retry to `migration-runner.ts`** for `prisma db push` with
exponential backoff (base 3s, max 5 attempts, ~100s worst case within
Lambda 5min timeout). Only P1001 / connection refused are retried.
- **Optimize connection parameters**: `connection_limit=1` (Lambda
handles one request per instance), `connect_timeout=30` (accommodates
auto-pause resume time).
## Changes
- `webapp/src/lib/prisma.ts` — Remove `console.log`, remove verbose
`log` option, add retry extension via `$extends`
- `webapp/src/jobs/migration-runner.ts` — Extract `runPrismaDbPush` with
retry loop, structured logging
- `cdk/lib/constructs/database.ts` — Change connection options to
`?connection_limit=1&connect_timeout=30`
## Verification
- [ ] `console.log(process.env.DATABASE_URL)` is removed
- [ ] After Aurora auto-pause resume, the first request recovers via
retry
- [ ] Non-retryable errors (e.g. auth failure) are thrown immediately
without retry
- [ ] `cdk deploy` succeeds even when Aurora is resuming from 0 ACU
- [ ] `tsc --noEmit` passes
- [ ] `prettier --check` passes
---------
Co-authored-by: Kazuho Cryer-Shinozuka <malaysia.cryer@gmail.com>1 parent 84be605 commit 7c05dfb
File tree
5 files changed
+110
-32
lines changed- cdk
- lib/constructs
- test/__snapshots__
- webapp/src
- jobs
- lib
5 files changed
+110
-32
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
96 | 96 | | |
97 | 97 | | |
98 | 98 | | |
99 | | - | |
100 | | - | |
101 | | - | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
102 | 103 | | |
103 | 104 | | |
104 | 105 | | |
| |||
Lines changed: 6 additions & 6 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
723 | 723 | | |
724 | 724 | | |
725 | 725 | | |
726 | | - | |
| 726 | + | |
727 | 727 | | |
728 | 728 | | |
729 | 729 | | |
| |||
772 | 772 | | |
773 | 773 | | |
774 | 774 | | |
775 | | - | |
| 775 | + | |
776 | 776 | | |
777 | 777 | | |
778 | 778 | | |
| |||
3575 | 3575 | | |
3576 | 3576 | | |
3577 | 3577 | | |
3578 | | - | |
| 3578 | + | |
3579 | 3579 | | |
3580 | 3580 | | |
3581 | 3581 | | |
| |||
3624 | 3624 | | |
3625 | 3625 | | |
3626 | 3626 | | |
3627 | | - | |
| 3627 | + | |
3628 | 3628 | | |
3629 | 3629 | | |
3630 | 3630 | | |
| |||
3952 | 3952 | | |
3953 | 3953 | | |
3954 | 3954 | | |
3955 | | - | |
| 3955 | + | |
3956 | 3956 | | |
3957 | 3957 | | |
3958 | 3958 | | |
| |||
4001 | 4001 | | |
4002 | 4002 | | |
4003 | 4003 | | |
4004 | | - | |
| 4004 | + | |
4005 | 4005 | | |
4006 | 4006 | | |
4007 | 4007 | | |
| |||
Lines changed: 6 additions & 6 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
744 | 744 | | |
745 | 745 | | |
746 | 746 | | |
747 | | - | |
| 747 | + | |
748 | 748 | | |
749 | 749 | | |
750 | 750 | | |
| |||
793 | 793 | | |
794 | 794 | | |
795 | 795 | | |
796 | | - | |
| 796 | + | |
797 | 797 | | |
798 | 798 | | |
799 | 799 | | |
| |||
3405 | 3405 | | |
3406 | 3406 | | |
3407 | 3407 | | |
3408 | | - | |
| 3408 | + | |
3409 | 3409 | | |
3410 | 3410 | | |
3411 | 3411 | | |
| |||
3454 | 3454 | | |
3455 | 3455 | | |
3456 | 3456 | | |
3457 | | - | |
| 3457 | + | |
3458 | 3458 | | |
3459 | 3459 | | |
3460 | 3460 | | |
| |||
3758 | 3758 | | |
3759 | 3759 | | |
3760 | 3760 | | |
3761 | | - | |
| 3761 | + | |
3762 | 3762 | | |
3763 | 3763 | | |
3764 | 3764 | | |
| |||
3807 | 3807 | | |
3808 | 3808 | | |
3809 | 3809 | | |
3810 | | - | |
| 3810 | + | |
3811 | 3811 | | |
3812 | 3812 | | |
3813 | 3813 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
27 | 27 | | |
28 | 28 | | |
29 | 29 | | |
30 | | - | |
31 | | - | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
32 | 43 | | |
33 | 44 | | |
34 | 45 | | |
35 | 46 | | |
36 | | - | |
37 | | - | |
38 | | - | |
39 | | - | |
40 | | - | |
41 | | - | |
42 | | - | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
43 | 52 | | |
44 | 53 | | |
45 | 54 | | |
46 | 55 | | |
47 | | - | |
48 | | - | |
49 | | - | |
50 | | - | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
51 | 70 | | |
52 | | - | |
| 71 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
| 1 | + | |
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
8 | 8 | | |
9 | | - | |
10 | | - | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
11 | 69 | | |
12 | 70 | | |
0 commit comments