Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Redis 4.x Cookbook

You're reading from   Redis 4.x Cookbook Over 80 hand-picked recipes for effective Redis development and administration

Arrow left icon
Product type Paperback
Published in Feb 2018
Publisher Packt
ISBN-13 9781783988167
Length 382 pages
Edition 1st Edition
Tools
Arrow right icon
Authors (2):
Arrow left icon
Zuofei Wang Zuofei Wang
Author Profile Icon Zuofei Wang
Zuofei Wang
Pengcheng Huang Pengcheng Huang
Author Profile Icon Pengcheng Huang
Pengcheng Huang
Arrow right icon
View More author details
Toc

Table of Contents (15) Chapters Close

Preface 1. Getting Started with Redis FREE CHAPTER 2. Data Types 3. Data Features 4. Developing with Redis 5. Replication 6. Persistence 7. Setting Up High Availability and Cluster 8. Deploying to a Production Environment 9. Administrating Redis 10. Troubleshooting Redis 11. Extending Redis with Redis Modules 12. The Redis Ecosystem 13. Windows Environment Setup
14. Other Books You May Enjoy

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.
You have been reading a chapter from
Redis 4.x Cookbook
Published in: Feb 2018
Publisher: Packt
ISBN-13: 9781783988167
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €18.99/month. Cancel anytime