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.