Caching in C++: Redis, Memcached, and Built-in Solutions: A Complete Guide

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Introduction: Why Caching is Needed in C++ and How It Speeds Up Applications

\n\n

In modern C++ development, performance is a critical factor. Every extra database query, every repeated calculation of a complex function, or every disk read is a loss of milliseconds that, at the scale of a high-load service, turns into seconds of downtime. Caching solves this problem by storing the results of expensive operations in fast-access memory.

\n\n

In this article, we will look at three main approaches to caching in C++: external systems (Redis, Memcached) and built-in solutions (std::unordered_map, LRU cache). You will learn when to choose which tool, how to properly configure a cache, and what pitfalls await you.

\n\n

1. External Caching Systems: Redis and Memcached

\n\n

1.1 Redis: A Powerful In-Memory Store with Data Structure Support

\n\n

Redis is not just a cache, but a full-fledged data structure server running in RAM. It supports strings, lists, sets, hash tables, and even queues. For C++, there are several client libraries, the most popular being hiredis (in C) and redis-plus-plus (in C++).

\n\n

Example of connecting to and using Redis via redis-plus-plus:

\n\n
#include <sw/redis++/redis++.h>\n#include <iostream>\n\nint main() {\n    // Connect to Redis on localhost\n    sw::redis::Redis redis("tcp://127.0.0.1:6379");\n\n    // Write a value to the cache\n    redis.set("user:1001", "{\\"name\\":\\"Alice\\",\\"age\\":30}");\n\n    // Read the value\n    auto val = redis.get("user:1001");\n    if (val) {\n        std::cout << "Cached data: " << *val << std::endl;\n    }\n\n    // Set TTL (time to live) — 60 seconds\n    redis.expire("user:1001", 60);\n\n    return 0;\n}\n
\n\n

Pros of Redis:

\n
    \n
  • Support for complex data structures (lists, hashes, sets)
  • \n
  • Built-in replication and persistence mechanisms
  • \n
  • Support for transactions and Lua scripts
  • \n
  • Ability to use as a pub/sub message broker
  • \n
\n\n

Cons of Redis:

\n
    \n
  • Requires a separate server (additional infrastructure)
  • \n
  • Consumes more memory than Memcached due to structure metadata
  • \n
  • Not suitable for very large data volumes (limited by RAM)
  • \n
\n\n

1.2 Memcached: A Lightweight and Fast Distributed Cache

\n\n

Memcached is a simpler system designed exclusively for caching key-value pairs. It is ideal for storing small objects (up to 1 MB). For C++, the libmemcached library is used.

\n\n

Example of working with Memcached in C++:

\n\n
#include <libmemcached/memcached.h>\n#include <iostream>\n#include <cstring>\n\nint main() {\n    memcached_server_st *servers;\n    memcached_st *memc;\n    memcached_return rc;\n\n    memc = memcached_create(nullptr);\n    servers = memcached_server_list_append(nullptr, "127.0.0.1", 11211, &rc);\n    rc = memcached_server_push(memc, servers);\n\n    // Write to cache with TTL of 300 seconds\n    const char *key = "session_token_abc123";\n    const char *value = "valid";\n    rc = memcached_set(memc, key, strlen(key), value, strlen(value), 300, 0);\n\n    // Read from cache\n    size_t value_length;\n    uint32_t flags;\n    char *result = memcached_get(memc, key, strlen(key), &value_length, &flags, &rc);\n    if (result) {\n        std::cout << "Cached value: " << result << std::endl;\n        free(result);\n    }\n\n    memcached_free(memc);\n    return 0;\n}\n
\n\n

Pros of Memcached:

\n
    \n
  • Maximum performance for simple get/set operations
  • \n
  • Minimal memory consumption for metadata
  • \n <

Blogs

Book Recommendations