02 Quick Setup and Usage of Redis

02 Quick Setup and Usage of Redis #

Redis is an open-source, in-memory data store developed in C language. It is often used for databases, caching, and message queues, among other things. Due to its powerful features and simple design, Redis is highly favored by developers and companies and has almost captured the entire market share of in-memory databases.

1 Redis Features #

Redis has many excellent features, which is why it is so popular.

1) Support for multiple data types #

Redis supports multiple data types, such as strings, hashes, lists, sets, sorted sets, HyperLogLog, streams, geospatial coordinates, etc. Each type has its own use cases and satisfies the needs of all developers.

2) Comprehensive functionality #

Redis provides many functionalities, such as message queues, automatic expiration, transactions, data persistence, distributed locks, nearby people, slow query analysis, Sentinel and clustering, etc.

3) High performance #

Redis is an in-memory database, so it has inherent advantages in terms of performance (memory operations are much faster than disk operations). In addition, Redis uses more efficient algorithms and data structures at the underlying level to maximize its performance.

4) Wide range of programming language support #

Redis has numerous client libraries provided by developers, which can be found on https://redis.io/clients. These clients support various programming languages, as shown in the following image: redis-client.png As can be seen, almost all programming languages have corresponding client support.

5) Easy to use #

Although Redis has a rich API, the methods of operations are very simple, and the number of parameters required is also minimal. This allows developers to quickly get started, and Redis also provides comprehensive documentation.

6) High activity/rapid version iteration #

Redis has a high level of activity, as can be seen from the open-source project of Redis at https://github.com/antirez/redis. Furthermore, Redis has a fast version iteration, with over two hundred released versions so far.

7) I/O multiplexing model #

Redis uses a multiplexed I/O model, where “multiplexed” refers to multiple network connections and “I/O” refers to reusing the same thread. By using multiplexed I/O technology, a single thread can efficiently handle multiple connection requests, reducing the time spent on creating and destroying threads. This achieves efficient processing of a large number of concurrent requests.

2 Redis Development History #

Redis has gone through the following stages of development:

  • Initial version of Redis was released in May 2009;
  • Redis 2.6 was released in 2012, with a major code refactoring and removal of all clustering-related code;
  • Redis 2.8 was released in November 2013, introducing partial master-slave replication functionality;
  • Redis 3.0 was released in April 2015, adding clustering functionality;
  • Redis 4.0 was released in July 2017, optimizing replication functionality and introducing hybrid persistence;
  • Redis 5.0 was released in October 2018, adding the Stream data type;
  • Redis 6.0 stable version is planned for release in March to May 2020.

3 Redis Installation #

Redis provides official server installation packages for Linux and MacOS. While formal support is not provided for Windows, there are still multiple ways to install Redis on Windows. The latter part of this article will cover these methods, but for now, let’s focus on the installation of Redis on Linux and MacOS platforms.

1) Source Code Installation #

a) Download the source code package #

Go to the website: https://redis.io/download, select the desired version for installation, and click the “Download” button, as shown in the following image: redis-download.png

b) Extract the installation package #

Use the command: tar zxvf redis-5.0.7.tar.gz

c) Go to the Redis directory #

Use the command: cd /usr/local/redis-5.0.7/

d) Compile and install #

Use the command: sudo make install Once the installation is complete, as shown in the following image: mac-insert1.png If there is no exception message displayed, as shown in the image above, it means Redis has been successfully installed.

2) Docker Installation #

The prerequisite for using Docker is having Docker installed. If Docker is not installed on your local machine, Linux users can use the command yum -y install docker to install Docker online. For non-Linux platforms, you need to download and install Docker Desktop from the official website. The download link is: https://docs.docker.com/get-started/ As shown in the following image: image.png Select the appropriate platform, download, and install it. Once you have Docker installed, you can install the Redis server on Docker by following the steps below:

a) Pull the Redis image #

Installing Redis on Windows #

As mentioned earlier, the official Redis does not provide support for Windows. However, there are still several methods available for installing the Redis server on Windows, such as using Docker, virtual machines, the Windows 10 subsystem, or third-party versions of Redis.

The Docker installation method is the same as the one described in the first part of this article, so it will not be repeated here. You can also install Redis by using a virtual machine with Linux, or by utilizing the Linux subsystem provided by Windows 10.

If you prefer a simpler method, you can use third-party Redis installation packages. For example, Microsoft provides an open-source project called Win64 Redis, which is supported and maintained by them. The latest supported version is 3.2, and it can be downloaded from the following project address: https://github.com/MicrosoftArchive/redis/releases. Simply download the .msi or .zip file and proceed with the installation.

Using Redis #

After Redis is installed, there are several important executable files in the src and /usr/local/bin directories that can be used for various purposes, as shown in the table below:

Executable File Function
redis-server Start Redis server
redis-cli Redis command line tool
redis-benchmark Benchmark testing tool
redis-check-aof AOF persistence file detection and repair tool
redis-check-dump RDB persistence file detection and repair tool
redis-sentinel Start redis-sentinel

Starting Redis #

To start the Redis server, we can use the redis-server executable file. By executing the src/redis-server command in the Redis installation directory, the Redis service will start, as shown in the following image:

Start Redis

As you can see, Redis has started successfully. However, with this startup method, the Redis service will exit when the console is closed. Redis services are not set to run in the background by default, so we need to modify the configuration file (redis.conf). Find the line daemonize no and change it to daemonize yes. After restarting the service, Redis will run in the background and will not exit with the console.

The daemonize configuration is as follows:

Daemonize Redis

Using Visualization Tools to Operate Redis #

After starting Redis, you can use different client tools to connect to and operate Redis. For example, Redis Desktop Manager was used to connect in this article. More Redis visualization tools will be introduced later in this course. As shown in the image below:

Redis Visualization Tool

In the Redis server, there are 16 default database instances, named db0 through db15. However, these database instances are different from traditional relational database instances. In traditional databases, instances are configured using connection strings, whereas in Redis, there is only one connection string and you cannot specify which database instance to use.

To switch between database instances in Redis, simply execute the select n command. For example, to connect to db1, use the command select 1 to select the desired instance. By default, Redis connects to the db0 instance.

Tip: If you use flushall, all data in the Redis database will be cleared.

The number of database instances in Redis can be changed by modifying the configuration file. In redis.conf, find the line databases 16 and modify the number. After restarting the Redis service, the change will take effect.

Using redis-cli to Operate Redis #

redis-cli is the official client tool provided by Redis. It can be used to connect to and operate Redis through the command line. To connect to and operate Redis, use the src/redis-cli command in the Redis installation directory, as shown in the image below:

redis-cli

Conclusion #

This article has introduced the features and development history of Redis, as well as the installation process on Windows, Linux, and MacOS. The Docker installation method is applicable to all platforms. On Linux and MacOS platforms, Redis can be installed online or from source code. On Windows, Redis can be installed using virtual machines, subsystems, or third-party Redis installation packages. After successful installation, Redis can be started with the redis-server command, and connected to and operated using the redis-cli tool. By default, redis-server runs Redis in the foreground. To run Redis in the background, modify the configuration by setting daemonize yes. With these steps, you can quickly start using Redis.