Optimizing for Performance
In this guide, we’ll go over some of the ways you can optimize your application for a more performant Better Auth app.
Caching
Caching is a powerful technique that can significantly improve the performance of your Better Auth application by reducing the number of database queries and speeding up response times.
Cookie Cache
Calling your database every time useSession
or getSession
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:
Read more about cookie caching.
Framework Caching
Here are examples of how you can do caching in different frameworks and environments:
SSR Optimizations
If you're using a framework that supports server-side rendering, it's usually best to pre-fetch user session on the server and use it as a fallback on the client.
Database optimizations
Optimizing database performance is essential to get the best out of Better 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.