Integrations
Secondary Storage
Integrate GoBetterAuth with secondary storage
Secondary storage allows you to use various key-value store options such as in-memory, database and custom implementations (e.g. Redis) for managing session data, rate limiting counters, and other high-frequency records. This is useful for offloading intensive operations from your main database to a fast storage layer.
Why Use Secondary Storage?
- Performance: Store session and rate limit data in RAM or fast key-value stores.
- Scalability: Offload high-churn data from your main database.
- Flexibility: Implement your own storage adapter for Redis, Valkey or any key-value store.
Configuration
By default, GoBetterAuth uses an in-memory secondary storage. But you can configure it to use any of the available built-in providers:
import (
gobetterauthconfig "github.com/GoBetterAuth/go-better-auth/config"
gobetterauthmodels "github.com/GoBetterAuth/go-better-auth/models"
)
// In-Memory Secondary Storage
config := gobetterauthconfig.NewConfig(
// ...other config options
gobetterauthconfig.WithSecondaryStorage(
gobetterauthmodels.SecondaryStorageConfig{
// in-memory storage
Type: gobetterauthmodels.SecondaryStorageTypeMemory,
MemoryOptions: &gobetterauthmodels.SecondaryStorageMemoryOptions{},
// or Database storage
// Type: gobetterauthmodels.SecondaryStorageTypeDatabase,
// DatabaseOptions: &gobetterauthmodels.SecondaryStorageDatabaseOptions{},
},
),
)To customise secondary storage, you can supply a custom implementation by following these steps:
Build your own custom adapter by implementing the SecondaryStorage interface:
import (
"context"
"time"
gobetterauthmodels "github.com/GoBetterAuth/go-better-auth/models"
)
type SecondaryStorage interface {
Get(ctx context.Context, key string) (any, error)
Set(ctx context.Context, key string, value any, ttl *time.Duration) error
Delete(ctx context.Context, key string) error
Incr(ctx context.Context, key string, ttl *time.Duration) (int, error)
}type MyCustomSecondaryStorage struct {
// ...your fields
}
// Implement the required methods for the SecondaryStorage interface...
func (storage *MyCustomSecondaryStorage) Get(ctx context.Context, key string) (any, error) {
// ...implementation
}
// ...rest of the methods// Then use your custom storage in the config:
import (
gobetterauthconfig "github.com/GoBetterAuth/go-better-auth/config"
gobetterauthmodels "github.com/GoBetterAuth/go-better-auth/models"
)
config := gobetterauthconfig.NewConfig(
gobetterauthconfig.WithSecondaryStorage(
gobetterauthmodels.SecondaryStorageConfig{
Type: gobetterauthmodels.SecondaryStorageTypeCustom,
Storage: &MyCustomSecondaryStorage{/* ... */},
},
),
)