Caching in C++: Redis and other popular tools
In high-load C++ applications, data access speed is critical. Caching can reduce server response time by 10–100 times, lowering the load on databases and external APIs. In this article, we will review the main libraries for working with cache in C++: from classic Redis to embedded engines LevelDB and RocksDB.
1. Redis and client libraries for C++
Redis is the most popular in-memory key-value store, supporting complex data structures (lists, sets, hashes). For C++, there are two main clients:
- hiredis — a minimalistic C library (official), easily integrated into C++ via wrappers.
- redis-plus-plus — a modern C++17 library with full coverage of Redis commands, support for connection pools, pipeline, and transactions.
Example with hiredis (synchronous)
#include <hiredis/hiredis.h>#include <iostream>
int main() { redisContext *c = redisConnect("127.0.0.1", 6379); if (c == nullptr || c->err) { std::cerr << "Connection error: " << c->errstr << std::endl; return 1; }
redisReply *reply = (redisReply*)redisCommand(c, "SET key hello"); freeReplyObject(reply);
reply = (redisReply*)redisCommand(c, "GET key"); if (reply->type == REDIS_REPLY_STRING) { std::cout << "GET key: " << reply->str << std::endl; } freeReplyObject(reply); redisFree(c); return 0;}Example with redis-plus-plus (asynchronous + pool)
#include <sw/redis++/redis++.h>#include <iostream>
int main() { sw::redis::Redis redis("tcp://127.0.0.1:6379");
// Set with TTL (5 seconds) redis.setex("user:42", 5, "John Doe");
// Get auto val = redis.get("user:42"); if (val) { std::cout << "Cached user: " << *val << std::endl; } else { std::cout << "Key expired or not found" << std::endl; } return 0;}Pros of Redis: high speed (microseconds), rich set of commands, replication, clustering. Cons: consumes RAM, requires a separate server.
2. Memcached and libmemcached
Memcached is the simplest distributed cache that stores data only in RAM. For C++, the libmemcached library (or its C++ wrapper libmemcachedutil) is used.
Example with libmemcached
#include <libmemcached/memcached.h>#include <iostream>#include <cstring>
int main() { memcached_st *memc = memcached_create(nullptr); memcached_server_add(memc, "127.0.0.1", 11211);
const char *key = "session_token"; const char *value = "abc123"; memcached_return_t rc = memcached_set(memc, key, strlen(key), value, strlen(value), 0, 0); // 0 - no compression, 0 - TTL
if (rc == MEMCACHED_SUCCESS) { size_t value_length; uint32_t flags; char *result = memcached_get(memc, key, strlen(key), &value_length, &flags, &rc); if (rc == MEMCACHED_SUCCESS) { std::cout << "Get: " << result << std::endl; free(result); } } memcached_free(memc); return 0;}When to choose Memcached: if you need simple caching without complex data structures, and maximum performance is important. Redis wins in functionality, Memcached in simplicity.
3. Embedded engines: LevelDB and RocksDB
If the cache should be local (
Also in library
Machine Learning in JavaScript: Libraries for Node.js — Overview and Examples
EventKit for Swift: Complete Guide with Examples
Sphinx Documentation Generator: Python Docstrings to Docs
Caching in C# (.NET): Redis, MemoryCache and Other Solutions