32,39 €
Leverage the power of Redis 4.x to develop, optimize and administer your Redis solutions with ease
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.
This book is for database administrators, developers and architects who want to tackle the common and not so common problems associated with the different development and administration-related tasks in Redis. A fundamental understanding of Redis is expected to get the best out of this book.
Pengcheng Huang Pengcheng Huang has been working as a software engineer and team lead of the Big Data Infrastructure team at China Minsheng Bank (ranked No.29 in the Top 1000 World Banks in 2017) for more than 5 years providing Big data infrastructure services for the whole bank. Also, as the technical director of Redis at the bank, he devotes much of his energy making better use of Redis in the production environment. He is also a Redis contributor. You can reach him on LinkedIn by searching for gnuhpc. Zuofei Wang Zuofei Wang is an experienced software engineer living in the San Francisco Bay area. With more than 5 years of experience in the software industry, he has worked on projects with different technologies and is currently employed by Airbnb Inc. Zuofei is passionate about learning new things and sharing his knowledge. He also enjoys reading, traveling, and ham radio in his spare time.Sie lesen das E-Book in den Legimi-Apps auf:
Seitenzahl: 387
Veröffentlichungsjahr: 2018
Copyright © 2018 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the authors, nor Packt Publishing or its dealers and distributors, will be held liable for any damages caused or alleged to have been caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.
Commissioning Editor: Amey VarangaonkarAcquisition Editor:Namrata PatilContent Development Editor:Amrita NoronhaTechnical Editor:Jovita AlvaCopy Editor: Safis EditingProject Coordinator:Shweta H BirwatkarProofreader: Safis EditingIndexer:Mariammal ChettiyarGraphics:Jisha ChirayilProduction Coordinator:Aparna Bhagat
First published: February 2018
Production reference: 1270218
Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK.
ISBN 978-1-78398-816-7
www.packtpub.com
Mapt is an online digital library that gives you full access to over 5,000 books and videos, as well as industry leading tools to help you plan your personal development and advance your career. For more information, please visit our website.
Spend less time learning and more time coding with practical eBooks and Videos from over 4,000 industry professionals
Improve your learning with Skill Plans built especially for you
Get a free eBook or video every month
Mapt is fully searchable
Copy and paste, print, and bookmark content
Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at [email protected] for more details.
At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters, and receive exclusive discounts and offers on Packt books and eBooks.
I still remember how surprised I was when for the first time I heard about Redis. It's so elegant and powerful, like a Swiss Army knife for your backend applications, and of course, it’s blazing fast. In modern data centers, the unit cost of RAM is getting lower and lower; it's no surprise that Redis is playing an important role in storage stacks of modern applications. Frankly speaking, Redis is way beyond an in-memory cache.
I’m a database engineer. Codis, a distributed Redis middleware, is my first open source project, which is widely used in the community. I’m quite proud of it. Codis provides a proxy-based solution to tackle the scalability problem of Redis and it’s an alternative to Redis Cluster. As one of the earliest adopters and developers of Redis in China, I witnessed the increasing popularity of Redis. Still more thanks to Redis that I met my dear friend, Redis expert, and contributor, Pengcheng Huang. When he told me he wanted to write a book about Redis, I told him I would definitely buy one. Finally, he made it, and it’s my honor and pleasure to review the early version and write a foreword in this book.
You won’t regret choosing this book. Many significant changes and features were introduced after Redis 4.0 was released. Whether you are new to Redis or an experienced Redis developer like me, you’ll learn new tricks from this book.
Dongxu Huang
PingCap CTO and Codislabs cofounder
As a contributor and member of the Redisson project as well as a long-time Redis advocate, I have witnessed Redis steadily taking over the world over the years as it evolved from one version to another. After having several conversations with many members of both Redis and Redisson communities, I couldn’t help but wonder: wouldn’t it be great if there were a canonical book with the most up-to-date information on Redis so that Redis users don’t have to trawl through tons of Stack Overflow questions to get the answers they were looking for?
Quite obviously, Pengcheng and Zuofei shared the same vision.
I have known Pengcheng for quite some time. He is a well-known Redis enthusiast in the Redis Chinese community, managing a very active user group full of members ranging from Redis core contributors and tools/library authors, to Redis advocates and everyday users. This book is about what Pengcheng knows best: Redis, or Redis version 4 to be more precise. As one might imagine, he doesn’t manage a user group for a living. He serves as a team lead of big data infrastructure and the technical director of Redis in China Minsheng Bank. His responsibility is to ensure that Redis, as one of the most important parts of the bank’s infrastructure, is being implemented and managed correctly throughout. It is his work at the bank that had helped plant the seeds that would eventually grow into this book.
As a result, the book covers a wide range of topics and is organized in a logical order, primarily catering to beginners and intermediate users. Beginners, you can find plenty of useful examples, diagrams, and how-to guides to help you read through the entire book. Intermediary readers, you will be very pleased to see that the authors were not shy when diving into the depths of how everything in Redis works, along with recommendations for further reading on each topic.
Personally, I quite like the detailed explanations of each Redis configuration option. I think this book will serve me well as a useful handbook, to be kept at the side of my table for quick reference. I hope you will find this book as enjoyable to read as I have, and that it will prove useful as you explore the many wonderful features of Redis 4.
Rui Gu
Long-time Redis advocate, contributor and core developer, Redisson Team
Pengcheng Huang has been working as a software engineer and team lead of the Big Data Infrastructure team at China Minsheng Bank (ranked No.29 in the Top 1000 World Banks in 2017) for more than 5 years providing Big data infrastructure services for the whole bank. Also, as the technical director of Redis at the bank, he devotes much of his energy making better use of Redis in the production environment. He is also a Redis contributor. You can reach him on LinkedIn by searching for gnuhpc.
Zuofei Wang is an experienced software engineer living in the San Francisco Bay area. With more than 5 years of experience in the software industry, he has worked on projects with different technologies and is currently employed by Airbnb Inc. Zuofei is passionate about learning new things and sharing his knowledge. He also enjoys reading, traveling, and ham radio in his spare time.
Domagoj Katavic has a master's degree in electronics and computer engineering. Currently, he works at Vectra Networks, a network security company that identifies real-time cyber attacks.
Previously, he has worked at Planet9 Energy (a UK Energy supplier), Codeanywhere (a cloud IDE), and as an assistant professor at FESB, the University of Split, Croatia.
Ihor Malinovskiy is an addicted programmer from Ukraine. He started his career as a web developer in 2009 and has worked in different domains, such as advertising, healthcare, and cloud software, since then. Ihor is an open source enthusiast and was the core developer in an OpenStack project. In 2013, he started his own product—a desktop GUI for Redis, called Redis Desktop Manager.
If you're interested in becoming an author for Packt, please visit authors.packtpub.com and apply today. We have worked with thousands of developers and tech professionals, just like you, to help them share their insight with the global tech community. You can make a general application, apply for a specific hot topic that we are recruiting an author for, or submit your own idea.
Title Page
Copyright and Credits
Redis 4.x Cookbook
Dedication
Packt Upsell
Why subscribe?
PacktPub.com
Foreword
Contributors
About the authors
About the reviewers
Packt is searching for authors like you
Preface
Who this book is for
What this book covers
To get the most out of this book
Download the example code files
Download the color images
Conventions used
Sections
Getting ready…
How to do it…
How it works…
There's more…
See also
Get in touch
Reviews
Getting Started with Redis
Introduction
Downloading and installing Redis
Getting ready…
How to do it...
How it works...
There's more...
See also
Starting and shutting down Redis
Getting ready…
How to do it...
How it works...
There's more...
See also
Connecting to Redis with redis-cli
Getting ready…
How to do it...
How it works...
There's more...
See also
Getting server information
Getting ready…
How to do it...
How it works...
There's more…
See also
Understanding the Redis Event Model
Getting ready…
How to do it...
How it works...
There's more...
See also
Understanding the Redis protocol
Getting ready…
How to do it...
How it works...
See also
Data Types
Introduction
Using the string data type
Getting ready…
How to do it...
How it works...
There's more...
See also
Using the list data type
Getting ready…
How to do it...
How it works...
There's more...
See also
Using the hash data type
Getting ready…
How to do it...
How it works...
There's more...
See also
Using the set data type
Getting ready…
How to do it...
How it works...
There's more...
Using the sorted set data type
Getting ready…
How to do it...
How it works...
There's more...
See also
Using the HyperLogLog data type
Getting ready…
How to do it...
How it works...
There's more...
See also
Using the Geo data type
Getting ready…
How to do it...
How it works...
There's more...
See also
Managing keys
Getting ready…
How to do it...
How it works...
There's more...
See also
Data Features
Introduction
Using bitmaps
Getting ready…
How to do it…
How it works…
There's more...
See also
Setting expiration on keys
Getting ready…
How to do it...
How it works...
There's more...
See also
Using SORT
Getting ready…
How to do it…
There's more...
See also
Using pipelines
Getting ready…
How to do it...
How it works...
There's more...
See also
Understanding Redis transactions
Getting ready…
How to do it...
How it works...
There's more...
See also
Using PubSub
Getting ready…
How to do it...
How it works...
There's more...
See also
Using Lua
Getting ready…
How to do it...
How it works...
There's more...
See also
Debugging Lua
Getting ready…
How to do it...
How it works...
There's more...
See also
Developing with Redis
Introduction
When to use Redis in your application
Session store
Analytics
Leaderboards
Queues
Latest N records
Caching
There's more…
See also
Using the correct data types
Getting ready…
How to do it...
How it works...
There's more...
See also
Using the correct Redis APIs
Getting ready…
How to do it
How it works...
There's more...
See also
Connecting to Redis with Java
Getting ready…
How to do it...
Connecting to the Redis Server
Using pipeline in Jedis
Using transactions in Jedis
Running Lua scripts in Jedis
Using a connection pool in Jedis
How it works...
See also
Connecting to Redis with Python
Getting ready…
How to do it...
Connecting to the Redis Server
Using pipelines
Running Lua scripts
How it works...
There's more...
See also
Connecting to Redis with Spring Data Redis
Getting ready…
How to do it...
How it works...
See also
Writing a MapReduce job for Redis
Getting ready…
How to do it...
How it works...
See also
Writing a Spark job for Redis
Getting ready…
How to do it...
How it works...
There's more...
See also
Replication
Introduction
Setting up Redis replication
Getting ready…
How to do it...
How it works...
There's more...
See also
Optimizing replication
Getting ready…
How to do it...
How it works...
There's more...
See also
Troubleshooting replication
Getting ready…
How to do it...
How it works...
There's more...
See also
Persistence
Introduction
Manipulating RDB
Getting ready…
How to do it...
How it works...
There's more...
See also
Exploring RDB
Getting ready…
How to do it...
How it works...
There's more...
See also
Manipulating AOF
Getting ready…
How to do it...
How it works...
There's more...
See also
Exploring AOF
Getting ready…
How to do it...
How it works...
There's more...
See also
Combining RDB and AOF
Getting ready…
How to do it...
How it works...
There's more...
Setting Up High Availability and Cluster
Introduction
Setting up Sentinel
Getting ready…
How to do it...
How it works...
There's more…
See also
Testing Sentinel
Getting ready…
How to do it...
How it works...
Triggering a master failover manually
Simulating a master down
Simulating two slaves down
Simulating one Sentinel down
Simulating two Sentinels down
There's more…
Administrating Sentinel
Getting ready…
How to do it...
How it works...
There's more...
Setting up Redis Cluster
Getting ready…
How to do it...
How it works...
There's more...
See also
Testing Redis Cluster
Getting ready…
How to do it...
How it works...
There's more...
See also
Administrating Redis Cluster
Getting ready…
How to do it...
How it works...
There's more...
See also
Deploying to a Production Environment
Introduction
Deploying Redis on Linux
Getting ready…
How to do it...
How it works...
There's more...
See also
Securing Redis
Getting ready…
How to do it...
How it works...
There's more...
See also
Setting client connection options
Getting ready…
How to do it...
How it works...
See also
Configuring memory policy
Getting ready…
How to do it...
How it works...
There's more...
See also
Benchmarking
Getting ready…
How to do it...
How it works...
There's more...
See also
Logging
Getting ready…
How to do it...
How it works...
Administrating Redis
Introduction
Managing Redis Server configurations
Getting ready…
How to do it...
How it works...
Operating Redis using bin/redis-cli
Getting ready…
How to do it...
How it works...
There's more...
See also
Backup and restore
Getting ready…
How to do it...
Backing up Redis data
Restoring Redis data from an RDB file
How it works...
Monitoring memory
Getting ready…
How to do it...
How it works...
There is more...
See also
Managing clients
Getting ready…
How to do it...
How it works...
There's more...
See also
Data migration
Getting ready…
How to do it...
How it works...
There's more...
See also
Troubleshooting Redis
Introduction
Health checking in Redis
Getting ready…
How to do it...
How it works...
See also
Identifying slow queries using the SLOWLOG
Getting ready…
How to do it...
How it works...
There's more…
See also
Troubleshooting latency issues
Getting ready…
How to do it...
How it works...
There's more…
See also
Troubleshooting memory issues
Getting ready…
How to do it...
How it works...
There's more…
See also
Troubleshooting crash issues
Getting ready…
How to do it...
How it works...
See also
Extending Redis with Redis Modules
Introduction
Loading a Redis module
Getting ready…
How to do it...
How it works...
See also
Writing a Redis module
Getting ready…
How to do it...
How it works...
There's more...
See also
The Redis Ecosystem
Introduction
The Redisson client
See also
Twemproxy
See also
Codis – a proxy-based high-performance Redis Cluster solution
See also
The CacheCloud Redis management system
See also
Pika – a Redis-compatible NoSQL database
See also
Windows Environment Setup
Other Books You May Enjoy
Leave a review - let other readers know what you think
Redis, as a popular key-value in-memory data store, is gaining more and more attention recently for its high-performance, diversified data types with powerful APIs, high availability, and scalable architecture. It has been successfully ranked at number 9 in the top 10 databases in the DB-Engine Complete Ranking since 2017. Even before that, it had been at the top of the key-value stores ranking provided by DB-Engine for quite a long time. From the early version, 2.x, to the latest version, 4.x, many fantastic features of Redis have been added to help those who want to deliver low-latency services in their business scenarios.
Based on the latest version of Redis 4.x, Redis Cookbook offers you both step-by-step recipes and relevant background information. It covers everything from a basic understanding of Redis data types to advanced aspects of Redis such as high availability, clustering, administration, and troubleshooting.
With the concept of Learning by doing is the best approach in mind, we have spared no effort to deliver the knowledge of Redis through real use cases in writing the recipes in this book. In other words, this book offers out-of-the-box solutions for many common development and maintenance problems. Especially on your personal computer, you will be able to catch the key points of each recipe easily by following the steps in the How to do itsection of every recipe. Furthermore, it's not enough for you to know just how to reach your goal with Redis; explanatory notes of the steps you take within a certain task will be provided in the How it works...section. We also offer essential relevant information and necessary explanations of the internal workings of Redis in the There's more... section. The better you understand how Redis works, the more you can make informed decisions regarding the trade-offs that are involved in engineering. Every recipe is structured in this organized way.
Lastly, we do hope this book gives you a better understanding of Redis and enables you to learn more best practices when you use Redis in your environment.
This book is for developers, architects, and database administrators who want to start using Redis or improve their Redis knowledge. If you want to design high-performant, scalable database solutions using Redis, this book will guide you towards gaining a comprehensive and in-depth understanding with various kinds of practical recipes. DBAs looking for solutions to common problems encountered during their day-to-day tasks while working with Redis will also find this book useful. The book covers all the aspects of using Redis and provides solutions and hints for everyday usage. Some basic understanding of Redis is expected but is not required to get the best out of this book.
Chapter 1, Getting Started with Redis, covers the step-by-step installation of a Redis Server and basic operations, including starting and shutting down the Redis Server, connecting to Redis with redis-cli, and getting server information. The Redis Event Model and Redis protocol are also covered at the end of this chapter.
Chapter 2, Data Types, talks about Redis data types and their common operational API commands. All data types that are supported in Redis 4.x (string, list, hash, Set, Sorted Set, HyperLogLog, and Geo) will be covered in this chapter. Basic Redis key management is also discussed.
Chapter 3, Data Features, covers a few useful Redis features that make data manipulation easier. It first shows how to use bitmaps, sort, and setting key expiration. You will then be introduced to three important features of Redis: pipeline, transaction, and PubSub. At the end of this chapter, we will demonstrate how to write and debug Lua scripts in Redis.
Chapter 4, Developing with Redis, demonstrates how to develop applications with Redis. First, it discusses the usage scenario of Redis and the choice of data types and APIs. Then, it shows examples of developing Java and Python applications with the Redis Client libraries, Jedis and redis-py. Lastly, we will cover examples of using Redis in the Spring Framework and writing a MapReduce/Spark job will also be covered.
Chapter 5, Replication, covers the Redis replication mechanism. It shows how to set up a Redis slave and explains how Redis replication works. Then, topics of optimizing and troubleshooting Redis replication will be discussed.
Chapter 6, Persistence, introduces the two persistence options in Redis: RDB and AOF. It shows how to enable RDB and AOF persistence in Redis and explains how things work behind the scenes. It also talks about the difference between RDB and AOF and how to utilize the combination of these two options.
Chapter 7, Setting Up High Availability and Cluster, covers the high availability of Redis. It demonstrates how to set up Redis Sentinels and Redis Clusters. A few experiments will be shown to explain how Redis Sentinel and Cluster work.
Chapter 8,Deploying to a Production Environment, discusses the things to notice when deploying Redis in a production environment. It first talks about the operation system, network, and security concerns. Then, it touches upon the topics of tweaking configuration and logging Redis. Setting up the LRU policy is also included. Lastly, we will look at Redis benchmarking.
Chapter 9, Administrating Redis, covers various Redis administration tasks, including updating server settings, using redis-cli, backing up and restoring data, managing memory usage, managing client, and data migration.
Chapter 10, Troubleshooting Redis, provides some practical examples on troubleshooting Redis issues. It covers the usage of slow log to identify slow queries and demonstrate common cases of troubleshooting latency, memory, and crash issues.
Chapter 11, Extending Redis with Redis Module, discusses how to extend the Redis functionality with the Redis Module. It talks about how the Redis module works and then demonstrates how to build Redis Modules with Redis Modules SDK.
Chapter 12, The Redis Ecosystem, talks about third-party components for Redis. A few popular tools, clients, and proxies are also briefly introduced.
Appendix, Windows Environment Setup, explains how to configure Windows environment for Redis.
All the examples in this book are to be run on Redis 4.x. A Linux environment is preferred, while macOS X is also supported. If you are using Windows, it is recommended to install and run a Linux OS in VirtualBox or VMWare. You also need JDK 1.8+ and Python 2.7+ or 3.4+ to run the code examples.
You can download the example code files for this book from your account at www.packtpub.com. If you purchased this book elsewhere, you can visit www.packtpub.com/support and register to have the files emailed directly to you.
You can download the code files by following these steps:
Log in or register at
www.packtpub.com
.
Select the
SUPPORT
tab.
Click on
Code Downloads & Errata
.
Enter the name of the book in the
Search
box and follow the onscreen instructions.
Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:
WinRAR/7-Zip for Windows
Zipeg/iZip/UnRarX for Mac
7-Zip/PeaZip for Linux
The code bundle for the book is also hosted on GitHub at https://github.com/PacktPublishing/Redis-4.x-Cookbook. In case there's an update to the code, it will be updated on the existing GitHub repository.
We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!
We also provide a PDF file that has color images of the screenshots/diagrams used in this book. You can download it here: http://www.packtpub.com/sites/default/files/downloads/Redis4xCookbook_ColorImages.pdf.
There are a number of text conventions used throughout this book.
CodeInText: Indicates code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles. Here is an example: "Open a Terminal and connect to Redis with redis-cli."
A block of code is set as follows:
for i in `seq 10` do nohup node generator.js hash 1000000 session:${i} & done
Any command-line input or output is written as follows:
127.0.0.1:6379> SETBIT "users_tried_reservation" 100 1
(integer) 0
Bold: Indicates a new term, an important word, or words that you see onscreen. For example, words in menus or dialog boxes appear in the text like this. Here is an example: "Click Import Project from Sources and select the redis-4.0.1 sub-directory in the coding directory."
In this book, you will find several headings that appear frequently (Getting ready, How to do it..., How it works..., There's more..., and See also).
To give clear instructions on how to complete a recipe, use these sections as follows:
This section tells you what to expect in the recipe and describes how to set up any software or any preliminary settings required for the recipe.
This section contains the steps required to follow the recipe.
This section usually consists of a detailed explanation of what happened in the previous section.
This section consists of additional information about the recipe in order to make you more knowledgeable about the recipe.
This section provides helpful links to other useful information for the recipe.
Feedback from our readers is always welcome.
General feedback: Email [email protected] and mention the book title in the subject of your message. If you have questions about any aspect of this book, please email us at [email protected].
Errata: Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you have found a mistake in this book, we would be grateful if you would report this to us. Please visit www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details.
Piracy: If you come across any illegal copies of our works in any form on the internet, we would be grateful if you would provide us with the location address or website name. Please contact us at [email protected] with a link to the material.
If you are interested in becoming an author: If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, please visit authors.packtpub.com.
Please leave a review. Once you have read and used this book, why not leave a review on the site that you purchased it from? Potential readers can then see and use your unbiased opinion to make purchase decisions, we at Packt can understand what you think about our products, and our authors can see your feedback on their book. Thank you!
For more information about Packt, please visit packtpub.com.
In this chapter, we will cover the following recipes:
Downloading and installing Redis
Starting and shutting down Redis
Connecting to Redis with redis-cli
Getting server information
Understanding the Redis Event Model
Understanding the Redis protocol
Redis is a very popular, memory-based, lightweight key-value database. Strictly speaking, Redis is a data structure server, according to Matt Stancliff (@mattsta), one of the most important contributors to Redis (https://matt.sh/thinking-in-redis-part-one). The author of Redis, Salvatore Sanfilippo (@Antirez), first called it Redis, which stands for REmote DIctionary Server because Redis natively implements various kinds of data structures in memory, and provides a variety of APIs for manipulating these data structures. More importantly, Redis supports high-performance command processing, high availability/scalability architectures, and also data persistence features as a long running data store service.
With the development of high-concurrency and low-latency systems, the use of Redis is becoming more and more widespread. Redis has been ranked in the top ten of the DB-Engine complete ranking (https://db-engines.com/en/ranking) since 2017. Before that, it was ranked at the top of the key-value stores ranking (https://db-engines.com/en/ranking/key-value+store) for quite a long time.
The goal of this chapter is to guide readers to quickly set up a simple Redis instance and learn about some common operations, such as starting, connecting, and shutting down a Redis Server. Retrieving basic information from a Redis Server is also introduced. Moreover, it's essential to have a better understanding of the event model and communication protocol of Redis before learning Redis. The last two sections of this chapter include a detailed discussion of the event model and protocol of Redis.
Redis has an active community on GitHub. Large numbers of pull requests have been made and merged during the years, and the author, Antirez, has always given a timely response on the issues section in GitHub. Hence, the release cycles of Redis are very rapid. From the early versions, 2.6/2.8 to 3.0/3.2, which were widely used, and then to the latest 4.x version, each release of Redis offers some essential enhancements and bug fixes. So using the latest version of Redis, if possible, is one of the best practices. In this book, we are adopting the latest version of Redis 4.0.1.
Redis is an open software written in pure C language so that we can install it by compilation. Major operating systems also include Redis binary packages in their software repository, although the Redis version is often a little out of date.
You can find the download link and basic installation steps at https://redis.io/download. If you would like to build Redis by compiling source code in Linux/Unix/macOS, both the gcc compiler and C Standard Library libc are needed in your environment. When it comes to OS repository installation, all you need are an internet connection and the correct repository configuration.
We will demonstrate the compilation installation of Redis in Ubuntu 16.04.2 LTS (Xenial Xerus). The downloading and building steps are as follows:
Set up building tools:
$ sudo apt-get install build-essential
Create a directory and enter it for Redis:
$ mkdir /redis $ cd /redis
Then, download Redis:
$ wget http://download.redis.io/releases/redis-4.0.1.tar.gz
Untar it and enter the directory:
$ tar zxvf redis-4.0.1.tar.gz $ cd redis-4.0.1
Create a directory for the Redis configuration file and copy the default configuration file into it:
$ mkdir /redis/conf $ cp redis.conf /redis/conf/
Building dependencies:
$ cd deps $ make hiredis lua jemalloc linenoise $ cd ..
Do the compilation:
$ make
If everything goes well, the following message will be shown. It means that the compilation has been done successfully:
It's a good idea to run 'make test' ;) make[1]: Leaving directory '/redis/redis-4.0.1/src'
Install Redis:
$ make PREFIX=/redis install
The following messages represent the success of installation:
Enter the
/redis
directory and verify that the Redis binary files have been generated:
$ ls /redis/bin redis-benchmark redis-check-aof redis-check-rdb redis-cli redis-sentinel redis-server
Congratulations! You have completed the Redis compilation installation.
Compared to the compilation installation, using apt-get in Ubuntu to install Redis is much easier. Let's take a look:
First, update software repository index:
$ sudo apt-get update
And then start the installation:
$ sudo apt-get install redis-server
When it's finished, check if Redis has been set up in your environment:
$ which redis-server
When it comes to the Redis version selection, bear in mind that Redis follows the standard practice of versioning, which is major.minor.patch level. An even-numbered minor stands for a stable release, while an odd-numbered minor means it's an unstable version, although there are a few versions using an odd minor for Redis.
The differences between building Redis by compiling and building via a software repository, are that the former can add optimization or debugging options when compiling, and also own the flexibility of specifying the installation location during installation.
After installation, there are some executable files in the bin directory. Their description and remarks are shown in the following table:
File name
Description
Remarks
redis-server
Redis Server
redis-sentinel
Redis Sentinel
A soft link for redis-server.
redis-cli
Redis Console Tool
redis-check-rdb
Redis RDB Check Tool
redis-check-aof
Redis Append Only Files (AOF) Check Tool
redis-benchmark
Redis Benchmarking Tool
For Windows, you can obtain the Redis release of Windows, which the Microsoft Open Technologies group used to maintain at: https://github.com/MicrosoftArchive/redis/releases.
Just download the .msi executable file and give it a double-click to install, keeping the default configurations.
For macOS, there is no big difference from the procedures in Linux. You can also install Redis by issuing the command, brew install redis on macOS.
For the impact of different compilation options on Redis performance, refer to Matt Stancliff's evaluation of Redis performance for different versions by taking different compilation options:
https://matt.sh/redis-benchmark-compilers
For security concerns, a non-root user should be used for Redis and the
Securing Redis
recipe
in
Chapter 8
,
Deploying to a Production Environment
will have a detailed discussion
You can further refer to
https://github.com/antirez/redis
for more information
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.
You need to finish the installation of the Redis Server, as we described in the Downloading and installing Redis recipe in this chapter.
The steps for starting and shutting down a Redis Server are as follows:
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:
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
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
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:
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`
The more graceful and recommended way to stop Redis is calling the
shutdown
command in
redis-cli
:
$ cd /redis $ bin/redis-cli shutdown
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
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.
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.
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.
In the development and maintenance of Redis, the redis-cli in the bin directory is the most commonly used tool. This section gives a brief description of its usage so that readers can get a brief idea of how to connect to and use Redis with redis-cli.
You need an up-and-running Redis Server, as we described in the Starting and shutting down Redis recipe in this chapter.
The steps for connecting to Redis using redis-cli down a Redis Server are as follows:
Open a Terminal and connect to Redis with
redis-cli
:
$ bin/redis-cli 127.0.0.1:6379>
The pattern of the preceding prompt is IP:port, indicating redis-cli has connected to this Redis instance successfully.
Send some simple commands for testing. More data types and features will be discussed in the following chapters.
First, set two string key-value pairs:
foo value1
,
bar value2
:
127.0.0.1:6379> set foo value1 OK 127.0.0.1:6379> set bar value2 OK
After that, fetch the values we just set:
127.0.0.1:6379> get foo "value1" 127.0.0.1:6379> get bar "value2"
Finally, we terminate the Redis instance by sending the
shutdown
command:
$ bin/redis-cli 127.0.0.1:6379> shutdown not connected>
After shutting down, the Command Prompt changed to
not connected
. Then, we quit from
redis-cli
and make the connection again with
redis-cli
. The following error message will be shown:
not connected>quit $ bin/redis-cli Could not connect to Redis at 127.0.0.1:6379: Connection refused Could not connect to Redis at 127.0.0.1:6379: Connection refused not connected>
By default, redis-cli connects to a Redis instance running on localhost at default port 6379. You can also specify the hostname/IP address the Redis Server is running on with the -h option. Just make sure that the network connectivity between the redis-cli side and the Redis Server side has no problem.
redis-cli allows you to specify the port with the -p option, if your Redis Server is not running on the default port 6379. This option is also useful if you would like to connect to multiple Redis instances with different binding ports on the same host.
Also, if a Redis instance is protected by password, the -a option can be used to set the password when connecting to Redis.
In addition, if a Unix socket file is enabled in Redis, you can connect to the Redis Server simply by using the -s option.
It is often necessary to do some data prototype verification before hooking up your application to Redis. redis-cli is a very useful tool for this. It provides an interactive command-line interface for you to quickly verify your data design. In the daily maintenance of Redis Server, redis-cli also offers a set of commands, including obtaining the metrics, manipulating system states, and performing configuration settings.
Refer to
Chapter 9
,
Administrating Redis
for a more detailed discussion on how to manage a Redis instance with
redis-cli
The most comprehensive and important information about a Redis instance can be obtained with redis-cli with the INFO command. In this section, we will see how to use the INFO command to fetch these essential statistics.
You need an up-and-running Redis Server as we described in the Starting and shutting down Redis recipe in this chapter.
Follow these steps to get server information of Redis:
Connect to a Redis instance and then use the
INFO
command:
$ bin/redis-cli 127.0.0.1:6379> INFO
The result looks as follows:
# Server redis_version:4.0.1 ... # Clients connected_clients:1 ... # Memory used_memory:828352 used_memory_human:808.94K used_memory_rss:9420800 used_memory_rss_human:8.98M ...
