Getting Started With Rust and Redis

A hands-on tutorial on how to use Redis with the Rust programming language

Abhishek Gupta
Jan 20 · 7 min read

Are you learning Rust and looking for ways to get hands-on practice with concrete examples? A good approach might be to try and integrate Rust with external systems. Why not try to integrate it with Redis, which is a powerful, versatile database but dead simple to get started with!

Image for post
Image for post
Photo by Danial RiCaRoS on Unsplash

In this blog post, you will learn how to use the Rust programming language to interact with Redis using the redis-rs client. We will walk through commonly used Redis data structures such as String, Hash, List etc. The Redis client used in the sample code exposes both high and low-level APIs and you will see both these styles in action.

Code is available on GitHub —

Redis is an in-memory data structure store which is often used as a database, cache, and message broker. It provides data structures such as strings, hashes, lists, sets, sorted sets with range queries, bitmaps, hyperloglogs, geospatial indexes, and streams.

In this blog post, I have included additional instructions for how to make the sample program work with Azure Cache for Redis, but it will work with any Redis instance (e.g. a local setup using Docker). Azure Cache for Redis offers both the Redis open-source and a commercial product from Redis Labs as a managed service.


You will need Rust (version 1.39 or above) installed on your computer. If you intend to use Azure Cache for Redis, simply create a free Azure subscription, and setup an Azure Cache for Redis instance using the Azure portal.

For the purposes of this tutorial, I would recommend setting up a Basic tier instance which is ideal for development/test and non-critical workloads.

You can also choose to simply use the Redis Docker container as such:

That’s it, you’re ready to get started!

Code walk through

For you to get a better understanding, this section covers a step-by-step walk through of the code. It covers all the functions, each of which covers a specific Redis data structure. It is followed by the Run the sample application section.

The connect function is used to establish a connection to Redis. It expects host name and the password to be passed in as environment variables REDIS_HOSTNAME and REDIS_PASSWORD respectively. The format for the connection URL is <uri scheme>://<username>:<password>@<hostname>.

Azure Cache for Redis only accepts secure connections with TLS 1.2 as the minimum required version. The URI scheme would be rediss in case of a TLS connection, otherwise it's redis.

The call to redis::Client::open performs basic validation while get_connection() actually initiates the connection — the program exits if the connectivity fails due to any reason such as an incorrect password.

This function covers SET, GET, and INCR commands. The low-level API is used for SET and GET, which sets and retrieves the value for a key named foo. The INCRBY command is executed using a high-level API i.e. incr increments the value of a key (named counter) by 2 followed by a call to get to retrieve it.

The below code snippet demonstrates the functionality of a Redis HASH data structure. HSET is invoked using the low-level API to store information (name, version, repo) about Redis drivers (clients). For example, details for the Rust driver (one being used in this sample code!) is captured in form of a BTreeMap and then passed on to the low-level API. It is then retrieved using HGETALL.

HSET can also be executed using a high-level API using hset_multiple that accepts an array of tuples. hget is then executed to fetch the value for a single attribute (the repo in this case).

In the function below, you can see how to use a LIST data structure. LPUSH is executed (with the low-level API) to add an entry to the list and the high-level lpop method is used to retrieve that from the list. Then, the rpush method is used to add a couple of entries to the list which are then fetched using the low-level lrange method.

Here you can see some of the SET operations. The sadd (high-level API) method is used to add couple of entries to a SET named users. SISMEMBER is then executed (low-level API) to check whether user1 exists. Finally, smembers is used to fetch and iterate over all the set entries in the form of a Vector (Vec).

sorted_set function below demonstrates the Sorted Set data structure. ZADD is invoked (with the low-level API) to add a random integer score for a player (player-1). Next, the zadd method (high-level API) is used to add more players (player-2 to player-5) and their respective (randomly generated) scores. The number of entries in the sorted set is figured out using ZCARD and that's used as the limit to the ZRANGE command (invoked with the low-level API) to list out the players with their scores in ascending order.

Now that you’ve gone through the code, it’s time to run the application and check the output.

Run the sample application

Start by cloning the GitHub repo:

If you’re using a local Redis server (e.g. with Docker) over an insecure connection without any password, simply use:

If you’re using Azure Cache for Redis, fetch the Host name and Access Keys from the Azure portal. Set the respective environment variables:

To run the application:

You will see an output as such:

Important: If you were using Azure Cache for Redis, please ensure that you delete the Redis instance.


In this tutorial, you learned how to use the Rust driver for Redis to connect and execute operations in Azure Cache for Redis. If you found this helpful, you may want to explore these additional resources:

The Startup

Medium's largest active publication, followed by +771K people. Follow to join our community.

Abhishek Gupta

Written by

Currently working with Kafka, Databases, Azure, Kubernetes and related open source projects | Confluent Community Catalyst (for Kafka)

The Startup

Medium's largest active publication, followed by +771K people. Follow to join our community.

Abhishek Gupta

Written by

Currently working with Kafka, Databases, Azure, Kubernetes and related open source projects | Confluent Community Catalyst (for Kafka)

The Startup

Medium's largest active publication, followed by +771K people. Follow to join our community.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store