Memcached doesn’t actually support UDP.
It’s easy to think it might, given how many other network services do, but Memcached has always been a TCP-only affair. This distinction is crucial because it dictates how Memcached handles connections, data transfer, and error recovery, all of which have significant performance implications.
Here’s how Memcached operates, and why you only ever deal with TCP:
Memcached’s core protocol is a simple, text-based request/response system. When a client wants to set a value, it sends a command like set mykey 0 60 5\r\nvalue\r\n. The server processes this, and if successful, responds with STORED\r\n. For gets, it’s get mykey\r\n, and the server responds with VALUE mykey 0 5\r\nvalue\r\nEND\r\n (or just END\r\n if the key isn’t found).
This request/response model is inherently suited to a connection-oriented protocol like TCP. TCP provides reliable, ordered delivery of data, which is essential for ensuring that commands are received correctly and that the correct responses are sent back. Imagine if a UDP packet containing your set command got lost or arrived out of order: Memcached wouldn’t know what to do, and your data would be corrupted or lost.
The fact that Memcached is TCP-only also means it doesn’t have to deal with the complexities of UDP’s connectionless nature. UDP is "fire and forget" – the sender doesn’t know if the packet arrived. This is great for high-volume, low-latency applications where occasional data loss is acceptable (like DNS lookups or streaming video), but terrible for a cache where data integrity is paramount.
Because Memcached uses TCP, it leverages TCP’s built-in mechanisms for:
- Connection Management: Clients establish a persistent TCP connection to the Memcached server. This avoids the overhead of setting up a new connection for every single request, which is a common pattern with UDP services.
- Reliability: TCP guarantees that data sent will arrive at the destination in the correct order. If packets are lost, TCP handles retransmission. This means your
setandgetoperations are much more likely to succeed without application-level error checking for network issues. - Flow Control: TCP manages the rate at which data is sent to prevent overwhelming the receiver. This is important for a high-throughput service like Memcached.
When you connect to Memcached, you’re always using TCP. There’s no memcached-udp command, and no standard library that talks UDP to Memcached. If you’re seeing discussions or code that seems to imply UDP usage, it’s almost certainly a misunderstanding or a custom wrapper that’s then using TCP under the hood.
The primary reason people might wish Memcached supported UDP is for potential performance gains by avoiding TCP’s overhead (like connection setup and acknowledgments). However, Memcached’s design prioritizes simplicity and reliability, and the overhead of TCP is generally well-managed by modern operating systems and network stacks. The performance benefits of a custom UDP implementation would likely be marginal and come at the cost of guaranteed delivery and easier development.
The next hurdle you’ll encounter is understanding how to optimize Memcached’s TCP connections for high-throughput scenarios, particularly around connection pooling and avoiding head-of-line blocking.