Overview
The @atxp/redis package provides a Redis-based OAuth database implementation for the Authorization Token Exchange Protocol (ATXP). It offers distributed OAuth token storage using Redis, designed for scalable applications that need shared token storage across multiple server instances or require high-performance, in-memory token caching.This package is designed to work seamlessly with
@atxp/client
and @atxp/express
packages when you need distributed storage that can be shared across multiple application instances. For single-instance applications, consider using @atxp/sqlite instead.Installation
The
@atxp/redis
package includes TypeScript definitions and requires Node.js 16 or higher. It automatically installs @atxp/common
as a dependency and requires a Redis server to be running.API Reference
Classes
RedisOAuthDatabase
The main class for managing OAuth tokens in a Redis database.
Configuration options for the Redis database connection.
Retrieves an OAuth token from Redis.
Unique identifier for the token to retrieve.
Deletes an OAuth token from Redis.
Unique identifier for the token to delete.
Checks if a token exists in Redis.
Unique identifier for the token to check.
Lists all token keys stored in Redis with the configured prefix.
Closes the Redis connection and releases resources.
Interfaces
RedisOAuthDatabaseOptions
Configuration options for the Redis OAuth database.
Complete Redis connection URL. Takes precedence over individual host/port/password/db options.
Redis server hostname. Defaults to ‘localhost’.
Redis server port. Defaults to 6379.
Redis server password for authentication.
Redis database number (0-15). Defaults to 0.
Prefix for all Redis keys to avoid conflicts. Defaults to ‘atxp:oauth:’.
Connection timeout in milliseconds. Defaults to 10000.
Delay between retry attempts in milliseconds. Defaults to 100.
Maximum number of retry attempts per request. Defaults to 3.
Usage Examples
Basic Setup
Create a Redis OAuth database instance:Advanced Configuration
Configure Redis with authentication and custom settings:Integration with ATXP Client
Use Redis storage with the ATXP client for distributed token management:Integration with ATXP Server
Use Redis storage with the ATXP server for distributed session management:Configuration
Connection Options
Redis supports multiple connection methods:Environment Variables
Configure Redis connection using environment variables:Key Management
Redis keys are automatically prefixed to avoid conflicts:Troubleshooting
Common Issues
Connection refused errors
Connection refused errors
If you encounter connection refused errors:
- Ensure Redis server is running
- Check if the host and port are correct
- Verify firewall settings allow Redis connections
- Test connection with redis-cli
Authentication failures
Authentication failures
If you’re experiencing authentication failures:
- Verify the password is correct
- Check if Redis requires authentication
- Ensure the username format is correct for Redis 6+
Memory issues with large token sets
Memory issues with large token sets
For applications with many tokens:
- Monitor Redis memory usage
- Configure appropriate eviction policies
- Consider token cleanup for expired entries
- Use Redis clustering for horizontal scaling
Network timeouts
Network timeouts
If you’re experiencing network timeouts:
- Increase connection timeout values
- Check network stability between application and Redis
- Consider using connection pooling
- Monitor Redis server performance
Performance Considerations
For optimal Redis performance:
- Use Redis clustering for high availability and horizontal scaling
- Configure appropriate memory eviction policies
- Monitor Redis memory usage and implement cleanup strategies
- Use connection pooling for high-concurrency applications
- Consider Redis persistence settings based on your requirements
Migration from Other Storage
If you’re migrating from another OAuth storage solution:- Export tokens from your current system
- Set up Redis server
- Import tokens using the
storeToken
method - Update your application to use the Redis database