Bun’s Redis client supports Redis server versions 7.2 and up.
redis.ts
Getting Started
To use the Redis client, you first need to create a connection:redis.ts
REDIS_URLVALKEY_URL- If not set, defaults to
"redis://localhost:6379"
Connection Lifecycle
The Redis client automatically handles connections in the background:redis.ts
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.Basic Usage
Create a publisher inpublisher.ts:
publisher.ts
subscriber.ts:
subscriber.ts
terminal
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 thepublish() method:
redis.ts
Subscriptions
Subscribe to channels with the.subscribe() method:
redis.ts
.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
enableAutoPipelining option to false:
redis.ts
Raw Commands
Use thesend 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
EXISTSreturns a boolean instead of a number (1 becomes true, 0 becomes false)SISMEMBERreturns a boolean (1 becomes true, 0 becomes false)
AUTHINFOQUITEXECMULTIWATCHSCRIPTSELECTCLUSTERDISCARDUNWATCHPIPELINESUBSCRIBEPSUBSCRIBEUNSUBSCRIBEUNPSUBSCRIBE
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:- The client starts with a small delay (50ms) and doubles it with each attempt
- Reconnection delay is capped at 2000ms (2 seconds)
- The client attempts to reconnect up to
maxRetriestimes (default: 20) - Commands executed during disconnection are:
- Queued if
enableOfflineQueueis true (default) - Rejected immediately if
enableOfflineQueueis false
- Queued if
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
ERR_REDIS_CONNECTION_CLOSED- Connection to the server was closedERR_REDIS_AUTHENTICATION_FAILED- Failed to authenticate with the serverERR_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
- Redis Sentinel
- Redis Cluster