Getting Started
To use the Redis client, you first need to create a connection:REDIS_URL
- If not set, defaults to
"redis://localhost:6379"
Connection Lifecycle
The Redis client automatically handles connections in the background:Basic Operations
String Operations
Numeric Operations
Hash Operations
Set Operations
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.enableAutoPipelining
option to false
:
Raw Commands
When you need to use commands that don’t have convenience methods, you can use thesend
method:
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:Connection Status and Monitoring
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
EXISTS
returns a boolean instead of a number (1 becomes true, 0 becomes false)SISMEMBER
returns a boolean (1 becomes true, 0 becomes false)
AUTH
INFO
QUIT
EXEC
MULTI
WATCH
SCRIPT
SELECT
CLUSTER
DISCARD
UNWATCH
PIPELINE
SUBSCRIBE
UNSUBSCRIBE
UNPSUBSCRIBE
Connection Options
When creating a client, you can pass various options to configure the connection: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
maxRetries
times (default: 10) - Commands executed during disconnection are:
- Queued if
enableOfflineQueue
is true (default) - Rejected immediately if
enableOfflineQueue
is false
- Queued if
Supported URL Formats
The Redis client supports various URL formats:Error Handling
The Redis client throws typed errors for different scenarios: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
Rate Limiting
Session Storage
Implementation Notes
Bun’s Redis client is implemented in Zig 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.RESP3 Protocol Support
Bun’s Redis client uses the newer RESP3 protocol by default, which provides more data types and features compared to RESP2:- Better error handling with typed errors
- Native Boolean responses
- Map/Dictionary responses (key-value objects)
- Set responses
- Double (floating point) values
- BigNumber support for large integer values
Limitations and Future Plans
Current limitations of the Redis client we are planning to address in future versions:- No dedicated API for pub/sub functionality (though you can use the raw command API)
- Transactions (MULTI/EXEC) must be done through raw commands for now
- Streams are supported but without dedicated methods
- Redis Sentinel
- Redis Cluster