Book Image

Redis Essentials

Book Image

Redis Essentials

Overview of this book

Redis is the most popular in-memory key-value data store. It's very lightweight and its data types give it an edge over the other competitors. If you need an in-memory database or a high-performance cache system that is simple to use and highly scalable, Redis is what you need. Redis Essentials is a fast-paced guide that teaches the fundamentals on data types, explains how to manage data through commands, and shares experiences from big players in the industry. We start off by explaining the basics of Redis followed by the various data types such as Strings, hashes, lists, and more. Next, Common pitfalls for various scenarios are described, followed by solutions to ensure you do not fall into common traps. After this, major differences between client implementations in PHP, Python, and Ruby are presented. Next, you will learn how to extend Redis with Lua, get to know security techniques such as basic authorization, firewall rules, and SSL encryption, and discover how to use Twemproxy, Redis Sentinel, and Redis Cluster to scale infrastructures horizontally. At the end of this book, you will be able to utilize all the essential features of Redis to optimize your project's performance.
Table of Contents (17 chapters)
Redis Essentials
Credits
About the Authors
Acknowledgments
About the Reviewers
www.PacktPub.com
Preface
5
Clients for Your Favorite Language (Become a Redis Polyglot)
Index

Preface

Redis is the most popular in-memory key-value data store. It is very lightweight and its data types give it an edge over other competitors. If you need an in-memory database or a high-performance cache system that is simple to use and highly scalable, Redis is what you should use.

This book is a fast-paced guide that teaches you the fundamentals of data types, explains how to manage data through commands, and shares experiences from big players in the industry.

What this book covers

Chapter 1, Getting Started (The Baby Steps), shows you how to install Redis and how to use redis-cli, the default Redis command-line interface. It also shows you how to install Node.js and goes through a quick JavaScript syntax reference. The String, List, and Hash data types are covered in detail, along with examples of redis-cli and Node.js.

Chapter 2, Advanced Data Types (Earning a Black Belt), is a continuation of the previous chapter. It presents the Set, Sorted Set, Bitmap, and HyperLogLog data types. All the examples here are implemented with redis-cli and Node.js.

Chapter 3, Time Series (A Collection of Observations), uses all of the knowledge of data types from the previous chapters to build a time series library in Node.js. The examples are incremental; the library is initially implemented using the String data type, and then the solution is improved and optimized by using the Hash data type. Uniqueness support is added to the String and Hash implementations by using the Sorted Set and HyperLogLog data types, respectively.

Chapter 4, Commands (Where the Wild Things Are), introduces Pub/Sub, transactions, and pipelines. It also introduces the scripting mechanism, which uses the Lua programming language to extend Redis. A quick Lua syntax reference is also presented. A great variety of Redis commands are presented in this chapter, including the administration commands and data type commands that were not covered in the previous chapters. This chapter also shows you how to change Redis's configuration to optimize different data types for memory or performance.

Chapter 5, Clients for Your Favorite Language (Become a Redis Polyglot), shows how to use Redis with PHP, Python, and Ruby. This chapter highlights the features that vary more frequently with clients in different languages: blocking commands, transactions, pipelines, and scripting.

Chapter 6, Common Pitfalls (Avoiding Traps), illustrates some common mistakes when using Redis in a production environment and related stories from real-world companies. The pitfalls in this chapter include using the wrong data type for a given problem, using too much swap space, and using inefficient backup strategies.

Chapter 7, Security Techniques (Guard Your Data), shows how to set up basic security with Redis, disable and obfuscate commands, protect Redis with firewall rules, and use client-to-server SSL encryption with stunnel.

Chapter 8, Scaling Redis (Beyond a Single Instance), introduces RDB and AOF persistence, replication via Redis slaves, and different methods of partitioning data across different hosts. This chapter also shows how to use twemproxy to distribute Redis data across different instances transparently.

Chapter 9, Redis Cluster and Redis Sentinel (Collective Intelligence), demonstrates the differences between Redis Cluster and Redis Sentinel, their goals, and how they fit into the CAP theorem. It also shows how to set up both Sentinel and Cluster, their configurations, and what happens in different failure scenarios. Redis Cluster is covered in more detail, since it is more complex and has different tools for managing a cluster of instances. Cluster administration is explained via native Redis commands and the redis-trib tool.

What you need for this book

The examples in this book assume that you have a computer with GNU/Linux or Mac OS X. We also assume that the following are installed:

  • Node.js 0.12.4

  • NPM

  • Redis 3.X

The following requirements are optional when using other Redis clients:

  • Python 2.7

  • pip

  • Ruby 1.9+

  • RubyGems

  • PHP 5.5+

  • Composer

Who this book is for

This book is intended for those with or without previous experience who want to learn about Redis. Using examples of real-world applications, this book shows problems solved by companies who have been using Redis for years.

Providing a foundation for an understanding of the capabilities of Redis, this book will teach you how to extend and scale Redis in real-life situations.

Conventions

In this book, you will find a number of text styles that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning.

A block of code is set as follows:

var redis = require("redis"); // 1
var client = redis.createClient(); // 2

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

var redis = require("redis"); // 1
var client = redis.createClient(); // 2
console.log("Redis Essentials"); // 3

Please note that all the code snippets in this book will have inline comments with numbers. After the code is presented, it will be explained by referencing those numbers.

Each command line starts with a dollar sign ($):

$ redis-server

The following conventions are used in this book for redis-cli:

  • Commands are written in bold uppercase letters (SET).

  • Keys are written in italicized lowercase letters (GET mykey).

  • Values are written without any text formatting (SET mykey "my value").

    $ redis-cli
    127.0.0.1:6379> SET mykey "my value"

In this book, all filenames, function names, and variable names are written in italics. Examples:

  • Create a file called my-filename.js.

  • Execute the function myFunctionName.

  • Create a variable called myVariableName.

All data types will be shown with the first letter capitalized (for example, Strings, Lists, Bitmaps, Sets, Sorted Sets, and HyperLogLogs) so that we can distinguish between a Redis data type and another existing term.

Note

Warnings or important notes appear in a box like this.

Tip

Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let's know what you think about this book—what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of.

To send us general feedback, simply e-mail , and mention the book's title in the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files from your account at http://www.packtpub.com for all the Packt Publishing books you have purchased. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

All the code files from this book can be downloaded from GitHub as well: https://github.com/redis-essentials/book.

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title.

To view the previously submitted errata, go to https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata section.

Piracy

Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at with a link to the suspected pirated material.

We appreciate your help in protecting our authors and our ability to bring you valuable content.

Questions

If you have a problem with any aspect of this book, you can contact us at , and we will do our best to address the problem.