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 it section 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 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.
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.