Redis Client for C++: Overview and Practical Guide
Redis is a high-performance in-memory database widely used for caching, session management, message queues, and real-time analytics. For interacting with Redis in C++, there are several mature libraries. In this article, we will look at two of the most popular: hiredis (the official C client, easily integrated into C++) and Redis++ (a modern C++17 client with convenient syntax). You will learn how to install, connect, and use these tools in your projects.
1. The hiredis Library
hiredis is a minimalist C client developed by the same team as Redis. It is lightweight, fast, and supports synchronous and asynchronous requests. For C++ programmers, hiredis serves as an excellent foundation, as it can be called directly from C++ code, wrapping it in RAII objects.
Installing hiredis
Installing hiredis is done via a package manager or by building from source:
# Ubuntu/Debiansudo apt-get install libhiredis-dev
# macOS (Homebrew)brew install hiredis
# Building from sourcegit clone https://github.com/redis/hiredis.gitcd hiredismakesudo make installConnection and Basic Example
To work with hiredis, you need to include the header file and link against the library. Below is an example of connecting to Redis and executing simple commands:
#include <iostream>#include <hiredis/hiredis.h>
int main() { // Connect to Redis on localhost:6379 redisContext *c = redisConnect("127.0.0.1", 6379); if (c == nullptr || c->err) { std::cerr << "Connection error: " << c->errstr << std::endl; return 1; }
// Send SET command redisReply *reply = (redisReply*)redisCommand(c, "SET key1 Hello"); if (reply->type == REDIS_REPLY_STATUS) { std::cout << "SET: " << reply->str << std::endl; } freeReplyObject(reply);
// Send GET command reply = (redisReply*)redisCommand(c, "GET key1"); if (reply->type == REDIS_REPLY_STRING) { std::cout << "GET: " << reply->str << std::endl; } freeReplyObject(reply);
// Free resources redisFree(c); return 0;}Compilation is done with the -lhiredis flag:
g++ -o test test.cpp -lhiredis2. The Redis++ Library (sw/redis++ / redox)
Redis++ (also known as sw/redis++) is a modern C++17 client that provides a type-safe interface, support for connection pools, transactions, pipelines, and Redis Cluster. If you prefer writing in "pure" C++ without C-style code, Redis++ is the ideal choice.
Installing Redis++
Installation is done via vcpkg, Conan, or building from source:
# Via vcpkgvcpkg install redis-plus-plus
# Via Conanconan install redis++/1.3.3@
# Building from sourcegit clone https://github.com/sewenew/redis-plus-plus.gitcd redis-plus-plusmkdir build && cd buildcmake ..makesudo make installExample of Using Redis++
The library uses a Redis object to work with commands:
#include <iostream>#include <sw/redis++/redis++.h>
int main() { try { // Create a client sw::redis::Redis redis("tcp://127.0.0.1:6379");
// Set a value redis.set("mykey", "Hello, Redis++!");
// Get a value auto val = redis.get("mykey"); if (val) { std::cout << "GET: " << *val << std::endl; }
// Work with hashes redis.hset("user:1", "name", "Alice"); redis.hset("user:1", "age"