Skip to main content
Bun’s Redis client supports Redis server versions 7.2 and up.
Bun’s native Redis client has a Promise-based API with built-in connection management, fully typed responses, and TLS support.
redis.ts

Getting Started

To use the Redis client, you first need to create a connection:
redis.ts
By default, the client reads connection information from the following environment variables (in order of precedence):
  • REDIS_URL
  • VALKEY_URL
  • If not set, defaults to "redis://localhost:6379"

Connection Lifecycle

The Redis client automatically handles connections in the background:
redis.ts
You can also manually control the connection lifecycle:
redis.ts

Basic Operations

String Operations

redis.ts

Numeric Operations

redis.ts

Hash Operations

redis.ts

Set Operations

redis.ts

Pub/Sub

Bun provides native bindings for the Redis Pub/Sub protocol, added in Bun 1.2.23.
Redis Pub/Sub is experimental. We expect it to be stable, but we’re still looking for feedback and areas for improvement.

Basic Usage

Create a publisher in publisher.ts:
publisher.ts
In another file, create the subscriber in subscriber.ts:
subscriber.ts
In one shell, run your subscriber:
terminal
and, in another, run your publisher:
terminal
Subscribing takes over the RedisClient connection: a client with subscriptions can only call RedisClient.prototype.subscribe(). To send other commands to Redis, create a separate connection with .duplicate():
redis.ts

Publishing

Publish messages with the publish() method:
redis.ts

Subscriptions

Subscribe to channels with the .subscribe() method:
redis.ts
Unsubscribe with the .unsubscribe() method:
redis.ts

Advanced Usage

Command Execution and Pipelining

The client automatically pipelines commands, improving performance by sending multiple commands in a batch and processing responses as they arrive.
redis.ts
To disable automatic pipelining, set the enableAutoPipelining option to false:
redis.ts

Raw Commands

Use the send method to run any Redis command, including ones without a dedicated method. The first argument is the command name, and the second is an array of string arguments.
redis.ts

Connection Events

You can register handlers for connection events:
redis.ts

Connection Status and Monitoring

redis.ts

Type Conversion

The client automatically converts Redis responses to JavaScript values:
  • Integer responses are returned as JavaScript numbers
  • Bulk strings are returned as JavaScript strings
  • Simple strings are returned as JavaScript strings
  • Null bulk strings are returned as null
  • Array responses are returned as JavaScript arrays
  • Error responses throw JavaScript errors with appropriate error codes
  • Boolean responses (RESP3) are returned as JavaScript booleans
  • Map responses (RESP3) are returned as JavaScript objects
  • Set responses (RESP3) are returned as JavaScript arrays
Special handling for specific commands:
  • EXISTS returns a boolean instead of a number (1 becomes true, 0 becomes false)
  • SISMEMBER returns a boolean (1 becomes true, 0 becomes false)
The following commands disable automatic pipelining:
  • AUTH
  • INFO
  • QUIT
  • EXEC
  • MULTI
  • WATCH
  • SCRIPT
  • SELECT
  • CLUSTER
  • DISCARD
  • UNWATCH
  • PIPELINE
  • SUBSCRIBE
  • PSUBSCRIBE
  • UNSUBSCRIBE
  • UNPSUBSCRIBE

Connection Options

When creating a client, you can pass options to configure the connection:
redis.ts

Reconnection Behavior

When a connection is lost, the client automatically attempts to reconnect with exponential backoff:
  1. The client starts with a small delay (50ms) and doubles it with each attempt
  2. Reconnection delay is capped at 2000ms (2 seconds)
  3. The client attempts to reconnect up to maxRetries times (default: 20)
  4. Commands executed during disconnection are:
    • Queued if enableOfflineQueue is true (default)
    • Rejected immediately if enableOfflineQueue is false

Supported URL Formats

The Redis client supports various URL formats:
redis.ts

Error Handling

The Redis client throws typed errors for different scenarios:
redis.ts
Common error codes:
  • ERR_REDIS_CONNECTION_CLOSED - Connection to the server was closed
  • ERR_REDIS_AUTHENTICATION_FAILED - Failed to authenticate with the server
  • ERR_REDIS_INVALID_RESPONSE - Received an invalid response from the server

Example Use Cases

Caching

redis.ts

Rate Limiting

redis.ts

Session Storage

redis.ts

Implementation Notes

Bun’s Redis client is implemented in Rust and uses the Redis Serialization Protocol (RESP3). It reconnects automatically with exponential backoff and pipelines commands, so multiple commands can be sent without waiting for replies to previous ones.

Limitations and Future Plans

Limitations we plan to address in future versions:
  • Transactions (MULTI/EXEC) must be done through raw commands
Unsupported features:
  • Redis Sentinel
  • Redis Cluster