Redis, a Cache

Aditi Mittal
codeburst
Published in
6 min readJan 12, 2020

--

Redis is an open source, key-value pair data store which holds its database entirely in the memory. It uses disk only for the persistence. Redis supports relatively large number of data types when compared to many other key-value data stores. It can be used as a database, cache and message broker.

Redis has built-in replication, Lua scripting, LRU eviction, transactions and different levels of on-disk persistence, and provides high availability via Redis Sentinel and automatic partitioning with Redis Cluster.

In order to achieve its outstanding performance, Redis works with an in-memory dataset. Depending on the use case, data can be persisted either by dumping the dataset to disk every once in a while, or by appending each command to a log. Persistence can be optionally disabled, if only a feature-rich, networked, in-memory cache is required.

Install Redis on Mac

  1. Open the “Terminal” application, found in /Applications/Utilities/
  2. Run the following command into a single line of the terminal to install homebrew:
sudo /usr/bin/ruby -e “$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

3. Install redis via homebrew through the following command:

$ brew install redis

4. Launch redis server using configuration file by using the follwoing command on terminal:

$ redis-server /usr/local/etc/redis.conf

5. Test if redis server is running by

$ redis-cli ping

If it returns PONG, then the redis server is up and ready!

For setting Redis configurations, there are two options:

  1. There is a redis.conf file at the root directory of Redis
  2. By using Redis CONFIG command
redis 127.0.0.1:6379> CONFIG GET CONFIG_NAME
redis 127.0.0.1:6379> CONFIG SET SETTING_NAME NEW_VALUE

Data Types

String

Redis strings have a known length and not determined by any special terminating characters. Up to 512 megabytes can be stored in one string.

redis 127.0.0.1:6379> SET key1 "medium_article"
OK
redis 127.0.0.1:6379> GET key1
"medium_article"

Hash

A hash is a collection of key value pairs i.e. a map between string fields and string values.

redis 127.0.0.1:6379> HMSET key:1 name medium_user password test stats 1.4K
OK
redis 127.0.0.1:6379> HGETALL key:1
1) "name"
2) "medium_user"
3) "password"
4) "test"
5) "stats"
6) "1.4K"

key:1 is the key. A hash can store up to 2^32 -1 field-value pairs

List

It is a list of strings sorted by insertion order. Elements can be added to a Redis List on the head or on the tail.

redis 127.0.0.1:6379> lpush medium a
(integer) 1
redis 127.0.0.1:6379> lpush medium b
(integer) 2
redis 127.0.0.1:6379> lpush medium c
(integer) 3
redis 127.0.0.1:6379> lrange medium 0 10
1) "a"
2) "b"
3) "c"

Sets

Sets are an unordered collection of strings. Elements can be added, removed, and tested for existence. Every element can appear only once.

redis 127.0.0.1:6379> sadd tutoriallist redis
(integer) 1
redis 127.0.0.1:6379> sadd tutoriallist mongodb
(integer) 1
redis 127.0.0.1:6379> sadd tutoriallist rabitmq
(integer) 1
redis 127.0.0.1:6379> sadd tutoriallist rabitmq
(integer) 0
redis 127.0.0.1:6379> smembers tutoriallist
1) "rabitmq"
2) "mongodb"
3) "redis"

Sorted Sets

Sorted Sets are similar to Sets, non-repeating collections of Strings. The difference is, every member of a Sorted Set is associated with a score, that is used in order to take the sorted set ordered, from the smallest to the greatest score. While members are unique, the scores may be repeated.

redis 127.0.0.1:6379> zadd tutoriallist 0 redis
(integer) 1
redis 127.0.0.1:6379> zadd tutoriallist 0 mongodb
(integer) 1
redis 127.0.0.1:6379> zadd tutoriallist 0 rabitmq
(integer) 1
redis 127.0.0.1:6379> zadd tutoriallist 0 rabitmq
(integer) 0
redis 127.0.0.1:6379> ZRANGEBYSCORE tutoriallist 0 1000
1) "redis"
2) "mongodb"
3) "rabitmq"

HyperLogLog

It is is an algorithm that uses randomization in order to provide an approximation of the number of unique elements in a set using just a constant, and small amount of memory.

redis 127.0.0.1:6379> PFADD medium "redis"
1) (integer) 1
redis 127.0.0.1:6379> PFADD medium "rcnn"
1) (integer) 1
redis 127.0.0.1:6379> PFADD medium "rabbitmq"
1) (integer) 1
redis 127.0.0.1:6379> PFADD medium "springboot"
1) (integer) 1
redis 127.0.0.1:6379> PFCOUNT medium
(integer) 4

Publish Subscribe

