Fact-checked by Grok 2 weeks ago

Memcached

Memcached is a free and open-source, high-performance, object caching system designed primarily to accelerate dynamic web applications by reducing the load on persistent data stores such as databases. It serves as an in-memory key-value store for small chunks of arbitrary data, including strings and serialized objects, commonly derived from database query results, responses, or page rendering outputs, with each value limited to 1 in size. Originally developed in 2003 by Brad Fitzpatrick of Danga Interactive to address scalability challenges at the LiveJournal blogging platform, Memcached quickly evolved into a generic caching solution through community contributions and has since been integral to large-scale distributed systems. Licensed under the Revised BSD license, it emphasizes simplicity, enabling rapid deployment and integration via client libraries for most major programming languages. Its adoption spans numerous high-traffic websites, with Facebook reporting itself as the world's largest user, deploying large-scale clusters to handle billions of cache requests per second. At its core, Memcached employs a client-server architecture where clients perform consistent hashing to map keys to servers, creating a unified virtual memory pool across a cluster without server-side coordination, replication, or durability guarantees. This design supports horizontal scaling by adding nodes seamlessly and delivers average O(1) time complexity for get and set operations, allowing high-end servers to process millions of keys per second. Data management relies on a least recently used (LRU) eviction policy combined with configurable time-to-live (TTL) expirations, ensuring efficient use of volatile RAM while preventing indefinite growth.

History and Development

Origins and Initial Release

Memcached was developed in 2003 by Brad Fitzpatrick, a software engineer at Danga Interactive, to address scaling challenges faced by the blogging platform during periods of high traffic. , which had grown to handle over 50 million dynamic page views per day and peaks exceeding 1,000 requests per second, experienced significant database load from repeated queries for common elements such as user profiles, recent posts, and friend lists. These issues were exacerbated by the limitations of MySQL's storage engine, which struggled with concurrency, and the slower performance of for write-heavy operations, prompting the need for an in-memory caching layer to store and retrieve frequently accessed data without hitting the database. By caching such "hot" data, Memcached aimed to reduce query volumes dramatically—for instance, transforming multi-ID lookups like "SELECT * FROM users WHERE id IN (1,2,3)" into direct memory accesses. The initial implementation was a prototype written in Perl and released as open-source software on May 22, 2003, under the management of Danga Interactive. This early version quickly proved insufficient for production-scale performance due to Perl's overhead in handling high-throughput network I/O. Shortly thereafter, it was rewritten in C by Anatoly Vorobey, a LiveJournal engineer, to leverage asynchronous I/O via libevent and achieve better efficiency with features like a slab-based memory allocator to prevent fragmentation and ensure constant-time operations. The C implementation enabled Memcached to support least-recently-used (LRU) eviction policies and hash table storage, making it suitable for distributed deployment across multiple servers. At , Memcached was deployed across 28 instances on 10 hosts, caching approximately 30 GB of data and achieving hit rates of 90-93%, which eliminated around 100,000 database queries per second during peaks and improved overall system uptime to over 190 days. By 2005, the technology had spread beyond LiveJournal to other high-traffic sites, including , where it was adopted in August 2005 to combat growing database query sizes and slow page loads on web servers. It has since been adopted by sites such as . This early adoption underscored Memcached's role in enabling scalable web architectures for dynamic content delivery.

Evolution and Key Versions

Memcached was initially released as open-source software under the Revised BSD license in 2003 by Brad Fitzpatrick at Danga Interactive for use at LiveJournal. Following its inception, the project transitioned to community maintenance, with ongoing development coordinated through the official memcached.org website and its associated mailing lists and repositories. Key milestones in Memcached's evolution include the 1.2 series, released starting September 2006, which introduced UDP protocol support for multicast and broadcast scenarios, alongside configurable slab sizes to reduce memory fragmentation. The 1.3 series in early 2009 integrated multi-threaded support by default, eliminating the need for separate build configurations and enabling better scalability on multi-core systems through libevent-based asynchronous I/O. Version 1.4.0, released in July 2009, enhanced the slab allocator with per-slab-class statistics—such as hit rates for gets, sets, and evictions—to facilitate monitoring and tuning of memory usage. Subsequent releases focused on reliability and . The 1.5 series, beginning with 1.5.0 in July 2017, included improvements to handling, notably disabling it by default in later patches like 1.5.6 to mitigate amplification-based DDoS attacks while preserving optional enablement for legitimate use cases. Version 1.6.0, released March 8, 2020, introduced external flash storage (extstore) for disk overflow, allowing persistent caching of items exceeding available limits without full eviction. The latest stable release, 1.6.39 on July 28, 2025, primarily addresses bug fixes, performance optimizations, and security patches, including resolutions for potential denial-of-service vulnerabilities in connection handling. contributions have been pivotal, with notable forks such as Couchbase's of Membase (now part of ), which extends Memcached's core with persistence and clustering features while maintaining protocol compatibility.

Design Principles

Core Concepts

Memcached functions as a simple key-value store, where arbitrary data objects—such as strings or serialized structures—are associated with unique keys for storage and retrieval. Keys are typically strings up to 250 characters, and values can hold up to 1 MB of , with the server treating all objects opaquely without interpreting their content or enforcing schemas. There are no built-in relationships between keys, and querying is limited to exact key matches, emphasizing its role as a lightweight rather than a full database. Each stored item includes an optional expiration time (time-to-live, or ) to automatically evict stale data, further aligning with caching semantics. By default, Memcached operates entirely in memory using RAM for , providing sub-millisecond access times through its mechanism, until version 1.6.0 introduced optional extensions for to augment . In distributed deployments, multiple independent Memcached servers collaborate without direct communication, forming a logical hash ring for data sharding where clients apply algorithms to determine key placement across nodes. This client-side partitioning ensures scalability and with minimal reconfiguration when servers are added or removed. Notably, Memcached lacks built-in , replication, or features, prioritizing , low overhead, and maximum throughput—capable of handling millions of operations per second on commodity hardware—to focus solely on volatile caching.

Data Structures and Eviction Policies

