userver: Redis
Loading...
Searching...
No Matches
Redis

The redis asynchronous driver provides an interface to work with Redis standalone instances, as well as Redis Sentinels and Clusters.

Take note that Redis is not able to guarantee a strong consistency. It is possible for Redis to lose writes that were acknowledged, and vice versa.

Main features

  • Convenient methods for Redis commands returning proper C++ types
  • Support for bulk operations (MGET, MSET, etc). Driver splits data into smaller chunks if necessary to increase server responsiveness
  • Support for different strategies of choosing the most suitable Redis instance
  • Request timeouts management with transparent retries
  • Deadline propagation
  • Cluster autotopology

Metrics

Metric name Description
redis.instances_count current number of Redis instances
redis.last_ping_ms last measured ping value
redis.is_ready 1 if connected and ready, 0 otherwise
redis.not_ready_ms milliseconds since last ready status
redis.reconnects reconnect counter
redis.session-time-ms milliseconds since connected to instance
redis.timings query timings
redis.errors counter of failed requests

See Service Statistics and Metrics (Prometheus/Graphite/...) for info on how to get the metrics.

Usage

To use Redis you must add the component components::Redis and configure it according to the documentation. After that you can make requests via storages::redis::Client:

void RedisClientSampleUsage(storages::redis::Client& client) {
client.Rpush("sample_list", "a", {}).Get();
client.Rpush("sample_list", "b", {}).Get();
const auto length = client.Llen("sample_list", {}).Get();
EXPECT_EQ(length, 2);
}

Timeouts

Request timeout can be set for a single request via redis::CommandControl argument.

Dynamic option REDIS_DEFAULT_COMMAND_CONTROL can be used to set default values.

To interrupt a request on the client side, you can use cancellation mechanism to cancel the task that executes the Redis request:

std::optional<std::string> RedisClientCancelRequest(
auto result = client.Get("foo", {});
// Throws redis::RequestCancelledException if Redis was not
// fast enough to answer
return result.Get();
}

Redis driver does not guarantee that the cancelled request was not executed by the server.

Redis Cluster Autotopology

Cluster autotopology makes it possible to do resharding of the cluster without Secdist changes and service restart.

With this feature entries in the Secdist are now treated not as a list of all the cluster hosts, but only as input points through which the service discowers the configuration of the entire cluster. Therefore, it is not recommended to delete instances that are listed in secdist from the cluster.

The cluster configuration is checked

  • at the start of the service
  • and periodically
  • and if a MOVED response is received from Redis

If a change in the cluster topology was detected during the check (hashslot distribution change, master change, or new replicas discowered), then the internal representation of the topology is recreated, the new topology is gets ready (new connections may appear in it), and after that the active topology is replaced.

Enabling/Disabling Redis Cluster Autotopology

At the moment aotopology could be nables via an experiment in static config of a service:

#yaml
userver_experiments:
- redis-cluster-autotopology

The autotopology could be disabled by the dynamic config option REDIS_CLUSTER_AUTOTOPOLOGY_ENABLED_V2.