Betterauth is Slow?

import { products, favorites } from "@/db/schema"; import { useDrizzle } from "~/server/utils/drizzle"; import { and, count, eq, sql } from "drizzle-orm"; import { auth } from "~/server/utils/auth"; export default defineEventHandler(async (event) => { const page = Number(getQuery(event).page || "1"); const limit = 8; const offset = (page - 1) * limit; console.time("Auth Decoding") const session = await auth.api.getSession({ headers: event.headers, }); console.timeEnd("Auth Decoding") //700ms
6 Replies
Prestgg
PrestggOP4w ago
700ms on Localhost preview mode. Please help me
Ping
Ping4w ago
You need to optimize your setup, wether using caching or using database indexes. Learn more here: https://www.better-auth.com/docs/guides/optimizing-for-performance
Optimizing for Performance | Better Auth
A guide to optimizing your Better Auth application for performance.
Prestgg
PrestggOP4w ago
thanks
stu
stu3w ago
Did you end up getting better performance? I'm working on Astro, and struggling with slow better-auth speed as well. Tried enabling the session cookies, but didn't see much of a difference. Have yet to run the DB indexing yet though, hoping that speeds things up. I ran the DB indexing, but I'm still getting like a 3 second delay just fetching the session server side. My code:
const timestampPreSession = new Date().toISOString();
console.log(`${timestampPreSession} - pre session`);
const session = await auth(env).api.getSession({
headers: Astro.request.headers,
});

const timestampPost = new Date().toISOString();
console.log(`${timestampPost} - post session`);
const timestampPreSession = new Date().toISOString();
console.log(`${timestampPreSession} - pre session`);
const session = await auth(env).api.getSession({
headers: Astro.request.headers,
});

const timestampPost = new Date().toISOString();
console.log(`${timestampPost} - post session`);
It logs:
2025-05-05T12:19:43.865Z - pre session
2025-05-05T12:19:46.985Z - post session
2025-05-05T12:19:43.865Z - pre session
2025-05-05T12:19:46.985Z - post session
Over 3 seconds. Here is how auth is being initialized:
export const auth = (env: Env) =>
betterAuth({
database: {
dialect: new PostgresJSDialect({
postgres: authGetDb(),
}),
// dialect,
type: "postgres",
},
emailAndPassword: {
enabled: true,
sendResetPassword: async (
{ user, token, url }: { user: User; token: string; url: string },
_
) => {
await sendResetPasswordEmail({
email: user.email,
url,
});
},
resetPasswordTokenExpiresIn: 3600, // 1 hour
},
secret: BETTER_AUTH_SECRET,
cookieCache: {
enabled: true,
maxAge: 5 * 60, // Cache duration in seconds
},
});

// this is used instead of the getDb function since better-auth is having trouble with Hyperdrive connections
const authGetDb = () => {
// Connection URL from environment variables
// let connectionString = env.HYPERDRIVE.connectionString;
let connectionString = RENDER_DB_URL;

const sql = postgres(connectionString, {
ssl: true,
// Debug options - remove in production
debug: true,
});

return sql;
};
export const auth = (env: Env) =>
betterAuth({
database: {
dialect: new PostgresJSDialect({
postgres: authGetDb(),
}),
// dialect,
type: "postgres",
},
emailAndPassword: {
enabled: true,
sendResetPassword: async (
{ user, token, url }: { user: User; token: string; url: string },
_
) => {
await sendResetPasswordEmail({
email: user.email,
url,
});
},
resetPasswordTokenExpiresIn: 3600, // 1 hour
},
secret: BETTER_AUTH_SECRET,
cookieCache: {
enabled: true,
maxAge: 5 * 60, // Cache duration in seconds
},
});

// this is used instead of the getDb function since better-auth is having trouble with Hyperdrive connections
const authGetDb = () => {
// Connection URL from environment variables
// let connectionString = env.HYPERDRIVE.connectionString;
let connectionString = RENDER_DB_URL;

const sql = postgres(connectionString, {
ssl: true,
// Debug options - remove in production
debug: true,
});

return sql;
};
Is this typical performance?
Ping
Ping3w ago
This is not typical performance. It's very likely latency issues to your database. There are users who can get their session using a remote DB in less than 30 ms.
stu
stu3w ago
I see, will investigate that. Using a Render Postgres instance. Btw, any chance we can get Better-Auth working with Cloudflare Hyperdrive? Or are there no plans for that at the moment?

Did you find this page helpful?