Memcached employs a mechanism to manage its in-memory storage efficiently, dividing the allocated into fixed-size pages of 1 MB each. These pages are then subdivided into slab classes, where each class consists of chunks of a specific size tailored to common item sizes, starting from 80 bytes for the smallest class and scaling up to 1 MB for the largest. This approach minimizes fragmentation by pre-allocating chunks of predetermined sizes rather than dynamically allocating space for each item, with the growth factor between slab sizes configurable via the -f command-line option to optimize for workload patterns. Each stored item in Memcached follows a defined internal that includes the (an arbitrary ), the (raw ), user-supplied flags (a 32-bit for application-specific ), an expiration time represented as a time-to-live () in seconds (with values up to 2,592,000 seconds for relative TTL; larger values are interpreted as Unix timestamps for absolute expiration time), and a Compare-And-Swap () (a 64-bit ) to enable operations. The overhead for this is approximately 32 bytes without CAS or 40 bytes with it enabled, plus the key , ensuring compact representation while supporting features like conditional updates. Key lengths are limited to 250 bytes, while the total size of each item, including key, , and overhead, is limited to 1 MB to fit within the slab allocation chunks; the binary protocol adheres to the same limits but provides more efficient handling of binary keys without ASCII restrictions. Memcached does not provide built-in compression for items, relying instead on clients to compress before to reduce memory usage for compressible payloads. For eviction, Memcached primarily uses a segmented Least Recently Used (LRU) policy, introduced as the default in version 1.5.0, which maintains separate LRU queues per slab class to track item recency based on access patterns without updating timestamps on every fetch. This policy divides each slab's LRU into sub-queues—HOT for recently active items, WARM for moderately used ones, COLD for less active, and TEMP for short-lived items—using bit flags to demote or promote items asynchronously via a background maintainer thread, ensuring efficient eviction when free chunks are unavailable after attempting to reclaim expired items first. Older versions prior to 1.5.0 used a simpler LRU implementation based on a doubly linked list, but the modern LRU has become the standard for its balance of accuracy and low overhead. To enhance memory efficiency beyond pure RAM constraints, versions 1.6.0 and later introduce an extstore feature that allows overflow to flash storage when is full, flushing less active item values from the LRU tail to disk while retaining keys, flags, TTLs, and in RAM slabs for quick lookups. This approach replaces large slab chunks with smaller entries, reducing RAM footprint for evicted items, though it introduces trade-offs for disk access; configuration options like write buffers and page sizes enable tuning for specific environments.

Technical Architecture

Protocol and Communication

Memcached employs a client-server communication model utilizing either a text-based ASCII or a binary , both operating over or on the port 11211. The ASCII serves as the and is designed for simplicity, using human-readable text lines terminated by CRLF (\r\n) for commands and responses, while unstructured binary data blocks handle the actual stored values. This facilitates straightforward parsing but incurs overhead from text processing. The binary , introduced in version 1.2, offers a more compact, structured alternative with fixed-length headers and opcodes, though it is now deprecated in favor of newer options like the meta text . In the ASCII protocol, client requests begin with a command name followed by parameters specific to the operation, such as key, flags, expiration time (exptime), and byte length of the value. For storage commands like set, the format is: set <key> <flags> <exptime> <bytes> [noreply]\r\n<value>\r\n, where <key> is an arbitrary string up to 250 bytes, <flags> is a client-defined 16-bit or 32-bit integer stored with the value, <exptime> specifies expiration in seconds (0 for no expiration) or Unix timestamp, <bytes> indicates the value length excluding the trailing CRLF, and [noreply] suppresses the response for fire-and-forget operations. Core storage commands include set (unconditional store), add (store only if key absent), and replace (store only if key present), all returning STORED\r\n on success or NOT_STORED\r\n if conditions fail. Retrieval uses get <key>*\r\n, yielding VALUE <key> <flags> <bytes> [<cas unique>]\r\n<value>\r\n...END\r\n, where the optional CAS (check-and-set) unique is a 64-bit identifier for atomic updates. Deletion employs delete <key> [noreply]\r\n, responding with DELETED\r\n or NOT_FOUND\r\n. Arithmetic operations like incr <key> <value> [noreply]\r\n and decr <key> <value> [noreply]\r\n treat values as unsigned 64-bit integers, returning the new value or NOT_FOUND\r\n, with decrements clamped at 0. The flush_all [delay] [noreply]\r\n command invalidates all items immediately or after a delay in seconds, returning OK\r\n. The protocol enhances efficiency through a 24-byte request header including magic bytes, , length, extra length, , total body length, opaque (client context), and , followed by optional extras, , and . distinguish commands (e.g., 0x00 for get, 0x01 for set, 0x08 for delete, 0x0A/0x0B for incr/decr), with quiet variants (e.g., 0x11 for setq) omitting responses. It reduces overhead via fixed structures and encoding, supports larger payloads through explicit length fields, and improves handling with 16-bit status codes (e.g., 0x0001 for not found, 0x0003 for too large) in responses, which include a similar header plus an optional text. Despite these benefits, adoption was limited due to and concerns, leading to its . Both protocols support for low-latency, scenarios, particularly in high-connection environments, but lacks reliability guarantees, risking without retransmission. Requests must fit a single , precluding multi-key gets, and clients treat absent responses as misses; an 8-byte framing header aids in sequencing multi-packet responses, though reassembly adds client burden. remains preferred for operations requiring acknowledgments, such as commands, to ensure delivery.

Server-Side Components

The Memcached server employs a multi-threaded , utilizing separate threads for listening on the network , processing client connections, and performing background tasks such as LRU maintenance scans. This design allows the server to leverage multiple CPU cores for parallelism, with worker threads handling incoming requests independently while coordinating access to shared data structures through locks. At the core of its I/O handling is an event-driven model powered by the library, which enables efficient of sockets and supports the management of thousands of concurrent connections without blocking. Each worker thread maintains its own to process commands and client interactions, minimizing contention and ensuring low-latency responses even under high load. The listener thread accepts new connections and dispatches them to available workers, while background threads handle non-blocking operations to enforce policies asynchronously. Since 1.4.23, the LRU uses segmented queues (HOT, WARM, ) with shuffling managed by these background threads. Memcached operates without built-in clustering mechanisms; each server instance functions independently as a standalone , relying on client-side logic for data distribution across multiple servers using techniques like . For monitoring and diagnostics, the server provides a built-in stats command accessible via the , which reports key metrics including usage (total bytes allocated), hit rates (get_hits and get_misses ratios), and uptime since startup. Additional verbosity levels (e.g., -vv) expose slab class details and LRU statistics, while the system supports extensible reporting through configuration options like LRU crawler activation for expiration scans. Resource management is configurable to prevent overload, with a default maximum of simultaneous connections tunable via the -c flag to accommodate varying workloads. Item sizes are limited to 1 per key-value pair, allocated within slab classes for efficient memory partitioning, and overall memory usage is capped by the -m option (default 64 ), reserving space in 1 pages with minimal overhead for hash tables and buffers.

