xref: /memcached-1.4.29/doc/protocol.txt (revision fe2629fd)
1Protocol
2--------
3
4Clients of memcached communicate with server through TCP connections.
5(A UDP interface is also available; details are below under "UDP
6protocol.") A given running memcached server listens on some
7(configurable) port; clients connect to that port, send commands to
8the server, read responses, and eventually close the connection.
9
10There is no need to send any command to end the session. A client may
11just close the connection at any moment it no longer needs it. Note,
12however, that clients are encouraged to cache their connections rather
13than reopen them every time they need to store or retrieve data.  This
14is because memcached is especially designed to work very efficiently
15with a very large number (many hundreds, more than a thousand if
16necessary) of open connections. Caching connections will eliminate the
17overhead associated with establishing a TCP connection (the overhead
18of preparing for a new connection on the server side is insignificant
19compared to this).
20
21There are two kinds of data sent in the memcache protocol: text lines
22and unstructured data.  Text lines are used for commands from clients
23and responses from servers. Unstructured data is sent when a client
24wants to store or retrieve data. The server will transmit back
25unstructured data in exactly the same way it received it, as a byte
26stream. The server doesn't care about byte order issues in
27unstructured data and isn't aware of them. There are no limitations on
28characters that may appear in unstructured data; however, the reader
29of such data (either a client or a server) will always know, from a
30preceding text line, the exact length of the data block being
31transmitted.
32
33Text lines are always terminated by \r\n. Unstructured data is _also_
34terminated by \r\n, even though \r, \n or any other 8-bit characters
35may also appear inside the data. Therefore, when a client retrieves
36data from a server, it must use the length of the data block (which it
37will be provided with) to determine where the data block ends, and not
38the fact that \r\n follows the end of the data block, even though it
39does.
40
41Keys
42----
43
44Data stored by memcached is identified with the help of a key. A key
45is a text string which should uniquely identify the data for clients
46that are interested in storing and retrieving it.  Currently the
47length limit of a key is set at 250 characters (of course, normally
48clients wouldn't need to use such long keys); the key must not include
49control characters or whitespace.
50
51Commands
52--------
53
54There are three types of commands.
55
56Storage commands (there are six: "set", "add", "replace", "append"
57"prepend" and "cas") ask the server to store some data identified by a
58key. The client sends a command line, and then a data block; after
59that the client expects one line of response, which will indicate
60success or failure.
61
62Retrieval commands (there are two: "get" and "gets") ask the server to
63retrieve data corresponding to a set of keys (one or more keys in one
64request). The client sends a command line, which includes all the
65requested keys; after that for each item the server finds it sends to
66the client one response line with information about the item, and one
67data block with the item's data; this continues until the server
68finished with the "END" response line.
69
70All other commands don't involve unstructured data. In all of them,
71the client sends one command line, and expects (depending on the
72command) either one line of response, or several lines of response
73ending with "END" on the last line.
74
75A command line always starts with the name of the command, followed by
76parameters (if any) delimited by whitespace. Command names are
77lower-case and are case-sensitive.
78
79Expiration times
80----------------
81
82Some commands involve a client sending some kind of expiration time
83(relative to an item or to an operation requested by the client) to
84the server. In all such cases, the actual value sent may either be
85Unix time (number of seconds since January 1, 1970, as a 32-bit
86value), or a number of seconds starting from current time. In the
87latter case, this number of seconds may not exceed 60*60*24*30 (number
88of seconds in 30 days); if the number sent by a client is larger than
89that, the server will consider it to be real Unix time value rather
90than an offset from current time.
91
92
93Error strings
94-------------
95
96Each command sent by a client may be answered with an error string
97from the server. These error strings come in three types:
98
99- "ERROR\r\n"
100
101  means the client sent a nonexistent command name.
102
103- "CLIENT_ERROR <error>\r\n"
104
105  means some sort of client error in the input line, i.e. the input
106  doesn't conform to the protocol in some way. <error> is a
107  human-readable error string.
108
109- "SERVER_ERROR <error>\r\n"
110
111  means some sort of server error prevents the server from carrying
112  out the command. <error> is a human-readable error string. In cases
113  of severe server errors, which make it impossible to continue
114  serving the client (this shouldn't normally happen), the server will
115  close the connection after sending the error line. This is the only
116  case in which the server closes a connection to a client.
117
118
119In the descriptions of individual commands below, these error lines
120are not again specifically mentioned, but clients must allow for their
121possibility.
122
123
124Storage commands
125----------------
126
127First, the client sends a command line which looks like this:
128
129<command name> <key> <flags> <exptime> <bytes> [noreply]\r\n
130cas <key> <flags> <exptime> <bytes> <cas unique> [noreply]\r\n
131
132- <command name> is "set", "add", "replace", "append" or "prepend"
133
134  "set" means "store this data".
135
136  "add" means "store this data, but only if the server *doesn't* already
137  hold data for this key".
138
139  "replace" means "store this data, but only if the server *does*
140  already hold data for this key".
141
142  "append" means "add this data to an existing key after existing data".
143
144  "prepend" means "add this data to an existing key before existing data".
145
146  The append and prepend commands do not accept flags or exptime.
147  They update existing data portions, and ignore new flag and exptime
148  settings.
149
150  "cas" is a check and set operation which means "store this data but
151  only if no one else has updated since I last fetched it."
152
153- <key> is the key under which the client asks to store the data
154
155- <flags> is an arbitrary 16-bit unsigned integer (written out in
156  decimal) that the server stores along with the data and sends back
157  when the item is retrieved. Clients may use this as a bit field to
158  store data-specific information; this field is opaque to the server.
159  Note that in memcached 1.2.1 and higher, flags may be 32-bits, instead
160  of 16, but you might want to restrict yourself to 16 bits for
161  compatibility with older versions.
162
163- <exptime> is expiration time. If it's 0, the item never expires
164  (although it may be deleted from the cache to make place for other
165  items). If it's non-zero (either Unix time or offset in seconds from
166  current time), it is guaranteed that clients will not be able to
167  retrieve this item after the expiration time arrives (measured by
168  server time).
169
170- <bytes> is the number of bytes in the data block to follow, *not*
171  including the delimiting \r\n. <bytes> may be zero (in which case
172  it's followed by an empty data block).
173
174- <cas unique> is a unique 64-bit value of an existing entry.
175  Clients should use the value returned from the "gets" command
176  when issuing "cas" updates.
177
178- "noreply" optional parameter instructs the server to not send the
179  reply.  NOTE: if the request line is malformed, the server can't
180  parse "noreply" option reliably.  In this case it may send the error
181  to the client, and not reading it on the client side will break
182  things.  Client should construct only valid requests.
183
184After this line, the client sends the data block:
185
186<data block>\r\n
187
188- <data block> is a chunk of arbitrary 8-bit data of length <bytes>
189  from the previous line.
190
191After sending the command line and the data blockm the client awaits
192the reply, which may be:
193
194- "STORED\r\n", to indicate success.
195
196- "NOT_STORED\r\n" to indicate the data was not stored, but not
197because of an error. This normally means that the
198condition for an "add" or a "replace" command wasn't met.
199
200- "EXISTS\r\n" to indicate that the item you are trying to store with
201a "cas" command has been modified since you last fetched it.
202
203- "NOT_FOUND\r\n" to indicate that the item you are trying to store
204with a "cas" command did not exist.
205
206
207Retrieval command:
208------------------
209
210The retrieval commands "get" and "gets" operates like this:
211
212get <key>*\r\n
213gets <key>*\r\n
214
215- <key>* means one or more key strings separated by whitespace.
216
217After this command, the client expects zero or more items, each of
218which is received as a text line followed by a data block. After all
219the items have been transmitted, the server sends the string
220
221"END\r\n"
222
223to indicate the end of response.
224
225Each item sent by the server looks like this:
226
227VALUE <key> <flags> <bytes> [<cas unique>]\r\n
228<data block>\r\n
229
230- <key> is the key for the item being sent
231
232- <flags> is the flags value set by the storage command
233
234- <bytes> is the length of the data block to follow, *not* including
235  its delimiting \r\n
236
237- <cas unique> is a unique 64-bit integer that uniquely identifies
238  this specific item.
239
240- <data block> is the data for this item.
241
242If some of the keys appearing in a retrieval request are not sent back
243by the server in the item list this means that the server does not
244hold items with such keys (because they were never stored, or stored
245but deleted to make space for more items, or expired, or explicitly
246deleted by a client).
247
248
249Deletion
250--------
251
252The command "delete" allows for explicit deletion of items:
253
254delete <key> [noreply]\r\n
255
256- <key> is the key of the item the client wishes the server to delete
257
258- "noreply" optional parameter instructs the server to not send the
259  reply.  See the note in Storage commands regarding malformed
260  requests.
261
262The response line to this command can be one of:
263
264- "DELETED\r\n" to indicate success
265
266- "NOT_FOUND\r\n" to indicate that the item with this key was not
267  found.
268
269See the "flush_all" command below for immediate invalidation
270of all existing items.
271
272
273Increment/Decrement
274-------------------
275
276Commands "incr" and "decr" are used to change data for some item
277in-place, incrementing or decrementing it. The data for the item is
278treated as decimal representation of a 64-bit unsigned integer.  If
279the current data value does not conform to such a representation, the
280incr/decr commands return an error (memcached <= 1.2.6 treated the
281bogus value as if it were 0, leading to confusion). Also, the item
282must already exist for incr/decr to work; these commands won't pretend
283that a non-existent key exists with value 0; instead, they will fail.
284
285The client sends the command line:
286
287incr <key> <value> [noreply]\r\n
288
289or
290
291decr <key> <value> [noreply]\r\n
292
293- <key> is the key of the item the client wishes to change
294
295- <value> is the amount by which the client wants to increase/decrease
296the item. It is a decimal representation of a 64-bit unsigned integer.
297
298- "noreply" optional parameter instructs the server to not send the
299  reply.  See the note in Storage commands regarding malformed
300  requests.
301
302The response will be one of:
303
304- "NOT_FOUND\r\n" to indicate the item with this value was not found
305
306- <value>\r\n , where <value> is the new value of the item's data,
307  after the increment/decrement operation was carried out.
308
309Note that underflow in the "decr" command is caught: if a client tries
310to decrease the value below 0, the new value will be 0.  Overflow in
311the "incr" command will wrap around the 64 bit mark.
312
313Note also that decrementing a number such that it loses length isn't
314guaranteed to decrement its returned length.  The number MAY be
315space-padded at the end, but this is purely an implementation
316optimization, so you also shouldn't rely on that.
317
318Touch
319-----
320
321The "touch" command is used to update the expiration time of an existing item
322without fetching it.
323
324touch <key> <exptime> [noreply]\r\n
325
326- <key> is the key of the item the client wishes the server to delete
327
328- <exptime> is expiration time. Works the same as with the update commands
329  (set/add/etc). This replaces the existing expiration time. If an existing
330  item were to expire in 10 seconds, but then was touched with an
331  expiration time of "20", the item would then expire in 20 seconds.
332
333- "noreply" optional parameter instructs the server to not send the
334  reply.  See the note in Storage commands regarding malformed
335  requests.
336
337The response line to this command can be one of:
338
339- "TOUCHED\r\n" to indicate success
340
341- "NOT_FOUND\r\n" to indicate that the item with this key was not
342  found.
343
344Slabs Reassign
345--------------
346
347NOTE: This command is subject to change as of this writing.
348
349The slabs reassign command is used to redistribute memory once a running
350instance has hit its limit. It might be desireable to have memory laid out
351differently than was automatically assigned after the server started.
352
353slabs reassign <source class> <dest class>\r\n
354
355- <source class> is an id number for the slab class to steal a page from
356
357A source class id of -1 means "pick from any valid class"
358
359- <dest class> is an id number for the slab class to move a page to
360
361The response line could be one of:
362
363- "OK" to indicate the page has been scheduled to move
364
365- "BUSY [message]" to indicate a page is already being processed, try again
366  later.
367
368- "BADCLASS [message]" a bad class id was specified
369
370- "NOSPARE [message]" source class has no spare pages
371
372- "NOTFULL [message]" dest class must be full to move new pages to it
373
374- "UNSAFE [message]" source class cannot move a page right now
375
376- "SAME [message]" must specify different source/dest ids.
377
378Slabs Automove
379--------------
380
381NOTE: This command is subject to change as of this writing.
382
383The slabs automove command enables a background thread which decides on its
384own when to move memory between slab classes. Its implementation and options
385will likely be in flux for several versions. See the wiki/mailing list for
386more details.
387
388The automover can be enabled or disabled at runtime with this command.
389
390slabs automove <0|1>
391
392- 0|1|2 is the indicator on whether to enable the slabs automover or not.
393
394The response should always be "OK\r\n"
395
396- <0> means to set the thread on standby
397
398- <1> means to run the builtin slow algorithm to choose pages to move
399
400- <2> is a highly aggressive mode which causes pages to be moved every time
401  there is an eviction. It is not recommended to run for very long in this
402  mode unless your access patterns are very well understood.
403
404LRU_Crawler
405-----------
406
407NOTE: This command (and related commands) are subject to change as of this
408writing.
409
410The LRU Crawler is an optional background thread which will walk from the tail
411toward the head of requested slab classes, actively freeing memory for expired
412items. This is useful if you have a mix of items with both long and short
413TTL's, but aren't accessed very often. This system is not required for normal
414usage, and can add small amounts of latency and increase CPU usage.
415
416lru_crawler <enable|disable>
417
418- Enable or disable the LRU Crawler background thread.
419
420The response line could be one of:
421
422- "OK" to indicate the crawler has been started or stopped.
423
424- "ERROR [message]" something went wrong while enabling or disabling.
425
426lru_crawler sleep <microseconds>
427
428- The number of microseconds to sleep inbetween each item checked for
429  expiration. Smaller numbers will obviously impact the system more.
430  A value of "0" disables the sleep, "1000000" (one second) is the max.
431
432The response line could be one of:
433
434- "OK"
435
436- "CLIENT_ERROR [message]" indicating a format or bounds issue.
437
438lru_crawler tocrawl <32u>
439
440- The maximum number of items to inspect in a slab class per run request. This
441  allows you to avoid scanning all of very large slabs when it is unlikely to
442  find items to expire.
443
444The response line could be one of:
445
446- "OK"
447
448- "CLIENT_ERROR [message]" indicating a format or bound issue.
449
450lru_crawler crawl <classid,classid,classid|all>
451
452- Takes a single, or a list of, numeric classids (ie: 1,3,10). This instructs
453  the crawler to start at the tail of each of these classids and run to the
454  head. The crawler cannot be stopped or restarted until it completes the
455  previous request.
456
457  The special keyword "all" instructs it to crawl all slabs with items in
458  them.
459
460The response line could be one of:
461
462- "OK" to indicate successful launch.
463
464- "BUSY [message]" to indicate the crawler is already processing a request.
465
466- "BADCLASS [message]" to indicate an invalid class was specified.
467
468Statistics
469----------
470
471The command "stats" is used to query the server about statistics it
472maintains and other internal data. It has two forms. Without
473arguments:
474
475stats\r\n
476
477it causes the server to output general-purpose statistics and
478settings, documented below.  In the other form it has some arguments:
479
480stats <args>\r\n
481
482Depending on <args>, various internal data is sent by the server. The
483kinds of arguments and the data sent are not documented in this version
484of the protocol, and are subject to change for the convenience of
485memcache developers.
486
487
488General-purpose statistics
489--------------------------
490
491Upon receiving the "stats" command without arguments, the server sents
492a number of lines which look like this:
493
494STAT <name> <value>\r\n
495
496The server terminates this list with the line
497
498END\r\n
499
500In each line of statistics, <name> is the name of this statistic, and
501<value> is the data.  The following is the list of all names sent in
502response to the "stats" command, together with the type of the value
503sent for this name, and the meaning of the value.
504
505In the type column below, "32u" means a 32-bit unsigned integer, "64u"
506means a 64-bit unsigned integer. '32u.32u' means two 32-bit unsigned
507integers separated by a colon (treat this as a floating point number).
508
509|-----------------------+---------+-------------------------------------------|
510| Name                  | Type    | Meaning                                   |
511|-----------------------+---------+-------------------------------------------|
512| pid                   | 32u     | Process id of this server process         |
513| uptime                | 32u     | Number of secs since the server started   |
514| time                  | 32u     | current UNIX time according to the server |
515| version               | string  | Version string of this server             |
516| pointer_size          | 32      | Default size of pointers on the host OS   |
517|                       |         | (generally 32 or 64)                      |
518| rusage_user           | 32u.32u | Accumulated user time for this process    |
519|                       |         | (seconds:microseconds)                    |
520| rusage_system         | 32u.32u | Accumulated system time for this process  |
521|                       |         | (seconds:microseconds)                    |
522| curr_items            | 32u     | Current number of items stored            |
523| total_items           | 32u     | Total number of items stored since        |
524|                       |         | the server started                        |
525| bytes                 | 64u     | Current number of bytes used              |
526|                       |         | to store items                            |
527| curr_connections      | 32u     | Number of open connections                |
528| total_connections     | 32u     | Total number of connections opened since  |
529|                       |         | the server started running                |
530| connection_structures | 32u     | Number of connection structures allocated |
531|                       |         | by the server                             |
532| reserved_fds          | 32u     | Number of misc fds used internally        |
533| cmd_get               | 64u     | Cumulative number of retrieval reqs       |
534| cmd_set               | 64u     | Cumulative number of storage reqs         |
535| cmd_flush             | 64u     | Cumulative number of flush reqs           |
536| cmd_touch             | 64u     | Cumulative number of touch reqs           |
537| get_hits              | 64u     | Number of keys that have been requested   |
538|                       |         | and found present                         |
539| get_misses            | 64u     | Number of items that have been requested  |
540|                       |         | and not found                             |
541| delete_misses         | 64u     | Number of deletions reqs for missing keys |
542| delete_hits           | 64u     | Number of deletion reqs resulting in      |
543|                       |         | an item being removed.                    |
544| incr_misses           | 64u     | Number of incr reqs against missing keys. |
545| incr_hits             | 64u     | Number of successful incr reqs.           |
546| decr_misses           | 64u     | Number of decr reqs against missing keys. |
547| decr_hits             | 64u     | Number of successful decr reqs.           |
548| cas_misses            | 64u     | Number of CAS reqs against missing keys.  |
549| cas_hits              | 64u     | Number of successful CAS reqs.            |
550| cas_badval            | 64u     | Number of CAS reqs for which a key was    |
551|                       |         | found, but the CAS value did not match.   |
552| touch_hits            | 64u     | Numer of keys that have been touched with |
553|                       |         | a new expiration time                     |
554| touch_misses          | 64u     | Numer of items that have been touched and |
555|                       |         | not found                                 |
556| auth_cmds             | 64u     | Number of authentication commands         |
557|                       |         | handled, success or failure.              |
558| auth_errors           | 64u     | Number of failed authentications.         |
559| evictions             | 64u     | Number of valid items removed from cache  |
560|                       |         | to free memory for new items              |
561| reclaimed             | 64u     | Number of times an entry was stored using |
562|                       |         | memory from an expired entry              |
563| bytes_read            | 64u     | Total number of bytes read by this server |
564|                       |         | from network                              |
565| bytes_written         | 64u     | Total number of bytes sent by this server |
566|                       |         | to network                                |
567| limit_maxbytes        | 32u     | Number of bytes this server is allowed to |
568|                       |         | use for storage.                          |
569| threads               | 32u     | Number of worker threads requested.       |
570|                       |         | (see doc/threads.txt)                     |
571| conn_yields           | 64u     | Number of times any connection yielded to |
572|                       |         | another due to hitting the -R limit.      |
573| hash_power_level      | 32u     | Current size multiplier for hash table    |
574| hash_bytes            | 64u     | Bytes currently used by hash tables       |
575| hash_is_expanding     | bool    | Indicates if the hash table is being      |
576|                       |         | grown to a new size                       |
577| expired_unfetched     | 64u     | Items pulled from LRU that were never     |
578|                       |         | touched by get/incr/append/etc before     |
579|                       |         | expiring                                  |
580| evicted_unfetched     | 64u     | Items evicted from LRU that were never    |
581|                       |         | touched by get/incr/append/etc.           |
582| slab_reassign_running | bool    | If a slab page is being moved             |
583| slabs_moved           | 64u     | Total slab pages moved                    |
584| crawler_reclaimed     | 64u     | Total items freed by LRU Crawler          |
585| lrutail_reflocked     | 64u     | Times LRU tail was found with active ref. |
586|                       |         | Items moved to head to avoid OOM errors.  |
587|-----------------------+---------+-------------------------------------------|
588
589Settings statistics
590-------------------
591CAVEAT: This section describes statistics which are subject to change in the
592future.
593
594The "stats" command with the argument of "settings" returns details of
595the settings of the running memcached.  This is primarily made up of
596the results of processing commandline options.
597
598Note that these are not guaranteed to return in any specific order and
599this list may not be exhaustive.  Otherwise, this returns like any
600other stats command.
601
602|-------------------+----------+----------------------------------------------|
603| Name              | Type     | Meaning                                      |
604|-------------------+----------+----------------------------------------------|
605| maxbytes          | size_t   | Maximum number of bytes allows in this cache |
606| maxconns          | 32       | Maximum number of clients allowed.           |
607| tcpport           | 32       | TCP listen port.                             |
608| udpport           | 32       | UDP listen port.                             |
609| inter             | string   | Listen interface.                            |
610| verbosity         | 32       | 0 = none, 1 = some, 2 = lots                 |
611| oldest            | 32u      | Age of the oldest honored object.            |
612| evictions         | on/off   | When off, LRU evictions are disabled.        |
613| domain_socket     | string   | Path to the domain socket (if any).          |
614| umask             | 32 (oct) | umask for the creation of the domain socket. |
615| growth_factor     | float    | Chunk size growth factor.                    |
616| chunk_size        | 32       | Minimum space allocated for key+value+flags. |
617| num_threads       | 32       | Number of threads (including dispatch).      |
618| stat_key_prefix   | char     | Stats prefix separator character.            |
619| detail_enabled    | bool     | If yes, stats detail is enabled.             |
620| reqs_per_event    | 32       | Max num IO ops processed within an event.    |
621| cas_enabled       | bool     | When no, CAS is not enabled for this server. |
622| tcp_backlog       | 32       | TCP listen backlog.                          |
623| auth_enabled_sasl | yes/no   | SASL auth requested and enabled.             |
624| item_size_max     | size_t   | maximum item size                            |
625| maxconns_fast     | bool     | If fast disconnects are enabled              |
626| hashpower_init    | 32       | Starting size multiplier for hash table      |
627| slab_reassign     | bool     | Whether slab page reassignment is allowed    |
628| slab_automove     | bool     | Whether slab page automover is enabled       |
629| hash_algorithm    | char     | Hash table algorithm in use                  |
630| lru_crawler       | bool     | Whether the LRU crawler is enabled           |
631| lru_crawler_sleep | 32       | Microseconds to sleep between LRU crawls     |
632| lru_crawler_tocrawl| 32u     | Max items to crawl per slab per run          |
633|-------------------+----------+----------------------------------------------|
634
635
636Item statistics
637---------------
638CAVEAT: This section describes statistics which are subject to change in the
639future.
640
641The "stats" command with the argument of "items" returns information about
642item storage per slab class. The data is returned in the format:
643
644STAT items:<slabclass>:<stat> <value>\r\n
645
646The server terminates this list with the line
647
648END\r\n
649
650The slabclass aligns with class ids used by the "stats slabs" command. Where
651"stats slabs" describes size and memory usage, "stats items" shows higher
652level information.
653
654The following item values are defined as of writing.
655
656Name                   Meaning
657------------------------------
658number                 Number of items presently stored in this class. Expired
659                       items are not automatically excluded.
660age                    Age of the oldest item in the LRU.
661evicted                Number of times an item had to be evicted from the LRU
662                       before it expired.
663evicted_nonzero        Number of times an item which had an explicit expire
664                       time set had to be evicted from the LRU before it
665                       expired.
666evicted_time           Seconds since the last access for the most recent item
667                       evicted from this class. Use this to judge how
668                       recently active your evicted data is.
669outofmemory            Number of times the underlying slab class was unable to
670                       store a new item. This means you are running with -M or
671                       an eviction failed.
672tailrepairs            Number of times we self-healed a slab with a refcount
673                       leak. If this counter is increasing a lot, please
674                       report your situation to the developers.
675reclaimed              Number of times an entry was stored using memory from
676                       an expired entry.
677expired_unfetched      Number of expired items reclaimed from the LRU which
678                       were never touched after being set.
679evicted_unfetched      Number of valid items evicted from the LRU which were
680                       never touched after being set.
681crawler_reclaimed      Number of items freed by the LRU Crawler.
682
683Note this will only display information about slabs which exist, so an empty
684cache will return an empty set.
685
686
687Item size statistics
688--------------------
689CAVEAT: This section describes statistics which are subject to change in the
690future.
691
692The "stats" command with the argument of "sizes" returns information about the
693general size and count of all items stored in the cache.
694WARNING: This command WILL lock up your cache! It iterates over *every item*
695and examines the size. While the operation is fast, if you have many items
696you could prevent memcached from serving requests for several seconds.
697
698The data is returned in the following format:
699
700<size> <count>\r\n
701
702The server terminates this list with the line
703
704END\r\n
705
706'size' is an approximate size of the item, within 32 bytes.
707'count' is the amount of items that exist within that 32-byte range.
708
709This is essentially a display of all of your items if there was a slab class
710for every 32 bytes. You can use this to determine if adjusting the slab growth
711factor would save memory overhead. For example: generating more classes in the
712lower range could allow items to fit more snugly into their slab classes, if
713most of your items are less than 200 bytes in size.
714
715
716Slab statistics
717---------------
718CAVEAT: This section describes statistics which are subject to change in the
719future.
720
721The "stats" command with the argument of "slabs" returns information about
722each of the slabs created by memcached during runtime. This includes per-slab
723information along with some totals. The data is returned in the format:
724
725STAT <slabclass>:<stat> <value>\r\n
726STAT <stat> <value>\r\n
727
728The server terminates this list with the line
729
730END\r\n
731
732|-----------------+----------------------------------------------------------|
733| Name            | Meaning                                                  |
734|-----------------+----------------------------------------------------------|
735| chunk_size      | The amount of space each chunk uses. One item will use   |
736|                 | one chunk of the appropriate size.                       |
737| chunks_per_page | How many chunks exist within one page. A page by         |
738|                 | default is less than or equal to one megabyte in size.   |
739|                 | Slabs are allocated by page, then broken into chunks.    |
740| total_pages     | Total number of pages allocated to the slab class.       |
741| total_chunks    | Total number of chunks allocated to the slab class.      |
742| get_hits        | Total number of get requests serviced by this class.     |
743| cmd_set         | Total number of set requests storing data in this class. |
744| delete_hits     | Total number of successful deletes from this class.      |
745| incr_hits       | Total number of incrs modifying this class.              |
746| decr_hits       | Total number of decrs modifying this class.              |
747| cas_hits        | Total number of CAS commands modifying this class.       |
748| cas_badval      | Total number of CAS commands that failed to modify a     |
749|                 | value due to a bad CAS id.                               |
750| touch_hits      | Total number of touches serviced by this class.          |
751| used_chunks     | How many chunks have been allocated to items.            |
752| free_chunks     | Chunks not yet allocated to items, or freed via delete.  |
753| free_chunks_end | Number of free chunks at the end of the last allocated   |
754|                 | page.                                                    |
755| mem_requested   | Number of bytes requested to be stored in this slab[*].  |
756| active_slabs    | Total number of slab classes allocated.                  |
757| total_malloced  | Total amount of memory allocated to slab pages.          |
758|-----------------+----------------------------------------------------------|
759
760* Items are stored in a slab that is the same size or larger than the
761  item.  mem_requested shows the size of all items within a
762  slab. (total_chunks * chunk_size) - mem_requested shows memory
763  wasted in a slab class.  If you see a lot of waste, consider tuning
764  the slab factor.
765
766
767Connection statistics
768---------------------
769The "stats" command with the argument of "conns" returns information
770about currently active connections and about sockets that are listening
771for new connections. The data is returned in the format:
772
773STAT <file descriptor>:<stat> <value>\r\n
774
775The server terminates this list with the line
776
777END\r\n
778
779The following "stat" keywords may be present:
780
781|---------------------+------------------------------------------------------|
782| Name                | Meaning                                              |
783|---------------------+------------------------------------------------------|
784| addr                | The address of the remote side. For listening        |
785|                     | sockets this is the listen address. Note that some   |
786|                     | socket types (such as UNIX-domain) don't have        |
787|                     | meaningful remote addresses.                         |
788| state               | The current state of the connection. See below.      |
789| secs_since_last_cmd | The number of seconds since the most recently        |
790|                     | issued command on the connection. This measures      |
791|                     | the time since the start of the command, so if       |
792|                     | "state" indicates a command is currently executing,  |
793|                     | this will be the number of seconds the current       |
794|                     | command has been running.                            |
795|---------------------+------------------------------------------------------|
796
797The value of the "state" stat may be one of the following:
798
799|----------------+-----------------------------------------------------------|
800| Name           | Meaning                                                   |
801|----------------+-----------------------------------------------------------|
802| conn_closing   | Shutting down the connection.                             |
803| conn_listening | Listening for new connections or a new UDP request.       |
804| conn_mwrite    | Writing a complex response, e.g., to a "get" command.     |
805| conn_new_cmd   | Connection is being prepared to accept a new command.     |
806| conn_nread     | Reading extended data, typically for a command such as    |
807|                | "set" or "put".                                           |
808| conn_parse_cmd | The server has received a command and is in the middle    |
809|                | of parsing it or executing it.                            |
810| conn_read      | Reading newly-arrived command data.                       |
811| conn_swallow   | Discarding excess input, e.g., after an error has         |
812|                | occurred.                                                 |
813| conn_waiting   | A partial command has been received and the server is     |
814|                | waiting for the rest of it to arrive (note the difference |
815|                | between this and conn_nread).                             |
816| conn_write     | Writing a simple response (anything that doesn't involve  |
817|                | sending back multiple lines of response data).            |
818|----------------+-----------------------------------------------------------|
819
820
821
822Other commands
823--------------
824
825"flush_all" is a command with an optional numeric argument. It always
826succeeds, and the server sends "OK\r\n" in response (unless "noreply"
827is given as the last parameter). Its effect is to invalidate all
828existing items immediately (by default) or after the expiration
829specified.  After invalidation none of the items will be returned in
830response to a retrieval command (unless it's stored again under the
831same key *after* flush_all has invalidated the items). flush_all
832doesn't actually free all the memory taken up by existing items; that
833will happen gradually as new items are stored. The most precise
834definition of what flush_all does is the following: it causes all
835items whose update time is earlier than the time at which flush_all
836was set to be executed to be ignored for retrieval purposes.
837
838The intent of flush_all with a delay, was that in a setting where you
839have a pool of memcached servers, and you need to flush all content,
840you have the option of not resetting all memcached servers at the
841same time (which could e.g. cause a spike in database load with all
842clients suddenly needing to recreate content that would otherwise
843have been found in the memcached daemon).
844
845The delay option allows you to have them reset in e.g. 10 second
846intervals (by passing 0 to the first, 10 to the second, 20 to the
847third, etc. etc.).
848
849
850"version" is a command with no arguments:
851
852version\r\n
853
854In response, the server sends
855
856"VERSION <version>\r\n", where <version> is the version string for the
857server.
858
859"verbosity" is a command with a numeric argument. It always succeeds,
860and the server sends "OK\r\n" in response (unless "noreply" is given
861as the last parameter). Its effect is to set the verbosity level of
862the logging output.
863
864"quit" is a command with no arguments:
865
866quit\r\n
867
868Upon receiving this command, the server closes the
869connection. However, the client may also simply close the connection
870when it no longer needs it, without issuing this command.
871
872
873UDP protocol
874------------
875
876For very large installations where the number of clients is high enough
877that the number of TCP connections causes scaling difficulties, there is
878also a UDP-based interface. The UDP interface does not provide guaranteed
879delivery, so should only be used for operations that aren't required to
880succeed; typically it is used for "get" requests where a missing or
881incomplete response can simply be treated as a cache miss.
882
883Each UDP datagram contains a simple frame header, followed by data in the
884same format as the TCP protocol described above. In the current
885implementation, requests must be contained in a single UDP datagram, but
886responses may span several datagrams. (The only common requests that would
887span multiple datagrams are huge multi-key "get" requests and "set"
888requests, both of which are more suitable to TCP transport for reliability
889reasons anyway.)
890
891The frame header is 8 bytes long, as follows (all values are 16-bit integers
892in network byte order, high byte first):
893
8940-1 Request ID
8952-3 Sequence number
8964-5 Total number of datagrams in this message
8976-7 Reserved for future use; must be 0
898
899The request ID is supplied by the client. Typically it will be a
900monotonically increasing value starting from a random seed, but the client
901is free to use whatever request IDs it likes. The server's response will
902contain the same ID as the incoming request. The client uses the request ID
903to differentiate between responses to outstanding requests if there are
904several pending from the same server; any datagrams with an unknown request
905ID are probably delayed responses to an earlier request and should be
906discarded.
907
908The sequence number ranges from 0 to n-1, where n is the total number of
909datagrams in the message. The client should concatenate the payloads of the
910datagrams for a given response in sequence number order; the resulting byte
911stream will contain a complete response in the same format as the TCP
912protocol (including terminating \r\n sequences).
913