Redis-rs for Rust: Complete Guide with Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

What is Redis-rs and why do you need it?

\n\n

Redis-rs is an asynchronous and synchronous client for Redis, written in the Rust language. It is one of the most popular and actively maintained libraries for working with Redis in the Rust ecosystem. It provides a convenient, safe, and high-performance interface for interacting with a Redis server.

\n\n

Why do you need Redis-rs?

\n
    \n
  • High performance: Rust is known for its speed and memory safety. Redis-rs inherits these qualities, ensuring minimal overhead when working with Redis.
  • \n
  • Asynchronicity: The library is natively designed to work with asynchronous runtimes such as Tokio and async-std, allowing you to handle thousands of connections without blocking threads.
  • \n
  • Type safety: Rust's strict type system helps avoid many errors at compile time. Redis-rs leverages this advantage by providing typed commands.
  • \n
  • Cluster and replication support: The library supports working with Redis clusters, automatic request redirection, and working with replicas.
  • \n
  • Active community: Redis-rs has a large developer audience, is regularly updated, and is well-documented.
  • \n
\n\n

This library is ideal for building high-load web applications, caching systems, message queues, session stores, and any other projects that require fast data access via Redis.

\n\n

Installing Redis-rs

\n\n

To install Redis-rs in your Rust project, add the dependency to the Cargo.toml file:

\n\n
[dependencies]\nredis = "0.27.0"  # Check the latest version on crates.io\ntokio = { version = "1.0", features = ["full"] }  # For asynchronous execution\n
\n\n

Then run the command:

\n\n
cargo build
\n\n

If you plan to use the synchronous client, you don't need Tokio, but for asynchronous usage, it is required.

\n\n

Quick start with code example

\n\n

Let's write a simple program that connects to Redis, writes a value, and reads it back.

\n\n
use redis::AsyncCommands;\nuse tokio;\n\n#[tokio::main]\nasync fn main() -> redis::RedisResult<()> {\n    // Establish a connection to the Redis server\n    let client = redis::Client::open("redis://127.0.0.1:6379/")?;\n    let mut con = client.get_async_connection().await?;\n\n    // Set the key "hello" with the value "world"\n    con.set("hello", "world").await?;\n\n    // Get the value for the key "hello"\n    let value: String = con.get("hello").await?;\n    println!("Value of key 'hello': {}", value);\n\n    Ok(())\n}\n
\n\n

This example demonstrates basic work with Redis-rs: connecting, writing, and reading data. Note the use of #[tokio::main] — this is a macro that launches the Tokio asynchronous runtime.

\n\n

Main methods and functions of Redis-rs

\n\n

Let's look at the 10 most frequently used methods. All examples assume you have an asynchronous connection con, obtained via client.get_async_connection().await?.

\n\n

1. set() — setting a value

\n

Description: Sets the value for the specified key. If the key already exists, the value is overwritten.

\n

Parameters:

\n
    \n
  • key: K — the key (any type implementing ToRedisArgs)
  • \n
  • value: V — the value (any type implementing ToRedisArgs)
  • \n
\n

Example:

\n
con.set("user:1:name", "Alice").await?;\ncon.set("counter", 42).await?;
\n\n

2. get() — getting a value

\n

Description: Gets the value for the specified key. Returns

Recommendations