Implementation and Configuration

Client Libraries and APIs

Developers integrate Memcached into applications through language-specific client libraries that abstract the underlying protocol and handle distribution across multiple servers. The official C client library, libmemcached, provides a thread-safe for interacting with Memcached servers, supporting both text and protocols while emphasizing low usage and high for embedded or web applications. Community-maintained libraries extend this functionality to other languages, enabling seamless adoption in diverse ecosystems. For , XMemcached offers a high-performance, multithreaded client supporting efficient operations over multiple nodes; while Spymemcached provides an asynchronous, single-threaded option (last updated in 2017), XMemcached is more actively maintained (last release November 2023). In , pylibmc serves as a high-performance wrapper around libmemcached, providing a simple for quick integration while maintaining compatibility with standard memcached behaviors. developers commonly use the PECL memcached extension, which leverages libmemcached to deliver robust server communication, including support for session storage and object caching. For environments, memjs implements a pure client that adheres to the , facilitating caching in asynchronous applications with built-in SASL support. Client libraries bear key responsibilities in managing distributed clusters, including hashing keys to select target servers and handling dynamic cluster changes. Most implementations employ algorithms, such as those based on CRC32 or , to map keys to servers in a way that minimizes data remapping when nodes are added or removed, ensuring balanced distribution without central coordination. Additionally, clients detect server failures through timeouts or connection errors and perform by redirecting requests to available nodes, while rebalancing occurs implicitly via the hashing mechanism to redistribute load across the updated server list. Common API patterns across these libraries enhance efficiency and usability in production settings. Connection pooling is a standard feature, allowing multiple reusable connections to servers to handle concurrent requests without excessive overhead, as seen in XMemcached's NIO-based pools or pylibmc's threaded connections. Batch operations like multi-get and multi-set reduce network round-trips by fetching or storing multiple key-value pairs in a single call, a capability supported natively in the Memcached protocol and exposed in client such as Java's MemcachedClient.getAll(). For storing complex data structures, libraries often include options, such as converting objects to strings before transmission, preserving type information while adhering to Memcached's string-based storage model. These libraries find widespread use in popular frameworks and cloud environments, streamlining caching in high-traffic applications. In the stack, the PECL extension integrates directly with and for session handling and query result caching. Ruby on Rails leverages clients like Dalli, which builds on libmemcached, to enable declarative caching in web apps. In architectures, they support distributed caching layers for inter-service communication. AWS ElastiCache provides tailored clients, such as enhanced versions of XMemcached, for managed Memcached clusters, allowing automatic scaling and integration with EC2 or Lambda-based services. Regarding protocol and feature support, modern libraries prioritize the binary for its compactness and reduced parsing overhead compared to the text protocol, enabling faster operations in bandwidth-constrained scenarios. Some implementations extend this with optional data compression, such as zlib in pylibmc, to minimize transmission sizes for larger values without altering core compatibility.

Server Setup and Options

Memcached can be installed on various operating systems using package managers, which handle dependencies and security updates automatically. On and systems, the command sudo apt-get install memcached installs the server along with its runtime dependencies. For and distributions, sudo yum install memcached or sudo dnf install memcached is used similarly. On macOS, Homebrew facilitates installation via brew install memcached. For systems requiring custom builds, compiling from source is an option; this necessitates prerequisites like a C compiler and library, installed via sudo apt-get install build-essential libevent-dev on / or sudo yum install libevent-devel on /. The source code is downloaded with wget https://memcached.org/latest, extracted, configured (e.g., ./configure --prefix=/usr/local), and built using make && sudo make install. Once installed, Memcached runs as a daemon, typically managed by the operating system's service framework, such as starting it with sudo systemctl start memcached on systemd-based systems. Configuration occurs primarily through command-line options passed when starting the , often via service files or init scripts. The -m option sets the maximum memory allocation in megabytes, defaulting to 64 MB if unspecified, which should be tuned based on available system and expected to prevent excessive . The -p flag specifies the port, defaulting to 11211, while -l defines the interface to bind to, such as an for multi-homed servers. For concurrency, -t controls the number of worker threads, defaulting to 4, which can be increased on multi-core systems to handle higher connection volumes. These options allow the to listen for client connections efficiently without delving into implementations. Security-related flags enhance protection in production environments. Using -U 0 disables the protocol entirely, mitigating risks from unauthenticated amplification attacks since Memcached's UDP mode lacks built-in access controls. The -S option enables (Simple Authentication and Security Layer) support for authenticated access, requiring compatible clients. Verbose logging with -vv provides detailed output for , capturing connection attempts and errors to aid . Monitoring Memcached involves its built-in statistics endpoint, accessible via the stats command over on port 11211, which reports metrics like current items, counts, and usage. For production , integration with tools like is common through exporters that scrape the stats interface, enabling dashboards for key performance indicators such as hit rates and . Best practices for production deployment emphasize security and efficiency. Memcached should run behind a , restricting access to trusted networks only, as it lacks native or fine-grained authorization. Memory allocation via -m ought to be set conservatively, typically reserving about 50% of available for to account for overhead and growth, adjusted per workload to minimize evictions. Enabling auto-start with ensures reliability, using unit files to manage restarts and resource limits.

Usage and Applications

Basic Operations

