Redis Tutorial

By Maurizio Farina | Posted on November 2017

Overview

Redis(REmote DIctionary Server) is an open-source, networked, in-memory, key-value data store with optional durability. Redis supports different kinds of values: Sets, Sorted Sets, Hashes, Lists, Strings, Bit Arrays, HyperLogLogs, Geospatial Indexes.

The data structures are highly optimized to be managed in memory including specialized commands that help to execute complex functionality effortlessly avoiding overhead on the network and efficiently in memory.

Redis

What are the main differences with Memcached? Redis manages multiple types of data and includes specialized operations to work with them, so not just SET and GET. Redis supports Clustering and Replication features.

The project is mainly developed by Salvatore Sanfilippo.

Replication + Cluster

Redis Cluster is a data sharding with automatic management, handling failover and replication. The data are split across multiple node; each node stores a subset of the full data;

Slaves replicate the master and act as fallback instances. When a failover occurs a suitable slave of the unavailable Master node will then step up and will be promoted to takeover as a new Master.

Replication: Replication involves a master server which serves reads and writes, and duplicates all data to one or more slave servers (which serves reads but not writes). Slaves can be used to replace a master in case of failure spread read request load, or to perform backups of the database to reduce load on the master.

Sentinel

Redis Sentinel can be used to manage replicated servers (not clustered, see below.) Clients connect to a Sentinel and request a master or slave to communicate with, the sentinels handle health checks of the masters/slaves, and will automatically promote a slave if a master is unreachable. You need to have at least 3 sentinels running so that they can agree on reachability of nodes, and to ensure the sentinels aren’t a single point of failure.

Cluster handles its own promotion and does not need Sentinel in front of it.

Redis Sentinel provides high availability for Redis.

  • Monitoring: check master or slave services healthy
  • Notification: notify when a service goes down.
  • Automatic Failover: starts a failover scenario and assigns one of slaves as a master
  • Configuration Provider: configures and share master service information when a new service is added to cluster.

When to use Redis

Redis runs in memory and can handle extremely high throughput (millions of operations/second) of updates and reads, with relatively little hardware. See Redis Benchmarks for more details.

The other database engines are generally disk-based; to handle frequent real-time updates and high volume at extremely low latencies would require several tens or hundreds of server instances.

Redis, due to its functionality, can be used in different IT scenarios:

  • as a database
  • as a cache system
  • as a message broker

Play with Redis

1
docker run --name try-redis -p 6379:6379 -v /host-redis-folder:/data -d redis redis-server --appendonly yes

There are many open source management tool for Redis. Redis Desktop Manager is a cross-platform GUI management tool.

Redis command line interface

redis-cli is a CLI tool that allows to send commands to Redis.

It has two main modes:

  • an interactive mode: as a terminal where the user types commands and get replies
  • a command mode: commands as arguments. redis-cli executes commands and print the result on the standard output

The offical Redis.io web site includes an useful help about Redis commands

Example of redis-cli (interactive mode)

1
2
3
~$ redis-cli -h 127.0.0.1 -p 6379 -a mysupersecretpassword
127.0.0.1:6379> PING
PONG

Example of redis-cli (command mode)

1
2
~$ redis-cli -h 127.0.0.1 -p 6379 -a mysupersecretpassword PING
PONG

References

Link Description
Data types Introduction to Redis data types
Redis commands The full list of Redis commands
Redis official documentation Redis official documentation
Redis Docker image Docker Hub
Redis Desktop Manager a Windows application to manage Redis server