SaaS Forge uses Upstash Redis over its REST API for rate limiting and content caching. REST works from serverless functions without connection pools.
Setup
- Create a Redis database at console.upstash.com, in the region closest to your app.
- Copy the REST URL and token:
NEXT_PUBLIC_ALLOW_RATE_LIMIT="upstash"
UPSTASH_REDIS_REST_URL="https://….upstash.io"
UPSTASH_REDIS_REST_TOKEN="..."
NEXT_PUBLIC_ALLOW_RATE_LIMIT="upstash" makes production refuse to boot without the credentials, and pnpm doctor checks them with a PING.
Rate limits
| Limiter | Window | Applies to | Keyed by |
|---|---|---|---|
ratelimit | 5 requests / 60 s, sliding | Session routes with rateLimit: "default": avatar and CMS uploads (and downloads in the open-source repo) | User ID |
chatRateLimit | 20 requests / 60 s | Session routes with rateLimit: "chat" (none by default; AI chat is metered by credits instead) | User ID |
apiKeyRateLimit | 60 requests / 60 s | REST API calls with an API key (API Keys module) | API key |
| Better Auth | 100 requests / 60 s, 3 / 60 s for email sign-in | /api/auth/* | Client |
Limiters are defined in apps/web/server/ratelimit.ts, and each route picks one in apps/web/lib/route-policy.ts. Over the limit, guardRoute answers 429. Rate-limited routes need Upstash; without it they fail.
Content caching
With Upstash configured (and a CMS other than constant), these are cached for an hour:
| Key | Contents | Cleared by |
|---|---|---|
<name>-landing-page:cms:v1 | Landing page content | Saving in Admin → CMS |
<name>-documentation:v1 | Docs index (titles, slugs, categories) | Saving in Admin → Docs |
<name> is NEXT_PUBLIC_SAAS_NAME in lowercase. Postgres doc bodies are read from the database on every request.
Cache your own expensive reads the same way:
import { redis } from "@/server/redis";
const key = `reports:summary:${userId}`;
const cached = await redis.get<Summary>(key);
if (cached) return cached;
const summary = await buildSummary(userId);
await redis.set(key, summary, { ex: 300 });
return summary;
Rendering and hosting
- Signed-in pages are client components that fetch through tRPC. Server components prefetch with
queryClient.ensureQueryData(...)and hydrate, so the first paint has data. - Public pages that prefetch through the tRPC server proxy read request headers for the session, so they render per request. Redis is what keeps them cheap.
next/imagecaches optimized CMS images for 31 days (images.minimumCacheTTL).- On serverless hosts, use a pooled Postgres connection string and keep functions in the same region as the database.
Measuring
- Vercel Speed Insights (already mounted) for real-user Core Web Vitals.
- The admin SEO audit with a PageSpeed key for lab scores.
PRISMA_QUERY_LOGGING=trueto see every query and its duration.ANALYZE=truebuilds with@next/bundle-analyzeronce you add it tonext.config.mjs.