Memcached provides a simple set of commands for storing, retrieving, and managing key-value data in its in-memory cache, primarily through a text-based protocol that allows clients to interact directly with the server. These operations are atomic and designed for high-speed access, with keys limited to 250 bytes and values up to 1 MB by default. Storing data in Memcached can be accomplished using the set command, which overwrites any existing value associated with the key or creates a new entry if none exists. The syntax is set <key> <flags> <exptime> <bytes>\r\n<data>\r\n, where <flags> are client-defined opaque integers, <exptime> specifies the expiration time in seconds (0 for no expiration), and <bytes> indicates the data length. For conditional storage, the add command stores the value only if the key does not already exist, returning NOT_STORED if it does; its syntax mirrors set. Similarly, the replace command updates the value only for existing keys, failing with NOT_STORED otherwise. These commands promote items to the top of the least recently used (LRU) eviction queue upon successful storage or access. Retrieving data uses the get command for single or multiple keys, specified as space-separated in the request (e.g., get key1 key2\r\n for multi-get), which returns the , flags, and on a , or on a miss, followed by END. The gets variant includes a 64-bit (CAS) identifier for updates, enabling safe modifications without conditions; its response format appends the CAS to each . Multi-get operations are efficient for batch retrievals, reducing network round-trips. Deleting data is handled by the delete command, which removes the item if the exists, returning DELETED on success or NOT_FOUND if absent; syntax is delete <key> [cas <value>]\r\n, with optional for versioned deletes. For counters, incr and decr perform increments or decrements on 64-bit unsigned integer , requiring an initial numeric set via set or similar; they wrap around on /underflow and return the new or NOT_FOUND if the key lacks a numeric . Syntax: incr/decr <key> <value>\r\n, where <value> is the . Expiration is managed via the <exptime> parameter in storage commands, interpreted as seconds from the current time (up to 30 days; values beyond are treated as Unix timestamps for absolute expiration), with 0 indicating no automatic removal. The flush_all command invalidates all items immediately or after a specified delay in seconds (e.g., flush_all [delay]\r\n), without immediately freeing memory to avoid performance impacts; it returns OK. Error handling in Memcached uses simple response strings: NOT_STORED for failed storage attempts like conditional add or replace, END to terminate multi-get responses, NOT_FOUND for missing keys in deletes or increments, and OK for successes. Cache effectiveness is often measured by the hit/miss ratio, calculated as get_hits / (get_hits + get_misses) from server statistics, where a high ratio (ideally in the high 90s) indicates frequent cache utilization over backend fetches.

Advanced Use Cases

Memcached finds extensive application in session storage for web applications, where it caches user session data to minimize database queries and enhance response times. In frameworks like , sessions can be configured to use Memcached as the backend cache, storing transient user information such as authentication states or preferences directly in memory for rapid retrieval. Similarly, integrates Memcached to cache session-related objects, reducing load on underlying databases during high-traffic scenarios. This approach is particularly effective for stateless web architectures, as it allows sessions to persist across server instances without persistent storage overhead. For analytics, Memcached serves as an efficient store for counters tracking events like page views, likes, or interactions, enabling quick increments and decrements without database round-trips. Its operations, such as incr and decr, facilitate safe updates in distributed environments, supporting applications that require low-latency aggregation. , for instance, employs Memcached to cache query results for dynamic features including leaderboards and vote counters, which helps manage the platform's high-velocity traffic while maintaining near- updates. In environments, Memcached integrates seamlessly with managed services that provide scalability and reliability. for Memcached offers auto-scaling capabilities, automatically adjusting cluster node counts based on metrics like CPU utilization or request rates to handle variable workloads without manual intervention. Cloud's Memorystore for Memcached delivers a fully managed, compatible service with built-in and scaling, allowing applications to provision clusters that support up to petabytes of in-memory capacity across regions. These integrations abstract infrastructure management, enabling developers to focus on application logic while benefiting from Memcached's performance. Hybrid caching strategies often pair Memcached with complementary systems like or databases to address limitations such as lack of persistence. Memcached handles hot, frequently accessed data in memory for speed, while provides durability for critical items through snapshotting or append-only files, creating a tiered that balances and data reliability. Additionally, since version 1.6.0, Memcached's extstore feature enables disk overflow for "cold" data, extending effective capacity to flash storage like NVMe SSDs without altering the core , thus supporting larger datasets in resource-constrained setups. Notable case studies illustrate Memcached's role in large-scale systems. At , the (The Associations and Objects) data store uses Memcached as a graph-aware cache layered over , storing social graph edges and associations to accelerate queries for billions of daily interactions, achieving sub-millisecond latencies for common traversals. (now X) leverages a customized Memcached , twemcache, to cache precomputed user timelines, reducing rendering times for home feeds by serving tweet sequences directly from memory and mitigating database strain during peak loads of over 300,000 . These implementations highlight Memcached's adaptability in handling complex, high-throughput workloads.

Security Considerations

Authentication Mechanisms

Memcached provides authentication primarily through the (SASL), which was introduced in version 1.4.3. SASL allows for pluggable authentication mechanisms, with support for the PLAIN mechanism for simple username and password authentication, as well as external mechanisms such as SASLDB for storing credentials in a database file. These mechanisms operate over the binary protocol and require clients to initiate authentication before performing operations. To enable SASL, Memcached must be compiled with the --enable-sasl option, which depends on the presence of the SASL library. The is then started with the -S flag to activate SASL commands and enforce the binary protocol. occurs via a SASL file, typically located at /etc/sasl2/memcached.conf, which specifies the mechanism list (e.g., mech_list: ) and the path to the credential database (e.g., sasldb_path: /etc/sasl2/memcached/sasldb2). Users are added to the database using the saslpasswd2 utility, and the SASL_CONF_PATH must point to the directory when launching the . Client libraries such as libmemcached must be built with SASL support to authenticate, using functions like memcached_set_sasl_auth_data to provide . Memcached lacks native access control lists (ACLs), so access is managed by binding the server to specific interfaces or addresses using the -l option (e.g., -l 127.0.0.1 to restrict to ). For broader control, administrators rely on external tools like firewalls (e.g., or ufw) to limit incoming connections to trusted IP ranges, or proxies such as to enforce and rules before reaching the Memcached instances. Best practices for securing Memcached include enabling TCP-only mode with the -U 0 flag to disable , as exposing publicly increases risks of attacks. For remote access, connections should be tunneled through a VPN rather than exposing the service directly. A key limitation of Memcached's is the absence of built-in ; SASL credentials and data are transmitted in plain text over . To mitigate this, TLS wrappers like can be used to encrypt traffic between clients and the server.

