Working with Prisma Accelerate
1. Setting Up Prisma Accelerate
npm install @prisma/extension-accelerate
npx prisma generate --no-engine
| Step | Detail |
|---|---|
| Enable in PDP | Console → project → Accelerate |
| URL | prisma://accelerate...?api_key=... |
| No engine build | --no-engine reduces bundle |
2. Configuring Connection Pooling
| Aspect | Detail |
|---|---|
| Global pooler | Edge regions worldwide |
| Serverless | Eliminates cold-start pool exhaustion |
| No PgBouncer needed | Managed for you |
3. Enabling Query Caching
import { PrismaClient } from "@prisma/client";
import { withAccelerate } from "@prisma/extension-accelerate";
const prisma = new PrismaClient().$extends(withAccelerate());
const users = await prisma.user.findMany({
cacheStrategy: { ttl: 60, swr: 300 }
});
| Option | Detail |
|---|---|
| ttl | Seconds cached as fresh |
| swr | Stale-while-revalidate window |
| tags | Group for invalidation |
4. Setting Cache TTL
| Workload | TTL Recommendation |
|---|---|
| Hot reference | 300-3600s |
| User feed | 10-60s + SWR |
| Critical writes | Skip cache |
5. Implementing Cache Invalidation
await prisma.$accelerate.invalidate({ tags: ["users"] });
| Strategy | Detail |
|---|---|
| Tag-based | Invalidate by label |
| All | Invalidate everything (last resort) |
6. Monitoring Cache Hit Rate
| Source | Detail |
|---|---|
| PDP dashboard | Hit ratio per query |
| Response info | X-Accelerate-Cache header (HIT/MISS) |
7. Using Edge Caching
| Benefit | Detail |
|---|---|
| Latency | <50ms in many regions |
| Origin offload | Reduce DB load |
| Vercel/Cloudflare | Native edge runtime support |
8. Configuring Per-Query Cache
| Pattern | Detail |
|---|---|
| Add cacheStrategy | Per call basis |
| Omit | Bypass cache |
| Conditional | Cache only for anonymous users |
9. Handling Cache Misses
| Behavior | Detail |
|---|---|
| Origin fetch | Goes to DB on miss |
| SWR | Serves stale + refreshes async |
| Stampede | Coalesced via pooler |
10. Optimizing with Accelerate
| Tip | Detail |
|---|---|
| Idempotent reads | Cacheable; avoid for tx-bound |
| Tag granularly | Surgical invalidations |
| Combine with select | Minimize payload over the wire |