Docs

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.

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:

auth.ts
const auth = new BetterAuth({
    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 here.

SSR Optimizations

Server-side rendering (SSR) is a powerful technique that allows you to render your application on the server and deliver fully rendered HTML to the client, significantly enhancing performance, especially for data-intensive or complex applications. To optimize SSR, minimize client-side data fetching by offloading data gathering to the server, which reduces initial load times. Implement caching strategies for frequently accessed data to decrease server load and improve response times. Additionally, simplify your rendering logic to ensure efficient server-side processing, consider using streaming to send parts of the rendered HTML as they become available, and prioritize critical data for the initial render while loading less essential data asynchronously afterward.

Database optimizations

Optimizing database performance is essential for any authentication library, including Better Auth. Efficient database interactions can significantly enhance the speed and reliability of user authentication processes. Since Better Auth supports a wide variety of databases, we cannot cover each one in detail regarding optimization techniques. However, we can provide some general tips and strategies to improve your database performance. Below are two critical techniques specifically tailored for Better Auth: using indexes and employing connection pools.

Using indexes

Indexes are powerful tools that can dramatically improve the performance of database queries, especially in an authentication context where speed is crucial. Here’s how to effectively utilize indexes in Better Auth:

  1. Types of Indexes: There are several types of indexes, including:

    • B-tree Indexes: The most common type, suitable for a wide range of queries.
    • Hash Indexes: Useful for equality comparisons but not for range queries.
    • Full-text Indexes: Designed for searching text within large text fields.
    • Spatial Indexes: Optimized for spatial data types, such as geographic coordinates.
  2. When to Use Indexes: Indexes are particularly beneficial for:

    • Columns frequently used in WHERE clauses.
    • Columns involved in JOIN operations.
    • Columns used in ORDER BY and GROUP BY clauses.
  3. Trade-offs: While indexes can significantly speed up read operations, they can also introduce overhead during write operations (INSERT, UPDATE, DELETE) because the index must be updated. Therefore, it's essential to strike a balance between read and write performance.

  4. Monitoring and Maintenance: Regularly monitor index usage and performance. Unused or rarely used indexes can be removed to reduce overhead. Additionally, consider rebuilding or reorganizing indexes periodically to maintain their efficiency.

You can read more about indexes in the database index wikipedia page.

Using a connection pool

A connection pool is a critical component for optimizing database interactions in Better Auth, particularly in high-traffic scenarios where multiple authentication requests occur simultaneously.

Here are some benefits of using a connection pool and some configuration options to consider:

  1. Benefits of Connection Pools:

    • Reduced Latency: By reusing existing database connections, Better Auth can minimize the time spent establishing new connections, leading to faster authentication responses.
    • Efficient Resource Management: Connection pools help manage database connections, preventing resource exhaustion and ensuring that the database can handle multiple authentication requests concurrently.
    • Improved Scalability: Connection pools allow Better Auth to scale effectively, accommodating a growing number of users without a corresponding increase in database connection overhead.
  2. Configuration Options:

    • Maximum Pool Size: Set this based on expected traffic. A higher limit allows more simultaneous connections but may strain the database if set too high.
    • Minimum Pool Size: Keep a baseline number of connections open to handle initial requests quickly.
    • Connection Timeout: Define how long to wait for a connection to become available before timing out, ensuring that users don’t experience long delays.
    • Idle Timeout: Specify how long a connection can remain idle before being closed, helping to manage resources effectively.
  3. Best Practices:

Always close connections when they are no longer needed to return them to the pool, preventing connection leaks. Monitor the performance of the connection pool and adjust configurations based on application load and usage patterns to optimize performance.

You can read more about connection pools in the connection pool wikipedia page.

On this page