Vulnerabilities and Mitigations

Memcached, by design, operates without built-in , making exposed instances vulnerable to unauthorized that can lead to cache poisoning or denial-of-service () attacks through resource exhaustion, such as fill attacks that overwhelm slabs with large or numerous items. A prominent example is the exploitation of support for distributed denial-of-service (DDoS) attacks, where small spoofed requests can elicit responses up to 51,000 times larger, as seen in the 2018 on that peaked at 1.35 Tbps using over 126 million packets per second from exposed Memcached servers. Historical vulnerabilities include CVE-2016-8704, an in the binary protocol's append/prepend functions that could trigger a heap-based and potential remote code execution, affecting versions up to 1.4.32 and fixed in 1.4.33. Another is CVE-2018-1000115, which enabled efficient traffic amplification via , allowing by flooding networks with magnified responses, impacting version 1.5.5 and addressed in subsequent releases. To mitigate these risks, administrators should disable UDP support using the -U 0 option, which has been the default since 1.5.6 to prevent attacks. Firewalls, such as rules blocking inbound traffic to port 11211 from untrusted sources, provide an additional layer of protection against unauthorized access and external exploits. tools like fail2ban can be configured to scan Memcached logs for suspicious patterns and automatically ban offending IPs, while regular updates to the latest —such as 1.6.39 released in July 2025—ensure patches for known issues are applied. As of 2025, Memcached deployments in cloud environments face heightened scrutiny due to misconfigurations in services like AWS ElastiCache, but no major new exploits have emerged since version 1.6.39, emphasizing the importance of network isolation and access controls over relying on the protocol's inherent simplicity.

Performance and Scaling

Optimization Strategies

Optimizing Memcached involves tuning its configuration and usage patterns to enhance speed and efficiency on a single instance, focusing on allocation, handling, effectiveness, and .

Memory Tuning

Memcached allocates memory using a , dividing the specified into fixed-size pages (default 1 ) that are further split into chunks for storing items of similar sizes. To adapt to specific workloads, the slab page size can be adjusted using the -I option, allowing larger pages (up to 128 ) for bigger objects or smaller ones to create more granular , which helps minimize internal fragmentation. For example, increasing the page size to 4 via -I 4m suits applications with larger keys, but requires testing to ensure it aligns with item distributions. Monitoring slab usage is essential; the stats slabs command, accessed via on port 11211, reveals details like chunk sizes, allocated pages, and utilization per . Keeping slab utilization below 80% is recommended to prevent excessive evictions and thrashing, as high fill rates (e.g., over 90% in a ) can lead to frequent LRU removals under load. Additionally, the chunk can be tuned with -f (default 1.25), lowering it to 1.08 for finer in variable-sized items to reduce wasted space.

Connection Optimization

Memcached's connection handling can bottleneck performance under high concurrency, so increasing the maximum simultaneous connections with the -c option (default 1024) allows more clients without rejection, though it must be balanced against available to avoid swapping. For instance, setting -c 4096 supports heavier loads but requires monitoring OS limits like file descriptors. Client-side pipelining, where multiple requests are sent over a single connection without waiting for responses, reduces latency by batching operations; libraries like libmemcached support this natively for throughput gains in multi-threaded environments. Tuning buffers is also critical—adjusting the OS-level net.core.rmem_max and net.core.wmem_max to 16 MB or higher minimizes packet overhead on high-speed networks, while enabling TCP_NODELAY prevents delays for small requests.

Hit Rate Improvement

Achieving high cache hit rates relies on strategic data management to minimize misses and backend fetches. Setting appropriate time-to-live (TTL) values during set or add operations ensures items expire before becoming stale, with expirations enforced using Unix timestamps with 1-second granularity based on the server's internal clock; items may expire at the start of the expiration second or immediately if set late in the second. Overly long TTLs waste memory on unused data, while short ones increase misses. Using multi-get operations (get key1 key2 ...) batches retrievals, reducing round-trip times and network calls, especially when grouping related keys (e.g., all user session data) on the same instance to optimize locality. Efficient serialization of objects before storage is key—avoiding large payloads (over 1 MB by default) by compressing or structuring data appropriately prevents slab mismatches and lowers eviction risks, as oversized items may fail allocation or consume disproportionate memory.

Profiling

Profiling Memcached involves using built-in stats and external tools to identify bottlenecks like high or . The stats command provides metrics, including ratios, counts (e.g., evicted_unfetched for unevicted items), and command ; tracking rates below 1% per second indicates healthy , while spikes signal memory pressure. For , memslap—a load generation tool from libmemcached—simulates workloads with configurable threads, concurrencies, and key distributions to measure throughput and under , such as running memslap -s [localhost](/page/Localhost) -t 10s -c 100 to test 100 concurrent clients. Regular helps correlate stats like cmd_get success rates with application performance.

Version-Specific Optimizations

Later versions of Memcached support features that yield measurable gains. The protocol, enabled with -B binary or client flags, uses compact fixed-width headers instead of ASCII parsing, resulting in significantly faster operations—up to 30% improvement in request processing under high loads compared to the text-based alternative. Enabling large pages with the -L flag leverages OS support (e.g., hugetlbfs on ) to allocate memory in fewer, larger chunks, reducing (TLB) misses and improving access speeds by 10-20% in memory-intensive scenarios. These options are available from Memcached 1.2.5 onward for binary and 1.4+ for large pages, but require configuration like vm.nr_hugepages. As of 2025, Memcached 1.6.39 includes further refinements to networking batching and support for newer storage backends, offering incremental performance gains in high-throughput scenarios.

Distributed Deployments

