Rust is a fast-growing programming language known for its super fast performance, thread safety, and for avoiding segmentation faults. Redis Enterprise, the in-memory database, delivers data access with high throughput and low latency. This is the type of performance you need for superfast applications built with Rust.
Connecting your Rust application to a Redis Enterprise database is pretty straightforward. You can use the popular redis crate, which is based on the redis-rs library. The library is flexible in type-conversions and provides access to Redis functionality for key-value access, pub/sub, data structure manipulation, etc. This site has detailed documentation and code snippets for simple Redis commands, pipelining, transactions, and messaging using pub/sub.
Async-redis is a new client library that’s gaining momentum. It uses Tokio and Futures libraries to support asynchronous calls to Redis. This library supports only a single endpoint of Redis. This works perfectly well with Redis Enterprise, because the server-side proxy in Redis Enterprise provides a single access point and routes your calls to the right shard in a cluster. Visit the async-redis documentation page for more information. The library provides three interfaces:
Visit the async-redis documentation page for more information.
If you do not already have Redis Enterprise, you can sign up for a free cloud account and create a new Redis Enterprise database for free on all the popular cloud platforms. If you are using Redis Enterprise software, or Redis Enterprise in your VPC, please follow those instructions to create a new database.
Once you have your Redis Enterprise database, you can get started with your cargo and test the database with a test program, as shown below. This example uses the redis-rs library.
Creating Cargo.toml with Redis dependency:
[dependencies]
redis = "0.8.0"
Sample test program, main.rs:extern crate redis;
use redis::Commands;
fn main(){
// Open a connection
let client = redis::Client::open("redis://<user>:<password>@<endpoint>:<port>").unwrap();
// set key = “Hello World”
let _: () = client.set(“key”,”Hello World”).unwrap();
// get key
let key : String = client.get(“key”).unwrap();
println!("key: {}", key);
}
If you’re connecting to Redis Cloud, log in to your Redis Enterprise account, open the dashboard for your database instance, and select the Configuration tab. Copy the endpoint and password from the screen and form your URL from this syntax:"redis://<arbitraryuser>:<password>@<endpoint>:<port>"
For example, in the screenshot shown above, the URL would look like the one below (this assumes “password” as the password and “u” as the arbitrary user name):"redis://u:password@redis-1111.c11.us-east-1-1.ec2.cloud.redis.com:1111"
If you are running a single instance of open source Redis on your local machine, you can connect your Rust application to your Redis instance as shown below:"redis://127.0.0.1:6379"
redis-rs or async-redis does not support SSL connections natively. For an added security measure, you can secure your connection using stunnel.