protocol.yml 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. specification:
  2. - https://github.com/memcached/memcached/blob/master/doc/protocol.txt
  3. - Expiration times:
  4. - Some commands involve a client sending some kind of expiration time (relative to an item or to
  5. an operation requested by the client) to the server.
  6. - In all such cases, the actual value sent may either be
  7. - Unix time (number of seconds since January 1, 1970, as a 32-bit value),
  8. - or a number of seconds starting from current time.
  9. - In the latter case, this number of seconds may not exceed 60*60*24*30 (number of seconds in 30
  10. days); if the number sent by a client is larger than that, the server will consider it to be
  11. real Unix time value rather than an offset from current time.
  12. - errors:
  13. - "ERROR\r\n": means the client sent a nonexistent command name.
  14. - "CLIENT_ERROR <error>\r\n": means some sort of client error in the input line, i.e. the input
  15. doesn't conform to the protocol in some way. <error> is a human-readable error string.
  16. - "SERVER_ERROR <error>\r\n": means some sort of server error prevents the server from carrying
  17. out the command. <error> is a human-readable error string. In cases of severe server errors,
  18. which make it impossible to continue serving the client (this shouldn't normally happen),
  19. the server will close the connection after sending the error line. This is the only case in
  20. which the server closes a connection to a client.
  21. - In the descriptions of individual commands below, these error lines are not again specifically
  22. mentioned, but clients must allow for their possibility.
  23. commands:
  24. - A command line always starts with the name of the command, followed by parameters (if any)
  25. delimited by whitespace.
  26. - Command names are lower-case and are case-sensitive.
  27. - Short list:
  28. - level 0: basic functionality
  29. - get
  30. - set
  31. - delete
  32. - flush_all
  33. - quit
  34. - level 1: debuggability
  35. - stats|stats * (some)
  36. - verbosity
  37. - version
  38. - level 2: update functions
  39. - add|replace
  40. - append|prepend
  41. - incr|decr
  42. - touch
  43. - stats (related to these functions)
  44. - level 3: cas
  45. - cas
  46. - gets
  47. - stats (cas-related)
  48. - level 4: legacy
  49. - slab_automove|slabs_reassign
  50. - stats (related to slabs)
  51. storage:
  52. - ask the server to store some data identified by a key.
  53. - The client sends a command line, and then a data block;
  54. - After that the client expects one line of response, which will indicate success or failure.
  55. - commands:
  56. - basic commands:
  57. - "add|replace|set <key> <flags> <exptime> <bytes> [noreply]\r\n"
  58. - add: store the data under the key, but only if data for the key does not already exist
  59. - replace: store the data under the key, but only if data for the key already exist
  60. - set: store the data under the key
  61. - update commands:
  62. - "append|prepend <key> <bytes> [noreply]\r\n"
  63. - append: add this data to an existing key after existing data (?)
  64. - prepend: add this data to an existing key before existing data (?)
  65. - cas:
  66. - "cas <key> <flags> <exptime> <bytes> <cas unique> [noreply]\r\n"
  67. - check and set operation which means "store this data but only if no one else has updated
  68. since I last fetched it."
  69. - command format:
  70. <key>: the key under which the client asks to store the data
  71. <flags>: is an arbitrary 16-bit unsigned integer (written out in decimal) that the server stores
  72. along with the data and sends back when the item is retrieved. Clients may use this as a bit
  73. field to store data-specific information; this field is opaque to the server. Note that in
  74. memcached 1.2.1 and higher, flags may be 32-bits, instead of 16, but you might want to
  75. restrict yourself to 16 bits for compatibility with older versions.
  76. <exptime>: is expiration time.:
  77. - If it's 0, the item never expires (although it may be deleted from the cache to make place
  78. for other items).
  79. - If it's non-zero (either Unix time or offset in seconds from current time), it is guaranteed
  80. that clients will not be able to retrieve this item after the expiration time arrives
  81. (measured byserver time).
  82. - If a negative value is given the item is immediately expired.
  83. <bytes>:
  84. - the number of bytes in the data block to follow, *not* including the delimiting \r\n.
  85. - <bytes> may be zero (in which case it's followed by an empty data block).
  86. <cas unique>:
  87. - only used by the cas command
  88. - a unique 64-bit value of an existing entry.
  89. - Clients should use the value returned from the "gets" command when issuing "cas" updates.
  90. "noreply":
  91. - optional parameter instructs the server to not send the reply.
  92. - NOTE: if the request line is malformed, the server can't parse "noreply" option reliably.
  93. In this case it may send the error to the client, and not reading it on the client side will
  94. break things. Client should construct only valid requests.
  95. data:
  96. - After this line, the client sends the data block:
  97. - <data block>\r\n
  98. - <data block> is a chunk of arbitrary 8-bit data of length <bytes> from the previous line.
  99. - responses:
  100. - After sending the command line and the data block the client awaits the reply, which may be:
  101. - "STORED\r\n", to indicate success.
  102. - "NOT_STORED\r\n" to indicate the data was not stored, but not because of an error.
  103. This normally means that the condition for an "add" or a "replace" command wasn't met.
  104. - "EXISTS\r\n" to indicate that the item you are trying to store with a "cas" command has been
  105. modified since you last fetched it.
  106. - "NOT_FOUND\r\n" to indicate that the item you are trying to store with a "cas" command did
  107. not exist.
  108. retrieval:
  109. - ask the server to retrieve data corresponding to a set of keys (one or more keys in one
  110. request).
  111. - The client sends a command line, which includes all the requested keys;
  112. - after that for each item the server finds it sends to the client one response line with
  113. information about the item, and one data block with the item's data;
  114. - this continues until the server finished with the "END" response line.
  115. - commands:
  116. - "get|gets <key>*\r\n"
  117. - <key>* means one or more key strings separated by whitespace.
  118. - After this command, the client expects zero or more items, each of which is received as a text
  119. line followed by a data block. After all the items have been transmitted, the server sends the
  120. string "END\r\n" to indicate the end of response.
  121. - Each item sent by the server looks like this:
  122. - VALUE <key> <flags> <bytes> [<cas unique>]\r\n
  123. - <data block>\r\n
  124. - <key> is the key for the item being sent
  125. - <flags> is the flags value set by the storage command
  126. - <bytes> is the length of the data block to follow, *not* including its delimiting \r\n
  127. - <cas unique> is a unique 64-bit integer that uniquely identifies this specific item.
  128. - <data block> is the data for this item.
  129. - If some of the keys appearing in a retrieval request are not sent back by the server in the
  130. item list this means that the server does not hold items with such keys (because they were
  131. never stored, or stored but deleted to make space for more items, or expired, or explicitly
  132. deleted by a client).
  133. other:
  134. - don't involve unstructured data.
  135. - In all of them, the client sends one command line, and expects (depending on the
  136. command) either one line of response, or several lines of response ending with "END" on the last
  137. line.
  138. - commands:
  139. - delete:
  140. - "delete <key> [noreply]\r\n"
  141. - <key> is the key of the item the client wishes the server to delete
  142. - "noreply" optional parameter instructs the server to not send the reply.
  143. - The response line to this command can be one of:
  144. - "DELETED\r\n" to indicate success
  145. - "NOT_FOUND\r\n" to indicate that the item with this key was not found.
  146. - See the "flush_all" command below for immediate invalidation of all existing items.
  147. - "flush_all [<delay>]\r\n":
  148. - It always succeeds, and the server sends "OK\r\n" in response (unless "noreply" is given as the last parameter).
  149. - Its effect is to invalidate all existing items immediately (by default) or after the
  150. expiration specified in <delay>.
  151. - After invalidation none of the items will be returned in response to a retrieval command
  152. (unless it's stored again under the same key *after* flush_all has invalidated the items).
  153. - flush_all doesn't actually free all the memory taken up by existing items; that will happen
  154. gradually as new items are stored.
  155. - The most precise definition of what flush_all does is the following: it causes all items
  156. whose update time is earlier than the time at which flush_all was set to be executed to be
  157. ignored for retrieval purposes.
  158. - The intent of flush_all with a delay, was that in a setting where you have a pool of
  159. memcached servers, and you need to flush all content, you have the option of not resetting
  160. all memcached servers at the same time (which could e.g. cause a spike in database load
  161. with all clients suddenly needing to recreate content that would otherwise have been found
  162. in the memcached daemon). The delay option allows you to have them reset in e.g. 10 second
  163. intervals (by passing 0 to the first, 10 to the second, 20 to the third, etc. etc.).
  164. - increment/decrement:
  165. - change data for some item in-place, incrementing or decrementing it.
  166. - "incr|decr <key> <value> [noreply]\r\n":
  167. - <key> is the key of the item the client wishes to change
  168. - <value> is the amount by which the client wants to increase/decrease the item. It is a
  169. decimal representation of a 64-bit unsigned integer.
  170. - "noreply" optional parameter instructs the server to not send the reply.
  171. - The response will be one of:
  172. - "NOT_FOUND\r\n" to indicate the item with this value was not found
  173. - "<value>\r\n" , where <value> is the new value of the item's data, after the
  174. increment/decrement operation was carried out.
  175. - The data for the item is treated as decimal representation of a 64-bit unsigned integer.
  176. - If the current data value does not conform to such a representation, the incr/decr commands
  177. return an error
  178. - The item must already exist for incr/decr to work; these commands won't pretend that a
  179. non-existent key exists with value 0; instead, they will fail.
  180. - Underflow in the "decr" command is caught: if a client tries to decrease the value below 0,
  181. the new value will be 0.
  182. - Overflow in the "incr" command will wrap around the 64 bit mark.
  183. - Decrementing a number such that it loses length isn't guaranteed to decrement its returned
  184. length. The number MAY be space-padded at the end, but this is purely an implementation
  185. optimization, so you also shouldn't rely on that.
  186. - "quit\r\n":
  187. - Upon receiving this command, the server closes the connection. However, the client may also
  188. simply close the connection when it no longer needs it, without issuing this command.
  189. - slabs automove:
  190. - "slabs automove <0|1|2>":
  191. - 0|1|2 is the indicator on whether to enable the slabs automover or not.
  192. - <0> means to set the thread on standby
  193. - <1> means to run the builtin slow algorithm to choose pages to move
  194. - <2> is a highly aggressive mode which causes pages to be moved every time there is an
  195. eviction. It is not recommended to run for very long in this mode unless your access
  196. patterns are very well understood.
  197. - The response should always be "OK\r\n"
  198. - slabs reassign:
  199. - distribute memory once a running instance has hit its limit. It might be desireable to have
  200. memory laid out differently than was automatically assigned after the server started.
  201. - "slabs reassign <source class> <dest class>\r\n":
  202. - <source class> is an id number for the slab class to steal a page from.
  203. - A source class id of -1 means "pick from any valid class"
  204. - <dest class> is an id number for the slab class to move a page to
  205. - The response line could be one of:
  206. - "OK" to indicate the page has been scheduled to move
  207. - "BUSY [message]" to indicate a page is already being processed, try again later.
  208. - "BADCLASS [message]" a bad class id was specified
  209. - "NOSPARE [message]" source class has no spare pages
  210. - "NOTFULL [message]" dest class must be full to move new pages to it
  211. - "UNSAFE [message]" source class cannot move a page right now
  212. - "SAME [message]" must specify different source/dest ids.
  213. - 1.5.0: "NOTE: This command is subject to change as of this writing."
  214. - stats:
  215. - "stats\r\n":
  216. - it causes the server to output general-purpose statistics and settings, documented below.
  217. - "stats <args>\r\n":
  218. - Depending on <args>, various internal data is sent by the server. The kinds of arguments
  219. and the data sent are not documented in this version of the protocol, and are subject to
  220. change for the convenience of memcache developers.
  221. - Upon receiving the "stats" command without arguments, the server sents a number of lines
  222. which look like this:
  223. - "STAT <name> <value>\r\n"
  224. - "<name>" is the name of this statistic,
  225. - "<value>" is the data.
  226. - The server terminates this list with the line:
  227. - "END\r\n"
  228. - refer to the protocl document for the actual list of values, liable to change on each
  229. version of memcached.
  230. - "stats settings\r\n":
  231. - returns details of the settings of the running memcached. This is primarily made up of
  232. the results of processing commandline options.
  233. - these are not guaranteed to return in any specific order and the list in the protocol
  234. document may not be exhaustive. Otherwise, this returns like any other stats command.
  235. - "stats items\r\n":
  236. - Rows look like "STAT items:<slabclass>:<stat> <value>\r\n"
  237. - <slabclass> aligns with class ids used by the "stats slabs" command. Where "stats slabs"
  238. describes size and memory usage, "stats items" shows higher level information.
  239. - <stat> is the name of the statistic being reported by the row
  240. - <value> is the value of the statistic
  241. - "stats sizes\r\n":
  242. - Rows look like "<size> <count>\r\n"
  243. - <size> is an approximate size of the item, within 32 bytes.
  244. - <count> is the amount of items that exist within that 32-byte range.
  245. - May also return "STAT sizes_status disabled"
  246. - Since 1.4.27, needs to be enabled from the CLI or command "STAT sizes_status disabled"
  247. which may not work
  248. - This is essentially a display of all of your items if there was a slab class for every 32
  249. bytes. You can use this to determine if adjusting the slab growth factor would save memory
  250. overhead. For example: generating more classes in the lower range could allow items to fit
  251. more snugly into their slab classes, if most of your items are less than 200 bytes in size.
  252. - "stats sizes_enable\r\n":
  253. - enable the histogram at runtime. This has a small overhead to every store or delete
  254. operation. If you don't want to incur this, leave it off.
  255. - "stats slabs\r\n" Row may look like:
  256. - "STAT <slabclass>:<stat> <value>\r\n"
  257. - <slabclass> aligns with class ids used by the "stats slabs" command.
  258. - <stat> is the name of the statistic being reported by the row
  259. - <value> is the value of the statistic
  260. - "STAT <stat> <value>\r\n"
  261. - <stat> is the name of the statistic being reported by the row
  262. - <value> is the value of the statistic
  263. - touch:
  264. - update the expiration time of an existing item without fetching it.
  265. - "touch <key> <exptime> [noreply]\r\n"
  266. - "<exptime>"
  267. - Expiration time, same as with the storage commands (set/add/etc).
  268. - This replaces the existing expiration time. If an existing item were to expire in 10
  269. seconds, but then was touched with an expiration time of "20", the item would then expire
  270. in 20 seconds.
  271. - The response line to this command can be one of:
  272. - "TOUCHED\r\n" to indicate success
  273. - "NOT_FOUND\r\n" to indicate that the item with this key was not found.
  274. - "verbosity <level>\r\n":
  275. - Always succeeds, and the server sends "OK\r\n" in response (unless "noreply" is given as the
  276. last parameter). Its effect is to set the verbosity level of the logging output.
  277. - "version\r\n":
  278. - Response: "VERSION <version>\r\n"
  279. - <version> is the version string for the server.
  280. udp:
  281. uses:
  282. - The UDP interface does not provide guaranteed delivery,
  283. - so should only be used for operations that aren't required to succeed;
  284. - typically used for "get" requests where a missing or incomplete response can simply be treated as a cache miss.
  285. format:
  286. - Each UDP datagram contains a simple frame header, followed by data in the same format as the
  287. TCP protocol described above.
  288. - The frame header is 8 bytes long. All values are 16-bit integers in network byte order, high byte first:
  289. 0-1: Request ID
  290. - The request ID is supplied by the client. Typically it will be a monotonically increasing
  291. value starting from a random seed, but the client is free to use whatever request IDs it likes.
  292. - The server's response will contain the same ID as the incoming request.
  293. - The client uses the request ID to differentiate between responses to outstanding requests if
  294. there are several pending from the same server;
  295. - Any datagrams with an unknown request ID are probably delayed responses to an earlier
  296. request and should be discarded.
  297. 2-3: Sequence number
  298. - The sequence number ranges from 0 to n-1, where n is the total number of datagrams in the message.
  299. - The client should concatenate the payloads of the datagrams for a given response in
  300. sequence number order; the resulting byte stream will contain a complete response in the
  301. same format as the TCP protocol (including terminating \r\n sequences).
  302. 4-5: Total number of datagrams in this message
  303. 6-7: Reserved for future use; must be 0
  304. - In the current implementation, requests must be contained in a single UDP datagram, but
  305. responses may span several datagrams.
  306. - The only common requests that would span multiple datagrams are huge multi-key "get" requests
  307. and "set" requests, both of which are more suitable to TCP transport for reliability reasons
  308. anyway.
  309. versions:
  310. - Some notable changes... see Changelog for full list
  311. - 2003 / 1.1.10: added flush_all command
  312. - 2006 / 1.2.0: added UDP transport
  313. - 2007 / 1.2.1: <flags> switched from a 16-bit value to a 32-bit value
  314. - 2007 / 1.2.2: added verbosity command
  315. - 2007 / 1.2.4:
  316. - added cas and gets commands
  317. - incr/decr values switched from 32-bit to 64-bit values
  318. - 2009 / 1.2.7: increment/decrement no longer treats a non-integer value as 0 but returns an error
  319. -