Memcached scales horizontally by deploying multiple independent instances, with distribution handled entirely on the client side rather than through server-to-server coordination. Clients maintain a list of available endpoints and use algorithms, such as the ketama method, to map cache keys to specific servers. This approach minimizes key remapping disruptions when servers are added or removed; for instance, libketama places multiple nodes per physical on a hash ring to balance load and reduce variance in . Client libraries, like those supporting ketama, automatically recompute the hash ring upon changes, ensuring seamless integration without requiring server modifications. Cluster management in Memcached relies on stateless that do not communicate with each other, placing the burden on clients to track the and handle . Clients periodically refresh their server list, often via files or mechanisms, and detect failures through connection timeouts or failed operations. When a node fails, clients rehash affected keys to remaining , which can lead to temporary misses and inconsistencies until the cache warms up from backend sources. This decentralized model supports high throughput but requires careful client to avoid hotspots or prolonged downtime during rebalancing. Replication in Memcached is not natively supported at the server level, so it must be implemented manually at the by writing data to multiple servers for redundancy. For example, applications can specify replication factors, directing sets to N servers via client libraries that fan out requests. like for Memcached provide automated replication options, particularly in serverless configurations, where data is automatically replicated across availability zones to enhance availability and without application changes. This contrasts with pure open-source deployments, where developers handle consistency trade-offs, such as during writes. Failover mechanisms operate , with libraries health via configurable timeouts—typically 1-5 seconds—to mark unresponsive servers as dead and route traffic elsewhere. Upon recovery or addition, clients reintegrate the server after verifying connectivity, often employing warm-up strategies like pre-populating the from persistent stores to mitigate "cold " effects and reduce backend load spikes. These strategies include during initial requests or batch repopulation scripts, ensuring quick restoration of hit rates post-failure. In large-scale environments, large-scale Memcached deployments can involve thousands of nodes across multiple clusters, as demonstrated by 's EVCache, which wraps Memcached and deploys over 22,000 instances across more than 200 clusters globally to handle petabytes of and billions of daily operations. deploys over 200 such clusters globally, using ketama-based sharding and application-level replication for . Similarly, tools like Facebook's mcrouter enhance in massive setups with thousands of servers, providing features such as pooling and shadowing to monitor and optimize traffic without altering core Memcached behavior.

