├── README.md └── spec.md /README.md: -------------------------------------------------------------------------------- 1 | This is the repository for the version 3 of the RESP protocol. 2 | RESP (REdis Serialization Protocol) is the protocol used in the [Redis](http://github.com/antirez/redis) database, however the protocol is designed to be used by other projects. With the version 3 of the protocol, currently a work in progress design, the protocol aims to become even more generally useful to other systems that want to implement a protocol which is simple, efficient, and with a very large landscape of client libraries implementations. 3 | 4 | Here you can find the [current RESP3 specification](spec.md). 5 | 6 | In the future this repository may add additional resources, such as example implementations of RESP or other resources. 7 | -------------------------------------------------------------------------------- /spec.md: -------------------------------------------------------------------------------- 1 | # RESP3 specification 2 | 3 | Versions history: 4 | * 1.0, 2 May 2018, Initial draft to get community feedbacks. 5 | * 1.1, 5 Nov 2018, Leave CRLF as terminators + improved "hello reply" section. 6 | * 1.2, 5 Nov 2018, A few things are now better specified in the document 7 | thanks to developers reading the specification and 8 | sending their feedbacks. No actual change to the protocol 9 | was made. 10 | * 1.3, 11 Mar 2019, Streamed strings and streamed aggregated types. 11 | 12 | ## Background 13 | 14 | The Redis protocol has served us well in the past years, showing that, if carefully designed, a simple human readable protocol is not the bottleneck in the client server communication, and that the simplicity of the design is a major advantage in creating a healthy client libraries ecosystem. 15 | 16 | Yet the Redis experience has shown that after about six years from its introduction (when it replaced the initial Redis protocol), the current RESP protocol could be improved, especially in order to make client implementations simpler and to support new features. 17 | 18 | At the same time the basic structure is tested and sounding, and there are several things to save from the old design: 19 | 20 | * RESP is human readable. This confers to the protocol the important quality of being simple to observe and debug. 21 | * Despite being human readable, RESP is no slower than a binary protocol with fixed length fields, and in many cases can be more compact. Often Redis commands have few arguments composed of few bytes each. Similarly many replies are short. The protocol prefixed lengths represented as decimal digits will save space on the wire, compared to 64 bit integers, and will not be slower to parse. It is possible to design a more efficient binary protocol only introducing the complexity of a variable length encoding, defeating the goal of simplicity. 22 | * The design is very simple: this makes the specification and the resulting implementations short and easy to understand. 23 | 24 | At the same time, RESP has flaws. One of the most obvious is the fact that it has not enough semantic power to allow the client to implicitly understand what kind of conversion is appropriate. For instance the Redis commands `LRANGE`, `SMEMBERS` and `HGETALL` will all reply an array, called in RESP v2 terms a *multi bulk reply*. However the three commands actually return an array, a set and a map. 25 | Currently, from the point of view of the client, the conversion of the reply to the programming language type is command-dependent. Clients need to remember what command was called in order to turn the Redis reply into a reply object of some kind. What is worse is that clients need to know about each command, or alternatively provide a lower level interface letting users select the appropriate conversion. 26 | 27 | Similarly RESP lacks important data types: floating point numbers and boolean values were returned respectively as strings and integers. Null values had a double representation, called *null bulk* and *null multi bulk*, which is useless, since the semantic value of telling apart null arrays from null strings is non existent. 28 | 29 | Finally there was no way to return binary safe errors. When implementing generic APIs producing the protocol, implementations must check and remove potential newlines from the error string. 30 | 31 | The limitations stated so far are the main motivations for a new version of RESP. However the redesign gave us a chance to consider different other improvements that may be worthwhile and make the protocol both more powerful and less dependent on the implicit state of the connection. 32 | 33 | The gist of such additional features are to provide the protocol with the ability to support a more generic *push mode* compared to the Pub/Sub mode used by Redis, which is not really built-in in the protocol, but is an agreement between the server and the client about the fact that the connection will consume replies as they arrive. Moreover it was useful to modify the protocol to return *out of band* data, such as attributes that augment the reply. Also the protocol was sometimes abused by the internals of Redis, like for example in the case of *replicaless replication*, in order to support streaming of large strings whose length is initially not known. This specification makes this special mode part of the RESP protocol itself. 34 | 35 | This specification describes RESP3 from scratch, and not just as a change from RESP v2. However differences between the two will be noted while describing the new protocol. 36 | 37 | ## Why existing serialization protocols are not good enough? 38 | 39 | In theory instead of improving RESP v2, we could use an existing serialization 40 | protocol like MessagePack, BSON or others, which are widely available 41 | and implemented. This is a viable approach, and could be a potential solution, 42 | however there are certain design goals that RESP has that are not aligned 43 | with using such serialization formats. 44 | 45 | The first and most important is the fact that such serialization protocols 46 | are not specifically designed for a request-response server-client chat, so 47 | clients and servers would be required to agree on an additional protocol on 48 | top of the underlying serialization protocol. Basically it is not possible 49 | to escape the problem of designing a Redis specific protocol, so 50 | to bind together the serialization and the semantic looks a more effective 51 | way to reach the goals of RESP3. 52 | 53 | A different problem is the fact that such serialization protocols are more 54 | complex than RESP, so client libraries would have to rely on a separated 55 | library implementing the serialization protocol. The library may not have 56 | support for streaming directly to a socket, and may return a buffer instead, 57 | leading to potential inefficiencies. Relying on a library in order to perform 58 | the serialization has the other disadvantage of having more moving parts 59 | where things may go wrong, or where different versions of Redis and 60 | serialization libraries may not work well together. 61 | 62 | Finally certain features like transferring large strings with initially 63 | unknown length, or streaming of arrays, two features that RESP3 supports 64 | and that this specification describes, are not easy to found among 65 | existing serialization protocols. 66 | 67 | In short this specification was written believing that designing a good 68 | serialization format is different compared to designing a protocol 69 | specifically suited in order to support the chat between a client and 70 | its server. RESP3 aims to create a protocol which is not just suitable 71 | for Redis, but more broadly to solve the general problem of client-server 72 | communication in many scenarios even outside Redis and outside the 73 | database space. 74 | 75 | ## Conventions used in this document 76 | 77 | In order to illustrate the RESP3 protocol this specification will show 78 | fragments of the protocol in many sections. In addition of using the escaped 79 | string format, like "foo\r\n", we'll use a more readable format where 80 | "\r\n" will be presented as `` followed by an actual newline. Other 81 | special characters will be displayed as `<\xff>`, where `ff` is the 82 | hex code of the byte. 83 | 84 | So for instance the string `"*1\r\n$1\r\nA\r\n"` representing a RESP3 array with 85 | a single string `"A"` as unique element, will be presented like this: 86 | 87 | *1 88 | $1 89 | A 90 | 91 | However for the first part of this specification, both this format and 92 | the escaped string format will be used in order to make sure there is 93 | no confusion. In the latter sections however only one or the other 94 | will be used. 95 | 96 | When nested aggregate data structures are shown, indentation is used in 97 | order to make it more clear the actual structure of the protocol. For 98 | instance instead of writing: 99 | 100 | *2 101 | *2 102 | :1 103 | :2 104 | #t 105 | 106 | We'll write: 107 | 108 | *2 109 | *2 110 | :1 111 | :2 112 | #t 113 | 114 | Both the indentation and the newlines are hence only used in order to improve 115 | human readability and are not semantical in respect of the actual protocol. 116 | 117 | ## RESP3 overview 118 | 119 | RESP3 is an updated version of RESP v2, which is the protocol used in Redis 120 | starting with roughly version 2.0 (1.2 already supported it, but Redis 2.0 121 | was the first version to talk only this protocol). The name of this protocol 122 | is just **RESP3** and not RESP v3 or RESP 3.0. 123 | 124 | The protocol is designed to handle request-response chats between clients 125 | and servers, where the client performs some kind of request, and the server 126 | replies with some data. The protocol is especially suitable for databases due 127 | to its ability to return complex data types and associated information to 128 | augment the returned data (for instance the popularity index of a given 129 | information). 130 | 131 | The RESP3 protocol can be used asymmetrically, as it is in Redis: only a subset 132 | can be sent by the client to the server, while the server can return the full set 133 | of types available. This is due to the fact that RESP is designed to send non 134 | structured commands like `SET mykey somevalue` or `SADD myset a b c d`. Such 135 | commands can be represented as arrays, where each argument is an array element, 136 | so this is the only type the client needs to send to a server. However different 137 | applications willing to use RESP3 for other goals may just allow the protocol 138 | to be used in a "full duplex" fashion where both the ends can use the full set 139 | of types available. 140 | 141 | Not all parts of RESP3 are mandatory for clients and servers. In the specific 142 | case of Redis, RESP3 describes certain functionalities that will be useful 143 | in the future and likely will not be initially implemented. Other optional parts 144 | of RESP3 may be implemented by Redis only in specific situations, like the 145 | link between the primary database and its replicas, or client connections in 146 | a specific state. 147 | 148 | ## RESP3 types 149 | 150 | RESP3 abandons the confusing wording of the second version of RESP, and uses 151 | a much simpler to understand name for types, so you'll see no mention of 152 | *bulk reply* or *multi bulk reply* in this document. 153 | 154 | The following are the types implemented by RESP3: 155 | 156 | **Types equivalent to RESP version 2** 157 | 158 | * Array: an ordered collection of N other types 159 | * Blob string: binary safe strings 160 | * Simple string: a space efficient non binary safe string 161 | * Simple error: a space efficient non binary safe error code and message 162 | * Number: an integer in the signed 64 bit range 163 | 164 | **Types introduced by RESP3** 165 | 166 | * Null: a single null value replacing RESP v2 `*-1` and `$-1` null values. 167 | * Double: a floating point number 168 | * Boolean: true or false 169 | * Blob error: binary safe error code and message. 170 | * Verbatim string: a binary safe string that should be displayed to humans without any escaping or filtering. For instance the output of `LATENCY DOCTOR` in Redis. 171 | * Map: an ordered collection of key-value pairs. Keys and values can be any other RESP3 type. 172 | * Set: an unordered collection of N other types. 173 | * Attribute: Like the Map type, but the client should keep reading the reply ignoring the attribute type, and return it to the client as additional information. 174 | * Push: Out of band data. The format is like the Array type, but the client should just check the first string element, stating the type of the out of band data, a call a callback if there is one registered for this specific type of push information. Push types are not related to replies, since they are information that the server may push at any time in the connection, so the client should keep reading if it is reading the reply of a command. 175 | * Hello: Like the Map type, but is sent only when the connection between the client and the server is established, in order to welcome the client with different information like the name of the server, its version, and so forth. 176 | * Big number: a large number non representable by the Number type 177 | 178 | ## Simple types 179 | 180 | This section describes all the RESP3 types which are not aggregate types. 181 | They consist of just a single typed element. 182 | 183 | **Blob string** 184 | 185 | The general form is `$\r\n\r\n`. It is basically exactly like 186 | in the previous version of RESP. 187 | 188 | The string `"hello world"` is represented by the following protocol: 189 | 190 | $11 191 | helloworld 192 | 193 | Or as an escaped string: 194 | 195 | "$11\r\nhelloworld\r\n" 196 | 197 | The length field is limited to the range of an unsigned 64 bit 198 | integer. Zero is a valid length, so the empty string is represented by: 199 | 200 | "$0\r\n\r\n" 201 | 202 | **Simple string** 203 | 204 | The general form is `+\r\n`, so "hello world" is encoded as 205 | 206 | +hello world 207 | 208 | Or as an escaped string: 209 | 210 | "+hello world\r\n" 211 | 212 | Simple strings cannot contain the `` nor the `` characters inside. 213 | 214 | **Simple error** 215 | 216 | This is exactly like a simple string, but the initial byte is `-` instead 217 | of `+`: 218 | 219 | -ERR this is the error description 220 | 221 | Or as an escaped string: 222 | 223 | "-ERR this is the error description\r\n" 224 | 225 | The first word in the error is in upper case and describes the error 226 | code. The remaining string is the error message itself. 227 | The `ERR` error code is the generic one. The error code is useful for 228 | clients to distinguish among different error conditions without having 229 | to do pattern matching in the error message, that may change. 230 | 231 | **Number** 232 | 233 | The general form is `:\r\n`, so the number 1234 is encoded as 234 | 235 | :1234 236 | 237 | Or as an escaped string: 238 | 239 | ":1234\r\n" 240 | 241 | Valid numbers are in the range of the signed 64 bit integer. 242 | Larger numbers should use the Big Number type instead. 243 | 244 | **Null** 245 | 246 | The null type is encoded just as `_\r\n`, which is just the underscore 247 | character followed by the `CR` and `LF` characters. 248 | 249 | **Double** 250 | 251 | The general form is `,\r\n`. For instance 1.23 is 252 | encoded as: 253 | 254 | ,1.23 255 | 256 | Or as an escaped string: 257 | 258 | ",1.23\r\n" 259 | 260 | To just start with `.` assuming an initial zero is invalid. 261 | Exponential format is invalid. 262 | To completely miss the decimal part, that is, the point followed by other 263 | digits, is valid, so the number 10 may be returned both using the number 264 | or double format: 265 | 266 | ":10\r\n" 267 | ",10\r\n" 268 | 269 | However the client library should return a floating point number in one 270 | case and an integer in the other case, if the programming language in which 271 | the client is implemented has a clear distinction between the two types. 272 | 273 | In addition the double reply may return positive or negative infinity 274 | as the following two stings: 275 | 276 | ",inf\r\n" 277 | ",-inf\r\n" 278 | 279 | So client implementations should be able to handle this correctly. 280 | 281 | **Boolean** 282 | 283 | True and false values are just represented using `#t\r\n` and `#f\r\n` 284 | sequences. Client libraries implemented in programming languages without 285 | the boolean type should return to the client the canonical values used 286 | to represent true and false in such languages. For instance a C program 287 | should likely return an integer type with a value of 0 or 1. 288 | 289 | **Blob error** 290 | 291 | The general form is `!\r\n\r\n`. It is exactly like the String 292 | type. However like the Simple error type, the first uppercase word represents 293 | the error code. 294 | 295 | The error `"SYNTAX invalid syntax"` is represented by the following protocol: 296 | 297 | !21 298 | SYNTAX invalid syntax 299 | 300 | Or as an escaped string: 301 | 302 | "!21\r\nSYNTAX invalid syntax\r\n" 303 | 304 | **Verbatim string** 305 | 306 | This is exactly like the Blob string type, but the initial byte is `=` instead 307 | of `$`. Moreover the first three bytes provide information about the format 308 | of the following string, which can be `txt` for plain text, or `mkd` for 309 | markdown. The fourth byte is always `:`. Then the real string follows. 310 | 311 | For instance this is a valid verbatim string: 312 | 313 | =15 314 | txt:Some string 315 | 316 | Normal client libraries may ignore completely the difference between this 317 | type and the String type, and return a string in both cases. However interactive 318 | clients such as command line interfaces (for instance `redis-cli`), knows that 319 | the output must be presented to the human user as it is, without quoting 320 | the string. 321 | 322 | For example the Redis command `LATENCY DOCTOR` outputs a report that includes 323 | newlines. It's basically a plain text document. However currently `redis-cli` 324 | requires special handling to avoid quoting the resulting string as it normally 325 | does when a string is received. 326 | 327 | From the `redis-cli` source code: 328 | 329 | ```c 330 | output_raw = 0; 331 | if (!strcasecmp(command,"info") || 332 | ... [snip] ... 333 | (argc == 3 && !strcasecmp(command,"latency") && 334 | !strcasecmp(argv[1],"graph")) || 335 | (argc == 2 && !strcasecmp(command,"latency") && 336 | !strcasecmp(argv[1],"doctor"))) 337 | { 338 | output_raw = 1; 339 | } 340 | ``` 341 | 342 | With the introduction of verbatim strings clients can be simplified not 343 | having to remember if the output must be escaped or not. 344 | 345 | **Big number** 346 | 347 | This type represents very large numbers that are out of the range of 348 | the signed 64 bit numbers that can be represented by the Number type. 349 | 350 | The general form is `(\r\n`, like in the following example: 351 | 352 | (3492890328409238509324850943850943825024385 353 | 354 | Or as an escaped string: 355 | 356 | "(3492890328409238509324850943850943825024385\r\n" 357 | 358 | Big numbers can be positive or negative, but they must not include a 359 | decimal part. Client libraries written in languages with support for big 360 | numbers should just return a big number. When big numbers are not available 361 | the client should return a string, signaling however that the reply is 362 | a big integer when possible (it depends on the API used by the client 363 | library). 364 | 365 | ## Aggregate data types overview 366 | 367 | The types described so far are simple types that just define a single 368 | item of a given type. However the core of RESP3 is the ability to represent 369 | different kinds of aggregate data types having different semantic meaning, 370 | both from the type perspective, and from the protocol perspective. 371 | 372 | In general an aggregate type has a given format stating what is the type 373 | of the aggregate, and how many elements there are inside the aggregate. 374 | Then the single elements follow. Elements of the aggregate type can be, in turn, 375 | other aggregate types, so it is possible to have an array of arrays, or 376 | a map of sets, and so forth. Normally Redis commands will use just a subset 377 | of the possibilities. However with Lua scripts or using Redis modules any 378 | combination is possible. From the point of view of the client library however 379 | this is not complex to handle: every type fully specifies how the client 380 | should translate it to report it to the user, so all the aggregated data types 381 | are implemented as recursive functions that then read N other types. 382 | 383 | The format for every aggregate type in RESP3 is always the same: 384 | 385 | ... numelements other types ... 386 | 387 | The aggregate type char for the Array is `*`, so to represent an array 388 | of three Numbers 1, 2, 3, the following protocol will be emitted: 389 | 390 | *3 391 | :1 392 | :2 393 | :3 394 | 395 | Or as an escaped string: 396 | 397 | "*3\r\n:1\r\n:2\r\n:3\r\n" 398 | 399 | Of course an array can also contain other nested arrays: 400 | 401 | *2 402 | *3 403 | :1 404 | $5 405 | hello 406 | :2 407 | #f 408 | 409 | The above represents the array `[[1,"hello",2],false]` 410 | 411 | Client libraries should return the arrays with a sensible type representing 412 | ordered sequences of elements, accessible at random indexes in constant 413 | or logarithmic time. For instance a Ruby client should return a 414 | *Ruby array* type, while Python should use a *Python list*, and so forth. 415 | 416 | ## Map type 417 | 418 | Maps are represented exactly as arrays, but instead of using the `*` 419 | byte, the encoded value starts with a `%` byte. Moreover the number of 420 | following elements must be even. Maps represent a sequence of field-value 421 | items, basically what we could call a dictionary data structure, or in 422 | other terms, an hash. 423 | 424 | For instance the dictionary represented in JSON by: 425 | 426 | { 427 | "first":1, 428 | "second":2 429 | } 430 | 431 | Is represented in RESP3 as: 432 | 433 | %2 434 | +first 435 | :1 436 | +second 437 | :2 438 | 439 | Note that after the `%` character, what follows is not, like in the array, 440 | the number of single items, but the number of field-value pairs. 441 | 442 | Maps can have any other type as field and value, however Redis will use 443 | only a subset of the available possibilities. For instance it is very unlikely 444 | that Redis commands would return an Array as a key, however Lua scripts 445 | and modules will likely be able to do so. 446 | 447 | Client libraries should return Maps using the idiomatic dictionary type 448 | available. However low level languages like C will likely still return 449 | an array of items, but with type information so that the user can tell 450 | the reply is actually a dictionary. 451 | 452 | ## Set reply 453 | 454 | Sets are exactly like the Array type, but the first byte is `~` instead 455 | of `*`: 456 | 457 | ~5 458 | +orange 459 | +apple 460 | #t 461 | :100 462 | :999 463 | 464 | However they are semantically different because the represented 465 | items are *unordered* collections of elements, so the client library should 466 | return a type that, while not necessarily ordered, has a *test for existence* 467 | operation running in constant or logarithmic time. 468 | 469 | Since many programming languages lack a native set type, a sensible 470 | choice is to return an Hash where the fields are the elements inside the 471 | Set type, and the values are just *true* values or any other value. 472 | 473 | In lower level programming languages such as C, the type should be still 474 | reported as a linear array, together with type information to signal the 475 | user it is a Set type. 476 | 477 | Normally Set replies should not contain the same element emitted multiple 478 | times, but the protocol does not enforce that: client libraries should try 479 | to handle such case, and in case of repeated elements, do some effort to 480 | avoid returning duplicated data, at least if some form of hash is used in 481 | order to return the reply. Otherwise when returning an array just reading 482 | what the protocol contains, duplicated items if present could be passed 483 | by client libraries to the caller. Many implementations will find it very 484 | natural to avoid duplicates. For instance they'll try to add every read 485 | element in some Map or Hash or Set data type, and adding the same element 486 | again will either replace the old copy or will fail silently, retaining the 487 | old copy. 488 | 489 | ## Attribute type 490 | 491 | The attribute type is exactly like the Map type, but instead of the `%` 492 | first byte, the `|` byte is used. Attributes describe a dictionary exactly 493 | like the Map type, however the client should not consider such a dictionary 494 | part of the reply, but *just auxiliary data* that is used in order to 495 | augment the reply. 496 | 497 | For instance newer versions of Redis may include the ability to report, for 498 | every executed command, the popularity of keys. So the reply to the command 499 | `MGET a b` may be the following: 500 | 501 | |1 502 | +key-popularity 503 | %2 504 | $1 505 | a 506 | ,0.1923 507 | $1 508 | b 509 | ,0.0012 510 | *2 511 | :2039123 512 | :9543892 513 | 514 | The actual reply to `MGET` was just the two items array `[2039123,9543892]`, 515 | however the attributes specify the popularity (frequency of requests) of the 516 | keys mentioned in the original command, as floating point numbers from 0 517 | to 1 (at least in the example, the actual Redis implementation may differ). 518 | 519 | When a client reads a reply and encounters an attribute type, it should read 520 | the attribute, and continue reading the reply. The attribute reply should 521 | be accumulated separately, and the user should have a way to access such 522 | attributes. For instance, if we imagine a session in an higher level language, 523 | something like that could happen: 524 | 525 | > r = Redis.new 526 | # 527 | 528 | > r.mget("a","b") 529 | # 530 | 531 | > r 532 | [2039123,9543892] 533 | 534 | > r.attribs 535 | {:key-popularity => {:a => 0.1923, :b => 0.0012}} 536 | 537 | Attributes can appear anywhere before a valid part of the protocol identifying 538 | a given type, and will inform only the part of the reply that immediately 539 | follows, like in the following example: 540 | 541 | *3 542 | :1 543 | :2 544 | |1 545 | +ttl 546 | :3600 547 | :3 548 | 549 | In the above example the third element of the array has an associated 550 | auxiliary information of `{ttl:3600}`. Note that it's not up to the client 551 | library to interpret the attributes, they'll just be passed to the caller 552 | in a sensible way. 553 | 554 | ## Push type 555 | 556 | A push connection is one where the usual *request-response* mode of the 557 | protocol is no longer true, and the server may send to the client asynchronous 558 | data which was not explicitly requested. 559 | 560 | In Redis there is already the concept of a connection pushing data in at least 561 | three different parts of the Redis protocol: 562 | 563 | 1. Pub/Sub is a push-mode connection, where clients receive published data. 564 | 2. The `MONITOR` command implements an *ad-hoc* push mode with a kinda unspecified protocol which is obvious to parse, but still, unspecified. 565 | 3. The Master-Replica link may, at a first glance, be considered a push mode connection. However actually in this case the client (which is the replica), even if is the entity starting the connection, will configure the connection like if the master is a client sending commands, so in practical terms, it is unfair to call this a push mode connection. 566 | 567 | Let's ignore the master-replica link since it is an internal protocol, and 568 | as already noted, is an edge case, and focus on the Pub/Sub and `MONITOR` 569 | modes. They have a common problem: 570 | 571 | 1. The fact that the connection is in push mode is a private *state* of the connection. Otherwise the data we get from Pub/Sub do not contain anything that at the protocol level to make them distinguishable from other replies. 572 | 2. The connection can only be used for Pub/Sub or `MONITOR` once setup in this way, because there is no way (because of the previous problem) in order to tell apart replies from commands and push data. 573 | 574 | Moreover a connection for Pub/Sub cannot be used also for `MONITOR` or any other kind of push notifications. For this reasons RESP3 introduces an explicit 575 | push data type, attempting to solve the above issues. 576 | 577 | RESP3 push data is represented from the point of view of the protocol exactly 578 | like the Array type. However the first byte is `>` instead of `*`, and the 579 | first element of the array is always a String item, representing the kind 580 | of push data the server is sending to the client. All the other fields in the 581 | push array are type dependent, which means that depending on the type string 582 | as first argument, the remaining items will be interpreted following different 583 | conventions. The existing push data in RESP version 2 will be represented 584 | in RESP3 by the push types `pubsub` and `monitor`. 585 | 586 | This is an example of push data: 587 | 588 | >4 589 | +pubsub 590 | +message 591 | +somechannel 592 | +this is the message 593 | 594 | *Note that the above format uses simple strings for simplicity, the 595 | actual Redis implementation would use blob strings instead* 596 | 597 | In the above example the push data type is `pubsub`. For this type, if 598 | the next element is `message` we know that it's a Pub/Sub message (other 599 | sub types may be subscribe, unsubscribe, and so forth), which is followed 600 | by the channel name and the message itself. 601 | 602 | Push data may be interleaved with any protocol data, but always at the top 603 | level, so the client will never find push data in the middle of a Map reply 604 | for instance. 605 | 606 | Clients should normally react to the publication of a push data by invoking 607 | a callback associated with the push data type. Synchronous clients may 608 | instead enter a loop and return every new message or command reply. 609 | 610 | Note that in this mode it is possible to get both replies and push messages 611 | at the same time, interleaved in any way, however the order of the commands 612 | and their replies is not affected: if a command is called, the next reply 613 | (that is not a push reply) received will be the one relative of this command, 614 | and so forth, even if it is possible that there will be push data items to 615 | consume before. 616 | 617 | For instance after a `GET key` command, it is possible to get the two following 618 | valid replies: 619 | 620 | >4 621 | +pubsub 622 | +message 623 | +somechannel 624 | +this is the message 625 | $9 626 | Get-Reply 627 | 628 | Or in inverse order: 629 | 630 | $9 631 | Get-Reply 632 | >4 633 | +pubsub 634 | +message 635 | +somechannel 636 | +this is the message 637 | 638 | Still the client will know that the first non push type reply processed 639 | will be the actual reply to GET. 640 | 641 | However synchronous clients may of course miss for a long time that there is 642 | something to read in the socket, because they only read after a command is 643 | executed, so the client library should still allow the user to specify if the 644 | connection should be monitored for new messages in some way (usually by 645 | entering some loop) or not. For asynchronous clients the implementation is a 646 | lot more obvious. 647 | 648 | ## Streamed strings 649 | 650 | Normally RESP strings have a prefixed length: 651 | 652 | $1234 653 | .... 1234 bytes of data here ... 654 | 655 | Unfortunately this is not always optimal. 656 | 657 | Sometimes it is very useful to transfer a large string from the server 658 | to the client, or the other way around, without knowing in advance the 659 | size of such string. Redis already uses this feature internally, however it 660 | is a private extension of the protocol in the case of RESP2. For RESP3 661 | we want it to be part of the specification, because we found other uses 662 | for the feature where it is crucial that the client has support for it. 663 | 664 | For instance in diskless replication the Redis master sends the RDB file 665 | for the first synchronization to its replica without generating the file 666 | to disk: instead the output of the RDB file, that is incrementally generated 667 | from the keyspace data in memory, is directly sent to the socket. In this 668 | case we don't have any way to know in advance the final length of the 669 | string we are transferring. 670 | 671 | The protocol we used internally was something like that: 672 | 673 | $EOF:<40 bytes marker> 674 | ... any number of bytes of data here not containing the marker ... 675 | <40 bytes marker> 676 | 677 | As we already specified, this was just a *private extension* only known 678 | by the server itself. It uses an EOF marker that is generated in a pseudo 679 | random way, and is practically impossible to collide with normal data. However 680 | such approach, we found, have certain limits when extended to be a known, 681 | well documented mechanism that Redis can use when talking with other clients. 682 | We were worried expecially by the following issues: 683 | 684 | 1. Generating the EOF: failing at that makes the protocol very fragile, and often even experienced developers don't know much about probability, counting, and randomness. 685 | 2. Parsing the EOF: while not so hard, is non trivial. The client need to implement an algorithm that can detect the EOF even while reading it in two separated calls. 686 | 687 | For this reason instead the final version of this specification proposes 688 | a chunked encoding approach, that is often used in order protocols. 689 | 690 | The protocol can be easily explained by a small example, in which the 691 | string "Hello world" is transmitted without knowing its size in advance: 692 | 693 | $? 694 | ;4 695 | Hell 696 | ;5 697 | o wor 698 | ;1 699 | d 700 | ;0 701 | 702 | Basically the transfer starts with `$?`. We use the same prefix as normal 703 | strings, that is `$`, but later instead of the count we use a question mark 704 | in order to communicate the client that this is a chunked encoding transfer, 705 | and we don't know the final size yet. 706 | 707 | Later the differnet parts are transferred like that: 708 | 709 | ; 710 | ... coun bytes of data ... 711 | 712 | The transferring program can send as many parts as it likes, there are no 713 | limits. Finally in order to signal that the transfer has ended, a part 714 | with length zero, without any data, is transferred: 715 | 716 | ;0 717 | 718 | Note that right now the Redis server does not implement such protocol, that 719 | is, there is no command that will reply like that, however it is likely that 720 | we'll soon implement this ability at least for modules. However it is currently 721 | not planned to have support to send streamed strings to the server, as 722 | part of a command. 723 | 724 | ## Streamed aggregated data types 725 | 726 | Sometimes it is useful to send an aggregated data type whose size is not 727 | known in advance. Imagine a search application written as a Redis module 728 | that collects the data it finds in the inverted index using multiple threads 729 | and, as it finds results, it sends such results to the client. 730 | 731 | In this, and many other situations, the size of the result set is not known 732 | in advance. So far the only possibility to solve the issue has been to 733 | buffer the result in memory, and at the end send everything, because the 734 | old RESP2 protocol had no mechanism in order to send aggregated data types 735 | without specifying the final number of elements immediately. 736 | 737 | For instance the Array type is like that: 738 | 739 | *123 (number of items) 740 | :1 (items...) 741 | :2 742 | ... 743 | 744 | RESP3 extends this mechanism, allowing to send all the aggregated data 745 | types of type Array, Set and Map, not specifying the length, but instead 746 | using an explicit terminator. The transfer is initiated like that 747 | (in the case of the Array type): 748 | 749 | *? 750 | 751 | So instead of the length, we just use a '?' character. Then we can 752 | continue to reply with other RESP3 types: 753 | 754 | :1 755 | :2 756 | :3 757 | 758 | Finally we can terminate the Array using a special **END type**, that 759 | has the following format: 760 | 761 | . 762 | 763 | Unbound Sets are exactly like Arrays with the difference that the transfer 764 | wills tart with `~` instead of `*` character. However with the Map type 765 | things are marginally different: the program emitting the protocol *must* 766 | make sure that it emits an even number of elements, since every couple 767 | represents a field-value pair: 768 | 769 | %? 770 | +a 771 | :1 772 | +b 773 | :2 774 | . 775 | 776 | Currently there is no Redis 6 command that uses such extension to the protocol, 777 | however it is possible that modules running in Redis 6 will use such feature 778 | so it is suggested for client libraries to implement this part of 779 | the specification ASAP, and if this is not the case, to clearly document that 780 | this part of RESP3 is not supported. 781 | 782 | ## The HELLO command and connection handshake 783 | 784 | RESP connections should always start sending a special command called HELLO. 785 | This step accomplishes two things: 786 | 787 | 1. It allows servers to be backward compatible with RESP2 versions. This is needed in Redis in order to switch more gently to the version 3 of the protocol. 788 | 2. The `HELLO` command is useful in order to return information about the server and the protocol, that the client can use for different goals. 789 | 790 | The HELLO command has the following format and arguments: 791 | 792 | HELLO [AUTH ] 793 | 794 | Currently only the AUTH option is available, in order to authenticate the client 795 | in case the server is configured in order to be password protected. Redis 6 796 | will support ACLs, however in order to just use a general password like in 797 | Redis 5 clients should use "default" as username (all lower case). 798 | 799 | The first argument of the command is the protocol version we want the connection 800 | to be set. By default the connection starts in RESP2 mode. If we specify a 801 | connection version which is too big and is not supported by the server, it should 802 | reply with a -NOPROTO error. Example: 803 | 804 | Client: HELLO 4 805 | Server: -NOPROTO sorry this protocol version is not supported 806 | 807 | Then the client may retry with a lower protocol version. 808 | 809 | Similarly the client can easily detect a server that is only able to speak 810 | RESP2: 811 | 812 | Client: HELLO 3 AUTH default mypassword 813 | Server: -ERR unknown command 'HELLO' 814 | 815 | It can then proceed by sending the AUTH command directly and continue talking 816 | RESP2 to the server. 817 | 818 | Note that even if the protocol is supported, the HELLO command may return an 819 | error and perform no action (so the connection will remain in RESP2 mode) in case 820 | the authentication credentials are wrong: 821 | 822 | Client: HELLO 3 AUTH default mypassword 823 | Server: -ERR invalid password 824 | (the connection remains in RESP2 mode) 825 | 826 | The successful reply to the HELLO command is just a map reply. 827 | The information in the Hello reply is in part server dependent, but there are 828 | certain fields that are mandatory for all the RESP3 implementations: 829 | 830 | * server: "redis" (or other software name) 831 | * version: the server version 832 | * proto: the maximum version of the RESP protocol supported 833 | 834 | In addition, in the case of the RESP3 implementation of Redis, the following 835 | fields will also be emitted: 836 | 837 | * id: the client connection ID 838 | * mode: "standalone", "sentinel" or "cluster" 839 | * role: "master" or "replica" 840 | * modules: list of loaded modules as an array of strings 841 | 842 | The exact number and value of fields emitted by Redis is however currently 843 | a work in progress, you should not rely on the above list. 844 | 845 | ## Acknowledgements 846 | 847 | This specification was written by Salvatore Sanfilippo, however the design was informed by multiple people that contributed worthwhile ideas and improvements. A special thank to: 848 | 849 | * Dvir Volk 850 | * Yao Yue 851 | * Yossi Gottlieb 852 | * Marc Gravell 853 | * Nick Craver 854 | 855 | For the conversation and ideas to make this specification better. 856 | 857 | ## FAQ 858 | 859 | * **Why the RESP3 line break was not changed to a single character?** 860 | 861 | Because that would require a client to send command, and parse replies, in 862 | a different way based on the fact the client is in RESP v2 or v3 mode. 863 | Even a client supporting only RESP3, would start sending the `HELLO` command 864 | with CRLF as separators, since initially the connection is in RESP v2 mode. 865 | The parsing code would also be designed in order to accept the different line 866 | break based on the conditions. All in all the saving of one byte did not made 867 | enough sense in light of a more complex client implementation, especially since 868 | the way RESP3 is designed, it is mostly a superset of RESP2, so most clients 869 | will just have to add the parsing of the new data types supported without 870 | touching the implementation of the old ones. 871 | 872 | ## TODOs in this specification 873 | 874 | * Document the optional "inline" protocol. 875 | * Document pipelining 876 | --------------------------------------------------------------------------------