Book Image

Redis 4.x Cookbook

Book Image

Redis 4.x Cookbook

Overview of this book

Redis is considered the world's most popular key-value store database. Its versatility and the wide variety of use cases it enables have made it a popular choice of database for many enterprises. Based on the latest version of Redis, this book provides both step-by-step recipes and relevant the background information required to utilize its features to the fullest. It covers everything from a basic understanding of Redis data types to advanced aspects of Redis high availability, clustering, administration, and troubleshooting. This book will be your great companion to master all aspects of Redis. The book starts off by installing and configuring Redis for you to get started with ease. Moving on, all the data types and features of Redis are introduced in detail. Next, you will learn how to develop applications with Redis in Java, Python, and the Spring Boot web framework. You will also learn replication tasks, which will help you to troubleshoot replication issues. Furthermore, you will learn the steps that need to be undertaken to ensure high availability on your cluster and during production deployment. Toward the end of the book, you will learn the topmost tasks that will help you to troubleshoot your ecosystem efficiently, along with extending Redis by using different modules.
Table of Contents (21 chapters)
Title Page
Dedication
Packt Upsell
Foreword
Contributors
Preface
13
Windows Environment Setup
Index

Starting and shutting down Redis


Before accessing Redis, the Redis Server must be started in a proper way. Similarly, under certain circumstances, you have to stop the Redis service. This recipe will show you how to start and stop a Redis Server.

Getting ready…

You need to finish the installation of the Redis Server, as we described in the Downloading and installing Redis recipe in this chapter.

How to do it...

The steps for starting and shutting down a Redis Server are as follows:

  1. You can start a Redis Server with the default configurations:
$ bin/redis-server

Your server should now start up as shown in the following screenshot:

  1. To start a Redis Server using a configuration file, such as the configuration file we copied from the source code package in the installation receipt, type the following:
$ bin/redis-server conf/redis.conf
  1. If you have installed Redis from the repository of an operating system, you can start up Redis using the init.d script:
$ /etc/init.d/redis-server start 
  1. To run redis-server as a daemon in the background at start up, you can edit the configuration file and set the daemonize parameter to yes and start with this configuration:
$ vim conf/redis.conf
daemonize yes 
$ bin/redis-server conf/redis.conf

The message Configuration loaded shown in the following screenshot indicates the configuration has already taken place:

  1. Correspondingly, you may use Ctrl + C (if Redis started in the foreground), or use Kill + PID (if you run Redis in the background) to stop the Redis service:
$ kill `pidof redis-server` 
  1. The more graceful and recommended way to stop Redis is calling the shutdown command in redis-cli:
$ cd /redis 
$ bin/redis-cli shutdown 
  1. Redis can also be shut down by the init.d script, in case you installed it from the repository of the operating system:
$ /etc/init.d/redis-server stop 

How it works...

The term instance in Redis represents a redis-server process. Multiple instances of Redis can run on the same host, as long as they use different configurations, such as different binding ports, data persistence paths, log paths, and so on.

Starting and stopping the Redis instance are basic operations. There is not much to note when starting Redis, but for a data service, stopping a Redis service deserves more attention, because as a data store service, it is of great importance for you to learn how to stop the Redis Server gracefully in order to maintain data integrity.

The reason why using the shutdown command to stop Redis is highly recommended is that if you care about data integrity and have already set persistence for Redis to save your data in memory to disk (the persistence of Redis will be discussed in Chapter 6, Persistence), issuing the shutdown command not only terminates the process, but also takes a series of other actions.

First, the redis-server will stop all the clients, and then one persistence action will be performed if the persistence has been enabled. Afterwards, it will clean the .pid file and socket file if there are any, and finally quit the process. By adopting this strategy, Redis does its best to prevent any data loss. Conversely, if the kill command is used rudely, to terminate the redis-server process, data may get lost because it has not been persisted before the server is shut down.

It should be noted that using kill or other process management tools to send a SIGTERM signal (15 signal) to the Redis process is basically equivalent to the shutdown command for gracefully stopping the redis-server.

There's more...

Configuration parameters can be added to the command redis-server while starting, which is quite useful when deploying multiple instances on a single host. We can have a single configuration file of common configuration parameters used by multiple instances on the same host. Meanwhile, the unique configuration parameters of each instance can be passed in the command line on startup. This way, the cost of maintaining multiple configuration files is eliminated, and instances can be distinguished easily via ps or other system commands.

In addition, you can manage your Redis instance using process management tools such as systemd, supervisord, or Monit, which can also prevent you from messing up when you deploy multiple instances on a single host. All we need to pay attention to are the startup configuration parameters mentioned previously and exit signal handling mechanisms.

See also

  • Refer to https://redis.io/topics/signals to learn more about how Redis handles various kinds of signals, especially finding out the slight but important differences among these signal handling mechanisms. Additionally, refer to https://redis.io/commands/shutdown for more details about gracefully shutting down a Redis instance.
  • For the process management tool to control the start up/shutdown of Redis, https://git.io/v5chR is an example for systemd configuration for a Redis Server.
  • Furthermore, you can refer to Chapter 6, Persistence for persistence of Redis.