It is a messaging system in which the senders (or publishers) sends the messages and the receivers (subscribers) receive them. The messages are transferred by a link called channel. A client can subscribe any number of channels.

Example: one client subscribes a channel named ‘chat’.

redis 127.0.0.1:6379> SUBSCRIBE chat
Reading messages... (press Ctrl-C to quit)
1) "subscribe"
2) "chat"
3) (integer) 1

Two clients are publishing messages on the same channel named ‘chat’

redis 127.0.0.1:6379> PUBLISH chat "Redis is a database"
(integer) 1
redis 127.0.0.1:6379> PUBLISH chat "Medium article on redis"
(integer) 1
1) "message"
2) "chat"
3) "Redis is a database"
1) "message"
2) "chat"
3) "Medium article on redis"

Redis Persistence

Redis provides a different range of persistence options:

  • The RDB persistence — performs point-in-time snapshots of dataset at specified intervals.
  • The AOF persistence — logs every write operation received by the server, that will be played again at server startup, reconstructing the original dataset.
  • Persistence can be completely disabled.
  • It is possible to combine both AOF and RDB in the same instance. When Redis restarts the AOF file will be used to reconstruct the original dataset since it is guaranteed to be the most complete.

RDB advantages

  • Compact single-file point-in-time representation of Redis data. It is easy to restore different versions of the data set in case of disasters.
  • Very good for disaster recovery, being a single compact file that can be transferred to far data centers, or onto Amazon S3 (possibly encrypted).
  • Allows faster restarts with big datasets compared to AOF.

RDB disadvantages

  • RDB is NOT good in case of minimizing the chance of data loss in case Redis stops working (for example after a power outage).
  • RDB needs to fork() often in order to persist on disk using a child process. Fork() can be time consuming if the dataset is big, and may result in Redis to stop serving clients for some millisecond or even for one second if the dataset is very big and the CPU performance not great.

AOF advantages

  • Using AOF Redis is much more durable, with different fsync policies: no fsync at all, fsync every second, fsync at every query.
  • The AOF log is an append only log, so there are no seeks, nor corruption problems if there is a power outage. Even if the log ends with an half-written command for some reason (disk full or other reasons) the redis-check-aof tool is able to fix it easily.
  • Redis is able to automatically rewrite the AOF in background when it gets too big. The rewrite is completely safe as while Redis continues appending to the old file, a completely new one is produced with the minimal set of operations needed to create the current data set, and once this second file is ready Redis switches the two and starts appending to the new one.
  • AOF contains a log of all the operations one after the other in an easy to understand and parse format. AOF file can be easily exported.

AOF disadvantages

  • AOF files are usually bigger than the equivalent RDB files for the same dataset.
  • AOF can be slower than RDB depending on the exact fsync policy. In general with fsync set to every second performance is still very high, and with fsync disabled it should be exactly as fast as RDB even under high load. Still RDB is able to provide more guarantees about the maximum latency even in the case of an huge write load.
  • Bugs in specific commands (for instance there was one involving blocking commands like BRPOPLPUSH) causing the AOF produced to not reproduce exactly the same dataset on reloading. However, these kind of bugs are almost impossible with RDB persistence.

Both persistence methods should be used for data safety. If data is important, but still can live with a few minutes of data loss in case of disasters, RDB alone can be used. Using AOF alone is discouraged since to have an RDB snapshot from time to time is a great idea for doing database backups, for faster restarts, and in the event of bugs in the AOF engine.

Backup

SAVE command is used to create a backup of the current database.

127.0.0.1:6379> SAVE

This command will create a dump.rdb file in Redis directory.

Restore Redis Data

To restore Redis data, move dump.rdb file into Redis directory and start the server. To get Redis directory, use CONFIG command of Redis

127.0.0.1:6379>CONFIG get dir

Bgsave

BGSAVE command will start the backup process and run this in the background.

127.0.0.1:6379> BGSAVE
Background saving started

Partitioning

It is the process of splitting data into multiple instances, so that every instance will only contain a subset of the keys.

Advantages

  • It allows for much larger databases, using the sum of the memory of many computers.
  • It allows to scale the computational power to multiple cores and multiple computers, and the network bandwidth to multiple computers and network adapters.

Disadvantages

  • Operations involving multiple keys are usually not supported.
  • Redis transactions involving multiple keys cannot be used.
  • The partitioning granuliary is the key, so it is not possible to shard a dataset with a single huge key like a very big sorted set.
  • When partitioning is used, data handling is more complex.
  • Adding and removing the capacity can be complex.

I would love to hear your feedback. Another article on redis clustering and their commands coming soon. Stay tuned!

--

--