Optimizing for Performance
A guide to optimizing your Entro Auth application for performance.
In this guide, we’ll go over some of the ways you can optimize your application for a more performant Entro Auth app.
Caching
Caching is a powerful technique that can significantly improve the performance of your Entro Auth application by reducing the number of database queries and speeding up response times.
Cookie Cache
Calling your database every time useSession or getSession is invoked isn’t ideal, especially if sessions don’t change frequently. Cookie caching handles this by storing session data in a short-lived, signed cookie similar to how JWT access tokens are used with refresh tokens.
To turn on cookie caching, just set session.cookieCache in your auth config:
import { entroAuth } from "@entro314labs/entro-auth";
export const auth = entroAuth({
session: {
cookieCache: {
enabled: true,
maxAge: 5 * 60, // Cache duration in seconds
},
},
});Read more about cookie caching.
Framework Caching
Here are examples of how you can do caching in different frameworks and environments:
Since Next v15, we can use the "use cache" directive to cache the response of a server function.
export async function getUsers() {
'use cache'
const { users } = await auth.api.listUsers();
return users
}Learn more about NextJS use cache directive.
Background Tasks
On serverless platforms (Vercel, Cloudflare Workers), you can improve response times by deferring non-critical work—cleanup, analytics, rate limit updates, email sending—to run after the response is sent. Configure advanced.backgroundTasks and use ctx.context.runInBackground or ctx.context.runInBackgroundOrAwait in hooks.
import { entroAuth } from "@entro314labs/entro-auth";
import { createAuthMiddleware } from "@entro314labs/entro-auth/api";
import { waitUntil } from "@vercel/functions";
export const auth = entroAuth({
advanced: {
backgroundTasks: { handler: waitUntil },
},
hooks: {
after: createAuthMiddleware(async (ctx) => {
if (ctx.path === "/sign-up/email") {
ctx.context.runInBackground(logSignUp(ctx.context.newSession?.user.id));
}
}),
},
});See the Context documentation for more examples.
SSR Optimizations
If you're using a framework that supports server-side rendering, it's usually best to pre-fetch the user session on the server and use it as a fallback on the client.
const session = await auth.api.getSession({
headers: await headers(),
});
//then pass the session to the clientDatabase optimizations
Optimizing database performance is essential to get the best out of Entro Auth.
Recommended fields to index
| Table | Fields | Plugin |
|---|---|---|
| users | email | |
| accounts | userId | |
| sessions | userId, token | |
| verifications | identifier | |
| invitations | email, organizationId | organization |
| members | userId, organizationId | organization |
| organizations | slug | organization |
| passkey | userId | passkey |
| twoFactor | secret | twoFactor |
We intend to add indexing support in our schema generation tool in the future.
Bundle Size Optimization
If you're using an ORM adapter such as Drizzle, you can reduce your bundle size by using @entro314labs/entro-auth/minimal instead of @entro314labs/entro-auth. This version excludes Kysely, which is only needed when using direct database connections.
Usage
Simply import from @entro314labs/entro-auth/minimal instead of @entro314labs/entro-auth:
import { entroAuth } from "@entro314labs/entro-auth/minimal";
import { drizzleAdapter } from "@entro314labs/entro-auth/adapters/drizzle";
import { db } from "./database";
export const auth = entroAuth({
database: drizzleAdapter(db, {
provider: "pg", // or "mysql", "sqlite"
}),
});Limitations:
- Direct database connections are not supported (you must use an adapter)
- Built-in migrations are not supported. Use external migration tools (or use
@entro314labs/entro-authif you need built-in migration support)