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

Hello Redis (command-line interface examples)


Redis comes with several executables. In this section, we are going to focus on redis-server and redis-cli.

redis-server is the actual Redis data store. It can be started in standalone mode or in cluster mode. For now, we are only going to use the single-instance mode and later (in Chapter 9, Redis Cluster and Redis Sentinel (Collective Intelligence)) we will cover cluster mode.

redis-cli is a command-line interface that can perform any Redis command (it is a Redis client). It makes learning to execute commands in Redis more intuitive.

This chapter is also going to introduce a Node.js client, and later (in Chapter 5, Clients for Your Favorite Language (Become a Redis Polyglot)) we will see how to use Redis with PHP, Python, and Ruby clients.

By default, Redis binds to port 6379, runs in standalone mode, and can be started with this line:

$ redis-server

Since no configuration was specified in this example, Redis will use default configurations.

It will output its PID (process ID) and the port that the clients should connect to, which is 6379 by default.

Note

Important note:

The following conventions will be 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").

The next snippet shows how to connect to the Redis server using redis-cli. Once connected, we use the SET command to create a key with a string value and then the GET command to read the key value:

$ redis-cli
127.0.0.1:6379> SET philosopher "socrates"
OK
127.0.0.1:6379> GET philosopher
"socrates"
127.0.0.1:6379>

The HELP command is useful for learning about command syntax. It displays the command parameters with a summary and examples. See the following example:

$ redis-cli
127.0.0.1:6379> HELP SET

  SET key value [EX seconds] [PX milliseconds] [NX|XX]
  summary: Set the string value of a key
  since: 1.0.0
  group: string

The KEYS command is also useful, as it returns all stored keys that match a pattern (it is a glob-style pattern, like the Unix shell glob pattern). In the following code, all stored key names that start with the letter "p" are returned:

$ redis-cli
127.0.0.1:6379> KEYS p*
1) "philosopher"

The redis-cli is a great tool for debugging and testing commands, but making real examples and applications using redis-cli is impractical. This book is going to use the JavaScript language and Node.js to support examples and explanations. We chose JavaScript because of its current popularity. The Node.js website (https://nodejs.org) provides binaries for Mac OS X, Windows, and Linux, which makes installation of Node.js really simple. Keep in mind that this is not a JavaScript book; we are going to use basic features of the language in our examples. If you do not know how to code in JavaScript, do not worry. A quick syntax reference is presented, and it should be enough to understand all the examples in this book.

Note

You can reproduce all the samples presented here in your favorite language. Redis will produce the same results regardless of the programming language.