Accelerate

Accelerate development cycles through automated performance optimization and seamless tool integration

Accelerate is a community skill for implementing Prisma Accelerate connection pooling and caching, covering global database connections, query caching, cache invalidation, connection management, and edge-compatible database access patterns.

What Is This?

Overview

Accelerate provides patterns for using Prisma Accelerate to optimize database access in serverless and edge environments. It covers global connection pooling that maintains database connections across serverless function invocations, query caching that stores frequently accessed results at the edge for reduced latency, cache invalidation strategies using time-based expiration and on-demand purging, connection management that handles pool sizing, timeout configuration, and connection limits for serverless workloads, and edge-compatible database access that routes queries through Accelerate proxy for edge runtime compatibility. The skill enables developers to use Prisma in serverless and edge environments without connection exhaustion or cold start latency.

Who Should Use This

This skill serves developers deploying Prisma applications on serverless platforms like Vercel or Cloudflare Workers, teams optimizing database query performance with edge caching, and engineers managing database connection limits in high-concurrency serverless environments.

Why Use It?

Problems It Solves

Serverless functions create new database connections on each cold start exhausting connection pools. Edge runtimes cannot establish direct TCP connections to databases. Frequently queried data adds latency when fetched from the database on every request. Connection timeout errors occur when serverless concurrency exceeds database connection limits.

Core Highlights

Connection pooling maintains persistent connections shared across serverless invocations. Query cache stores results at edge locations for sub-millisecond repeated access. Edge proxy enables Prisma queries from edge runtimes that lack TCP support. Cache strategies combine time-based and on-demand invalidation.

How to Use It?

Basic Usage

// Prisma with Accelerate
import { PrismaClient }
  from '@prisma/client';
import { withAccelerate }
  from '@prisma/extension-'
    + 'accelerate';

const prisma = new PrismaClient()
  .$extends(withAccelerate());

// Cached query
async function getProducts() {
  return prisma.product
    .findMany({
      cacheStrategy: {
        ttl: 60,
        swr: 300,
      },
    });
}

// Uncached query for
// fresh data
async function getUser(
  id: string
) {
  return prisma.user
    .findUnique({
      where: { id },
    });
}

// Tagged cache for
// invalidation
async function getPosts() {
  return prisma.post.findMany({
    cacheStrategy: {
      ttl: 120,
      tags: ['posts'],
    },
    orderBy: {
      createdAt: 'desc' },
    take: 20,
  });
}

Real-World Examples

// Cache invalidation
import { PrismaClient }
  from '@prisma/client';
import { withAccelerate }
  from '@prisma/extension-'
    + 'accelerate';

const prisma = new PrismaClient()
  .$extends(withAccelerate());

// Create post and invalidate
async function createPost(
  data: {
    title: string;
    content: string;
    authorId: string;
  }
) {
  const post =
    await prisma.post.create({
      data });

  // Invalidate cached posts
  await prisma.$accelerate
    .invalidate({
      tags: ['posts'],
    });

  return post;
}

// Edge-compatible handler
// Works in Vercel Edge,
// Cloudflare Workers
export async function GET(
  request: Request
) {
  const url = new URL(
    request.url);
  const category =
    url.searchParams
      .get('category');

  const products =
    await prisma.product
      .findMany({
        where: category
          ? { category }
          : undefined,
        cacheStrategy: {
          ttl: 60,
          swr: 120,
          tags: ['products'],
        },
      });

  return Response.json(
    products);
}

Advanced Tips

Use stale-while-revalidate (swr) cache strategy to serve cached data while refreshing in the background for eventual freshness. Tag cache entries by entity type to invalidate related queries on mutations. Set the DATABASE_URL to the Accelerate proxy URL in edge deployments while keeping the direct URL for migrations.

When to Use It?

Use Cases

Deploy a Next.js API with Prisma on Vercel Edge Functions using Accelerate for database access. Cache product listing queries with tag-based invalidation on product updates. Scale a serverless API beyond database connection limits with connection pooling.

Related Topics

Prisma Accelerate, connection pooling, query caching, edge database access, and serverless optimization.

Important Notes

Requirements

Prisma Accelerate account with a generated API key. Prisma Client with the @prisma/extension-accelerate package. Database accessible from Prisma Accelerate proxy servers.

Usage Recommendations

Do: use cache tags for entity-based invalidation when mutations affect cached queries. Set appropriate TTL values based on how frequently data changes. Use swr for data that can tolerate brief staleness during revalidation.

Don't: cache user-specific queries that should not be shared across requests. Set extremely long TTL values for data that changes frequently. Use Accelerate proxy URL for database migrations which should connect directly.

Limitations

Accelerate adds a proxy hop that slightly increases latency for uncached queries. Cache invalidation is eventually consistent across edge locations. Free tier has connection and query limits that may not suit high-traffic applications.