Expand description
§fredis
An asynchronous, high-performance client for Redis and Valkey in Rust.
[!NOTE] fredis is an actively maintained fork of aembke/fred.rs. Because the upstream repository is no longer actively maintained, this fork was created to modernize the codebase, support Cloudflare Workers (TCP sockets), upgrade dependencies (such as
rand 0.10,oneshot 0.2,rustls 0.23), migrate to Rust Edition 2024, and provide continuous maintenance and new features.
§Installation
Add fredis to your Cargo.toml:
[dependencies]
fredis = "10.1.0"Or via cargo:
cargo add fredis§Quick Example
use fredis::prelude::*;
use std::time::Duration;
#[tokio::main]
async fn main() -> Result<(), Error> {
let config = Config::from_url("redis://localhost:6379/1")?;
let client = Builder::from_config(config)
.with_connection_config(|config| {
config.connection_timeout = Duration::from_secs(5);
config.tcp = TcpConfig {
nodelay: Some(true),
..Default::default()
};
})
.build()?;
client.init().await?;
client.on_error(|(error, server)| async move {
println!("{:?}: Connection error: {:?}", server, error);
Ok(())
});
// Convert responses to common Rust types
let foo: Option<String> = client.get("foo").await?;
assert!(foo.is_none());
client.set("foo", "bar", None, None, false).await?;
// Or use turbofish to declare response types
println!("Foo: {:?}", client.get::<String, _>("foo").await?);
client.quit().await?;
Ok(())
}See the examples directory for more usage patterns.
§Highlights
- Cloudflare Workers Support: Experimental support for Cloudflare Workers via TCP sockets (
cloudflarefeature). - Modern Rust: Built on Rust Edition 2024 (MSRV 1.85).
- Protocol Flexibility: Full support for both RESP2 and RESP3 protocol modes.
- Deployment Topologies: Works seamlessly with Clustered, Centralized, and Sentinel server topologies.
- TLS & Security: Secure connections via
native-tlsorrustls(withaws-lc-rsorringbackends). - High Performance:
- Automatic command pipelining across Tokio tasks
- Zero-copy frame parsing powered by
redis-protocol - Round-robin client pooling (
PoolandDynamicPool) - Round-robin replica routing
- Rich Feature Set:
- Publish-Subscribe and keyspace event streams
- Built-in mocking layer for testing
- Lua scripts and functions
- Transactions and Client Tracking (client-side caching)
- Distributed tracing integration
§Feature Flags
§Client Features
| Name | Default | Description |
|---|---|---|
transactions | x | Enable a Transaction interface. |
cloudflare | Enable experimental support for Cloudflare Workers (TCP sockets). | |
enable-rustls | Enable TLS via rustls with the aws-lc-rs crypto backend. | |
enable-rustls-ring | Enable TLS via rustls with the ring crypto backend. | |
enable-native-tls | Enable TLS via native-tls. | |
vendored-openssl | Enable native-tls/vendored. | |
dynamic-pool | Enable dynamic client pooling that scales connections based on metrics. | |
metrics | Enable latency, network latency, and payload size tracking. | |
full-tracing | Enable detailed tracing across all commands and network frames. | |
partial-tracing | Enable lightweight tracing for top-level commands. | |
blocking-encoding | Offload encoding/decoding to blocking tasks (useful for heavy payloads). | |
custom-reconnect-errors | Customize error types that trigger automatic reconnection. | |
monitor | Enable the MONITOR command interface. | |
sentinel-client | Enable direct communication with Sentinel nodes. | |
sentinel-auth | Use distinct authentication credentials for Sentinel nodes. | |
subscriber-client | Enable managed subscriber client for pub/sub channels. | |
serde-json | Enable automatic conversion between Redis types and JSON via serde_json. | |
mocks | Enable mocking interfaces for intercepting commands in unit/integration tests. | |
dns | Override DNS lookup logic via hickory-resolver. | |
replicas | Route read commands to replica nodes. | |
default-nil-types | Enable looser parsing for nil values. | |
sha-1 | Enable hashing Lua scripts. | |
unix-sockets | Enable Unix domain socket support. | |
credential-provider | Dynamically load auth credentials at runtime. | |
tcp-user-timeouts | Configure TCP_USER_TIMEOUT on TCP sockets. | |
glommio | Enable experimental Glommio runtime support (Linux only). |
§Command Interfaces
Command interfaces start with i- to control which public command sets are compiled:
| Name | Default | Description |
|---|---|---|
i-std | x | Enable standard data structure interfaces (keys, hashes, lists, sets, streams, etc.). |
i-all | Enable all command interfaces listed below. | |
i-acl | Enable ACL commands. | |
i-client | Enable CLIENT commands. | |
i-cluster | Enable CLUSTER commands. | |
i-config | Enable CONFIG commands. | |
i-geo | Enable geospatial commands (GEOADD, GEODIST, etc.). | |
i-hashes | Enable hash commands (HGET, HSET, etc.). | |
i-hyperloglog | Enable HyperLogLog commands (PFADD, PFCOUNT, etc.). | |
i-keys | Enable general key management commands (GET, SET, DEL, EXPIRE, etc.). | |
i-lists | Enable list commands (LPUSH, RPOP, etc.). | |
i-memory | Enable MEMORY commands. | |
i-pubsub | Enable Publish/Subscribe commands (SUBSCRIBE, PUBLISH, etc.). | |
i-scripts | Enable Lua scripting & functions (EVAL, FUNCTION, etc.). | |
i-server | Enable server administration commands (SHUTDOWN, BGSAVE, etc.). | |
i-sets | Enable set commands (SADD, SMEMBERS, etc.). | |
i-slowlog | Enable SLOWLOG commands. | |
i-sorted-sets | Enable sorted set commands (ZADD, ZRANGE, etc.). | |
i-streams | Enable Redis streams commands (XADD, XREAD, XGROUP, etc.). | |
i-tracking | Enable Client Tracking (client-side caching). |
§Redis Stack & Module Features
| Name | Default | Description |
|---|---|---|
i-redis-json | Enable RedisJSON commands (JSON.SET, JSON.GET, etc.). | |
i-redisearch | Enable RediSearch commands (FT.CREATE, FT.SEARCH, etc.). | |
i-time-series | Enable Redis TimeSeries commands (TS.ADD, TS.RANGE, etc.). | |
i-redis-stack | Enable all Redis Stack features (i-redis-json, i-redisearch, i-time-series). | |
i-hexpire | Enable field-level hash expiration (HEXPIRE, HTTL, etc., Redis >= 7.4). |
§License
Licensed under either of:
- Apache License, Version 2.0 (LICENSE-APACHE)
- MIT license (LICENSE-MIT)
at your option.
Re-exports§
pub extern crate bytes;pub extern crate bytes_utils;pub extern crate native_tls;pub extern crate rustls;pub extern crate rustls_native_certs;pub extern crate serde_json;pub extern crate socket2;pub extern crate tracing;
Modules§
- clients
- Redis client implementations.
- error
- Error structs returned by Redis commands.
- interfaces
- Traits that implement portions of the Redis interface.
- mocks
mocks - An interface for mocking commands.
- monitor
monitor - An interface to run the
MONITORcommand. - prelude
- Convenience module to import a
RedisClient, all possible interfaces, error types, and common argument types or return value types. - types
- The structs and enums used by the Redis client.
- util
- Various client utility functions.
Macros§
- cmd
- Shorthand to create a CustomCommand.
- json_
quote i-redis-json - A helper macro to wrap a string value in quotes via the json macro.