ioredis, and background job processing with bullmq.
Installation
Install the required dependencies:ioredis: High-performance Redis client for Node.js with full TypeScript support.dotenv: Load environment variables from.envfile.@types/ioredis: TypeScript definitions for ioredis (usually not needed as ioredis has built-in types).
Environment variables with dotenv
Add Redis configuration to your.env file:
Redis connection setup
Create a Redis client configuration:Basic usage examples
Simple key-value operations
Caching database queries
Pub/Sub Pattern
Rate limiting
Advanced Redis operations
Hash operations
List operations
Set operations
Sorted set operations
Job queues with BullMQ
BullMQ is a robust, Redis-based job queue for Node.js. It is ideal for offloading heavy tasks (like sending emails, video processing, or generating reports) to background processes.Installation
Shared connection configuration
BullMQ manages its own connections (one for the queue, one for the worker, and one for blocking commands). It is best to share the connection options rather than a single client instance.1. Defining a queue (Producer)
This code typically runs in your web server API (e.g., when a user signs up).2. Processing jobs (Worker)
The worker processes jobs from the queue. In production, this often runs as a separate service or process.3. Delayed jobs
You can schedule jobs to run in the future using the delay option. This relies on Redis keyspace notifications.Sevalla Redis service
To use Redis on Sevalla:- Create a Redis service in your Sevalla dashboard
- Copy the connection credentials from the service details
-
Add Redis environment variables to your application:
Or use a single URL:
- Deploy your application - it will automatically connect to Redis
Best practices
-
Set appropriate TTLs - Don’t let cache grow indefinitely, use
SETEXorEXPIRE - Handle errors gracefully - Always wrap Redis calls in try-catch blocks
-
Close connections on shutdown - Use
redis.quit()in SIGTERM handler -
Use pipeline for multiple commands - Reduce network round-trips:
-
Monitor memory usage - Use
redis.info('memory')to track memory - Use appropriate data structures - Choose the right Redis data type for your use case
- Implement cache invalidation - Delete stale cache when data changes
- Use Redis for session storage - Instead of memory-based sessions for horizontal scaling
-
Set connection timeout - Configure
connectTimeoutandretryStrategy
Common issues
Connection refused
If you get “connection refused” errors:- Check that Redis is running:
redis-cli ping - Verify the host and port in your configuration
- Ensure the firewall allows connections on port 6379
- On Sevalla, verify the Redis service is running
Authentication errors
If you get authentication errors:- Verify the password is correct
- Check if Redis requires authentication:
redis-cli CONFIG GET requirepass - Ensure the password is set in the environment variables
Memory issues
If Redis runs out of memory:- Set
maxmemoryandmaxmemory-policyin Redis config - Use appropriate TTLs for cached data
- Monitor memory usage:
redis-cli INFO memory - Consider using Redis eviction policies
Connection timeouts
For production, configure the retry strategy:Performance tips
-
Use pipelining for bulk operations:
-
Use SCAN instead of KEYS:
-
Use Redis transactions when needed:
-
Enable automatic pipelining: