Protocol -------- Clients of memcached communicate with server through TCP connections. (A UDP interface is also available; details are below under "UDP protocol.") A given running memcached server listens on some (configurable) port; clients connect to that port, send commands to the server, read responses, and eventually close the connection. There is no need to send any command to end the session. A client may just close the connection at any moment it no longer needs it. Note, however, that clients are encouraged to cache their connections rather than reopen them every time they need to store or retrieve data. This is because memcached is especially designed to work very efficiently with a very large number (many hundreds, more than a thousand if necessary) of open connections. Caching connections will eliminate the overhead associated with establishing a TCP connection (the overhead of preparing for a new connection on the server side is insignificant compared to this). There are two kinds of data sent in the memcache protocol: text lines and unstructured data. Text lines are used for commands from clients and responses from servers. Unstructured data is sent when a client wants to store or retrieve data. The server will transmit back unstructured data in exactly the same way it received it, as a byte stream. The server doesn't care about byte order issues in unstructured data and isn't aware of them. There are no limitations on characters that may appear in unstructured data; however, the reader of such data (either a client or a server) will always know, from a preceding text line, the exact length of the data block being transmitted. Text lines are always terminated by \r\n. Unstructured data is _also_ terminated by \r\n, even though \r, \n or any other 8-bit characters may also appear inside the data. Therefore, when a client retrieves data from a server, it must use the length of the data block (which it will be provided with) to determine where the data block ends, and not the fact that \r\n follows the end of the data block, even though it does. Keys ---- Data stored by memcached is identified with the help of a key. A key is a text string which should uniquely identify the data for clients that are interested in storing and retrieving it. Currently the length limit of a key is set at 250 characters (of course, normally clients wouldn't need to use such long keys); the key must not include control characters or whitespace. Commands -------- There are three types of commands. Storage commands (there are six: "set", "add", "replace", "append" "prepend" and "cas") ask the server to store some data identified by a key. The client sends a command line, and then a data block; after that the client expects one line of response, which will indicate success or failure. Retrieval commands ("get", "gets", "gat", and "gats") ask the server to retrieve data corresponding to a set of keys (one or more keys in one request). The client sends a command line, which includes all the requested keys; after that for each item the server finds it sends to the client one response line with information about the item, and one data block with the item's data; this continues until the server finished with the "END" response line. All other commands don't involve unstructured data. In all of them, the client sends one command line, and expects (depending on the command) either one line of response, or several lines of response ending with "END" on the last line. A command line always starts with the name of the command, followed by parameters (if any) delimited by whitespace. Command names are lower-case and are case-sensitive. Meta Commands [EXPERIMENTAL: MAY CHANGE] ------------- Meta commands are a set of new ASCII-based commands. They follow the same structure of the basic commands but have a new flexible feature set. Meta commands incorporate most features available in the binary protocol, as well as a flag system to make the commands flexible rather than having a large number of high level commands. These commands completely replace the usage of basic Storage and Retrieval commands. Meta commands are EXPERIMENTAL and may change syntax as of this writing. They are documented below the basic commands. These work mixed with normal protocol commands on the same connection. All existing commands continue to work. The meta commands will not replace specialized commands that do not sit in the Storage or Retrieval categories noted in the 'Commands' section above. All meta commands follow a basic syntax: cm <...>\r\n Where 'cm' is a 2 character command code. The number of tokens supplied is based off of the flags used. Responses look like: RC <...>\r\n Where 'rc' is a 2 character return code. The number of tokens returned are based off of the flags used. Flags are single character codes, ie 'q' or 'k' or 'I', which adjust the behavior of the command. The flags are reflected in the response. The order of which tokens are consumed or returned depend on the order of the flags given. For example, a metaget with flags of 'st' would return tokens for "size" and "TTL remaining" in that order. 'ts' would return "TTL remaining" then "size". Syntax errors are handled the same as noted under 'Error strings' section below. For usage examples beyond basic syntax, please see the wiki: https://github.com/memcached/memcached/wiki/MetaCommands Expiration times ---------------- Some commands involve a client sending some kind of expiration time (relative to an item or to an operation requested by the client) to the server. In all such cases, the actual value sent may either be Unix time (number of seconds since January 1, 1970, as a 32-bit value), or a number of seconds starting from current time. In the latter case, this number of seconds may not exceed 60*60*24*30 (number of seconds in 30 days); if the number sent by a client is larger than that, the server will consider it to be real Unix time value rather than an offset from current time. Note that a TTL of 1 will sometimes immediately expire. Time is internally updated on second boundaries, which makes expiration time roughly +/- 1s. This more proportionally affects very low TTL's. Error strings ------------- Each command sent by a client may be answered with an error string from the server. These error strings come in three types: - "ERROR\r\n" means the client sent a nonexistent command name. - "CLIENT_ERROR \r\n" means some sort of client error in the input line, i.e. the input doesn't conform to the protocol in some way. is a human-readable error string. - "SERVER_ERROR \r\n" means some sort of server error prevents the server from carrying out the command. is a human-readable error string. In cases of severe server errors, which make it impossible to continue serving the client (this shouldn't normally happen), the server will close the connection after sending the error line. This is the only case in which the server closes a connection to a client. In the descriptions of individual commands below, these error lines are not again specifically mentioned, but clients must allow for their possibility. Authentication -------------- Optional username/password token authentication (see -Y option). Used by sending a fake "set" command with any key: set \r\n username password\r\n key, flags, and exptime are ignored for authentication. Bytes is the length of the username/password payload. - "STORED\r\n" indicates success. After this point any command should work normally. - "CLIENT_ERROR [message]\r\n" will be returned if authentication fails for any reason. Storage commands ---------------- First, the client sends a command line which looks like this: [noreply]\r\n cas [noreply]\r\n - is "set", "add", "replace", "append" or "prepend" "set" means "store this data". "add" means "store this data, but only if the server *doesn't* already hold data for this key". "replace" means "store this data, but only if the server *does* already hold data for this key". "append" means "add this data to an existing key after existing data". "prepend" means "add this data to an existing key before existing data". The append and prepend commands do not accept flags or exptime. They update existing data portions, and ignore new flag and exptime settings. "cas" is a check and set operation which means "store this data but only if no one else has updated since I last fetched it." - is the key under which the client asks to store the data - is an arbitrary 16-bit unsigned integer (written out in decimal) that the server stores along with the data and sends back when the item is retrieved. Clients may use this as a bit field to store data-specific information; this field is opaque to the server. Note that in memcached 1.2.1 and higher, flags may be 32-bits, instead of 16, but you might want to restrict yourself to 16 bits for compatibility with older versions. - is expiration time. If it's 0, the item never expires (although it may be deleted from the cache to make place for other items). If it's non-zero (either Unix time or offset in seconds from current time), it is guaranteed that clients will not be able to retrieve this item after the expiration time arrives (measured by server time). If a negative value is given the item is immediately expired. - is the number of bytes in the data block to follow, *not* including the delimiting \r\n. may be zero (in which case it's followed by an empty data block). - is a unique 64-bit value of an existing entry. Clients should use the value returned from the "gets" command when issuing "cas" updates. - "noreply" optional parameter instructs the server to not send the reply. NOTE: if the request line is malformed, the server can't parse "noreply" option reliably. In this case it may send the error to the client, and not reading it on the client side will break things. Client should construct only valid requests. After this line, the client sends the data block: \r\n - is a chunk of arbitrary 8-bit data of length from the previous line. After sending the command line and the data block the client awaits the reply, which may be: - "STORED\r\n", to indicate success. - "NOT_STORED\r\n" to indicate the data was not stored, but not because of an error. This normally means that the condition for an "add" or a "replace" command wasn't met. - "EXISTS\r\n" to indicate that the item you are trying to store with a "cas" command has been modified since you last fetched it. - "NOT_FOUND\r\n" to indicate that the item you are trying to store with a "cas" command did not exist. Retrieval command: ------------------ The retrieval commands "get" and "gets" operate like this: get *\r\n gets *\r\n - * means one or more key strings separated by whitespace. After this command, the client expects zero or more items, each of which is received as a text line followed by a data block. After all the items have been transmitted, the server sends the string "END\r\n" to indicate the end of response. Each item sent by the server looks like this: VALUE []\r\n \r\n - is the key for the item being sent - is the flags value set by the storage command - is the length of the data block to follow, *not* including its delimiting \r\n - is a unique 64-bit integer that uniquely identifies this specific item. - is the data for this item. If some of the keys appearing in a retrieval request are not sent back by the server in the item list this means that the server does not hold items with such keys (because they were never stored, or stored but deleted to make space for more items, or expired, or explicitly deleted by a client). Deletion -------- The command "delete" allows for explicit deletion of items: delete [noreply]\r\n - is the key of the item the client wishes the server to delete - "noreply" optional parameter instructs the server to not send the reply. See the note in Storage commands regarding malformed requests. The response line to this command can be one of: - "DELETED\r\n" to indicate success - "NOT_FOUND\r\n" to indicate that the item with this key was not found. See the "flush_all" command below for immediate invalidation of all existing items. Increment/Decrement ------------------- Commands "incr" and "decr" are used to change data for some item in-place, incrementing or decrementing it. The data for the item is treated as decimal representation of a 64-bit unsigned integer. If the current data value does not conform to such a representation, the incr/decr commands return an error (memcached <= 1.2.6 treated the bogus value as if it were 0, leading to confusion). Also, the item must already exist for incr/decr to work; these commands won't pretend that a non-existent key exists with value 0; instead, they will fail. The client sends the command line: incr [noreply]\r\n or decr [noreply]\r\n - is the key of the item the client wishes to change - is the amount by which the client wants to increase/decrease the item. It is a decimal representation of a 64-bit unsigned integer. - "noreply" optional parameter instructs the server to not send the reply. See the note in Storage commands regarding malformed requests. The response will be one of: - "NOT_FOUND\r\n" to indicate the item with this value was not found - \r\n , where is the new value of the item's data, after the increment/decrement operation was carried out. Note that underflow in the "decr" command is caught: if a client tries to decrease the value below 0, the new value will be 0. Overflow in the "incr" command will wrap around the 64 bit mark. Note also that decrementing a number such that it loses length isn't guaranteed to decrement its returned length. The number MAY be space-padded at the end, but this is purely an implementation optimization, so you also shouldn't rely on that. Touch ----- The "touch" command is used to update the expiration time of an existing item without fetching it. touch [noreply]\r\n - is the key of the item the client wishes the server to touch - is expiration time. Works the same as with the update commands (set/add/etc). This replaces the existing expiration time. If an existing item were to expire in 10 seconds, but then was touched with an expiration time of "20", the item would then expire in 20 seconds. - "noreply" optional parameter instructs the server to not send the reply. See the note in Storage commands regarding malformed requests. The response line to this command can be one of: - "TOUCHED\r\n" to indicate success - "NOT_FOUND\r\n" to indicate that the item with this key was not found. Get And Touch ------------- The "gat" and "gats" commands are used to fetch items and update the expiration time of an existing items. gat *\r\n gats *\r\n - is expiration time. - * means one or more key strings separated by whitespace. After this command, the client expects zero or more items, each of which is received as a text line followed by a data block. After all the items have been transmitted, the server sends the string "END\r\n" to indicate the end of response. Each item sent by the server looks like this: VALUE []\r\n \r\n - is the key for the item being sent - is the flags value set by the storage command - is the length of the data block to follow, *not* including its delimiting \r\n - is a unique 64-bit integer that uniquely identifies this specific item. - is the data for this item. Meta Debug ---------- The meta debug command is a human readable dump of all available internal metadata of an item, minus the value. me \r\n - means one key string. The response looks like: ME =*\r\nEN\r\n Each of the keys and values are the internal data for the item. exp = expiration time la = time in seconds since last access cas = CAS ID fetch = whether an item has been fetched before cls = slab class id size = total size in bytes Others may be added. Meta Get -------- The meta get command is the generic command for retrieving key data from memcached. Based on the flags supplied, it can replace all of the commands: "get", "gets", "gat", "gats", "touch", as well as adding new options. mg *\r\n - means one key string. Unlike "get" metaget can only take a single key. - are a set of single character codes ended with a space or newline. - * means zero or more tokens, based on the flags supplied. They are consumed in order specified by the flags. After this command, the client expects an item to be returned, received as a text line followed by an optional data block. After the item is transferred, the server sends the string "EN\r\n" to indicate the end of the response. If a response line appearing in a retrieval request is not sent back by the server this means that the server does not have the item (because it was never stored, or stored but deleted to make space for more items, or expired, or explicitly deleted by a client). An item sent by the server looks like: VA *\r\n \r\n - are a reflection of the flags sent to the server. Extra flags can be added to the response based on the execution of the command. - * are tokens returned by the server, based on the flags supplied. They are added in order specified by the flags sent. - is the data for this item. Note that the data black is optional, requiring the 'v' flag to be supplied. The flags used by the 'mg' command are: - c: return item cas token - f: return client flags token - h: return whether item has been hit before as a 0 or 1 - k: return key as a token - l: return time since item was last accessed in seconds - O (token): opaque value, consumes a token and copies back with response - q: use noreply semantics for return codes. - s: return item size token - t: return item TTL remaining in seconds (-1 for unlimited) - u: don't bump the item in the LRU - v: return item value in These flags can modify the item: - N (token): vivify on miss, takes TTL as a argument - R (token): if token is less than remaining TTL win for recache - T (token): update remaining TTL These extra flags can be added to the response: - W: client has "won" the recache flag - X: item is stale - Z: tem has already sent a winning flag The flags are now repeated with detailed information where useful: - h: return whether item has been hit before as a 0 or 1 - l: return time since item was last accessed in seconds The above two flags return the value of "hit before?" and "last access time" before the command was processed. Otherwise this would always show a 1 for hit or always show an access time of "0" unless combined with the "u" flag. - O (token): opaque value, consumes a token and copies back with response The O (opaque) token is used by this and other commands to allow easier pipelining of requests while saving bytes on the wire for responses. For example: if pipelining three get commands together, you may not know which response belongs to which without also retrieving the key. If the key is very long this can generate a lot of traffic, especially if the data block is very small. Instead, you can supply an "O" flag for each mg with tokens of "1" "2" and "3", to match up responses to the request. Opaque tokens may be up to 32 bytes in length, and are a string similar to keys. - q: use noreply semantics for return codes. Noreply is a method of reducing the amount of data sent back by memcached to the client for normal responses. In the case of metaget, if an item is not available the "VA" line is normally not sent, and the response is terminated by "EN\r\n". With noreply enabled, the "EN\r\n" marker is suppressed. This allows you to pipeline several mg's together and read all of the responses without the "EN\r\n" lines in the middle. Errors are always returned. - u: don't bump the item in the LRU It is possible to access an item without causing it to be "bumped" to the head of the LRU. This also avoids marking an item as being hit or updating its last access time. - v: return item value in The data block for a metaget response is optional, requiring this flag to be passed in. These flags can modify the item: - N (token): vivify on miss, takes TTL as a argument Used to help with so called "dog piling" problems with recaching of popular items. If supplied, and metaget does not find the item in cache, it will create a stub item with the key and TTL as supplied. If such an item is created a 'W' flag is added to the response to indicate to a client that they have "won" the right to recache an item. The automatically created item has 0 bytes of data. Further requests will see a 'Z' flag to indicate that another client has already received the win flag. Can be combined with CAS flags to gate the update further. - R (token): if token is less than remaining TTL win for recache Similar to and can be combined with 'N'. If the remaining TTL of an item is below the supplied token, return a 'W' flag to indicate the client has "won" the right to recache an item. This allows refreshing an item before it leads to a miss. - T (token): update remaining TTL Similar to "touch" and "gat" commands, updates the remaining TTL of an item if hit. These extra flags can be added to the response: - W: client has "won" the recache flag When combined with N or R flags, a client may be informed they have "won" ownership of a cache item. This allows a single client to be atomically notified that it should cache or update an item. Further clients requesting the item can use the existing data block (if valid or stale), retry, wait, or take some other action while the item is missing. This is used when the act of recaching an item can cause undue load on another system (CPU, database accesses, time, and so on). - X: item is stale Items can be marked as stale by the metadelete command. This indicates to the client that an object needs to be updated, and that it should make a decision o if potentially stale data is safe to use. This is used when you want to convince clients to recache an item, but it's safe to use pre-existing data while the recache happens. - Z: item has already sent a winning flag When combined with the X flag, or the client N or R flags, this extra response flag indicates to a client that a different client is responsible for recaching this item. If there is data supplied it may use it, or the client may decide to retry later or take some other action. Meta Set -------- The meta set command a generic command for storing data to memcached. Based on the flags supplied, it can replace the commands: "set", "cas". as well as adding new options. ms *\r\n - means one key string. - are a set of single character codes ended with a space or newline. - * means zero or more tokens, based on the flags supplied. They are consumed in order specified by the flags. After this line, the client sends the data block: \r\n - is a chunk of arbitrary 8-bit data of length supplied by an 'S' flag and token from the previous line. If no 'S' flag is supplied the data is assumed to be 0 length. After sending the command line and the data block the client awaits the reply, which is of the format: CD *\r\n Where CD is one of: - "ST" (STORED), to indicate success. - "NS" (NOT_STORED), to indicate the data was not stored, but not because of an error. - "EX" (EXISTS), to indicate that the item you are trying to store with CAS semantics has been modified since you last fetched it. - "NF" (NOT_FOUND), to indicate that the item you are trying to store with CAS semantics did not exist. The flags used by the 'ms' command are: - C (token): compare CAS value when storing item - F (token): set client flags to token (32 bit unsigned numeric) - I: invalid. set-to-invalid if CAS is older than it should be. - k: return key as a token - O (token): opaque value, consumes a token and copies back with response - q: use noreply semantics for return codes - S (token): size of to store - T (token): Time-To-Live for item, see "Expiration" above. The flags are now repeated with detailed information where useful: - C (token): compare CAS value when storing item Similar to the basic "cas" command, only store item if the supplied token matches the current CAS value of the item. When combined with the 'I' flag, a CAS value that is _lower_ than the current value may be accepted, but the item will be marked as "stale", returning the X flag with mget requests. - F (token): set client flags to token (32 bit unsigned numeric) Sets flags to 0 if not supplied. - I: invalid. set-to-invalid if CAS is older than it should be. Functional when combined with 'C' flag above. - O (token): opaque value, consumes a token and copies back with response See description under 'Meta Get' - q: use noreply semantics for return codes Noreply is a method of reducing the amount of data sent back by memcached to the client for normal responses. In the case of metaset, a response that would start with "ST" (STORED) will not be sent. Any other code, such as "EX" (EXISTS) will still be returned. Errors are always returned. Meta Delete ----------- The meta delete command allows for explicit deletion of items, as well as marking items as "stale" to allow serving items as stale during revalidation. md *\r\n - means one key string. - are a set of single character codes ended with a space or newline. - * means zero or more tokens, based on the flags supplied. They are consumed in order specified by the flags. The response is in the format: CD *\r\n Where CD is one of: - "DE" (DELETED), to indicate success - "NF" (NOT_FOUND), to indicate that the item with this key was not found. - "EX" (EXISTS), to indicate that the supplied CAS token does not match the stored item. The flags used by the 'md' command are: - C (token): compare CAS value - I: invalidate. mark as stale, bumps CAS. - k: return key - O: opaque to copy back. - q: noreply - T (token): updates TTL, only when paired with the 'I' flag The flags are now repeated with detailed information where useful: - C (token): compare CAS value Can be used to only delete or mark-stale if a supplied CAS value matches. - I: invalidate. mark as stale, bumps CAS. Instead of removing an item, this will give the item a new CAS value and mark it as stale. This means when it is later fetched by metaget, the client will be supplied an 'X' flag to show the data is stale and needs to be recached. - O (token): opaque to copy back. See description under 'Meta Get' - q: noreply See description under 'Meta Set'. In the case of meta delete, this will hide response lines with the code "DE". It will still return any other responses. - T (token): updates TTL, only when paired with the 'I' flag When marking an item as stale with 'I', the 'T' flag can be used to update the TTL as well; limiting the amount of time an item will live while stale and waiting to be recached. Meta No-Op ---------- The meta no-op command exists solely to return a static response code. It takes no flags, no arguments. This returns the static response: "EN\r\n" Similar to a meta get command. This command is useful when used with the 'q' flag and pipelining commands. For example, with 'mg' the response lines are blank on miss, and the 'q' flag will hide the "EN\r\n" response code. If pipelining several 'mg's together with noreply semantics, an "mn\r\n" command can be tagged to the end of the chain, which will return an "EN\r\n", signalling to a client that all previous commands have been processed. Slabs Reassign -------------- NOTE: This command is subject to change as of this writing. The slabs reassign command is used to redistribute memory once a running instance has hit its limit. It might be desirable to have memory laid out differently than was automatically assigned after the server started. slabs reassign \r\n - is an id number for the slab class to steal a page from A source class id of -1 means "pick from any valid class" - is an id number for the slab class to move a page to The response line could be one of: - "OK" to indicate the page has been scheduled to move - "BUSY [message]" to indicate a page is already being processed, try again later. - "BADCLASS [message]" a bad class id was specified - "NOSPARE [message]" source class has no spare pages - "NOTFULL [message]" dest class must be full to move new pages to it - "UNSAFE [message]" source class cannot move a page right now - "SAME [message]" must specify different source/dest ids. Slabs Automove -------------- NOTE: This command is subject to change as of this writing. The slabs automove command enables a background thread which decides on its own when to move memory between slab classes. Its implementation and options will likely be in flux for several versions. See the wiki/mailing list for more details. The automover can be enabled or disabled at runtime with this command. slabs automove <0|1> - 0|1|2 is the indicator on whether to enable the slabs automover or not. The response should always be "OK\r\n" - <0> means to set the thread on standby - <1> means to return pages to a global pool when there are more than 2 pages worth of free chunks in a slab class. Pages are then re-assigned back into other classes as-needed. - <2> is a highly aggressive mode which causes pages to be moved every time there is an eviction. It is not recommended to run for very long in this mode unless your access patterns are very well understood. LRU Tuning ---------- Memcached supports multiple LRU algorithms, with a few tunables. Effort is made to have sane defaults however you are able to tune while the daemon is running. The traditional model is "flat" mode, which is a single LRU chain per slab class. The newer (with `-o modern` or `-o lru_maintainer`) is segmented into HOT, WARM, COLD. There is also a TEMP LRU. See doc/new_lru.txt for details. lru