References

  1. [1]
    memcached - a distributed memory object caching system
    Jul 28, 2025 · Memcached is an in-memory key-value store for small chunks of arbitrary data (strings, objects) from results of database calls, API calls, or page rendering.Docs · Downloads · About · Blog
  2. [2]
    Memcached Documentation
    Memcached is an in-memory key-value store for small arbitrary data (strings, objects) from results of database calls, API calls, or page rendering.Proxy API reference · Server Guide · Features · User Guide
  3. [3]
    a distributed memory object caching system - memcached
    memcached is a high-performance, distributed memory object caching system, generic in nature, but originally intended for use in speeding up dynamic web ...
  4. [4]
    Memcached | Distributed Key-Value Store - Amazon AWS
    The most important difference is Memcached is fully open source (BSD licensing) while Redis 7.2 is the last fully open source version for Redis. As of March ...
  5. [5]
    Scaling memcached at Facebook - Engineering at Meta
    Dec 12, 2008 · memcached is a high-performance, distributed memory object caching system. Here at Facebook, we're likely the world's largest user of memcached.
  6. [6]
    LiveJournal's Backend and memcached: Past, Present, and Future
    Blogging before blogging was a word, LiveJournal.com started off as a hobby project for Fitzpatrick and some friends and is now home to well over 4,000,000 ...Missing: 2003 | Show results with:2003
  7. [7]
    Gimme the cache! memcached turns 10 years old - Ars Technica
    May 21, 2013 · Brad Fitzpatrick wrote memcached for LiveJournal way back in 2003 (check out the initial CVS commit here). While waiting for new hardware to ...Missing: origins | Show results with:origins
  8. [8]
    rewritten binary branch - Google Groups
    So I finally sat down and figured out how to get branches properly rewritten. It was very tedious (involved lots of reading commit
  9. [9]
    Scaling memcache at Facebook - Engineering at Meta
    Apr 15, 2013 · Facebook started using memcached in August 2005 when Mark Zuckerberg downloaded it from the Internet and installed it on our Apache web ...Missing: adoption | Show results with:adoption
  10. [10]
    Memcached - Overview - Tutorials Point
    Memcached was developed by Brad Fitzpatrick for LiveJournal in 2003. However, it is now being used by Netlog, Facebook, Flickr, Wikipedia, Twitter, and YouTube ...
  11. [11]
    Memcached System Properties - DB-Engines
    Memcached System Properties ; Initial release, 2003 ; Current release, 1.6.29, June 2024 ; License info Commercial or Open Source, Open Source info BSD license.
  12. [12]
    [ANNOUNCE] Two new memcached releases - Mailing Lists
    Sep 9, 2006 · ... memcached-1.2.0.tar.gz What's new in 1.1.13: -- test suite (make test) -- better libevent detection -- 64 bit support (passes test suite at ...
  13. [13]
    ReleaseNotes132 · memcached/memcached Wiki - GitHub
    There is no longer a build-time distinction between a single-threaded and multi-threaded memcached. If you want a single-threaded memcached, ask for one thread ...
  14. [14]
    ReleaseNotes140
    ### Key Features in Memcached 1.4.0 (Slab Allocator Focus)
  15. [15]
    ReleaseNotes156 · memcached/memcached Wiki - GitHub
    Feb 27, 2018 · This is a bugfix release, but it primarily disables the UDP protocol by default. In the last few days reports of UDP amplification attacks utilizing inesure ...Missing: 2012 | Show results with:2012
  16. [16]
    ReleaseNotes160 · memcached/memcached Wiki - GitHub
    Mar 8, 2020 · 1.6.0 brings significant new features and fixes to memcached. The external flash storage (https://memcached.org/extstore) system is now ...Missing: history milestones
  17. [17]
    couchbase/memcached - GitHub
    Welcome to the Couchbase memcached project. This started as Couchbase's fork of the upstream memcached project. It has subsequently evolved since then.
  18. [18]
    Flash Storage - Memcached Documentation
    This feature is for extending memcached's memory space onto flash (or similar) storage. Requires memcached 1.6.0 or newer.
  19. [19]
    ConfiguringClient · memcached/memcached Wiki - GitHub
    Consistent Hashing describes methods for mapping keys to a list of servers, where adding or removing servers causes a very minimal shift in where keys map to.Missing: side | Show results with:side
  20. [20]
    Performance and Efficiency - Memcached Documentation
    How Memory Gets Allocated For Items. Memory assigned via the -m commandline ... $ ./memcached -vv slab class 1: chunk size 80 perslab 13107 slab class 2 ...
  21. [21]
    Basic Text Protocol - Memcached Documentation
    Basic Text Protocol. Memcached handles a small number of basic commands. Full documentation can be found in the Protocol Documentation.Missing: concepts | Show results with:concepts
  22. [22]
    Use Cases - Memcached Documentation
    One thing to keep in mind is memcached's 1 megabyte limit on item size, so storing the whole collection (ids, data) into memcached might not be the best idea.
  23. [23]
    LRU algorithm - Memcached
    In this post we delve into a reworking of memcached's Least Recently Used (LRU) algorithm which was made default when 1.5.0 was released.Missing: allocation | Show results with:allocation<|control11|><|separator|>
  24. [24]
    UserInternals · memcached/memcached Wiki - GitHub
    If your key + misc data + value is 50 bytes total, it will go into class 1, with an overhead loss of 30 bytes. If your data is 90 bytes total, it will go ...
  25. [25]
    Configuring - Memcached Documentation
    Since 1.5.6 memcached defaults to listening only on TCP. -l allows you to bind to specific interfaces or IP addresses. Memcached does not spend much, ...Commandline Arguments · Networking · Connection Limit
  26. [26]
    specifically - GitHub
    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 ...
  27. [27]
    Protocols - Memcached Documentation
    Protocols. Memcached supports a basic Text and Meta Text protocol. There is also a deprecated “binary” which no longer receives updates.Basic Text Protocol · Meta Text Protocol · Advisories · Binary SASL AuthMissing: specification | Show results with:specification
  28. [28]
    None
    ### Summary of Memcache Binary Protocol (protocol-binary.txt)
  29. [29]
    None
    ### Summary of Memcached's LRU Maintenance, Scans, Background Tasks, and Multi-Threaded Architecture
  30. [30]
    Maintenance - Memcached Documentation
    If memcached is started with -o track_sizes , some extra accounting is done internally to show a more detailed breakdown of the sizes of data stored within ...
  31. [31]
    memcached(1) - Linux man page
    Use <num> MB memory max to use for object storage; the default is 64 megabytes. Use <num> max simultaneous connections; the default is 1024.
  32. [32]
    couchbase/spymemcached - GitHub
    A simple, asynchronous, single-threaded memcached client written in java. code.google.com/p/spymemcached/. License. MIT license.
  33. [33]
    pylibmc · PyPI
    pylibmc is a Python client for memcached written in C. See the documentation at sendapatch.se/projects/pylibmc/ for more information.
  34. [34]
    Package :: memcached - PECL - PHP
    Available Releases. Version, State, Release Date, Downloads. 3.4.0, stable, 2025-10-13, memcached-3.4.0.tgz (91.1kB) DLL, [ Changelog ]. 3.3.0, stable, 2024-10- ...
  35. [35]
    memcachier/memjs: A memcache client for node using the ... - GitHub
    MemJS is a pure Node.js client library for using memcache, in particular, the MemCachier service. It uses the binary protocol and support SASL authentication.
  36. [36]
    User Guide · killme2008/xmemcached Wiki - GitHub
    Mar 18, 2019 · So Xmemcached support NIO connection pool, it can create multiple connections to one Memcached server. But you should know, they are not ...Table Of Contents · Aws Elasticcache Client · Advance Topic
  37. [37]
    Amazon ElastiCache for Microservices - DEV Community
    Feb 21, 2025 · ElastiCache for Memcached is a Memcached-compatible caching service that works seamlessly with popular tools that you use with existing ...<|separator|>
  38. [38]
    Configuring your ElastiCache client for efficient load balancing ...
    The ElastiCache Memcached Java client is based on the open-source spymemcached Java client, which has consistent hashing capabilities built in. The library ...
  39. [39]
    Server Guide - Memcached Documentation
    You should install memcached from a package provided by your operating system. The OS will solve dependencies for you and take care of security updates.
  40. [40]
    Downloads - Memcached
    Jul 28, 2025 · memcached - a distributed memory object caching system · Home · About · Downloads · Blog ...Missing: open BSD license 2003
  41. [41]
    Best Practices for Using Prometheus to Monitor Memcached
    Mar 1, 2024 · If you need to apply for memory, Memcached will divide a new page and allocate it to the required slab. Once a page is allocated, it will not be ...<|control11|><|separator|>
  42. [42]
    How To Install and Secure Memcached on Ubuntu 20.04
    Sep 27, 2021 · In this guide, you will learn how to install and configure a Memcached server. You'll also learn how to add authentication to secure Memcached.
  43. [43]
    Install and Secure Memcached on Debian 11 and Ubuntu 22.04
    Jun 3, 2024 · This guide walks through the installation steps for Memcached on Debian 11 and Ubuntu 22.04 LTS systems. Additionally, it goes over multiple solutions for ...
  44. [44]
    How to use sessions - Django documentation
    To store session data using Django's cache system, you'll first need to make sure you've configured your cache; see the cache documentation for details. Warning.
  45. [45]
  46. [46]
    On-demand scaling for Memcached clusters - Amazon ElastiCache
    You can define scaling policies through the AWS Application Auto Scaling service, and automatically adjust the number of nodes in Memcached clusters as needed, ...
  47. [47]
    Memorystore: in-memory Redis compatible data store | Google Cloud
    A fully managed in-memory service for Redis and Memcached that offers sub millisecond data access, scalability, and high availability.
  48. [48]
    Latency reduction of hybrid architectures with Amazon ElastiCache
    Mar 12, 2018 · Amazon ElastiCache is a fully managed, low-latency, in-memory data store that is compatible with Redis and Memcached. ElastiCache removes ...
  49. [49]
    [PDF] TAO: Facebook's Distributed Data Store for the Social Graph - USENIX
    Jun 26, 2013 · Facebook was originally built by storing the social graph in MySQL, querying it from PHP, and caching results in memcache [21]. This lookaside ...
  50. [50]
    The Infrastructure Behind Twitter: Scale - Blog - X
    Jan 19, 2017 · It is the primary cache for Tweet timelines and is backed by a customized version of Redis (implementing the HybridList). Haplo is read-only ...Missing: acceleration | Show results with:acceleration
  51. [51]
    SASLHowto · memcached/memcached Wiki - GitHub
    SASL (as described in RFC2222) is a standard for adding authentication mechanisms to protocols in a way that is protocol independent.
  52. [52]
    Binary SASL Auth - Memcached Documentation
    Overview. Most deployments of memcached today exist within trusted networks where clients may freely connect to any server and the servers don't discriminate ...<|control11|><|separator|>
  53. [53]
    SASL Memcached Now Available! - The Couchbase Blog
    Dec 16, 2014 · This post will attempt to answer this question, as well as explain the mechanics of memcached and SASL authentication for your applications.
  54. [54]
    Installation - Manual - PHP
    Information for installing this PECL extension may be found in the manual chapter titled Installation of PECL extensions.
  55. [55]
    How To Secure Memcached by Reducing Exposure - DigitalOcean
    Mar 1, 2018 · To secure Memcached, bind it to a local interface, disable UDP, and use firewalls to limit access to the private network interface.
  56. [56]
    Making Memcached highly-available - OpenStack Docs
    Jan 23, 2020 · Configuring Memcached through HAProxy¶. Setting haproxy in front of the Memcached servers and relying it in checking aliveness of the backends ...
  57. [57]
    Memcached Security Best Practices - Couchbase
    Dec 16, 2014 · Key Memcached security practices include firewalling, binding to localhost, using SASL, not running as root, and checking firewall settings.
  58. [58]
    stunnel TLS Proxy
    The stunnel program is designed to work as TLS encryption wrapper between remote clients and local (inetd-startable) or remote servers.
  59. [59]
    Memcached Unauthorized Access Vulnerability - Acunetix
    Memcached is designed for trusted environments, but public access is not recommended, and restricting access on production systems is advised.Missing: poisoning | Show results with:poisoning
  60. [60]
    Memcached Pentesting - Hackviser
    Cache Poisoning​. Inject malicious data into memcached cache to compromise application behavior. User Profile Poisoning​. # Poison cache with malicious dataMissing: fill | Show results with:fill
  61. [61]
    Memcached Injection - Beagle Security
    Feb 7, 2024 · 1. Denial of Service (DoS). Memcached injection can lead to a DoS condition where the Memcached server is overwhelmed with malicious requests.Missing: fill | Show results with:fill
  62. [62]
    What Is a Memcached DDoS Attack? - Akamai
    Memcached is widely used by companies like Facebook, Twitter, and YouTube. Memcached also has UDP support and is a main contributor to the attack vector.Missing: adoption | Show results with:adoption
  63. [63]
    February 28th DDoS Incident Report - The GitHub Blog
    Feb 28, 2018 · It was an amplification attack using the memcached-based approach described above that peaked at 1.35Tbps via 126.9 million packets per second.
  64. [64]
    CVE-2016-8704 Detail - NVD
    Jan 6, 2017 · An integer overflow in the process_bin_append_prepend function in Memcached, which is responsible for processing multiple commands of Memcached binary protocol ...
  65. [65]
    CVE-2016-8704 memcached - Red Hat Bugzilla
    ... Memcached binary protocol can be abused to cause heap overflow and lead to remote code execution. External References: http://www.talosintelligence.com ...
  66. [66]
  67. [67]
    UDP DDoS - Memcached Documentation
    A UDP DDoS attack on memcached uses spoofed requests, UDP's lack of handshake, and large responses to a victim's IP, causing overload.Missing: enhanced 2012
  68. [68]
    How to Use Fail2ban to Secure Your Linux Server - Tecmint
    Jun 19, 2024 · This tutorial will show you how to install fail2ban and setup basic configuration to protect your Linux system from brute-force attacks.
  69. [69]
    Security vulnerabilities addressed in ElastiCache
    You can find on this page a list of security vulnerabilities that have been addressed in ElastiCache. We recommend that you always upgrade to the latest ...Missing: environments | Show results with:environments
  70. [70]
    libketama: Consistent Hashing library for memcached clients
    Apr 10, 2007 · Ketama is an implementation of a consistent hashing algorithm, meaning you can add or remove servers from the memcached pool without causing a complete remap ...Missing: side sharding
  71. [71]
    [PDF] Performance at Scale with Amazon ElastiCache - Awsstatic
    We will focus on using Memcached as an in- memory cache pool, and using Redis for advanced datasets, such as game leaderboards and activity streams. ElastiCache ...
  72. [72]
    [PDF] Scaling Memcache at Facebook - USENIX
    Facebook uses memcached to scale a distributed key-value store, handling billions of requests per second, and improved the open-source version.Missing: adoption YouTube
  73. [73]
    [PDF] Dealing with Memcached Challenges - Couchbase
    Memcached clients have a list of memcached server node addresses (IP address and port) and use a consistent hashing algorithm (ketama) to determine which ...
  74. [74]
    Best practices for clients (Memcached) - Amazon ElastiCache
    Configure ElastiCache client for efficient load balancing · Configure preferred protocol for dual-stack Memcached · Configure internet access for a subnet in a ...Missing: production | Show results with:production
  75. [75]
    ElastiCache Serverless has a hidden feature: Memcached replication
    Dec 18, 2023 · ElastiCache Serverless for Memcached automatically replicates data across multiple AZs, a hidden feature, unlike standard ElastiCache Memcached.
  76. [76]
    Evolution of Application Data Caching : From RAM to SSD
    Jul 12, 2018 · In 2013, we introduced EVCache a distributed in-memory caching solution based on memcached that offers low-latency, high-reliability caching and storage.
  77. [77]
    Building a Global Caching System at Netflix: a Deep Dive to ... - InfoQ
    Oct 11, 2024 · Netflix's EVCache is deployed across four regions, comprising 200 Memcached clusters tailored to support various use cases. Each cluster is ...
  78. [78]
    Introducing mcrouter: A memcached protocol router for scaling ...
    Sep 15, 2014 · Mcrouter is a memcached protocol router that is used at Facebook to handle all traffic to, from, and between thousands of cache servers across dozens of ...