Skip to content

Using Redis

Redis is an in-memory data store that also offers a wealth of other features. After installing Redis you will be given access to the redis-cli and redis-server binaries (among others). Check this with ls -la /usr/bin | grep redis.

Redis CLI

Check to see all keys

sh
redis-cli --scan | head -N # Replace N with an integer
redis-cli --scan | head -N # Replace N with an integer

Get one value from KEY

sh
redis-cli get YOUR_TOPIC
redis-cli get YOUR_TOPIC

Set a key to hold a string

sh
redis-cli set YOUR_TOPIC "some string"

# equivalent (with out -n this echo adds \n)
# useful for using standard output
echo "some string" | redis-cli -x set YOUR_TOPIC

# equivalent (except adds \n), example of base64
# deserialization prior to setting key
echo "c29tZSBzdHJpbmcK" | base64 -d | redis-cli -x set YOUR_TOPIC
echo "FAAAAAAAAAAAAAoAFAAEAAgADAAKAAAAFMUSAMg+SQCZIRMAAAAAAA==" | base64 -d | redis-cli -x set YOUR_TOPIC
redis-cli set YOUR_TOPIC "some string"

# equivalent (with out -n this echo adds \n)
# useful for using standard output
echo "some string" | redis-cli -x set YOUR_TOPIC

# equivalent (except adds \n), example of base64
# deserialization prior to setting key
echo "c29tZSBzdHJpbmcK" | base64 -d | redis-cli -x set YOUR_TOPIC
echo "FAAAAAAAAAAAAAoAFAAEAAgADAAKAAAAFMUSAMg+SQCZIRMAAAAAAA==" | base64 -d | redis-cli -x set YOUR_TOPIC

You can also connect to a Redis server on an arbitrary host and port, for example

sh
redis-cli -h xxx.xxx.xxx.xxx -p yyyy --scan
redis-cli -h xxx.xxx.xxx.xxx -p yyyy --scan

Sorted Set Commands

  • ZADD adds an item to a sorted set
  • ZRANGE(BYSCORE) retrieves items from a sorted set
  • ZCARD retrieves the cardinality of the sorted set (number of items)

Redis Keyspace Notifications

Keyspace notifications are Redis's way of enabling pub/sub style communication. When an operation is conducted, Redis will emit two messages

  1. PUBLISH __keyspace@0__:<key> <op>: This message is a keyspace notification, which sends the name of the event on the keyspace channel, e.g., ZADD. Use this is if you're interested in activity occuring on a specific key, or on a specific key-prefix. In this case, use string slicing to extract to target key you seek to operate on. Beware that using this method a notification will be emitted for all operations on the key, depending on your call to redis-cli config set notify-keyspace-events KEA.
  2. PUBLISH __keyevent@0__:<op> <key>: This message is a keyevent notification, which sends the name of the key on the keyevent channel, e.g., YOUR_TOPIC. Use this if you're interested in events of a certain type on any key.

Example (C++) using redis plus plus Configure a connection to Redis

c++
std::unique_ptr<sw::redis::Redis> redis;
redis->command("config", "set", "notify-keyspace-events", "KEA");
std::unique_ptr<sw::redis::Redis> redis;
redis->command("config", "set", "notify-keyspace-events", "KEA");

Setup a subscriber

c++
auto subscriber = redis->subscriber();
try {
  subscriber.psubscribe("__keyspace@0__:prefix*");
} catch (const sw::redis::Error &e) {
  throw;
}
auto subscriber = redis->subscriber();
try {
  subscriber.psubscribe("__keyspace@0__:prefix*");
} catch (const sw::redis::Error &e) {
  throw;
}

Setup a callback

c++
subscriber->on_pmessage(
  [this](const std::string &p, const std::string &ch, std::string ev) {
    if (ev == "zadd") {
      auto i = ch.find("prefix");
      std::string target_key = ch.substr(i, ch.length() - i);
      // operate with target_key...
   }
});
subscriber->on_pmessage(
  [this](const std::string &p, const std::string &ch, std::string ev) {
    if (ev == "zadd") {
      auto i = ch.find("prefix");
      std::string target_key = ch.substr(i, ch.length() - i);
      // operate with target_key...
   }
});

Redis Configuration

The Redis configuration file, typically located at /etc/redis/redis.conf allows you to change the default behaviour of the Redis server. The configuration file is self-documented, so the best place to learn about various options is in the configuration file itself.

Allow Remote Connections

By default, the Redis server will reject clients not running on localhost. To change this behaviour, first add the target IP(s) to the bind section of the config. For example, the following snippet will bind the Redis server to 10.0.1.121. Note the - prefix, which specifies that Redis won't crash if that interface isn't available.

diff
- 127.0.0.1 -::1
+ 127.0.0.1 -10.0.1.121 -::1
- 127.0.0.1 -::1
+ 127.0.0.1 -10.0.1.121 -::1

Next, allow inbound connections. Before turning of protected mode, be absolutely sure your server isn't internet accessible.

diff
- protected-mode yes
+ protected-mode no
- protected-mode yes
+ protected-mode no

Alternatively, setup authentication with your server (see https://redis.io/commands/auth/).

Creating a DEB package from source code

First, checkout the redis and redis-debian repos on GitHub.

General steps:

  1. Access the source code, either by downloading it with something like curl, or by cloning the Git repo.
  2. Create a source tarball of the name redis_0.7.2.orig.tar.gz from the source code using the command tar -czvf redis-0.7.2.orig.tar.gz redis-0.7.2
    1. Does the folder being compressed also need the version name?
  3. Copy the debian folder from the redis-debian repo into the source code under the root of the project
  4. Replace any occurrences of @RELEASE@ with the name of the distribution you want, e.g., focal.
  5. Ensure you have a GPG key available
  6. Build the binary package with dpkg-buildpackage -kname@email, where the name@email is the owner of the associated GPG key.
sh
# This script assumes the redis/ directory already has the
# prescribed debian/ directory inside of it
VERSION=7.2.0
tar -czvf redis_${VERSION}.orig.tar.gz redis
sed -i "s/@RELEASE@/focal/g" redis/debian/changelog
cd redis && dpkg-buildpackage [email protected]
# This script assumes the redis/ directory already has the
# prescribed debian/ directory inside of it
VERSION=7.2.0
tar -czvf redis_${VERSION}.orig.tar.gz redis
sed -i "s/@RELEASE@/focal/g" redis/debian/changelog
cd redis && dpkg-buildpackage [email protected]

You should now have all the required packages for various Redis installations ready to go in the parent directory

Database Persistence

There are two options for database persistence with Redis. They can be used individually or together.

  1. RDB (Redis database): Point-in-time snapshots of your dataset at specified intervals.
  2. AOF (Append Only File): Logs every write operation received by the server. These operations can then be replayed again at server startup, reconstructing the original dataset.
Which one should I use?
  • Use both methods for the best data persistence safety (comparable to PortgreSQL).
  • Use RDB alone if your data is important but a few minutes of loss can be incurred.
  • Don't use AOD alone.

RDB persistence is enabled by default. It's settings can be configured in the self-documenting configuration at /etc/redis/redis/conf, just search save.

ADDITIONAL RESOURCES