Redis client for C++: complete guide to working with hiredis
Redis is a popular in-memory database used for caching, session management, message queues, and much more. To interact with Redis from C++ applications, there are several libraries, but the most common and reliable is hiredis. This minimalistic, fast, and stable library provides a clean C API that integrates easily into C++ projects. In this article, we will thoroughly cover how to install hiredis, connect it to a project, perform basic Redis operations, and handle possible errors.
What is hiredis?
hiredis is the official C client for Redis, developed by the same team as Redis itself. It supports:
- Synchronous and asynchronous connections
- Automatic reconnection on connection loss
- Command pipelining
- Working with binary data
- Full support for the Redis protocol (RESP2 and RESP3)
Although hiredis is written in C, it is perfectly suited for C++ projects: it can be wrapped in RAII classes, used with modern C++11/17 idioms, and easily combined with other libraries.
Installing hiredis
The easiest way to install it is via the system package manager. Examples for different OS:
Ubuntu / Debian
sudo apt updatesudo apt install libhiredis-devmacOS (Homebrew)
brew install hiredisWindows (vcpkg)
vcpkg install hiredisIf you prefer to build hiredis from source (for example, for a custom configuration):
git clone https://github.com/redis/hiredis.gitcd hiredismakesudo make installConnecting to a Redis server
After installing hiredis, you can connect it to your C++ project. Make sure the compiler settings include the paths to the header files and libraries:
-I/usr/include(or the path tohiredis.h)-lhiredisfor linking
Example of minimal code for connecting to Redis running on localhost:6379:
#include <hiredis/hiredis.h>#include <iostream>
int main() { // Connect to Redis redisContext *c = redisConnect("127.0.0.1", 6379); if (c == nullptr || c->err) { if (c) { std::cerr << "Connection error: " << c->errstr << std::endl; redisFree(c); } else { std::cerr << "Failed to allocate memory for context" << std::endl; } return 1; } std::cout << "Connection to Redis established!" << std::endl;
// Free resources redisFree(c); return 0;}Sending commands and receiving responses
hiredis provides the redisCommand function for sending commands. It returns a pointer to redisReply, which contains the result of the execution. Always check that the reply is not nullptr, and free the memory after use.
Let's look at basic operations: setting and getting a string value.
#include <hiredis/hiredis.h>#include <iostream>#include <string>
int main() { redisContext *c = redisConnect("127.0.0.1", 6379); if (!c || c->err) { std::cerr << "Connection error: " << (c ? c->errstr : "null") << std::endl; return 1; }
// Set the key "myname" with the value "C++ Developer" redisReply *reply = (redisReply*)redisCommand(c, "SET myname 'C++ Developer'"); if (reply == nullptr) { std::cerr << "Error executing SET command" << std::endl; redisFree(c); return