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. New in Bun 1.2.23Basic Usage
To get started publishing messages, you can set up a publisher inpublisher.ts:
publisher.ts
subscriber.ts:
subscriber.ts
terminal
terminal
The subscription mode takes over the
RedisClient connection. A
client with subscriptions can only call RedisClient.prototype.subscribe(). In
other words, applications which need to message Redis need a separate
connection, acquirable through .duplicate():redis.ts
Publishing
Publishing messages is done through thepublish() method:
redis.ts
Subscriptions
The BunRedisClient allows you to subscribe to channels through 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
When you need to use commands that don’t have convenience methods, you can use thesend method:
redis.ts
send method allows you to use any Redis command, even ones that don’t have dedicated methods in the client. The first argument is the command name, and the second argument is an array of string arguments.
Connection Events
You can register handlers for connection events:redis.ts
Connection Status and Monitoring
redis.ts
Type Conversion
The Redis client handles automatic type conversion for Redis responses:- 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)
AUTHINFOQUITEXECMULTIWATCHSCRIPTSELECTCLUSTERDISCARDUNWATCHPIPELINESUBSCRIBEUNSUBSCRIBEUNPSUBSCRIBE
Connection Options
When creating a client, you can pass various 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: 10) - 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 manages connections efficiently and provides automatic reconnection with exponential backoff. The client supports pipelining commands, meaning multiple commands can be sent without waiting for the replies to previous commands. This significantly improves performance when sending multiple commands in succession.Limitations and Future Plans
Current limitations of the Redis client we are planning to address in future versions:- Transactions (MULTI/EXEC) must be done through raw commands for now
- Redis Sentinel
- Redis Cluster