1<?xml version="1.0" encoding="US-ASCII"?>
2<!DOCTYPE rfc SYSTEM "xml2rfc/rfc2629.dtd">
3<?xml-stylesheet type='text/xsl' href='xml2rfc/rfc2629.xslt'?>
4<?rfc toc="yes"?>
5<?rfc strict="yes"?>
6<?rfc symrefs="yes"?>
7<?rfc sortrefs="yes" ?>
8<?rfc compact="yes" ?>
9<?rfc subcompact="yes" ?>
10<rfc category="info" docName="draft-stone-memcache-binary-01" ipr="none">
11
12  <front>
13
14    <title> Memcache Binary Protocol </title>
15
16    <author fullname="Aaron Stone" surname="Aaron Stone" role="editor">
17      <organization>Six Apart, Ltd.</organization>
18      <address>
19        <postal>
20          <street>548 4th Street</street>
21          <city>San Francisco</city>
22          <region>CA</region>
23          <code>94107</code>
24          <country>USA</country>
25        </postal>
26        <email>[email protected]</email>
27      </address>
28    </author>
29    <author fullname="Trond Norbye" surname="Norbye" givenname="Trond" role="editor">
30      <organization>Sun Microsystems, INC</organization>
31      <address>
32        <postal>
33          <street>Haakon VII g. 7B</street>
34          <city>Trondheim</city>
35          <code>NO-7485 Trondheim</code>
36          <country>Norway</country>
37        </postal>
38        <email>[email protected]</email>
39      </address>
40    </author>
41
42    <date day="19" month="July" year="2008" />
43
44    <area>Applications</area>
45
46    <keyword>memcache memcached cache</keyword>
47
48    <abstract>
49      <t>
50      This memo explains the memcache binary protocol for informational purposes.
51      </t>
52
53      <t>
54      Memcache is a high performance key-value cache. It is intentionally a
55      dumb cache, optimized for speed only. Applications using memcache do
56      not rely on it for data -- a persistent database with guaranteed reliability
57      is strongly recommended -- but applications can run much faster when
58      cached data is available in memcache.
59      </t>
60    </abstract>
61  </front>
62
63  <middle>
64    <section anchor="introduction" title="Introduction">
65      <t>
66      Memcache is a high performance key-value cache. It is intentionally a
67      dumb cache, optimized for speed only. Applications using memcache do
68      not rely on it for data -- a persistent database with guaranteed reliability
69      is strongly recommended -- but applications can run much faster when
70      cached data is available in memcache.
71      </t>
72      <t>
73      Memcache was originally written to make <xref target="LJ">LiveJournal</xref> go faster.
74      It now powers all of the fastest web sites that you love.
75      </t>
76      <section anchor="conventions" title="Conventions Used In This Document">
77        <t>The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT",
78        "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in this
79        document are to be interpreted as described in <xref target="KEYWORDS"/>.
80        </t>
81      </section>
82    </section>
83
84    <section anchor="packet" title="Packet Structure">
85      <t>
86      <figure>
87        <preamble>General format of a packet:</preamble>
88          <artwork>
89    Byte/     0       |       1       |       2       |       3       |
90       /              |               |               |               |
91      |0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|
92      +---------------+---------------+---------------+---------------+
93     0/ HEADER                                                        /
94      /                                                               /
95      /                                                               /
96      /                                                               /
97      +---------------+---------------+---------------+---------------+
98    24/ COMMAND-SPECIFIC EXTRAS (as needed)                           /
99     +/  (note length in th extras length header field)               /
100      +---------------+---------------+---------------+---------------+
101     m/ Key (as needed)                                               /
102     +/  (note length in key length header field)                     /
103      +---------------+---------------+---------------+---------------+
104     n/ Value (as needed)                                             /
105     +/  (note length is total body length header field, minus        /
106     +/   sum of the extras and key length body fields)               /
107      +---------------+---------------+---------------+---------------+
108     Total 24 bytes
109      </artwork></figure>
110      </t>
111
112      <t>
113      <figure>
114        <preamble>Request header:</preamble>
115          <artwork>
116    Byte/     0       |       1       |       2       |       3       |
117       /              |               |               |               |
118      |0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|
119      +---------------+---------------+---------------+---------------+
120     0| Magic         | Opcode        | Key length                    |
121      +---------------+---------------+---------------+---------------+
122     4| Extras length | Data type     | Reserved                      |
123      +---------------+---------------+---------------+---------------+
124     8| Total body length                                             |
125      +---------------+---------------+---------------+---------------+
126    12| Opaque                                                        |
127      +---------------+---------------+---------------+---------------+
128    16| CAS                                                           |
129      |                                                               |
130      +---------------+---------------+---------------+---------------+
131    Total 24 bytes
132      </artwork></figure>
133      </t>
134
135      <t>
136      <figure>
137        <preamble>Response header:</preamble>
138          <artwork>
139    Byte/     0       |       1       |       2       |       3       |
140       /              |               |               |               |
141      |0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|
142      +---------------+---------------+---------------+---------------+
143     0| Magic         | Opcode        | Key Length                    |
144      +---------------+---------------+---------------+---------------+
145     4| Extras length | Data type     | Status                        |
146      +---------------+---------------+---------------+---------------+
147     8| Total body length                                             |
148      +---------------+---------------+---------------+---------------+
149    12| Opaque                                                        |
150      +---------------+---------------+---------------+---------------+
151    16| CAS                                                           |
152      |                                                               |
153      +---------------+---------------+---------------+---------------+
154    Total 24 bytes
155      </artwork></figure>
156      </t>
157
158      <t>
159        Header fields:
160        <list hangIndent="20" style="hanging">
161          <t hangText="Magic">Magic number.</t>
162          <t hangText="Opcode">Command code.</t>
163          <t hangText="Key length">Length in bytes of the text key that follows the command extras.</t>
164          <t hangText="Status">Status of the response (non-zero on error).</t>
165          <t hangText="Extras length">Length in bytes of the command extras.</t>
166          <t hangText="Data type">Reserved for future use (Sean is using this soon).</t>
167          <t hangText="Reserved">Really reserved for future use (up for grabs).</t>
168          <t hangText="Total body length">Length in bytes of extra + key + value.</t>
169          <t hangText="Opaque">Will be copied back to you in the response.
170              FIXME: Can this be used to organize <xref target="UDP"/> packets?</t>
171          <t hangText="CAS">Data version check</t>
172        </list>
173      </t>
174    </section>
175
176    <section anchor="values" title="Defined Values">
177      <section anchor="value-magic" title="Magic Byte">
178        <t>
179        <list hangIndent="8" style="hanging">
180          <t hangText="0x80">Request packet for this protocol version</t>
181          <t hangText="0x81">Response packet for this protocol version</t>
182        </list>
183        </t>
184
185        <t>
186          Magic byte / version. For each version of the protocol, we'll use a
187          different request/reponse value pair. This is useful for protocol
188          analyzers to know what a packet is in isolation from which direction
189          it is moving. Note that it is common to run a memcached instance on a
190          host that also runs an application server. Such a host will both send
191          and receive memcache packets.
192        </t>
193
194        <t>
195          The version should hopefully correspond only to different meanings of
196          the command byte. In an ideal world, we will not change the header
197          format. As reserved bytes are given defined meaning, the protocol
198          version / magic byte values should be incremented.
199        </t>
200
201        <t>
202          Traffic analysis tools are encouraged to identify memcache packets
203          and provide detailed interpretation if the magic bytes are recognized
204          and otherwise to provide a generic breakdown of the packet. Note that
205          the key and value positions can always be identified even if the magic
206          byte or command opcode are not recognized.
207        </t>
208      </section>
209
210      <section anchor="value-status" title="Response Status">
211        <t>
212        Possible values of this two-byte field:
213        <list hangIndent="8" style="hanging">
214          <t hangText="0x0000">No error</t>
215          <t hangText="0x0001">Key not found</t>
216          <t hangText="0x0002">Key exists</t>
217          <t hangText="0x0003">Value too big</t>
218          <t hangText="0x0004">Invalid arguments</t>
219          <t hangText="0x0005">Item not stored</t>
220          <t hangText="0x0081">Unknown command</t>
221          <t hangText="0x0082">Out of memory</t>
222        </list>
223        </t>
224      </section>
225
226      <section anchor="value-opcodes" title="Command Opcodes">
227        <t>
228        Possible values of the one-byte field:
229        <list hangIndent="8" style="hanging">
230          <t hangText="0x00">Get</t>
231          <t hangText="0x01">Set</t>
232          <t hangText="0x02">Add</t>
233          <t hangText="0x03">Replace</t>
234          <t hangText="0x04">Delete</t>
235          <t hangText="0x05">Increment</t>
236          <t hangText="0x06">Decrement</t>
237          <t hangText="0x07">Quit</t>
238          <t hangText="0x08">Flush</t>
239          <t hangText="0x09">GetQ</t>
240          <t hangText="0x0A">No-op</t>
241          <t hangText="0x0B">Version</t>
242          <t hangText="0x0C">GetK</t>
243          <t hangText="0x0D">GetKQ</t>
244          <t hangText="0x0E">Append</t>
245          <t hangText="0x0F">Prepend</t>
246	  <t hangText="0x10">Stat</t>
247        </list>
248        </t>
249      </section>
250
251      <section anchor="value-types" title="Data Types">
252        <t>
253        Possible values of the one-byte field:
254        <list hangIndent="8" style="hanging">
255          <t hangText="0x00">Raw bytes</t>
256        </list>
257        </t>
258      </section>
259    </section>
260
261    <section title="Commands">
262      <section anchor="command-get" title="Get, Get Quietly, Get Key, Get Key Quietly">
263        <t>
264            Request:
265        </t>
266        <t>
267        <list style="empty">
268          <t>MUST NOT have extras.</t>
269          <t>MUST have key.</t>
270          <t>MUST NOT have value.</t>
271        </list>
272        </t>
273
274        <t>
275        <list style="symbols">
276          <t>4 byte flags</t>
277        </list>
278        </t>
279
280        <t>
281            Response (if found):
282        </t>
283        <t>
284        <list style="empty">
285          <t>MUST have extras.</t>
286          <t>MAY have key.</t>
287          <t>MAY have value.</t>
288        </list>
289        </t>
290
291        <t>
292        <list style="symbols">
293          <t>4 byte flags</t>
294        </list>
295        </t>
296
297        <t>
298      <figure>
299        <preamble>Extra data for the get commands:</preamble>
300          <artwork>
301    Byte/     0       |       1       |       2       |       3       |
302       /              |               |               |               |
303      |0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|
304      +---------------+---------------+---------------+---------------+
305     0| Flags                                                         |
306      +---------------+---------------+---------------+---------------+
307    Total 4 bytes
308      </artwork></figure>
309        </t>
310
311        <t>
312          The get command gets a single key. The getq command is both mum
313          on cache miss and quiet, holding its response until a non-quiet
314          command is issued. Getk and getkq differs from get and getq by
315          adding the key into the resonse packet.
316        </t>
317
318        <t>
319          You're not guaranteed a response to a getq/getkq cache hit until
320          you send a non-getq/getkq command later, which uncorks the
321          server which bundles up IOs to send to the client in one go.
322        </t>
323
324        <t>
325          Clients should implement multi-get (still important for
326          reducing network roundtrips!) as n pipelined requests, the
327          first n-1 being getq/getkq, the last being a regular
328          get/getk.  that way you're guaranteed to get a response, and
329          you know when the server's done.  you can also do the naive
330          thing and send n pipelined get/getks, but then you could potentially
331          get back a lot of "NOT_FOUND!" error code packets.
332          alternatively, you can send 'n' getq/getkqs, followed by an 'echo'
333          or 'noop' command.
334        </t>
335
336      </section>
337
338      <section anchor="command-set" title="Set, Add, Replace">
339        <t>
340        <list style="empty">
341          <t>MUST have extras.</t>
342          <t>MUST have key.</t>
343          <t>MUST have value.</t>
344        </list>
345        </t>
346
347        <t>
348          <list style="symbols">
349            <t>4 byte flags</t>
350            <t>4 byte expiration time</t>
351          </list>
352        </t>
353
354        <t>
355        <figure>
356          <preamble>Extra data for set/add/replace:</preamble>
357            <artwork>
358    Byte/     0       |       1       |       2       |       3       |
359       /              |               |               |               |
360      |0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|
361      +---------------+---------------+---------------+---------------+
362     0| Flags                                                         |
363      +---------------+---------------+---------------+---------------+
364     4| Expiration                                                    |
365      +---------------+---------------+---------------+---------------+
366    Total 8 bytes
367        </artwork></figure>
368        </t>
369
370        <t>
371        If the Data Version Check is present and nonzero, the set MUST succeed if the
372        key exists and has a version identifier identical to the provided value, and
373	MUST NOT succeed if the key does not exist or has a different version identifier.
374	The set response packet will include the same values in all three fields.
375        </t>
376
377        <t>
378        If the CAS field in the header is zero, the set MUST succeed unconditionally.
379	The set response packet will include idential values for flags and expiration,
380	and a new value for Data Version Check, which the client SHOULD keep track of.
381        </t>
382
383	<t>
384	The key MAY be reserved according to <xref target="command-delete"/>,
385	causing the set to fail.
386	</t>
387      </section>
388
389      <section anchor="command-delete" title="Delete">
390        <t>
391        <list style="empty">
392          <t>MUST NOT have extras</t>
393          <t>MUST have key.</t>
394          <t>MUST NOT have value.</t>
395        </list>
396        </t>
397
398
399        <t>
400	  Delete the item with the specific key.
401        </t>
402      </section>
403
404      <section anchor="command-incr" title="Increment, Decrement">
405        <t>
406        <list style="empty">
407          <t>MUST have extras.</t>
408          <t>MUST have key.</t>
409          <t>MUST NOT have value.</t>
410        </list>
411        </t>
412
413        <t>
414          <list style="symbols">
415            <t>8 byte value to add / subtract (FIXME: Is this unsigned?)</t>
416            <t>8 byte initial value (unsigned)</t>
417            <t>4 byte expiration time</t>
418          </list>
419        </t>
420
421        <t>
422      <figure>
423        <preamble>Extra data for incr/decr:</preamble>
424          <artwork>
425    Byte/     0       |       1       |       2       |       3       |
426       /              |               |               |               |
427      |0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|
428      +---------------+---------------+---------------+---------------+
429     0| Amount to add                                                 |
430      |                                                               |
431      +---------------+---------------+---------------+---------------+
432     8| Initial value                                                 |
433      |                                                               |
434      +---------------+---------------+---------------+---------------+
435    16| Expiration                                                    |
436      +---------------+---------------+---------------+---------------+
437    Total 20 bytes
438      </artwork></figure>
439        </t>
440
441        <t>
442      <figure>
443        <preamble>incr/decr response body:</preamble>
444          <artwork>
445    Byte/     0       |       1       |       2       |       3       |
446       /              |               |               |               |
447      |0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|
448      +---------------+---------------+---------------+---------------+
449     0| 64-bit unsigned response.                                     |
450      |                                                               |
451      +---------------+---------------+---------------+---------------+
452    Total 8 bytes
453      </artwork></figure>
454        </t>
455
456        <t>
457          These commands will either add or remove the specified
458          amount to the requested counter.
459
460          If the counter does not exist, one of two things may happen:
461
462          <list style="numbers">
463          <t>If the expiration value is all one-bits (0xffffffff), the
464             operation will fail with NOT_FOUND.</t>
465          <t>For all other expiration values, the operation will succeed
466             by seeding the value for this key with the provided initial
467             value to expire with the provided expiration time.</t>
468          </list>
469        </t>
470
471        <t>
472          Note that in the creation case, flags will be set to zero
473          (FIXME:  Should they be provided here as well?)
474        </t>
475      </section>
476
477      <section anchor="command-quit" title="quit">
478        <t>
479        <list style="empty">
480          <t>MUST NOT have extras.</t>
481          <t>MUST NOT have key.</t>
482          <t>MUST NOT have value.</t>
483        </list>
484        </t>
485
486        <t>
487        Close the connection to the server.
488        </t>
489      </section>
490
491      <section anchor="command-flush" title="Flush">
492        <t>
493        <list style="empty">
494          <t>MAY have extras</t>
495          <t>MUST NOT have key.</t>
496          <t>MUST NOT have value.</t>
497        </list>
498        </t>
499
500        <t>
501          <list style="symbols">
502            <t>4 byte expiration time</t>
503          </list>
504        </t>
505
506        <t>
507      <figure>
508        <preamble>Extra data for flush:</preamble>
509          <artwork>
510    Byte/     0       |       1       |       2       |       3       |
511       /              |               |               |               |
512      |0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|
513      +---------------+---------------+---------------+---------------+
514     0| Expiration                                                    |
515      +---------------+---------------+---------------+---------------+
516    Total 4 bytes
517      </artwork></figure>
518        </t>
519
520        <t>
521            Flush the items in the cache now or sometime in the future
522            specified by the expiration field. See the documentation of the
523            textual protocol for the full description on how to specify the
524            expiration time.
525        </t>
526
527      </section>
528
529      <section anchor="command-noop" title="noop">
530        <t>
531        <list style="empty">
532          <t>MUST NOT have extras.</t>
533          <t>MUST NOT have key.</t>
534          <t>MUST NOT have value.</t>
535        </list>
536        </t>
537
538        <t>
539        Used as a keep alive. Flushes outstanding getq/getkq's.
540        </t>
541      </section>
542
543      <section anchor="command-version" title="version">
544        <t>
545        <list style="empty">
546          <t>MUST NOT have extras.</t>
547          <t>MUST NOT have key.</t>
548          <t>MUST NOT have value.</t>
549        </list>
550        </t>
551
552        <t>
553          Requst the server version.
554        </t>
555        <t>
556	  The server respond with a packet containing the version string
557	  in the body with the following format: "x.y.z"
558        </t>
559      </section>
560
561      <section anchor="command-append" title="Append, Prepend">
562        <t>
563        <list style="empty">
564          <t>MUST NOT have extras.</t>
565          <t>MUST have key.</t>
566          <t>MUST have value.</t>
567        </list>
568        </t>
569
570        <t>
571          These commands will either append or prepend the specified
572          value to the requested key.
573        </t>
574        <t>
575          Please note that the CAS field in the header must match the
576          CAS version of the requested item.
577        </t>
578      </section>
579
580      <section anchor="command-stat" title="Stat">
581        <t>
582        <list style="empty">
583          <t>MUST NOT have extras.</t>
584          <t>MAY have key.</t>
585          <t>MUST NOT have value.</t>
586        </list>
587        </t>
588
589        <t>
590	  Request server statistics. Without a key specified the server will respond
591	  with a "default" set of statistics information. Each piece of statistical
592	  information is returned in it's own packet (key contains the
593	  name of the statistical item and the body contains the value in
594	  ASCII format). The sequence of return packet is terminated with a packet
595	  with no key and no value.
596        </t>
597      </section>
598
599    </section>
600
601    <section title="Example Session">
602    <t>
603        We start up our application, and asks for the value associated
604        with the 'Hello' key. To send the get request, the following values
605        must be inserted in the header (in network byte order):
606
607Field        (offset) (value)
608Magic        (0)    : 0x80
609Opcode       (1)    : 0x00
610Key length   (2,3)  : 0x0005
611Extra length (4)    : 0x00
612Data type    (5)    : 0x00
613Reserved     (6,7)  : 0x0000
614Total body   (8-11) : 0x00000005
615Opaque       (12-15): 0x00000000
616CAS          (16-23): 0x0000000000000000
617Extras              : None
618Key          (24-29): The textual string: "Hello"
619Value               : None
620
621The packet looks like:
622
623      <figure>
624        <preamble>Get request:</preamble>
625          <artwork>
626
627    Byte/     0       |       1       |       2       |       3       |
628       /              |               |               |               |
629      |0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|
630      +---------------+---------------+---------------+---------------+
631     0| 0x80          | 0x00          | 0x00          | 0x05          |
632      +---------------+---------------+---------------+---------------+
633     4| 0x00          | 0x00          | 0x00          | 0x00          |
634      +---------------+---------------+---------------+---------------+
635     8| 0x00          | 0x00          | 0x00          | 0x05          |
636      +---------------+---------------+---------------+---------------+
637    12| 0x00          | 0x00          | 0x00          | 0x00          |
638      +---------------+---------------+---------------+---------------+
639    16| 0x00          | 0x00          | 0x00          | 0x00          |
640      +---------------+---------------+---------------+---------------+
641    20| 0x00          | 0x00          | 0x00          | 0x00          |
642      +---------------+---------------+---------------+---------------+
643    24| 0x48 ('H')    | 0x65 ('e')    | 0x6c ('l')    | 0x6c ('l')    |
644      +---------------+---------------+---------------+---------------+
645    28| 0x6f ('o')    |
646      +---------------+
647
648      Total 29 bytes (24 byte header, and 5 bytes key)
649      </artwork></figure>
650      </t>
651
652      <t>
653          Since nobody has set this key yet, it returns "Not found".
654
655      <figure>
656        <preamble>Get response:</preamble>
657          <artwork>
658
659    Byte/     0       |       1       |       2       |       3       |
660       /              |               |               |               |
661      |0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|
662      +---------------+---------------+---------------+---------------+
663     0| 0x81          | 0x00          | 0x00          | 0x00          |
664      +---------------+---------------+---------------+---------------+
665     4| 0x00          | 0x00          | 0x00          | 0x01          |
666      +---------------+---------------+---------------+---------------+
667     8| 0x00          | 0x00          | 0x00          | 0x09          |
668      +---------------+---------------+---------------+---------------+
669    12| 0x00          | 0x00          | 0x00          | 0x00          |
670      +---------------+---------------+---------------+---------------+
671    16| 0x00          | 0x00          | 0x00          | 0x00          |
672      +---------------+---------------+---------------+---------------+
673    20| 0x00          | 0x00          | 0x00          | 0x00          |
674      +---------------+---------------+---------------+---------------+
675    24| 0x4e ('N')    | 0x6f ('o')    | 0x74 ('t')    | 0x20 (' ')    |
676      +---------------+---------------+---------------+---------------+
677    28| 0x66 ('f')    | 0x6f ('o')    | 0x75 ('u')    | 0x6e ('n')    |
678      +---------------+---------------+---------------+---------------+
679    32| 0x64 ('d')    |
680      +---------------+
681
682      Total 33 bytes (24 byte header, and 9 bytes value)
683      </artwork></figure>
684
685      The above packet is interpreted as:
686
687Field        (offset) (value)
688Magic        (0)    : 0x81
689Opcode       (1)    : 0x00
690Key length   (2,3)  : 0x0000
691Extra length (4)    : 0x00
692Data type    (5)    : 0x00
693Status       (6,7)  : 0x0001
694Total body   (8-11) : 0x00000009
695Opaque       (12-15): 0x00000000
696CAS          (16-23): 0x0000000000000000
697Extras              : None
698Key                 : None
699Value        (24-32): The textual string "Not found"
700      </t>
701
702      <t>
703      Well, looks like we need to add the key! Let's set it to "World" with
704      0xdeadbeef as flags and to expire in two hours. The following values
705      should be inserted into the header:
706
707Field        (offset) (value)
708Magic        (0)    : 0x80
709Opcode       (1)    : 0x02
710Key length   (2,3)  : 0x0005
711Extra length (4)    : 0x08
712Data type    (5)    : 0x00
713Reserved     (6,7)  : 0x0000
714Total body   (8-11) : 0x00000012
715Opaque       (12-15): 0x00000000
716CAS          (16-23): 0x0000000000000000
717Extras              :
718  Flags      (24-27): 0xdeadbeef
719  Expiry     (28-31): 0x00000e10
720Key          (32-36): The textual string "Hello"
721Value        (37-41): The textual string "World"
722
723The packet looks like:
724
725      <figure>
726        <preamble>Add request:</preamble>
727          <artwork>
728
729    Byte/     0       |       1       |       2       |       3       |
730       /              |               |               |               |
731      |0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|
732      +---------------+---------------+---------------+---------------+
733     0| 0x80          | 0x02          | 0x00          | 0x05          |
734      +---------------+---------------+---------------+---------------+
735     4| 0x08          | 0x00          | 0x00          | 0x00          |
736      +---------------+---------------+---------------+---------------+
737     8| 0x00          | 0x00          | 0x00          | 0x12          |
738      +---------------+---------------+---------------+---------------+
739    12| 0x00          | 0x00          | 0x00          | 0x00          |
740      +---------------+---------------+---------------+---------------+
741    16| 0x00          | 0x00          | 0x00          | 0x00          |
742      +---------------+---------------+---------------+---------------+
743    20| 0x00          | 0x00          | 0x00          | 0x00          |
744      +---------------+---------------+---------------+---------------+
745    24| 0xde          | 0xad          | 0xbe          | 0xef          |
746      +---------------+---------------+---------------+---------------+
747    28| 0x00          | 0x00          | 0x0e          | 0x10          |
748      +---------------+---------------+---------------+---------------+
749    32| 0x48 ('H')    | 0x65 ('e')    | 0x6c ('l')    | 0x6c ('l')    |
750      +---------------+---------------+---------------+---------------+
751    36| 0x6f ('o')    | 0x57 ('W')    | 0x6f ('o')    | 0x72 ('r')    |
752      +---------------+---------------+---------------+---------------+
753    40| 0x6c ('l')    | 0x64 ('d')    |
754      +---------------+---------------+
755
756      Total 42 bytes (24 byte header, 8 byte extras, 5 byte key and 5 byte value)
757      </artwork></figure>
758      </t>
759
760      <t>
761      The add succeeds and the following packet is returned:
762      <figure>
763        <preamble>Add response:</preamble>
764          <artwork>
765
766    Byte/     0       |       1       |       2       |       3       |
767       /              |               |               |               |
768      |0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|
769      +---------------+---------------+---------------+---------------+
770     0| 0x81          | 0x02          | 0x00          | 0x00          |
771      +---------------+---------------+---------------+---------------+
772     4| 0x00          | 0x00          | 0x00          | 0x00          |
773      +---------------+---------------+---------------+---------------+
774     8| 0x00          | 0x00          | 0x00          | 0x00          |
775      +---------------+---------------+---------------+---------------+
776    12| 0x00          | 0x00          | 0x00          | 0x00          |
777      +---------------+---------------+---------------+---------------+
778    16| 0x00          | 0x00          | 0x00          | 0x00          |
779      +---------------+---------------+---------------+---------------+
780    20| 0x00          | 0x00          | 0x00          | 0x01          |
781      +---------------+---------------+---------------+---------------+
782
783      Total 24 bytes
784      </artwork></figure>
785
786      The above packet is interpreted as:
787
788Field        (offset) (value)
789Magic        (0)    : 0x81
790Opcode       (1)    : 0x02
791Key length   (2,3)  : 0x0000
792Extra length (4)    : 0x00
793Data type    (5)    : 0x00
794Status       (6,7)  : 0x0000
795Total body   (8-11) : 0x00000000
796Opaque       (12-15): 0x00000000
797CAS          (16-23): 0x0000000000000001
798Extras              : None
799Key                 : None
800Value               : None
801
802    </t>
803
804    <t>
805      If the original get request is sent again, the key would be found and
806      the following packet is returned.
807
808      <figure>
809        <preamble>Get response:</preamble>
810          <artwork>
811
812    Byte/     0       |       1       |       2       |       3       |
813       /              |               |               |               |
814      |0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|
815      +---------------+---------------+---------------+---------------+
816     0| 0x81          | 0x00          | 0x00          | 0x00          |
817      +---------------+---------------+---------------+---------------+
818     4| 0x04          | 0x00          | 0x00          | 0x00          |
819      +---------------+---------------+---------------+---------------+
820     8| 0x00          | 0x00          | 0x00          | 0x09          |
821      +---------------+---------------+---------------+---------------+
822    12| 0x00          | 0x00          | 0x00          | 0x00          |
823      +---------------+---------------+---------------+---------------+
824    16| 0x00          | 0x00          | 0x00          | 0x00          |
825      +---------------+---------------+---------------+---------------+
826    20| 0x00          | 0x00          | 0x00          | 0x01          |
827      +---------------+---------------+---------------+---------------+
828    24| 0xde          | 0xad          | 0xbe          | 0xef          |
829      +---------------+---------------+---------------+---------------+
830    28| 0x57 ('W')    | 0x6f ('o')    | 0x72 ('r')    | 0x6c ('l')    |
831      +---------------+---------------+---------------+---------------+
832    32| 0x64 ('d')    |
833      +---------------+
834
835      Total 33 bytes (24 byte header, 4 byte extras and 5 byte value)
836      </artwork></figure>
837
838      The above packet is interpreted as:
839
840Field        (offset) (value)
841Magic        (0)    : 0x81
842Opcode       (1)    : 0x00
843Key length   (2,3)  : 0x0000
844Extra length (4)    : 0x04
845Data type    (5)    : 0x00
846Status       (6,7)  : 0x0000
847Total body   (8-11) : 0x00000009
848Opaque       (12-15): 0x00000000
849CAS          (16-23): 0x0000000000000001
850Extras              : None
851  Flags      (24-27): 0xdeadbeef
852Key                 : None
853Value        (28-32): The textual string "World"
854
855    </t>
856    <t>
857        If we send the add request one more time the add will fail, and the
858        following packet is returned:
859
860      <figure>
861        <preamble>Get response:</preamble>
862          <artwork>
863
864    Byte/     0       |       1       |       2       |       3       |
865       /              |               |               |               |
866      |0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|
867      +---------------+---------------+---------------+---------------+
868     0| 0x81          | 0x02          | 0x00          | 0x00          |
869      +---------------+---------------+---------------+---------------+
870     4| 0x00          | 0x00          | 0x00          | 0x02          |
871      +---------------+---------------+---------------+---------------+
872     8| 0x00          | 0x00          | 0x00          | 0x14          |
873      +---------------+---------------+---------------+---------------+
874    12| 0x00          | 0x00          | 0x00          | 0x00          |
875      +---------------+---------------+---------------+---------------+
876    16| 0x00          | 0x00          | 0x00          | 0x00          |
877      +---------------+---------------+---------------+---------------+
878    20| 0x00          | 0x00          | 0x00          | 0x00          |
879      +---------------+---------------+---------------+---------------+
880    24| 0x44 ('D')    | 0x61 ('a')    | 0x74 ('t')    | 0x61 ('a')    |
881      +---------------+---------------+---------------+---------------+
882    28| 0x20 (' ')    | 0x65 ('e')    | 0x78 ('x')    | 0x69 ('i')    |
883      +---------------+---------------+---------------+---------------+
884    32| 0x73 ('s')    | 0x74 ('t')    | 0x73 ('s')    | 0x20 (' ')    |
885      +---------------+---------------+---------------+---------------+
886    36| 0x66 ('f')    | 0x6f ('o')    | 0x72 ('r')    | 0x20 (' ')    |
887      +---------------+---------------+---------------+---------------+
888    40| 0x6b ('k')    | 0x65 ('e')    | 0x79 ('y')    | 0x2e ('.')    |
889      +---------------+---------------+---------------+---------------+
890
891      Total 44 bytes (24 byte header, 24 byte value)
892      </artwork></figure>
893
894      The above packet is interpreted as:
895
896Field        (offset) (value)
897Magic        (0)    : 0x81
898Opcode       (1)    : 0x02
899Key length   (2,3)  : 0x0000
900Extra length (4)    : 0x00
901Data type    (5)    : 0x00
902Status       (6,7)  : 0x0002
903Total body   (8-11) : 0x00000014
904Opaque       (12-15): 0x00000000
905CAS          (16-23): 0x0000000000000000
906Extras              : None
907Key                 : None
908Value        (24-43): The textual string "Data exists for key."
909    </t>
910    <t>
911        To send a "noop" command, insert the following values in the request:
912
913Field        (offset) (value)
914Magic        (0)    : 0x80
915Opcode       (1)    : 0x0a
916Key length   (2,3)  : 0x0000
917Extra length (4)    : 0x00
918Data type    (5)    : 0x00
919Reserved     (6,7)  : 0x0000
920Total body   (8-11) : 0x00000000
921Opaque       (12-15): 0x00000000
922CAS          (16-23): 0x0000000000000000
923Extras              : None
924Key                 : None
925Value               : None
926
927The packet looks like:
928
929      <figure>
930        <preamble>Noop request:</preamble>
931          <artwork>
932
933    Byte/     0       |       1       |       2       |       3       |
934       /              |               |               |               |
935      |0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|
936      +---------------+---------------+---------------+---------------+
937     0| 0x80          | 0x0a          | 0x00          | 0x00          |
938      +---------------+---------------+---------------+---------------+
939     4| 0x00          | 0x00          | 0x00          | 0x00          |
940      +---------------+---------------+---------------+---------------+
941     8| 0x00          | 0x00          | 0x00          | 0x00          |
942      +---------------+---------------+---------------+---------------+
943    12| 0x00          | 0x00          | 0x00          | 0x00          |
944      +---------------+---------------+---------------+---------------+
945    16| 0x00          | 0x00          | 0x00          | 0x00          |
946      +---------------+---------------+---------------+---------------+
947    20| 0x00          | 0x00          | 0x00          | 0x00          |
948      +---------------+---------------+---------------+---------------+
949
950      Total 24 bytes
951      </artwork></figure>
952      </t>
953      <t>
954        The server will respond with the following packet:
955
956      <figure>
957        <preamble>Noop response:</preamble>
958          <artwork>
959
960    Byte/     0       |       1       |       2       |       3       |
961       /              |               |               |               |
962      |0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|
963      +---------------+---------------+---------------+---------------+
964     0| 0x81          | 0x0a          | 0x00          | 0x00          |
965      +---------------+---------------+---------------+---------------+
966     4| 0x00          | 0x00          | 0x00          | 0x00          |
967      +---------------+---------------+---------------+---------------+
968     8| 0x00          | 0x00          | 0x00          | 0x00          |
969      +---------------+---------------+---------------+---------------+
970    12| 0x00          | 0x00          | 0x00          | 0x00          |
971      +---------------+---------------+---------------+---------------+
972    16| 0x00          | 0x00          | 0x00          | 0x00          |
973      +---------------+---------------+---------------+---------------+
974    20| 0x00          | 0x00          | 0x00          | 0x00          |
975      +---------------+---------------+---------------+---------------+
976
977      Total 24 bytes
978      </artwork></figure>
979
980      The above packet is interpreted as:
981
982Field        (offset) (value)
983Magic        (0)    : 0x81
984Opcode       (1)    : 0x0a
985Key length   (2,3)  : 0x0000
986Extra length (4)    : 0x00
987Data type    (5)    : 0x00
988Status       (6,7)  : 0x0000
989Total body   (8-11) : 0x00000000
990Opaque       (12-15): 0x00000000
991CAS          (16-23): 0x0000000000000000
992Extras              : None
993Key                 : None
994Value               : None
995        </t>
996        <t>
997            So lets go ahead and append '!' to the 'Hello' key. To do so,
998            insert the following in the packet:
999
1000Field        (offset) (value)
1001Magic        (0)    : 0x80
1002Opcode       (1)    : 0x0e
1003Key length   (2,3)  : 0x0005
1004Extra length (4)    : 0x00
1005Data type    (5)    : 0x00
1006Reserved     (6,7)  : 0x0000
1007Total body   (8-11) : 0x00000006
1008Opaque       (12-15): 0x00000000
1009CAS          (16-23): 0x0000000000000000
1010Extras              : None
1011Key          (24-28): The textual string "Hello"
1012Value        (29)   : None
1013
1014The packet looks like:
1015
1016      <figure>
1017        <preamble>Append request:</preamble>
1018          <artwork>
1019
1020    Byte/     0       |       1       |       2       |       3       |
1021       /              |               |               |               |
1022      |0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|
1023      +---------------+---------------+---------------+---------------+
1024     0| 0x80          | 0x0e          | 0x00          | 0x05          |
1025      +---------------+---------------+---------------+---------------+
1026     4| 0x00          | 0x00          | 0x00          | 0x00          |
1027      +---------------+---------------+---------------+---------------+
1028     8| 0x00          | 0x00          | 0x00          | 0x06          |
1029      +---------------+---------------+---------------+---------------+
1030    12| 0x00          | 0x00          | 0x00          | 0x00          |
1031      +---------------+---------------+---------------+---------------+
1032    16| 0x00          | 0x00          | 0x00          | 0x00          |
1033      +---------------+---------------+---------------+---------------+
1034    20| 0x00          | 0x00          | 0x00          | 0x00          |
1035      +---------------+---------------+---------------+---------------+
1036    24| 0x48 ('H')    | 0x65 ('e')    | 0x6c ('l')    | 0x6c ('l')    |
1037      +---------------+---------------+---------------+---------------+
1038    28| 0x6f ('o')    | 0x21 ('!')    |
1039      +---------------+---------------+
1040
1041      Total 30 bytes (24 byte header, 5 byte key, 1 byte value)
1042      </artwork></figure>
1043      </t>
1044      <t>
1045          The server will respond with the following packet
1046      <figure>
1047        <preamble>Append response:</preamble>
1048          <artwork>
1049
1050    Byte/     0       |       1       |       2       |       3       |
1051       /              |               |               |               |
1052      |0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|
1053      +---------------+---------------+---------------+---------------+
1054     0| 0x81          | 0x0e          | 0x00          | 0x00          |
1055      +---------------+---------------+---------------+---------------+
1056     4| 0x00          | 0x00          | 0x00          | 0x00          |
1057      +---------------+---------------+---------------+---------------+
1058     8| 0x00          | 0x00          | 0x00          | 0x00          |
1059      +---------------+---------------+---------------+---------------+
1060    12| 0x00          | 0x00          | 0x00          | 0x00          |
1061      +---------------+---------------+---------------+---------------+
1062    16| 0x00          | 0x00          | 0x00          | 0x00          |
1063      +---------------+---------------+---------------+---------------+
1064    20| 0x00          | 0x00          | 0x00          | 0x02          |
1065      +---------------+---------------+---------------+---------------+
1066
1067      Total 24 bytes
1068      </artwork></figure>
1069
1070      The above packet is interpreted as:
1071
1072Field        (offset) (value)
1073Magic        (0)    : 0x81
1074Opcode       (1)    : 0x0e
1075Key length   (2,3)  : 0x0000
1076Extra length (4)    : 0x00
1077Data type    (5)    : 0x00
1078Status       (6,7)  : 0x0000
1079Total body   (8-11) : 0x00000000
1080Opaque       (12-15): 0x00000000
1081CAS          (16-23): 0x0000000000000002
1082Extras              : None
1083Key                 : None
1084Value               : None
1085
1086    </t>
1087    <t>
1088        Lets prepend '!' to the 'Hello' key. To do so, insert the following in
1089        the packet:
1090
1091Field        (offset) (value)
1092Magic        (0)    : 0x80
1093Opcode       (1)    : 0x0f
1094Key length   (2,3)  : 0x0005
1095Extra length (4)    : 0x00
1096Data type    (5)    : 0x00
1097Reserved     (6,7)  : 0x0000
1098Total body   (8-11) : 0x00000006
1099Opaque       (12-15): 0x00000000
1100CAS          (16-23): 0x0000000000000002
1101Extras              : None
1102Key          (24-28): The textual string "Hello"
1103Value        (29)   : The textual string "!"
1104
1105The packet looks like:
1106
1107      <figure>
1108        <preamble>Prepend request:</preamble>
1109          <artwork>
1110
1111    Byte/     0       |       1       |       2       |       3       |
1112       /              |               |               |               |
1113      |0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|
1114      +---------------+---------------+---------------+---------------+
1115     0| 0x80          | 0x0f          | 0x00          | 0x05          |
1116      +---------------+---------------+---------------+---------------+
1117     4| 0x00          | 0x00          | 0x00          | 0x00          |
1118      +---------------+---------------+---------------+---------------+
1119     8| 0x00          | 0x00          | 0x00          | 0x06          |
1120      +---------------+---------------+---------------+---------------+
1121    12| 0x00          | 0x00          | 0x00          | 0x00          |
1122      +---------------+---------------+---------------+---------------+
1123    16| 0x00          | 0x00          | 0x00          | 0x00          |
1124      +---------------+---------------+---------------+---------------+
1125    20| 0x00          | 0x00          | 0x00          | 0x02          |
1126      +---------------+---------------+---------------+---------------+
1127    24| 0x48 ('H')    | 0x65 ('e')    | 0x6c ('l')    | 0x6c ('l')    |
1128      +---------------+---------------+---------------+---------------+
1129    28| 0x6f ('o')    | 0x21 ('!')    |
1130      +---------------+---------------+
1131
1132      Total 30 bytes (24 bytes header, 5 byte key, 1 byte value)
1133      </artwork></figure>
1134      </t>
1135      <t>
1136        The server will respond with the following packet:
1137
1138      <figure>
1139        <preamble>Prepend response:</preamble>
1140          <artwork>
1141
1142    Byte/     0       |       1       |       2       |       3       |
1143       /              |               |               |               |
1144      |0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|
1145      +---------------+---------------+---------------+---------------+
1146     0| 0x81          | 0x0f          | 0x00          | 0x00          |
1147      +---------------+---------------+---------------+---------------+
1148     4| 0x00          | 0x00          | 0x00          | 0x00          |
1149      +---------------+---------------+---------------+---------------+
1150     8| 0x00          | 0x00          | 0x00          | 0x00          |
1151      +---------------+---------------+---------------+---------------+
1152    12| 0x00          | 0x00          | 0x00          | 0x00          |
1153      +---------------+---------------+---------------+---------------+
1154    16| 0x00          | 0x00          | 0x00          | 0x00          |
1155      +---------------+---------------+---------------+---------------+
1156    20| 0x00          | 0x00          | 0x00          | 0x03          |
1157      +---------------+---------------+---------------+---------------+
1158
1159      Total 24 bytes
1160      </artwork></figure>
1161
1162      The above packet is interpreted as:
1163
1164Field        (offset) (value)
1165Magic        (0)    : 0x81
1166Opcode       (1)    : 0x0f
1167Key length   (2,3)  : 0x0000
1168Extra length (4)    : 0x00
1169Data type    (5)    : 0x00
1170Status       (6,7)  : 0x0000
1171Total body   (8-11) : 0x00000000
1172Opaque       (12-15): 0x00000000
1173CAS          (16-23): 0x0000000000000003
1174Extras              : None
1175Key                 : None
1176Value               : None
1177
1178    </t>
1179    <t>
1180        Let's just replace the content of the 'Hello' key with 'World'
1181        (with 0xdeadbeef as flags, 2 hour expiry time). The packet should
1182        have the following content:
1183
1184Field        (offset) (value)
1185Magic        (0)    : 0x80
1186Opcode       (1)    : 0x03
1187Key length   (2,3)  : 0x0005
1188Extra length (4)    : 0x08
1189Data type    (5)    : 0x00
1190Reserved     (6,7)  : 0x0000
1191Total body   (8-11) : 0x00000012
1192Opaque       (12-15): 0x00000000
1193CAS          (16-23): 0x0000000000000000
1194Extras              :
1195  Flags      (24-27): 0xdeadbeef
1196  Expiry     (28-31): 0x00000e10
1197Key          (32-36): The textual string "Hello"
1198Value        (37-41): The textual string "World"
1199
1200The packet looks like:
1201
1202      <figure>
1203        <preamble>Replace request:</preamble>
1204          <artwork>
1205
1206    Byte/     0       |       1       |       2       |       3       |
1207       /              |               |               |               |
1208      |0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|
1209      +---------------+---------------+---------------+---------------+
1210     0| 0x80          | 0x03          | 0x00          | 0x05          |
1211      +---------------+---------------+---------------+---------------+
1212     4| 0x08          | 0x00          | 0x00          | 0x00          |
1213      +---------------+---------------+---------------+---------------+
1214     8| 0x00          | 0x00          | 0x00          | 0x12          |
1215      +---------------+---------------+---------------+---------------+
1216    12| 0x00          | 0x00          | 0x00          | 0x00          |
1217      +---------------+---------------+---------------+---------------+
1218    16| 0x00          | 0x00          | 0x00          | 0x00          |
1219      +---------------+---------------+---------------+---------------+
1220    20| 0x00          | 0x00          | 0x00          | 0x00          |
1221      +---------------+---------------+---------------+---------------+
1222    24| 0xde          | 0xad          | 0xbe          | 0xef          |
1223      +---------------+---------------+---------------+---------------+
1224    28| 0x00          | 0x00          | 0x0e          | 0x10          |
1225      +---------------+---------------+---------------+---------------+
1226    32| 0x48 ('H')    | 0x65 ('e')    | 0x6c ('l')    | 0x6c ('l')    |
1227      +---------------+---------------+---------------+---------------+
1228    36| 0x6f ('o')    | 0x57 ('W')    | 0x6f ('o')    | 0x72 ('r')    |
1229      +---------------+---------------+---------------+---------------+
1230    40| 0x6c ('l')    | 0x64 ('d')    |
1231      +---------------+---------------+
1232
1233      Total 42 byes (24 byte header, 8 byte extras, 5 byte key and 5 byte value)
1234      </artwork></figure></t>
1235
1236      <t>
1237         The server will respond with the following packet
1238
1239      <figure>
1240        <preamble>Replace response:</preamble>
1241          <artwork>
1242
1243    Byte/     0       |       1       |       2       |       3       |
1244       /              |               |               |               |
1245      |0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|
1246      +---------------+---------------+---------------+---------------+
1247     0| 0x81          | 0x03          | 0x00          | 0x00          |
1248      +---------------+---------------+---------------+---------------+
1249     4| 0x00          | 0x00          | 0x00          | 0x00          |
1250      +---------------+---------------+---------------+---------------+
1251     8| 0x00          | 0x00          | 0x00          | 0x00          |
1252      +---------------+---------------+---------------+---------------+
1253    12| 0x00          | 0x00          | 0x00          | 0x00          |
1254      +---------------+---------------+---------------+---------------+
1255    16| 0x00          | 0x00          | 0x00          | 0x00          |
1256      +---------------+---------------+---------------+---------------+
1257    20| 0x00          | 0x00          | 0x00          | 0x04          |
1258      +---------------+---------------+---------------+---------------+
1259
1260      Total 24 bytes
1261      </artwork></figure>
1262
1263      The above packet is interpreted as:
1264
1265Field        (offset) (value)
1266Magic        (0)    : 0x81
1267Opcode       (1)    : 0x03
1268Key length   (2,3)  : 0x0000
1269Extra length (4)    : 0x00
1270Data type    (5)    : 0x00
1271Status       (6,7)  : 0x0000
1272Total body   (8-11) : 0x00000000
1273Opaque       (12-15): 0x00000000
1274CAS          (16-23): 0x0000000000000004
1275Extras              : None
1276Key                 : None
1277Value               : None
1278
1279     </t>
1280     <t>
1281        To delete the "Hello" key, set the following fields in the request:
1282
1283Field        (offset) (value)
1284Magic        (0)    : 0x80
1285Opcode       (1)    : 0x04
1286Key length   (2,3)  : 0x0005
1287Extra length (4)    : 0x00
1288Data type    (5)    : 0x00
1289Reserved     (6,7)  : 0x0000
1290Total body   (8-11) : 0x00000005
1291Opaque       (12-15): 0x00000000
1292CAS          (16-23): 0x0000000000000000
1293Extras              : None
1294Key                 : The textual string "Hello"
1295Value               : None
1296
1297The packet looks like:
1298
1299      <figure>
1300        <preamble>Delete request:</preamble>
1301          <artwork>
1302
1303    Byte/     0       |       1       |       2       |       3       |
1304       /              |               |               |               |
1305      |0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|
1306      +---------------+---------------+---------------+---------------+
1307     0| 0x80          | 0x04          | 0x00          | 0x05          |
1308      +---------------+---------------+---------------+---------------+
1309     4| 0x00          | 0x00          | 0x00          | 0x00          |
1310      +---------------+---------------+---------------+---------------+
1311     8| 0x00          | 0x00          | 0x00          | 0x05          |
1312      +---------------+---------------+---------------+---------------+
1313    12| 0x00          | 0x00          | 0x00          | 0x00          |
1314      +---------------+---------------+---------------+---------------+
1315    16| 0x00          | 0x00          | 0x00          | 0x00          |
1316      +---------------+---------------+---------------+---------------+
1317    20| 0x00          | 0x00          | 0x00          | 0x00          |
1318      +---------------+---------------+---------------+---------------+
1319    24| 0x48 ('H')    | 0x65 ('e')    | 0x6c ('l')    | 0x6c ('l')    |
1320      +---------------+---------------+---------------+---------------+
1321    28| 0x6f ('o')    |
1322      +---------------+
1323
1324      Total 29 bytes (24 byte header, 5 byte value)
1325      </artwork></figure>
1326      </t>
1327      <t>
1328      The server will respond with the following package:
1329      <figure>
1330        <preamble>Delete response:</preamble>
1331          <artwork>
1332
1333    Byte/     0       |       1       |       2       |       3       |
1334       /              |               |               |               |
1335      |0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|
1336      +---------------+---------------+---------------+---------------+
1337     0| 0x81          | 0x04          | 0x00          | 0x00          |
1338      +---------------+---------------+---------------+---------------+
1339     4| 0x00          | 0x00          | 0x00          | 0x00          |
1340      +---------------+---------------+---------------+---------------+
1341     8| 0x00          | 0x00          | 0x00          | 0x00          |
1342      +---------------+---------------+---------------+---------------+
1343    12| 0x00          | 0x00          | 0x00          | 0x00          |
1344      +---------------+---------------+---------------+---------------+
1345    16| 0x00          | 0x00          | 0x00          | 0x00          |
1346      +---------------+---------------+---------------+---------------+
1347    20| 0x00          | 0x00          | 0x00          | 0x00          |
1348      +---------------+---------------+---------------+---------------+
1349
1350      Total 24 bytes
1351      </artwork></figure>
1352
1353      The above packet is interpreted as:
1354
1355Field        (offset) (value)
1356Magic        (0)    : 0x81
1357Opcode       (1)    : 0x04
1358Key length   (2,3)  : 0x0000
1359Extra length (4)    : 0x00
1360Data type    (5)    : 0x00
1361Status       (6,7)  : 0x0000
1362Total body   (8-11) : 0x00000000
1363Opaque       (12-15): 0x00000000
1364CAS          (16-23): 0x0000000000000000
1365Extras              : None
1366Key                 : None
1367Value               : None
1368    </t>
1369
1370    <t>
1371        To flush the cache (delete all items), the set the following values
1372        in the request:
1373
1374Field        (offset) (value)
1375Magic        (0)    : 0x80
1376Opcode       (1)    : 0x08
1377Key length   (2,3)  : 0x0000
1378Extra length (4)    : 0x00
1379Data type    (5)    : 0x00
1380Reserved     (6,7)  : 0x0000
1381Total body   (8-11) : 0x00000000
1382Opaque       (12-15): 0x00000000
1383CAS          (16-23): 0x0000000000000000
1384Extras              : None
1385Key                 : None
1386Value               : None
1387
1388The packet looks like:
1389
1390      <figure>
1391        <preamble>Flush request:</preamble>
1392          <artwork>
1393
1394    Byte/     0       |       1       |       2       |       3       |
1395       /              |               |               |               |
1396      |0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|
1397      +---------------+---------------+---------------+---------------+
1398     0| 0x80          | 0x08          | 0x00          | 0x00          |
1399      +---------------+---------------+---------------+---------------+
1400     4| 0x00          | 0x00          | 0x00          | 0x00          |
1401      +---------------+---------------+---------------+---------------+
1402     8| 0x00          | 0x00          | 0x00          | 0x00          |
1403      +---------------+---------------+---------------+---------------+
1404    12| 0x00          | 0x00          | 0x00          | 0x00          |
1405      +---------------+---------------+---------------+---------------+
1406    16| 0x00          | 0x00          | 0x00          | 0x00          |
1407      +---------------+---------------+---------------+---------------+
1408    20| 0x00          | 0x00          | 0x00          | 0x00          |
1409      +---------------+---------------+---------------+---------------+
1410
1411      Total 24 bytes
1412      </artwork></figure></t>
1413
1414      <t>
1415          The server responds with:
1416      <figure>
1417        <preamble>Flush response:</preamble>
1418          <artwork>
1419
1420    Byte/     0       |       1       |       2       |       3       |
1421       /              |               |               |               |
1422      |0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|
1423      +---------------+---------------+---------------+---------------+
1424     0| 0x81          | 0x08          | 0x00          | 0x00          |
1425      +---------------+---------------+---------------+---------------+
1426     4| 0x00          | 0x00          | 0x00          | 0x00          |
1427      +---------------+---------------+---------------+---------------+
1428     8| 0x00          | 0x00          | 0x00          | 0x00          |
1429      +---------------+---------------+---------------+---------------+
1430    12| 0x00          | 0x00          | 0x00          | 0x00          |
1431      +---------------+---------------+---------------+---------------+
1432    16| 0x00          | 0x00          | 0x00          | 0x00          |
1433      +---------------+---------------+---------------+---------------+
1434    20| 0x00          | 0x00          | 0x00          | 0x00          |
1435      +---------------+---------------+---------------+---------------+
1436
1437      Total 24 bytes
1438      </artwork></figure>
1439
1440      The above packet is interpreted as:
1441
1442Field        (offset) (value)
1443Magic        (0)    : 0x81
1444Opcode       (1)    : 0x08
1445Key length   (2,3)  : 0x0000
1446Extra length (4)    : 0x00
1447Data type    (5)    : 0x00
1448Status       (6,7)  : 0x0000
1449Total body   (8-11) : 0x00000000
1450Opaque       (12-15): 0x00000000
1451CAS          (16-23): 0x0000000000000000
1452Extras              : None
1453Key                 : None
1454Value               : None
1455      </t>
1456
1457      <t>
1458        Lets use the increment function to increment a 64-bit variable stored
1459        as the key "counter". We specify 0 as the initial value, and set it to
1460        expire after 2 hours. To do so, the following values must be set in
1461        the request:
1462
1463Field        (offset) (value)
1464Magic        (0)    : 0x80
1465Opcode       (1)    : 0x05
1466Key length   (2,3)  : 0x0007
1467Extra length (4)    : 0x14
1468Data type    (5)    : 0x00
1469Reserved     (6,7)  : 0x0000
1470Total body   (8-11) : 0x0000001b
1471Opaque       (12-15): 0x00000000
1472CAS          (16-23): 0x0000000000000000
1473Extras              :
1474  delta      (24-31): 0x0000000000000001
1475  initial    (32-39): 0x0000000000000000
1476  exipration (40-43): 0x00000e10
1477Key                 : Textual string "counter"
1478Value               : None
1479
1480The packet looks like:
1481
1482      <figure>
1483        <preamble>Increment request:</preamble>
1484          <artwork>
1485
1486    Byte/     0       |       1       |       2       |       3       |
1487       /              |               |               |               |
1488      |0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|
1489      +---------------+---------------+---------------+---------------+
1490     0| 0x80          | 0x05          | 0x00          | 0x07          |
1491      +---------------+---------------+---------------+---------------+
1492     4| 0x14          | 0x00          | 0x00          | 0x00          |
1493      +---------------+---------------+---------------+---------------+
1494     8| 0x00          | 0x00          | 0x00          | 0x1b          |
1495      +---------------+---------------+---------------+---------------+
1496    12| 0x00          | 0x00          | 0x00          | 0x00          |
1497      +---------------+---------------+---------------+---------------+
1498    16| 0x00          | 0x00          | 0x00          | 0x00          |
1499      +---------------+---------------+---------------+---------------+
1500    20| 0x00          | 0x00          | 0x00          | 0x00          |
1501      +---------------+---------------+---------------+---------------+
1502    24| 0x00          | 0x00          | 0x00          | 0x00          |
1503      +---------------+---------------+---------------+---------------+
1504    28| 0x00          | 0x00          | 0x00          | 0x01          |
1505      +---------------+---------------+---------------+---------------+
1506    32| 0x00          | 0x00          | 0x00          | 0x00          |
1507      +---------------+---------------+---------------+---------------+
1508    36| 0x00          | 0x00          | 0x00          | 0x00          |
1509      +---------------+---------------+---------------+---------------+
1510    40| 0x00          | 0x00          | 0x0e          | 0x10          |
1511      +---------------+---------------+---------------+---------------+
1512    44| 0x63 ('c')    | 0x6f ('o')    | 0x75 ('u')    | 0x6e ('n')    |
1513      +---------------+---------------+---------------+---------------+
1514    48| 0x74 ('t')    | 0x65 ('e')    | 0x72 ('r')    |
1515      +---------------+---------------+---------------+
1516
1517      Total 51 bytes (24 byte header, 20 byte extras, 7 byte key)
1518      </artwork></figure></t>
1519
1520      <t>
1521          Since the key doesn't exist, the server will respond with the
1522          initial value:
1523
1524      <figure>
1525        <preamble>Increment response:</preamble>
1526          <artwork>
1527
1528    Byte/     0       |       1       |       2       |       3       |
1529       /              |               |               |               |
1530      |0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|
1531      +---------------+---------------+---------------+---------------+
1532     0| 0x81          | 0x05          | 0x00          | 0x00          |
1533      +---------------+---------------+---------------+---------------+
1534     4| 0x00          | 0x00          | 0x00          | 0x00          |
1535      +---------------+---------------+---------------+---------------+
1536     8| 0x00          | 0x00          | 0x00          | 0x08          |
1537      +---------------+---------------+---------------+---------------+
1538    12| 0x00          | 0x00          | 0x00          | 0x00          |
1539      +---------------+---------------+---------------+---------------+
1540    16| 0x00          | 0x00          | 0x00          | 0x00          |
1541      +---------------+---------------+---------------+---------------+
1542    20| 0x00          | 0x00          | 0x00          | 0x05          |
1543      +---------------+---------------+---------------+---------------+
1544    24| 0x00          | 0x00          | 0x00          | 0x00          |
1545      +---------------+---------------+---------------+---------------+
1546    28| 0x00          | 0x00          | 0x00          | 0x00          |
1547      +---------------+---------------+---------------+---------------+
1548
1549      Total 32 bytes (24 byte header, 8 byte value)
1550      </artwork></figure>
1551
1552      The above packet is interpreted as:
1553
1554Field        (offset) (value)
1555Magic        (0)    : 0x81
1556Opcode       (1)    : 0x05
1557Key length   (2,3)  : 0x0000
1558Extra length (4)    : 0x00
1559Data type    (5)    : 0x00
1560Status       (6,7)  : 0x0000
1561Total body   (8-11) : 0x00000008
1562Opaque       (12-15): 0x00000000
1563CAS          (16-23): 0x0000000000000005
1564Extras              : None
1565Key                 : None
1566Value               : 0x0000000000000000
1567      </t>
1568
1569      <t>
1570        Lets use the decrement function to decrement a 64-bit variable stored
1571        as the key "counter". We specify 0 as the initial value, and set it to
1572        expire after 2 hours. To do so, the following values must be set in
1573        the request:
1574
1575Field        (offset) (value)
1576Magic        (0)    : 0x80
1577Opcode       (1)    : 0x06
1578Key length   (2,3)  : 0x0007
1579Extra length (4)    : 0x14
1580Data type    (5)    : 0x00
1581Reserved     (6,7)  : 0x0000
1582Total body   (8-11) : 0x0000001b
1583Opaque       (12-15): 0x00000000
1584CAS          (16-23): 0x0000000000000000
1585Extras              :
1586  delta      (24-31): 0x0000000000000001
1587  initial    (32-39): 0x0000000000000000
1588  exipration (40-43): 0x00000e10
1589Key                 : Textual string "counter"
1590Value               : None
1591
1592The packet looks like:
1593
1594      <figure>
1595        <preamble>Decrement request:</preamble>
1596          <artwork>
1597
1598    Byte/     0       |       1       |       2       |       3       |
1599       /              |               |               |               |
1600      |0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|
1601      +---------------+---------------+---------------+---------------+
1602     0| 0x80          | 0x06          | 0x00          | 0x07          |
1603      +---------------+---------------+---------------+---------------+
1604     4| 0x14          | 0x00          | 0x00          | 0x00          |
1605      +---------------+---------------+---------------+---------------+
1606     8| 0x00          | 0x00          | 0x00          | 0x1b          |
1607      +---------------+---------------+---------------+---------------+
1608    12| 0x00          | 0x00          | 0x00          | 0x00          |
1609      +---------------+---------------+---------------+---------------+
1610    16| 0x00          | 0x00          | 0x00          | 0x00          |
1611      +---------------+---------------+---------------+---------------+
1612    20| 0x00          | 0x00          | 0x00          | 0x00          |
1613      +---------------+---------------+---------------+---------------+
1614    24| 0x00          | 0x00          | 0x00          | 0x00          |
1615      +---------------+---------------+---------------+---------------+
1616    28| 0x00          | 0x00          | 0x00          | 0x01          |
1617      +---------------+---------------+---------------+---------------+
1618    32| 0x00          | 0x00          | 0x00          | 0x00          |
1619      +---------------+---------------+---------------+---------------+
1620    36| 0x00          | 0x00          | 0x00          | 0x00          |
1621      +---------------+---------------+---------------+---------------+
1622    40| 0x00          | 0x00          | 0x0e          | 0x10          |
1623      +---------------+---------------+---------------+---------------+
1624    44| 0x63 ('c')    | 0x6f ('o')    | 0x75 ('u')    | 0x6e ('n')    |
1625      +---------------+---------------+---------------+---------------+
1626    48| 0x74 ('t')    | 0x65 ('e')    | 0x72 ('r')    |
1627      +---------------+---------------+---------------+
1628
1629      Total 51 bytes (24 byte header, 20 byte extras, 7 byte key)
1630      </artwork></figure></t>
1631
1632      <t>
1633          The server will decrement the counter and return the following packet:
1634
1635      <figure>
1636        <preamble>Decrement response:</preamble>
1637          <artwork>
1638
1639    Byte/     0       |       1       |       2       |       3       |
1640       /              |               |               |               |
1641      |0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|
1642      +---------------+---------------+---------------+---------------+
1643     0| 0x81          | 0x06          | 0x00          | 0x00          |
1644      +---------------+---------------+---------------+---------------+
1645     4| 0x00          | 0x00          | 0x00          | 0x00          |
1646      +---------------+---------------+---------------+---------------+
1647     8| 0x00          | 0x00          | 0x00          | 0x08          |
1648      +---------------+---------------+---------------+---------------+
1649    12| 0x00          | 0x00          | 0x00          | 0x00          |
1650      +---------------+---------------+---------------+---------------+
1651    16| 0x00          | 0x00          | 0x00          | 0x00          |
1652      +---------------+---------------+---------------+---------------+
1653    20| 0x00          | 0x00          | 0x00          | 0x00          |
1654      +---------------+---------------+---------------+---------------+
1655    24| 0x00          | 0x00          | 0x00          | 0x00          |
1656      +---------------+---------------+---------------+---------------+
1657    28| 0x00          | 0x00          | 0x00          | 0x00          |
1658      +---------------+---------------+---------------+---------------+
1659
1660      Total: 32 bytes (24 byte header, 8 byte value)
1661      </artwork></figure>
1662
1663      The above packet is interpreted as:
1664
1665Field        (offset) (value)
1666Magic        (0)    : 0x81
1667Opcode       (1)    : 0x06
1668Key length   (2,3)  : 0x0000
1669Extra length (4)    : 0x00
1670Data type    (5)    : 0x00
1671Status       (6,7)  : 0x0000
1672Total body   (8-11) : 0x00000008
1673Opaque       (12-15): 0x00000000
1674CAS          (16-23): 0x0000000000000000
1675Extras              : None
1676Key                 : None
1677Value               : 0x0000000000000000
1678
1679       You might be wondering why 0 is returned and not -1 (since the key
1680       exists with the value 0), but the decr-function will never return a
1681       negative number.
1682      </t>
1683
1684      <t>
1685          To end the session to the server, set the following values in the
1686          header:
1687
1688Field        (offset) (value)
1689Magic        (0)    : 0x80
1690Opcode       (1)    : 0x07
1691Key length   (2,3)  : 0x0000
1692Extra length (4)    : 0x00
1693Data type    (5)    : 0x00
1694Reserved     (6,7)  : 0x0000
1695Total body   (8-11) : 0x00000000
1696Opaque       (12-15): 0x00000000
1697CAS          (16-23): 0x0000000000000000
1698Extras              : None
1699Key                 : None
1700Value               : None
1701
1702The packet looks like:
1703
1704      <figure>
1705        <preamble>Quit request:</preamble>
1706          <artwork>
1707
1708    Byte/     0       |       1       |       2       |       3       |
1709       /              |               |               |               |
1710      |0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|
1711      +---------------+---------------+---------------+---------------+
1712     0| 0x80          | 0x07          | 0x00          | 0x00          |
1713      +---------------+---------------+---------------+---------------+
1714     4| 0x00          | 0x00          | 0x00          | 0x00          |
1715      +---------------+---------------+---------------+---------------+
1716     8| 0x00          | 0x00          | 0x00          | 0x00          |
1717      +---------------+---------------+---------------+---------------+
1718    12| 0x00          | 0x00          | 0x00          | 0x00          |
1719      +---------------+---------------+---------------+---------------+
1720    16| 0x00          | 0x00          | 0x00          | 0x00          |
1721      +---------------+---------------+---------------+---------------+
1722    20| 0x00          | 0x00          | 0x00          | 0x00          |
1723      +---------------+---------------+---------------+---------------+
1724
1725      Total 24 bytes
1726      </artwork></figure></t>
1727
1728      <t>
1729      The server will respond with the following packet:
1730
1731      <figure>
1732        <preamble>Quit response:</preamble>
1733          <artwork>
1734
1735    Byte/     0       |       1       |       2       |       3       |
1736       /              |               |               |               |
1737      |0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|
1738      +---------------+---------------+---------------+---------------+
1739     0| 0x81          | 0x07          | 0x00          | 0x00          |
1740      +---------------+---------------+---------------+---------------+
1741     4| 0x00          | 0x00          | 0x00          | 0x00          |
1742      +---------------+---------------+---------------+---------------+
1743     8| 0x00          | 0x00          | 0x00          | 0x00          |
1744      +---------------+---------------+---------------+---------------+
1745    12| 0x00          | 0x00          | 0x00          | 0x00          |
1746      +---------------+---------------+---------------+---------------+
1747    16| 0x00          | 0x00          | 0x00          | 0x00          |
1748      +---------------+---------------+---------------+---------------+
1749    20| 0x00          | 0x00          | 0x00          | 0x00          |
1750      +---------------+---------------+---------------+---------------+
1751
1752      Total 24 bytes
1753      </artwork></figure>
1754
1755      The above packet is interpreted as:
1756
1757Field        (offset) (value)
1758Magic        (0)    : 0x81
1759Opcode       (1)    : 0x07
1760Key length   (2,3)  : 0x0000
1761Extra length (4)    : 0x00
1762Data type    (5)    : 0x00
1763Status       (6,7)  : 0x0000
1764Total body   (8-11) : 0x00000000
1765Opaque       (12-15): 0x00000000
1766CAS          (16-23): 0x0000000000000000
1767Extras              : None
1768Key                 : None
1769Value               : None
1770      </t>
1771    </section>
1772
1773    <section anchor="security" title="Security Considerations">
1774      <t>
1775      Memcache has no authentication or security layers whatsoever.  It is
1776      RECOMMENDED that memcache be deployed strictly on closed, protected,
1777      back-end networks within a single data center, within a single cluster of
1778      servers, or even on a single host, providing shared caching for multiple
1779      applications. Memcache MUST NOT be made available on a public network.
1780      </t>
1781    </section>
1782
1783  </middle>
1784
1785  <back>
1786    <references title="Normative References">
1787      <reference anchor="LJ">
1788        <front>
1789          <title>LJ NEEDS MOAR SPEED</title>
1790          <author fullname="Brad Fitzpatrick">
1791            <organization>Danga Interactive</organization>
1792          </author>
1793          <date day="5" month="10" year="1999" />
1794          <abstract>
1795            <t>http://www.livejournal.com/</t>
1796          </abstract>
1797        </front>
1798      </reference>
1799      <dwdrfc-ref anchor='UDP' src='http://xml.resource.org/public/rfc/bibxml/reference.RFC.0768.xml'/>
1800      <dwdrfc-ref anchor='KEYWORDS' src='http://xml.resource.org/public/rfc/bibxml/reference.RFC.2119.xml'/>
1801    </references>
1802
1803    <section anchor="acknowledgments" title="Acknowledgments">
1804      <t>
1805      Thanks to Brad Fitzpatrick, Anatoly Vorobey, Steven Grimm, and Dustin
1806      Sallings, for their work on the memcached server.
1807      </t>
1808
1809      <t>
1810      Thanks to Sean Chittenden, Jonathan Steinert, Brian Aker, Evan Martin,
1811      Nathan Neulinger, Eric Hodel, Michael Johnson, Paul Querna, Jamie
1812      McCarthy, Philip Neustrom, Andrew O'Brien, Josh Rotenberg, Robin H.
1813      Johnson, Tim Yardley, Paolo Borelli, Eli Bingham, Jean-Francois
1814      Bustarret, Paul G, Paul Lindner, Alan Kasindorf, Chris Goffinet, Tomash
1815      Brechko, and others for their work reporting bugs and maintaining
1816      memcached client libraries and bindings in many languages.
1817      </t>
1818    </section>
1819  </back>
1820
1821</rfc>
1822
1823