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'?>
7
<?rfc sortrefs="yes" ?>
9
<?rfc subcompact="yes" ?>
10
<rfc category="info" docName="draft-stone-memcache-binary-01" ipr="none">
12
<title> Memcache Binary Protocol </title>
14
<author fullname="Aaron Stone" surname="Stone" role="editor">
15
<organization>Six Apart, Ltd.</organization>
18
<street>548 4th Street</street>
19
<city>San Francisco</city>
22
<country>USA</country>
24
<email>aaron@serendipity.palo-alto.ca.us</email>
27
<author fullname="Trond Norbye" surname="Norbye" role="editor">
28
<organization>Sun Microsystems, INC</organization>
31
<street>Haakon VII g. 7B</street>
32
<city>Trondheim</city>
33
<code>NO-7485 Trondheim</code>
34
<country>Norway</country>
36
<email>trond.norbye@sun.com</email>
39
<date day="28" month="August" year="2008" />
40
<area>Applications</area>
41
<keyword>memcache memcached cache</keyword>
44
This memo explains the memcache binary protocol for informational
48
Memcache is a high performance key-value cache. It is intentionally a
49
dumb cache, optimized for speed only. Applications using memcache do
50
not rely on it for data -- a persistent database with guaranteed
51
reliability is strongly recommended -- but applications can run much
52
faster when cached data is available in memcache.
58
<section anchor="introduction" title="Introduction">
60
Memcache is a high performance key-value cache. It is intentionally a
61
dumb cache, optimized for speed only. Applications using memcache should
62
not rely on it for data -- a persistent database with guaranteed
63
reliability is strongly recommended -- but applications can run much
64
faster when cached data is available in memcache.
67
Memcache was originally written to make
68
<xref target="LJ">LiveJournal</xref> faster. It now powers all of
69
the fastest web sites that you love.
71
<section anchor="conventions" title="Conventions Used In This Document">
73
The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT",
74
"SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in this
75
document are to be interpreted as described in
76
<xref target="KEYWORDS"/>.
81
<section anchor="packet" title="Packet Structure">
83
<preamble>General format of a packet:</preamble>
87
|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|
88
+---------------+---------------+---------------+---------------+
93
+---------------+---------------+---------------+---------------+
94
24/ COMMAND-SPECIFIC EXTRAS (as needed) /
95
+/ (note length in the extras length header field) /
96
+---------------+---------------+---------------+---------------+
98
+/ (note length in key length header field) /
99
+---------------+---------------+---------------+---------------+
100
n/ Value (as needed) /
101
+/ (note length is total body length header field, minus /
102
+/ sum of the extras and key length body fields) /
103
+---------------+---------------+---------------+---------------+
109
<preamble>Request header:</preamble>
111
Byte/ 0 | 1 | 2 | 3 |
113
|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|
114
+---------------+---------------+---------------+---------------+
115
0| Magic | Opcode | Key length |
116
+---------------+---------------+---------------+---------------+
117
4| Extras length | Data type | Reserved |
118
+---------------+---------------+---------------+---------------+
119
8| Total body length |
120
+---------------+---------------+---------------+---------------+
122
+---------------+---------------+---------------+---------------+
125
+---------------+---------------+---------------+---------------+
131
<preamble>Response header:</preamble>
133
Byte/ 0 | 1 | 2 | 3 |
135
|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|
136
+---------------+---------------+---------------+---------------+
137
0| Magic | Opcode | Key Length |
138
+---------------+---------------+---------------+---------------+
139
4| Extras length | Data type | Status |
140
+---------------+---------------+---------------+---------------+
141
8| Total body length |
142
+---------------+---------------+---------------+---------------+
144
+---------------+---------------+---------------+---------------+
147
+---------------+---------------+---------------+---------------+
154
<list hangIndent="20" style="hanging">
155
<t hangText="Magic">Magic number.</t>
156
<t hangText="Opcode">Command code.</t>
157
<t hangText="Key length">Length in bytes of the text key that follows the command extras.</t>
158
<t hangText="Status">Status of the response (non-zero on error).</t>
159
<t hangText="Extras length">Length in bytes of the command extras.</t>
160
<t hangText="Data type">Reserved for future use (Sean is using this soon).</t>
161
<t hangText="Reserved">Really reserved for future use (up for grabs).</t>
162
<t hangText="Total body length">Length in bytes of extra + key + value.</t>
163
<t hangText="Opaque">Will be copied back to you in the response.</t>
164
<t hangText="CAS">Data version check.</t>
169
<section anchor="values" title="Defined Values">
170
<section anchor="value-magic" title="Magic Byte">
172
<list hangIndent="8" style="hanging">
173
<t hangText="0x80">Request packet for this protocol version</t>
174
<t hangText="0x81">Response packet for this protocol version</t>
179
Magic byte / version. For each version of the protocol, we'll use a
180
different request/response value pair. This is useful for protocol
181
analyzers to distinguish the nature of the packet from the direction
182
which it is moving. Note, it is common to run a memcached instance on
183
a host that also runs an application server. Such a host will both
184
send and receive memcache packets.
188
The version should hopefully correspond only to different meanings of
189
the command byte. In an ideal world, we will not change the header
190
format. As reserved bytes are given defined meaning, the protocol
191
version / magic byte values should be incremented.
195
Traffic analysis tools are encouraged to identify memcache packets
196
and provide detailed interpretation if the magic bytes are recognized
197
and otherwise to provide a generic breakdown of the packet. Note, that
198
the key and value positions can always be identified even if the magic
199
byte or command opcode are not recognized.
203
<section anchor="value-status" title="Response Status">
205
Possible values of this two-byte field:
206
<list hangIndent="8" style="hanging">
207
<t hangText="0x0000">No error</t>
208
<t hangText="0x0001">Key not found</t>
209
<t hangText="0x0002">Key exists</t>
210
<t hangText="0x0003">Value too large</t>
211
<t hangText="0x0004">Invalid arguments</t>
212
<t hangText="0x0005">Item not stored</t>
213
<t hangText="0x0006">Incr/Decr on non-numeric value.</t>
214
<t hangText="0x0081">Unknown command</t>
215
<t hangText="0x0082">Out of memory</t>
220
<section anchor="value-opcodes" title="Command Opcodes">
222
Possible values of the one-byte field:
223
<list hangIndent="8" style="hanging">
224
<t hangText="0x00">Get</t>
225
<t hangText="0x01">Set</t>
226
<t hangText="0x02">Add</t>
227
<t hangText="0x03">Replace</t>
228
<t hangText="0x04">Delete</t>
229
<t hangText="0x05">Increment</t>
230
<t hangText="0x06">Decrement</t>
231
<t hangText="0x07">Quit</t>
232
<t hangText="0x08">Flush</t>
233
<t hangText="0x09">GetQ</t>
234
<t hangText="0x0A">No-op</t>
235
<t hangText="0x0B">Version</t>
236
<t hangText="0x0C">GetK</t>
237
<t hangText="0x0D">GetKQ</t>
238
<t hangText="0x0E">Append</t>
239
<t hangText="0x0F">Prepend</t>
240
<t hangText="0x10">Stat</t>
241
<t hangText="0x11">SetQ</t>
242
<t hangText="0x12">AddQ</t>
243
<t hangText="0x13">ReplaceQ</t>
244
<t hangText="0x14">DeleteQ</t>
245
<t hangText="0x15">IncrementQ</t>
246
<t hangText="0x16">DecrementQ</t>
247
<t hangText="0x17">QuitQ</t>
248
<t hangText="0x18">FlushQ</t>
249
<t hangText="0x19">AppendQ</t>
250
<t hangText="0x1A">PrependQ</t>
254
As a convention all of the commands ending with "Q" for
255
Quiet. A quiet version of a command will omit responses
256
that are considered uninteresting. Whether a given response
257
is interesting is dependent upon the command. See the
259
<xref target="command-get">set commands</xref>
260
and <xref target="command-set">set commands</xref> for
261
examples of commands that include quiet variants.
265
<section anchor="value-types" title="Data Types">
267
Possible values of the one-byte field:
268
<list hangIndent="8" style="hanging">
269
<t hangText="0x00">Raw bytes</t>
275
<section title="Commands">
276
<section anchor="command-introduction" title="Introduction">
278
All communication is initiated by a request from the client,
279
and the server will respond to each request with zero or
280
multiple packets for each request. If the status code of a response
281
packet is non-nil, the body of the packet will contain a textual error
282
message. If the status code is nil, the command opcode will define the
283
layout of the body of the message.
285
<section anchor="command-introduction-example" title="Example">
287
The following figure illustrates the packet layout for
288
a packet with an error message.
291
<preamble>Packet layout:</preamble>
294
Byte/ 0 | 1 | 2 | 3 |
296
|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|
297
+---------------+---------------+---------------+---------------+
298
0| 0x81 | 0x00 | 0x00 | 0x00 |
299
+---------------+---------------+---------------+---------------+
300
4| 0x00 | 0x00 | 0x00 | 0x01 |
301
+---------------+---------------+---------------+---------------+
302
8| 0x00 | 0x00 | 0x00 | 0x09 |
303
+---------------+---------------+---------------+---------------+
304
12| 0x00 | 0x00 | 0x00 | 0x00 |
305
+---------------+---------------+---------------+---------------+
306
16| 0x00 | 0x00 | 0x00 | 0x00 |
307
+---------------+---------------+---------------+---------------+
308
20| 0x00 | 0x00 | 0x00 | 0x00 |
309
+---------------+---------------+---------------+---------------+
310
24| 0x4e ('N') | 0x6f ('o') | 0x74 ('t') | 0x20 (' ') |
311
+---------------+---------------+---------------+---------------+
312
28| 0x66 ('f') | 0x6f ('o') | 0x75 ('u') | 0x6e ('n') |
313
+---------------+---------------+---------------+---------------+
316
Total 33 bytes (24 byte header, and 9 bytes value)
318
Field (offset) (value)
321
Key length (2,3) : 0x0000
322
Extra length (4) : 0x00
324
Status (6,7) : 0x0001
325
Total body (8-11) : 0x00000009
326
Opaque (12-15): 0x00000000
327
CAS (16-23): 0x0000000000000000
330
Value (24-32): The textual string "Not found"
336
<section anchor="command-get" title="Get, Get Quietly, Get Key, Get Key Quietly">
342
<t>MUST NOT have extras.</t>
343
<t>MUST have key.</t>
344
<t>MUST NOT have value.</t>
349
<list style="symbols">
359
<t>MUST have extras.</t>
361
<t>MAY have value.</t>
366
<list style="symbols">
373
<preamble>Extra data for the get commands:</preamble>
375
Byte/ 0 | 1 | 2 | 3 |
377
|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|
378
+---------------+---------------+---------------+---------------+
380
+---------------+---------------+---------------+---------------+
388
The get command gets a single key. The getq command is both mum
389
on cache miss and quiet, holding its response until a non-quiet
390
command is issued. Getk and getkq differs from get and getq by
391
adding the key into the response packet.
395
You're not guaranteed a response to a getq/getkq cache hit until
396
you send a non-getq/getkq command later, which uncorks the
397
server and bundles up IOs to send to the client in one go.
401
Clients should implement multi-get (still important for
402
reducing network roundtrips!) as n pipelined requests, the
403
first n-1 being getq/getkq, the last being a regular
404
get/getk. That way you're guaranteed to get a response, and
405
you know when the server's done. You can also do the naive
406
thing and send n pipelined get/getks, but then you could potentially
407
get back a lot of "NOT_FOUND" error code packets.
408
Alternatively, you can send 'n' getq/getkqs, followed by a
412
<section anchor="command-get-example" title="Example">
414
To request the data associated with the key "Hello" the
415
following fields must be specified in the packet.
418
<preamble>get request:</preamble>
420
Byte/ 0 | 1 | 2 | 3 |
422
|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|
423
+---------------+---------------+---------------+---------------+
424
0| 0x80 | 0x00 | 0x00 | 0x05 |
425
+---------------+---------------+---------------+---------------+
426
4| 0x00 | 0x00 | 0x00 | 0x00 |
427
+---------------+---------------+---------------+---------------+
428
8| 0x00 | 0x00 | 0x00 | 0x05 |
429
+---------------+---------------+---------------+---------------+
430
12| 0x00 | 0x00 | 0x00 | 0x00 |
431
+---------------+---------------+---------------+---------------+
432
16| 0x00 | 0x00 | 0x00 | 0x00 |
433
+---------------+---------------+---------------+---------------+
434
20| 0x00 | 0x00 | 0x00 | 0x00 |
435
+---------------+---------------+---------------+---------------+
436
24| 0x48 ('H') | 0x65 ('e') | 0x6c ('l') | 0x6c ('l') |
437
+---------------+---------------+---------------+---------------+
441
Total 29 bytes (24 byte header, and 5 bytes key)
443
Field (offset) (value)
446
Key length (2,3) : 0x0005
447
Extra length (4) : 0x00
449
Reserved (6,7) : 0x0000
450
Total body (8-11) : 0x00000005
451
Opaque (12-15): 0x00000000
452
CAS (16-23): 0x0000000000000000
454
Key (24-29): The textual string: "Hello"
458
<t>If the item exist on the server the following packet is returned,
459
otherwise a packet with status code != 0 will be returned (see
460
<xref target="command-introduction">Introduction</xref>)
463
<preamble>get/getq response:</preamble>
466
Byte/ 0 | 1 | 2 | 3 |
468
|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|
469
+---------------+---------------+---------------+---------------+
470
0| 0x81 | 0x00 | 0x00 | 0x00 |
471
+---------------+---------------+---------------+---------------+
472
4| 0x04 | 0x00 | 0x00 | 0x00 |
473
+---------------+---------------+---------------+---------------+
474
8| 0x00 | 0x00 | 0x00 | 0x09 |
475
+---------------+---------------+---------------+---------------+
476
12| 0x00 | 0x00 | 0x00 | 0x00 |
477
+---------------+---------------+---------------+---------------+
478
16| 0x00 | 0x00 | 0x00 | 0x00 |
479
+---------------+---------------+---------------+---------------+
480
20| 0x00 | 0x00 | 0x00 | 0x01 |
481
+---------------+---------------+---------------+---------------+
482
24| 0xde | 0xad | 0xbe | 0xef |
483
+---------------+---------------+---------------+---------------+
484
28| 0x57 ('W') | 0x6f ('o') | 0x72 ('r') | 0x6c ('l') |
485
+---------------+---------------+---------------+---------------+
489
Total 33 bytes (24 byte header, 4 byte extras and 5 byte value)
491
Field (offset) (value)
494
Key length (2,3) : 0x0000
495
Extra length (4) : 0x04
497
Status (6,7) : 0x0000
498
Total body (8-11) : 0x00000009
499
Opaque (12-15): 0x00000000
500
CAS (16-23): 0x0000000000000001
502
Flags (24-27): 0xdeadbeef
504
Value (28-32): The textual string "World"
508
<preamble>getk/getkq response:</preamble>
511
Byte/ 0 | 1 | 2 | 3 |
513
|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|
514
+---------------+---------------+---------------+---------------+
515
0| 0x81 | 0x00 | 0x00 | 0x05 |
516
+---------------+---------------+---------------+---------------+
517
4| 0x04 | 0x00 | 0x00 | 0x00 |
518
+---------------+---------------+---------------+---------------+
519
8| 0x00 | 0x00 | 0x00 | 0x09 |
520
+---------------+---------------+---------------+---------------+
521
12| 0x00 | 0x00 | 0x00 | 0x00 |
522
+---------------+---------------+---------------+---------------+
523
16| 0x00 | 0x00 | 0x00 | 0x00 |
524
+---------------+---------------+---------------+---------------+
525
20| 0x00 | 0x00 | 0x00 | 0x01 |
526
+---------------+---------------+---------------+---------------+
527
24| 0xde | 0xad | 0xbe | 0xef |
528
+---------------+---------------+---------------+---------------+
529
28| 0x48 ('H') | 0x65 ('e') | 0x6c ('l') | 0x6c ('l') |
530
+---------------+---------------+---------------+---------------+
531
32| 0x6f ('o') | 0x57 ('W') | 0x6f ('o') | 0x72 ('r') |
532
+---------------+---------------+---------------+---------------+
533
36| 0x6c ('l') | 0x64 ('d') |
534
+---------------+---------------+
536
Total 38 bytes (24 byte header, 4 byte extras, 5 byte key
539
Field (offset) (value)
542
Key length (2,3) : 0x0005
543
Extra length (4) : 0x04
545
Status (6,7) : 0x0000
546
Total body (8-11) : 0x00000009
547
Opaque (12-15): 0x00000000
548
CAS (16-23): 0x0000000000000001
550
Flags (24-27): 0xdeadbeef
551
Key (28-32): The textual string: "Hello"
552
Value (33-37): The textual string: "World"
558
<section anchor="command-set" title="Set, Add, Replace">
561
<t>MUST have extras.</t>
562
<t>MUST have key.</t>
563
<t>MUST have value.</t>
568
<list style="symbols">
570
<t>4 byte expiration time</t>
575
<preamble>Extra data for set/add/replace:</preamble>
577
Byte/ 0 | 1 | 2 | 3 |
579
|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|
580
+---------------+---------------+---------------+---------------+
582
+---------------+---------------+---------------+---------------+
584
+---------------+---------------+---------------+---------------+
590
If the Data Version Check (CAS) is nonzero, the requested
591
operation MUST only succeed if the item exists and has a CAS value
592
identical to the provided value.
596
Add MUST fail if the item already exist.
600
Replace MUST fail if the item doesn't exist.
604
Set should store the data unconditionally if the item exists
609
Quiet mutations only return responses on failure. Success
610
is considered the general case and is suppressed when in
611
quiet mode, but errors should not be allowed to go
615
<section anchor="command-set-example" title="Example">
616
<t>The following figure shows an add-command for
619
<t>Value: "World"</t>
620
<t>Flags: 0xdeadbeef</t>
621
<t>Expiry: in two hours</t>
625
<preamble>Add request:</preamble>
628
Byte/ 0 | 1 | 2 | 3 |
630
|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|
631
+---------------+---------------+---------------+---------------+
632
0| 0x80 | 0x02 | 0x00 | 0x05 |
633
+---------------+---------------+---------------+---------------+
634
4| 0x08 | 0x00 | 0x00 | 0x00 |
635
+---------------+---------------+---------------+---------------+
636
8| 0x00 | 0x00 | 0x00 | 0x12 |
637
+---------------+---------------+---------------+---------------+
638
12| 0x00 | 0x00 | 0x00 | 0x00 |
639
+---------------+---------------+---------------+---------------+
640
16| 0x00 | 0x00 | 0x00 | 0x00 |
641
+---------------+---------------+---------------+---------------+
642
20| 0x00 | 0x00 | 0x00 | 0x00 |
643
+---------------+---------------+---------------+---------------+
644
24| 0xde | 0xad | 0xbe | 0xef |
645
+---------------+---------------+---------------+---------------+
646
28| 0x00 | 0x00 | 0x0e | 0x10 |
647
+---------------+---------------+---------------+---------------+
648
32| 0x48 ('H') | 0x65 ('e') | 0x6c ('l') | 0x6c ('l') |
649
+---------------+---------------+---------------+---------------+
650
36| 0x6f ('o') | 0x57 ('W') | 0x6f ('o') | 0x72 ('r') |
651
+---------------+---------------+---------------+---------------+
652
40| 0x6c ('l') | 0x64 ('d') |
653
+---------------+---------------+
655
Total 42 bytes (24 byte header, 8 byte extras, 5 byte key and
658
Field (offset) (value)
661
Key length (2,3) : 0x0005
662
Extra length (4) : 0x08
664
Reserved (6,7) : 0x0000
665
Total body (8-11) : 0x00000012
666
Opaque (12-15): 0x00000000
667
CAS (16-23): 0x0000000000000000
669
Flags (24-27): 0xdeadbeef
670
Expiry (28-31): 0x00000e10
671
Key (32-36): The textual string "Hello"
672
Value (37-41): The textual string "World"
676
The response-packet contains no extra data, and the result of the
677
operation is signaled through the status code. If the command
678
succeeds, the CAS value for the item is returned in the CAS-field
682
<preamble>Successful add response:</preamble>
685
Byte/ 0 | 1 | 2 | 3 |
687
|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|
688
+---------------+---------------+---------------+---------------+
689
0| 0x81 | 0x02 | 0x00 | 0x00 |
690
+---------------+---------------+---------------+---------------+
691
4| 0x00 | 0x00 | 0x00 | 0x00 |
692
+---------------+---------------+---------------+---------------+
693
8| 0x00 | 0x00 | 0x00 | 0x00 |
694
+---------------+---------------+---------------+---------------+
695
12| 0x00 | 0x00 | 0x00 | 0x00 |
696
+---------------+---------------+---------------+---------------+
697
16| 0x00 | 0x00 | 0x00 | 0x00 |
698
+---------------+---------------+---------------+---------------+
699
20| 0x00 | 0x00 | 0x00 | 0x01 |
700
+---------------+---------------+---------------+---------------+
704
Field (offset) (value)
707
Key length (2,3) : 0x0000
708
Extra length (4) : 0x00
710
Status (6,7) : 0x0000
711
Total body (8-11) : 0x00000000
712
Opaque (12-15): 0x00000000
713
CAS (16-23): 0x0000000000000001
722
<section anchor="command-delete" title="Delete">
725
<t>MUST NOT have extras.</t>
726
<t>MUST have key.</t>
727
<t>MUST NOT have value.</t>
732
Delete the item with the specific key.
735
<section anchor="command-delete-example" title="Example">
736
<t>The following figure shows a delete message for the
739
<preamble>Delete request:</preamble>
741
Byte/ 0 | 1 | 2 | 3 |
743
|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|
744
+---------------+---------------+---------------+---------------+
745
0| 0x80 | 0x04 | 0x00 | 0x05 |
746
+---------------+---------------+---------------+---------------+
747
4| 0x00 | 0x00 | 0x00 | 0x00 |
748
+---------------+---------------+---------------+---------------+
749
8| 0x00 | 0x00 | 0x00 | 0x05 |
750
+---------------+---------------+---------------+---------------+
751
12| 0x00 | 0x00 | 0x00 | 0x00 |
752
+---------------+---------------+---------------+---------------+
753
16| 0x00 | 0x00 | 0x00 | 0x00 |
754
+---------------+---------------+---------------+---------------+
755
20| 0x00 | 0x00 | 0x00 | 0x00 |
756
+---------------+---------------+---------------+---------------+
757
24| 0x48 ('H') | 0x65 ('e') | 0x6c ('l') | 0x6c ('l') |
758
+---------------+---------------+---------------+---------------+
762
Total 29 bytes (24 byte header, 5 byte value)
764
Field (offset) (value)
767
Key length (2,3) : 0x0005
768
Extra length (4) : 0x00
770
Reserved (6,7) : 0x0000
771
Total body (8-11) : 0x00000005
772
Opaque (12-15): 0x00000000
773
CAS (16-23): 0x0000000000000000
775
Key : The textual string "Hello"
780
The response-packet contains no extra data, and the result of the
781
operation is signaled through the status code.
786
<section anchor="command-incr" title="Increment, Decrement">
789
<t>MUST have extras.</t>
790
<t>MUST have key.</t>
791
<t>MUST NOT have value.</t>
796
<list style="symbols">
797
<t>8 byte value to add / subtract</t>
798
<t>8 byte initial value (unsigned)</t>
799
<t>4 byte expiration time</t>
803
<preamble>Extra data for incr/decr:</preamble>
805
Byte/ 0 | 1 | 2 | 3 |
807
|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|
808
+---------------+---------------+---------------+---------------+
811
+---------------+---------------+---------------+---------------+
814
+---------------+---------------+---------------+---------------+
816
+---------------+---------------+---------------+---------------+
822
These commands will either add or remove the specified
823
amount to the requested counter.
826
If the counter does not exist, one of two things may happen:
829
<list style="numbers">
830
<t>If the expiration value is all one-bits (0xffffffff), the
831
operation will fail with NOT_FOUND.</t>
832
<t>For all other expiration values, the operation will succeed
833
by seeding the value for this key with the provided initial
834
value to expire with the provided expiration time. The flags
835
will be set to zero.</t>
839
<preamble>incr/decr response body:</preamble>
841
Byte/ 0 | 1 | 2 | 3 |
843
|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|
844
+---------------+---------------+---------------+---------------+
845
0| 64-bit unsigned response. |
847
+---------------+---------------+---------------+---------------+
851
<section anchor="command-incr-example" title="Example">
852
<t>The following figure shows an incr-command for
854
<t>Key: "counter"</t>
857
<t>Expiry: in two hours</t>
861
<preamble>Increment request:</preamble>
864
Byte/ 0 | 1 | 2 | 3 |
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| 0x80 | 0x05 | 0x00 | 0x07 |
869
+---------------+---------------+---------------+---------------+
870
4| 0x14 | 0x00 | 0x00 | 0x00 |
871
+---------------+---------------+---------------+---------------+
872
8| 0x00 | 0x00 | 0x00 | 0x1b |
873
+---------------+---------------+---------------+---------------+
874
12| 0x00 | 0x00 | 0x00 | 0x00 |
875
+---------------+---------------+---------------+---------------+
876
16| 0x00 | 0x00 | 0x00 | 0x00 |
877
+---------------+---------------+---------------+---------------+
878
20| 0x00 | 0x00 | 0x00 | 0x00 |
879
+---------------+---------------+---------------+---------------+
880
24| 0x00 | 0x00 | 0x00 | 0x00 |
881
+---------------+---------------+---------------+---------------+
882
28| 0x00 | 0x00 | 0x00 | 0x01 |
883
+---------------+---------------+---------------+---------------+
884
32| 0x00 | 0x00 | 0x00 | 0x00 |
885
+---------------+---------------+---------------+---------------+
886
36| 0x00 | 0x00 | 0x00 | 0x00 |
887
+---------------+---------------+---------------+---------------+
888
40| 0x00 | 0x00 | 0x0e | 0x10 |
889
+---------------+---------------+---------------+---------------+
890
44| 0x63 ('c') | 0x6f ('o') | 0x75 ('u') | 0x6e ('n') |
891
+---------------+---------------+---------------+---------------+
892
48| 0x74 ('t') | 0x65 ('e') | 0x72 ('r') |
893
+---------------+---------------+---------------+
894
Total 51 bytes (24 byte header, 20 byte extras, 7 byte key)
896
Field (offset) (value)
899
Key length (2,3) : 0x0007
900
Extra length (4) : 0x14
902
Reserved (6,7) : 0x0000
903
Total body (8-11) : 0x0000001b
904
Opaque (12-15): 0x00000000
905
CAS (16-23): 0x0000000000000000
907
delta (24-31): 0x0000000000000001
908
initial (32-39): 0x0000000000000000
909
exipration (40-43): 0x00000e10
910
Key : Textual string "counter"
915
If the key doesn't exist, the server will respond with the
916
initial value. If not the incremented value will be returned.
917
Let's assume that the key didn't exist, so the initial value
921
<preamble>Increment response:</preamble>
923
Byte/ 0 | 1 | 2 | 3 |
925
|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|
926
+---------------+---------------+---------------+---------------+
927
0| 0x81 | 0x05 | 0x00 | 0x00 |
928
+---------------+---------------+---------------+---------------+
929
4| 0x00 | 0x00 | 0x00 | 0x00 |
930
+---------------+---------------+---------------+---------------+
931
8| 0x00 | 0x00 | 0x00 | 0x08 |
932
+---------------+---------------+---------------+---------------+
933
12| 0x00 | 0x00 | 0x00 | 0x00 |
934
+---------------+---------------+---------------+---------------+
935
16| 0x00 | 0x00 | 0x00 | 0x00 |
936
+---------------+---------------+---------------+---------------+
937
20| 0x00 | 0x00 | 0x00 | 0x05 |
938
+---------------+---------------+---------------+---------------+
939
24| 0x00 | 0x00 | 0x00 | 0x00 |
940
+---------------+---------------+---------------+---------------+
941
28| 0x00 | 0x00 | 0x00 | 0x00 |
942
+---------------+---------------+---------------+---------------+
943
Total 32 bytes (24 byte header, 8 byte value)
945
Field (offset) (value)
948
Key length (2,3) : 0x0000
949
Extra length (4) : 0x00
951
Status (6,7) : 0x0000
952
Total body (8-11) : 0x00000008
953
Opaque (12-15): 0x00000000
954
CAS (16-23): 0x0000000000000005
957
Value : 0x0000000000000000
963
<section anchor="command-quit" title="quit">
966
<t>MUST NOT have extras.</t>
967
<t>MUST NOT have key.</t>
968
<t>MUST NOT have value.</t>
973
Close the connection to the server.
976
<section anchor="command-quit-example" title="Example">
978
<preamble>Quit request:</preamble>
980
Byte/ 0 | 1 | 2 | 3 |
982
|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|
983
+---------------+---------------+---------------+---------------+
984
0| 0x80 | 0x07 | 0x00 | 0x00 |
985
+---------------+---------------+---------------+---------------+
986
4| 0x00 | 0x00 | 0x00 | 0x00 |
987
+---------------+---------------+---------------+---------------+
988
8| 0x00 | 0x00 | 0x00 | 0x00 |
989
+---------------+---------------+---------------+---------------+
990
12| 0x00 | 0x00 | 0x00 | 0x00 |
991
+---------------+---------------+---------------+---------------+
992
16| 0x00 | 0x00 | 0x00 | 0x00 |
993
+---------------+---------------+---------------+---------------+
994
20| 0x00 | 0x00 | 0x00 | 0x00 |
995
+---------------+---------------+---------------+---------------+
998
Field (offset) (value)
1001
Key length (2,3) : 0x0000
1002
Extra length (4) : 0x00
1003
Data type (5) : 0x00
1004
Reserved (6,7) : 0x0000
1005
Total body (8-11) : 0x00000000
1006
Opaque (12-15): 0x00000000
1007
CAS (16-23): 0x0000000000000000
1014
The response-packet contains no extra data, and the result of the
1015
operation is signaled through the status code. The server will
1016
then close the connection.
1021
<section anchor="command-flush" title="Flush">
1023
<list style="empty">
1024
<t>MAY have extras.</t>
1025
<t>MUST NOT have key.</t>
1026
<t>MUST NOT have value.</t>
1031
<list style="symbols">
1032
<t>4 byte expiration time</t>
1036
<preamble>Extra data for flush:</preamble>
1038
Byte/ 0 | 1 | 2 | 3 |
1040
|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|
1041
+---------------+---------------+---------------+---------------+
1043
+---------------+---------------+---------------+---------------+
1048
Flush the items in the cache now or some time in the future as
1049
specified by the expiration field. See the documentation of the
1050
textual protocol for the full description on how to specify the
1053
<section anchor="command-flush-example" title="Example">
1055
To flush the cache (delete all items) in two hours, the set
1056
the following values in the request
1059
<preamble>Flush request:</preamble>
1061
Byte/ 0 | 1 | 2 | 3 |
1063
|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|
1064
+---------------+---------------+---------------+---------------+
1065
0| 0x80 | 0x08 | 0x00 | 0x00 |
1066
+---------------+---------------+---------------+---------------+
1067
4| 0x04 | 0x00 | 0x00 | 0x00 |
1068
+---------------+---------------+---------------+---------------+
1069
8| 0x00 | 0x00 | 0x00 | 0x04 |
1070
+---------------+---------------+---------------+---------------+
1071
12| 0x00 | 0x00 | 0x00 | 0x00 |
1072
+---------------+---------------+---------------+---------------+
1073
16| 0x00 | 0x00 | 0x00 | 0x00 |
1074
+---------------+---------------+---------------+---------------+
1075
20| 0x00 | 0x00 | 0x00 | 0x00 |
1076
+---------------+---------------+---------------+---------------+
1077
24| 0x00 | 0x00 | 0x0e | 0x10 |
1078
+---------------+---------------+---------------+---------------+
1079
Total 28 bytes (24 byte header, 4 byte body)
1081
Field (offset) (value)
1084
Key length (2,3) : 0x0000
1085
Extra length (4) : 0x04
1086
Data type (5) : 0x00
1087
Reserved (6,7) : 0x0000
1088
Total body (8-11) : 0x00000004
1089
Opaque (12-15): 0x00000000
1090
CAS (16-23): 0x0000000000000000
1092
Expiry (24-27): 0x000e10
1098
The response-packet contains no extra data, and the result of the
1099
operation is signaled through the status code.
1103
<section anchor="command-noop" title="noop">
1105
<list style="empty">
1106
<t>MUST NOT have extras.</t>
1107
<t>MUST NOT have key.</t>
1108
<t>MUST NOT have value.</t>
1113
Used as a keep alive. Flushes outstanding getq/getkq's.
1115
<section anchor="command-noop-example" title="Example">
1117
<preamble>Noop request:</preamble>
1119
Byte/ 0 | 1 | 2 | 3 |
1121
|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|
1122
+---------------+---------------+---------------+---------------+
1123
0| 0x80 | 0x0a | 0x00 | 0x00 |
1124
+---------------+---------------+---------------+---------------+
1125
4| 0x00 | 0x00 | 0x00 | 0x00 |
1126
+---------------+---------------+---------------+---------------+
1127
8| 0x00 | 0x00 | 0x00 | 0x00 |
1128
+---------------+---------------+---------------+---------------+
1129
12| 0x00 | 0x00 | 0x00 | 0x00 |
1130
+---------------+---------------+---------------+---------------+
1131
16| 0x00 | 0x00 | 0x00 | 0x00 |
1132
+---------------+---------------+---------------+---------------+
1133
20| 0x00 | 0x00 | 0x00 | 0x00 |
1134
+---------------+---------------+---------------+---------------+
1137
Field (offset) (value)
1140
Key length (2,3) : 0x0000
1141
Extra length (4) : 0x00
1142
Data type (5) : 0x00
1143
Reserved (6,7) : 0x0000
1144
Total body (8-11) : 0x00000000
1145
Opaque (12-15): 0x00000000
1146
CAS (16-23): 0x0000000000000000
1153
The response-packet contains no extra data, and the result of the
1154
operation is signaled through the status code.
1159
<section anchor="command-version" title="version">
1161
<list style="empty">
1162
<t>MUST NOT have extras.</t>
1163
<t>MUST NOT have key.</t>
1164
<t>MUST NOT have value.</t>
1169
Request the server version.
1172
The server responds with a packet containing the version string
1173
in the body with the following format: "x.y.z"
1175
<section anchor="command-version-example" title="Example">
1177
<preamble>Version request:</preamble>
1179
Byte/ 0 | 1 | 2 | 3 |
1181
|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|
1182
+---------------+---------------+---------------+---------------+
1183
0| 0x80 | 0x0b | 0x00 | 0x00 |
1184
+---------------+---------------+---------------+---------------+
1185
4| 0x00 | 0x00 | 0x00 | 0x00 |
1186
+---------------+---------------+---------------+---------------+
1187
8| 0x00 | 0x00 | 0x00 | 0x00 |
1188
+---------------+---------------+---------------+---------------+
1189
12| 0x00 | 0x00 | 0x00 | 0x00 |
1190
+---------------+---------------+---------------+---------------+
1191
16| 0x00 | 0x00 | 0x00 | 0x00 |
1192
+---------------+---------------+---------------+---------------+
1193
20| 0x00 | 0x00 | 0x00 | 0x00 |
1194
+---------------+---------------+---------------+---------------+
1197
Field (offset) (value)
1200
Key length (2,3) : 0x0000
1201
Extra length (4) : 0x00
1202
Data type (5) : 0x00
1203
Reserved (6,7) : 0x0000
1204
Total body (8-11) : 0x00000000
1205
Opaque (12-15): 0x00000000
1206
CAS (16-23): 0x0000000000000000
1211
<preamble>Version response:</preamble>
1213
Byte/ 0 | 1 | 2 | 3 |
1215
|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|
1216
+---------------+---------------+---------------+---------------+
1217
0| 0x81 | 0x0b | 0x00 | 0x00 |
1218
+---------------+---------------+---------------+---------------+
1219
4| 0x00 | 0x00 | 0x00 | 0x00 |
1220
+---------------+---------------+---------------+---------------+
1221
8| 0x00 | 0x00 | 0x00 | 0x05 |
1222
+---------------+---------------+---------------+---------------+
1223
12| 0x00 | 0x00 | 0x00 | 0x00 |
1224
+---------------+---------------+---------------+---------------+
1225
16| 0x00 | 0x00 | 0x00 | 0x00 |
1226
+---------------+---------------+---------------+---------------+
1227
20| 0x00 | 0x00 | 0x00 | 0x00 |
1228
+---------------+---------------+---------------+---------------+
1229
24| 0x31 ('1') | 0x2e ('.') | 0x33 ('3') | 0x2e ('.') |
1230
+---------------+---------------+---------------+---------------+
1233
Total 29 bytes (24 byte header, 5 byte body)
1235
Field (offset) (value)
1238
Key length (2,3) : 0x0000
1239
Extra length (4) : 0x00
1240
Data type (5) : 0x00
1241
Status (6,7) : 0x0000
1242
Total body (8-11) : 0x00000005
1243
Opaque (12-15): 0x00000000
1244
CAS (16-23): 0x0000000000000000
1247
Value : Textual string "1.3.1"
1253
<section anchor="command-append" title="Append, Prepend">
1255
<list style="empty">
1256
<t>MUST NOT have extras.</t>
1257
<t>MUST have key.</t>
1258
<t>MUST have value.</t>
1263
These commands will either append or prepend the specified
1264
value to the requested key.
1267
<section anchor="command-append-example" title="Example">
1268
<t>The following example appends '!' to the 'Hello' key.</t>
1270
<preamble>Append request:</preamble>
1272
Byte/ 0 | 1 | 2 | 3 |
1274
|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|
1275
+---------------+---------------+---------------+---------------+
1276
0| 0x80 | 0x0e | 0x00 | 0x05 |
1277
+---------------+---------------+---------------+---------------+
1278
4| 0x00 | 0x00 | 0x00 | 0x00 |
1279
+---------------+---------------+---------------+---------------+
1280
8| 0x00 | 0x00 | 0x00 | 0x06 |
1281
+---------------+---------------+---------------+---------------+
1282
12| 0x00 | 0x00 | 0x00 | 0x00 |
1283
+---------------+---------------+---------------+---------------+
1284
16| 0x00 | 0x00 | 0x00 | 0x00 |
1285
+---------------+---------------+---------------+---------------+
1286
20| 0x00 | 0x00 | 0x00 | 0x00 |
1287
+---------------+---------------+---------------+---------------+
1288
24| 0x48 ('H') | 0x65 ('e') | 0x6c ('l') | 0x6c ('l') |
1289
+---------------+---------------+---------------+---------------+
1290
28| 0x6f ('o') | 0x21 ('!') |
1291
+---------------+---------------+
1292
Total 30 bytes (24 byte header, 5 byte key, 1 byte value)
1294
Field (offset) (value)
1297
Key length (2,3) : 0x0005
1298
Extra length (4) : 0x00
1299
Data type (5) : 0x00
1300
Reserved (6,7) : 0x0000
1301
Total body (8-11) : 0x00000006
1302
Opaque (12-15): 0x00000000
1303
CAS (16-23): 0x0000000000000000
1305
Key (24-28): The textual string "Hello"
1310
The response-packet contains no extra data, and the result of the
1311
operation is signaled through the status code.
1316
<section anchor="command-stat" title="Stat">
1318
<list style="empty">
1319
<t>MUST NOT have extras.</t>
1320
<t>MAY have key.</t>
1321
<t>MUST NOT have value.</t>
1326
Request server statistics. Without a key specified the server will
1327
respond with a "default" set of statistics information. Each piece
1328
of statistical information is returned in its own packet (key
1329
contains the name of the statistical item and the body contains the
1330
value in ASCII format). The sequence of return packets is terminated
1331
with a packet that contains no key and no value.
1333
<section anchor="command-stat-example" title="Example">
1334
<t>The following example requests all statistics from the server</t>
1336
<preamble>Stat request:</preamble>
1338
Byte/ 0 | 1 | 2 | 3 |
1340
|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|
1341
+---------------+---------------+---------------+---------------+
1342
0| 0x80 | 0x10 | 0x00 | 0x00 |
1343
+---------------+---------------+---------------+---------------+
1344
4| 0x00 | 0x00 | 0x00 | 0x00 |
1345
+---------------+---------------+---------------+---------------+
1346
8| 0x00 | 0x00 | 0x00 | 0x00 |
1347
+---------------+---------------+---------------+---------------+
1348
12| 0x00 | 0x00 | 0x00 | 0x00 |
1349
+---------------+---------------+---------------+---------------+
1350
16| 0x00 | 0x00 | 0x00 | 0x00 |
1351
+---------------+---------------+---------------+---------------+
1352
20| 0x00 | 0x00 | 0x00 | 0x00 |
1353
+---------------+---------------+---------------+---------------+
1356
Field (offset) (value)
1359
Key length (2,3) : 0x0000
1360
Extra length (4) : 0x00
1361
Data type (5) : 0x00
1362
Reserved (6,7) : 0x0000
1363
Total body (8-11) : 0x00000000
1364
Opaque (12-15): 0x00000000
1365
CAS (16-23): 0x0000000000000000
1372
The server will send each value in a separate packet with
1373
an "empty" packet (no key / no value) to terminate the sequence.
1374
Each of the response packets look like the following example:
1377
<preamble>Stat response:</preamble>
1379
Byte/ 0 | 1 | 2 | 3 |
1381
|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|
1382
+---------------+---------------+---------------+---------------+
1383
0| 0x81 | 0x10 | 0x00 | 0x03 |
1384
+---------------+---------------+---------------+---------------+
1385
4| 0x00 | 0x00 | 0x00 | 0x00 |
1386
+---------------+---------------+---------------+---------------+
1387
8| 0x00 | 0x00 | 0x00 | 0x07 |
1388
+---------------+---------------+---------------+---------------+
1389
12| 0x00 | 0x00 | 0x00 | 0x00 |
1390
+---------------+---------------+---------------+---------------+
1391
16| 0x00 | 0x00 | 0x00 | 0x00 |
1392
+---------------+---------------+---------------+---------------+
1393
20| 0x00 | 0x00 | 0x00 | 0x00 |
1394
+---------------+---------------+---------------+---------------+
1395
24| 0x70 ('p') | 0x69 ('i') | 0x64 ('d') | 0x33 ('3') |
1396
+---------------+---------------+---------------+---------------+
1397
28| 0x30 ('0') | 0x37 ('7') | 0x38 ('8') |
1398
+---------------+---------------+---------------+
1399
Total 31 bytes (24 byte header, 3 byte key, 4 byte body)
1401
Field (offset) (value)
1404
Key length (2,3) : 0x0003
1405
Extra length (4) : 0x00
1406
Data type (5) : 0x00
1407
Status (6,7) : 0x0000
1408
Total body (8-11) : 0x00000007
1409
Opaque (12-15): 0x00000000
1410
CAS (16-23): 0x0000000000000000
1412
Key : The textual string "pid"
1413
Value : The textual string "3078"
1419
<section anchor="security" title="Security Considerations">
1421
Memcache has no authentication or security layers whatsoever. It is
1422
RECOMMENDED that memcache be deployed strictly on closed, protected,
1423
back-end networks within a single data center, within a single cluster of
1424
servers, or even on a single host, providing shared caching for multiple
1425
applications. Memcache MUST NOT be made available on a public network.
1432
<references title="Normative References">
1433
<reference anchor="LJ">
1435
<title>LJ NEEDS MOAR SPEED</title>
1436
<author fullname="Brad Fitzpatrick">
1437
<organization>Danga Interactive</organization>
1439
<date day="5" month="10" year="1999" />
1441
<t>http://www.livejournal.com/</t>
1445
<dwdrfc-ref anchor="KEYWORDS" src='http://xml.resource.org/public/rfc/bibxml/reference.RFC.2119.xml'/>
1448
<section anchor="acknowledgments" title="Acknowledgments">
1450
Thanks to Brad Fitzpatrick, Anatoly Vorobey, Steven Grimm, and Dustin
1451
Sallings, for their work on the memcached server.
1455
Thanks to Sean Chittenden, Jonathan Steinert, Brian Aker, Evan Martin,
1456
Nathan Neulinger, Eric Hodel, Michael Johnson, Paul Querna, Jamie
1457
McCarthy, Philip Neustrom, Andrew O'Brien, Josh Rotenberg, Robin H.
1458
Johnson, Tim Yardley, Paolo Borelli, Eli Bingham, Jean-Francois
1459
Bustarret, Paul G, Paul Lindner, Alan Kasindorf, Chris Goffinet, Tomash
1460
Brechko, and others for their work reporting bugs and maintaining
1461
memcached client libraries and bindings in many languages.