Session caching with cookies
By default, every call togetSession or useSession hits the database.
Cookie caching stores a short-lived signed copy of the session in the browser,
eliminating the database round-trip for repeated reads.
auth.ts
maxAge seconds.
Read more in the session management docs.
Framework-level caching
- Next.js
- React Router
- SolidStart
- TanStack Query
Use the Learn more in the Next.js
"use cache" directive (Next.js 15+) on server functions that
return user lists or other infrequently-changing data:use cache docs.SSR session prefetching
Pre-fetch the session on the server and pass it as initial data to the client to avoid a waterfall request:Background tasks
On serverless platforms, non-critical work like cleanup, analytics, and email can run after the response is sent using thebackgroundTasks option. This
reduces perceived latency without sacrificing correctness.
auth.ts
Database indexing
Adding indexes to the core tables has the highest impact at scale. The table below lists the fields that benefit most from indexes:Index support in the
generate / migrate CLI commands is planned for a
future release.Bundle size optimization
If you are using a custom ORM adapter (Prisma, Drizzle, MongoDB) you can reduce your server bundle by importing frombetter-auth/minimal. This
variant omits the bundled Kysely dependency.
- Prisma
- Drizzle
- MongoDB
auth.ts
Rate limiting
Better Auth includes built-in rate limiting. In high-traffic scenarios, usingsecondaryStorage (Redis, Cloudflare KV) for rate-limit counters instead of
in-memory storage avoids state loss across serverless invocations: