Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases now! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Redis 4.x Cookbook

You're reading from   Redis 4.x Cookbook Over 80 hand-picked recipes for effective Redis development and administration

Arrow left icon
Product type Paperback
Published in Feb 2018
Publisher Packt
ISBN-13 9781783988167
Length 382 pages
Edition 1st Edition
Tools
Arrow right icon
Authors (2):
Arrow left icon
Zuofei Wang Zuofei Wang
Author Profile Icon Zuofei Wang
Zuofei Wang
Pengcheng Huang Pengcheng Huang
Author Profile Icon Pengcheng Huang
Pengcheng Huang
Arrow right icon
View More author details
Toc

Table of Contents (15) Chapters Close

Preface 1. Getting Started with Redis FREE CHAPTER 2. Data Types 3. Data Features 4. Developing with Redis 5. Replication 6. Persistence 7. Setting Up High Availability and Cluster 8. Deploying to a Production Environment 9. Administrating Redis 10. Troubleshooting Redis 11. Extending Redis with Redis Modules 12. The Redis Ecosystem 13. Windows Environment Setup
14. Other Books You May Enjoy

Understanding the Redis protocol

As we described in the previous recipe, Redis is merely a non-blocking, I/O multiplexing TCP server that accepts and processes requests from clients. In other words, in spite of the complexity within the Redis Server, you can talk to Redis over the TCP connection in various languages. The term protocol stands for the language used between a server and a client in the networking communication. As for Redis, REdis Serialization Protocol (RESP) is the protocol. In this recipe, we'll see how the RESP works in Redis.

One thing should be noted: Even at first glance, it's a little bit advanced for a beginner to go through this recipe. However, we do believe that learning the RESP, as basic knowledge, is not that difficult and it will be of benefit to you, in that understanding the various kinds of clients and proxies implemented in different programming languages will no longer be a mystery to you.

Of course, you can skip this recipe if you like and it won't bother you too much when reading the following chapters.

Getting ready…

You need an up-and-running Redis Server, as we described in the Starting and shutting down Redis recipe in this chapter.

The tool, netcat (nc) should be installed. In this recipe, we have the nc command in Ubuntu 16.04.3 LTS for netcat. You can use Cygwin to install netcat if you work under Windows.

How to do it...

To understand the Redis protocol, take the following steps:

  1. Send the PING command to the Redis Server with netcat:

Instead of sending the command PING in redis-cli, let's build the command using RESP:

$ echo -e "*1\r\n\$4\r\nPING\r\n" | nc  127.0.0.1 6379
+PONG

 

  1. Use the SET and INCR commands to set an integer and increase it by one:
$ echo -e "*3\r\n\$3\r\nset\r\n\$5\r\nmykey\r\n\$1\r\n1\r\n" | nc 127.0.0.1 6379
+OK
$ echo -e "*2\r\n\$4\r\nINCR\r\n\$5\r\nmykey\r\n" | nc 127.0.0.1 6379
:2

You may encounter the following error when you send a non-existent command:

$ echo -e "*2\r\n\$3\r\ngot\r\n\$3\r\nfoo\r\n" | nc 127.0.0.1 6379
-ERR unknown command 'got'

Multi-commands can be combined and sent to the Redis Server in a single network transmission:

$ echo -e "*3\r\n\$3\r\nset\r\n\$3\r\nfoo\r\n\$3\r\nbar\r\n*2\r\n\$3\r\nget\r\n\$3\r\nfoo\r\n" | nc 127.0.0.1 6379
+OK $3 bar

How it works...

There is a big chance that you will go through these commands in tremendous confusion. As we stated in the Getting ready section in this recipe, these commands are the language in which the Redis Server and client talk to each other. It's easy for you to learn them in that there are only five types in RESP.

Let's take a look at each command.

First, we sent *1\r\n\$4\r\nPING\r\n to the Redis Server. The command begins with an asterisk indicating this is an arrays type.

Look at the following:

  • 1 stands for the size of this array.
  • \r\n (CRLF) is the terminator of each part in RESP.
  • The backslash before $4 is the escape character for the $ sign. $4 tells you that the following is a bulk string type of four characters long.
  • PING is the string itself.
  • +PONG is the string the PING command returned. The plus sign tells you that it's a simple string type.

The next type we talk about is the integer type. Look at :2, which is the result returned by the INCR command. The colon before the number indicates this is an integer.

Sometimes, the server may return an error type message beginning with a minus when a non-existent command has been processed, such as the got command shown previously.

In addition, for the consideration of performance, you may send multiple commands in a single call to the Redis Server using RESP.

To sum up, the client sends commands to a Redis Server as a RESP array of bulk strings. Then the server replies with one of the five aforementioned RESP types accordingly.

See also

You have been reading a chapter from
Redis 4.x Cookbook
Published in: Feb 2018
Publisher: Packt
ISBN-13: 9781783988167
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime