Skip to main content
Using Redis with your JavaScript application on Sevalla enhances performance by enabling fast in-memory data access, supports real-time features such as notifications or chat, and facilitates reliable background job processing, helping your app scale efficiently and handle high traffic. This guide covers Redis integration for caching, real-time features, using 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 .env file.
  • @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:
Load environment variables at the top of your entry 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:
  1. Create a Redis service in your Sevalla dashboard
  2. Copy the connection credentials from the service details
  3. Add Redis environment variables to your application:
    Or use a single URL:
  4. Deploy your application - it will automatically connect to Redis

Best practices

  1. Set appropriate TTLs - Don’t let cache grow indefinitely, use SETEX or EXPIRE
  2. Handle errors gracefully - Always wrap Redis calls in try-catch blocks
  3. Close connections on shutdown - Use redis.quit() in SIGTERM handler
  4. Use pipeline for multiple commands - Reduce network round-trips:
  5. Monitor memory usage - Use redis.info('memory') to track memory
  6. Use appropriate data structures - Choose the right Redis data type for your use case
  7. Implement cache invalidation - Delete stale cache when data changes
  8. Use Redis for session storage - Instead of memory-based sessions for horizontal scaling
  9. Set connection timeout - Configure connectTimeout and retryStrategy

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 maxmemory and maxmemory-policy in 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

  1. Use pipelining for bulk operations:
  2. Use SCAN instead of KEYS:
  3. Use Redis transactions when needed:
  4. Enable automatic pipelining: