How Memcached ASCII Protocol Differs From Binary Protocol
Memcached offers both a text-based ASCII protocol and a structured binary protocol, which differ significantly in parsing complexity, network overhead, and computational performance. While the ASCII protocol uses plain-text commands terminated by line breaks, requiring string scanning and delimiter checks, the binary protocol uses fixed-length, byte-aligned headers that allow direct memory access and faster decoding. Consequently, the binary protocol reduces CPU cycles spent on byte parsing, minimizes payload size overhead, and lowers network transfer latency for high-throughput caching environments.
Frame Structure and Parsing Overhead
The ASCII protocol operates as a human-readable text format where
commands, keys, flags, and values are separated by spaces and terminated
by CR-LF (\r\n) characters. To process an ASCII request,
the server must parse strings byte-by-byte, scan for delimiters, and
convert string-based arguments (such as key lengths, expiration times,
or integer values) into numeric types. This continuous string parsing
introduces non-trivial CPU overhead per request, especially when
handling thousands of concurrent connections.
In contrast, the binary protocol utilizes a standardized 24-byte header for all requests and responses. Key information—such as command opcodes, key length, extra header length, body length, and status codes—occupies fixed offset byte locations within the header. Instead of scanning for line breaks, the parser reads the static 24-byte structure directly, extracts exact field lengths, and immediately calculates memory offsets. This eliminates string tokenization entirely, drastically reducing CPU branch mispredictions and frame parsing latency.
Network Throughput and Payload Efficiency
Network efficiency varies between the two protocols due to how data and command metadata are serialized:
- Header Overhead: The ASCII protocol includes
command words like
GET,SET, orVALUEin plain text along with space characters and trailing line breaks. For small keys and values, this text metadata represents a sizable percentage of the total packet size. The binary protocol uses single-byte opcodes and compact numerical fields, keeping request headers small and predictable regardless of command type. - Numeric Encoding: In ASCII mode, numerical
parameters—such as counter values, flags, and TTLs—are encoded as
decimal string digits. An integer like
1000000takes 7 bytes as ASCII text, whereas the binary protocol transmits it as a compact 4-byte or 8-byte binary integer. - Framing Safety: Because ASCII relies on
\r\ndelimiters, transmitting binary blobs requires explicit byte-length counts to prevent binary data from accidentally triggering premature frame endings. The binary protocol handles raw byte arrays naturally through its explicit packet length fields, avoiding delimiter collisions and simplifying stream handling.
CPU Utilization and Execution Performance
By eliminating string tokenization and character-by-character scanning, the binary protocol requires fewer CPU cycles per operation compared to the ASCII protocol. This efficiency directly translates into higher packet-processing throughput (RPS) and lower response latency on saturated, high-traffic Memcached instances.
The binary protocol also includes features designed for optimization
that ASCII lacks, such as native quiet commands (e.g., GetQ
and SetQ). Quiet commands suppress successful response
messages from the server, allowing clients to pipeline multiple GET
requests over a single connection without reading individual success
responses for every missing key. This capability substantially reduces
network round-trips and socket I/O overhead under heavy batch
workloads.