Memcached Documentation

repository·master·Indexed Apr 15, 2026

https://github.com/memcached/memcached

High-performance, distributed memory object caching system. Features include multithreaded event-based key/value storage, optional TLS encryption, SASL authentication, extstore, and a Lua scripting proxy. Includes utilities like damemtop for monitoring and memcached-automove for slab rebalancing.

Tokens
18.1K
Snippets
46
Records
65
Agent score
96%

What's inside memcached

  1. Run the noreply benchmark script

    master

    Use the devtools/bench_noreply.pl script to benchmark Memcached performance with and without the noreply extension enabled. This script measures the throughput of update commands (add, set, replace, append, prepend, incr, decr, delete) over a network connection.

    Usage:

    ./devtools/bench_noreply.pl HOST:PORT [COUNT]
    • HOST:PORT: The address of the Memcached instance to test (e.g., 127.0.0.1:11211).
    • COUNT: (Optional) The number of iterations to run. Defaults to 10,000.

    Notes:

    • It is recommended to run this test over the network wire rather than localhost to avoid CPU-bound results.
    • The script runs the noreply test first to ensure no reply packets remain in the network buffer before the standard test begins.
    #!/usr/bin/env perl
    # Usage: ./devtools/bench_noreply.pl HOST:PORT [COUNT]
    # Example: ./devtools/bench_noreply.pl 127.0.0.1:11211 10000

    Sources: devtools/bench_noreply.pl

  2. Build from Git

    master

    To build Memcached from a local Git repository, you must first install autotools and libevent. On Debian-based systems:

    sudo apt-get install autotools-dev automake libevent-dev

    Standard Build:

    cd memcached
    ./autogen.sh
    ./configure
    make
    make test

    The binary will be created in the current folder. You can run it directly:

    ./memcached

    Verify Installation: Telnet to the default port to ensure it is running:

    telnet 127.0.0.1 11211
    stats

    Building the Proxy: If building the proxy, an extra step is required to fetch vendor dependencies:

    cd memcached
    cd vendor
    ./fetch.sh
    cd ..
    ./autogen.sh
    ./configure --enable-proxy
    make
    make test

    Sources: README.md

  3. Build from Tarball

    master

    If you have downloaded the source as a tarball, use the standard compilation process:

    1. Configure the build:
       ./configure
    1. Compile:
       make
    1. (Optional) Run tests:
       make test
    1. Install:
       make install

    Optional Features:

    • Enable TLS: Install OpenSSL development packages and run:
      ./configure --enable-tls
    • Enable Proxy: Run:
      ./configure --enable-proxy

    Sources: README.md

  4. Install and Configure memcached-automove-extstore

    master

    The memcached-automove-extstore script automates the rebalancing of memory pages between slab classes in Memcached with extstore enabled. It connects to the Memcached binary via the text protocol to read statistics and issue commands, using a rolling window of historical data to make decisions and avoid flapping.

    Installation: Ensure Python 3 is available. The script is typically located in the scripts/ directory of the repository.

    Basic Usage: Run the script with the target host and port. By default, it connects to localhost:11211.

    ./scripts/memcached-automove-extstore --host <host>:<port>

    Command-Line Options:

    • --host HOST:PORT: Host and port to connect to (default: localhost:11211).
    • -s, --sleep SECONDS: Seconds between runs (default: 1).
    • -v, --verbose: Enable verbose output to see decision details.
    • -a, --automove: Enable automatic page rebalancing (dry-run mode is default).
    • -w, --window SIZE: Rolling window size for decision history (default: 30).
    • -r, --ratio RATIO: Ratio limiting distance between low/high class ages (default: 0.8).
    • -f, --free RATIO: Free chunks/pages buffer ratio (default: 0.005).
    • -z, --size SIZE: Item size cutoff for storage (default: 512).

    Example: Dry-run with verbose logging:

    ./scripts/memcached-automove-extstore --host 192.168.1.10:11211 -v

    Example: Automatic rebalancing:

    ./scripts/memcached-automove-extstore --host 192.168.1.10:11211 -a
    #!/usr/bin/python3
    import argparse
    import socket
    import sys
    
    parser = argparse.ArgumentParser(description="daemon for rebalancing slabs")
    parser.add_argument("--host", help="host to connect to",
            default="localhost:11211", metavar="HOST:PORT")
    parser.add_argument("-s", "--sleep", help="seconds between runs",
                        type=int, default="1")
    parser.add_argument("-v", "--verbose", action="store_true")
    parser.add_argument("-a", "--automove", action="store_true", default=False,
                        help="enable automatic page rebalancing")
    parser.add_argument("-w", "--window", type=int, default="30",
                        help="rolling window size for decision history")
    parser.add_argument("-r", "--ratio", type=float, default=0.8,
                        help="ratio limiting distance between low/high class ages")
    parser.add_argument("-f", "--free", type=float, default=0.005,
                        help="free chunks/pages buffer ratio")
    parser.add_argument("-z", "--size", type=int, default=512,
                        help="item size cutoff for storage")
    
    args = parser.parse_args()
    
    host, port = args.host.split(':')
    
    # ... (rest of script logic)

    Sources: scripts/memcached-automove-extstore

  5. Manage Memcached Instances with Init Script

    master

    The scripts/memcached-init script provides a standard SysV init interface for managing one or more memcached instances. It automatically discovers configuration files located in /etc/memcached_*.conf to support multiple instances (e.g., server1, server2).

    # Start all configured instances
    /etc/init.d/memcached start
    
    # Start a specific instance (e.g., server1)
    /etc/init.d/memcached start server1
    
    # Stop all instances
    /etc/init.d/memcached stop
    
    # Stop a specific instance
    /etc/init.d/memcached stop server1
    
    # Restart a specific instance
    /etc/init.d/memcached restart server1
    
    # Check status of a specific instance
    /etc/init.d/memcached status server1

    Sources: scripts/memcached-init

  6. Use memcached-tool for Stats and Management

    master

    The memcached-tool script is a Perl utility for inspecting and managing a running memcached instance. It connects via TCP or Unix socket and executes commands to display slabs, stats, settings, sizes, keys, or dump data.

    Usage:

    memcached-tool <host[:port] | /path/to/socket> [mode] [options]

    Modes:

    • display (default): Shows slab class details (size, pages, count, eviction status).
    • stats: Shows general server statistics.
    • settings: Shows current configuration settings.
    • sizes: Shows memory usage per slab class (development command only).
    • dump [limit]: Dumps keys and values. Optionally limits the number of keys.
    • keys [-u] [limit]: Dumps keys only. Use -u to unescape special characters.

    Examples:

    # Show slab classes (default)
    memcached-tool 10.0.0.5:11211
    
    # Show general stats
    memcached-tool 10.0.0.5:11211 stats
    
    # Show settings
    memcached-tool 10.0.0.5:11211 settings
    
    # Dump up to 100 keys and values
    memcached-tool 10.0.0.5:11211 dump 100
    
    # Dump keys only, unescaped, limit 50
    memcached-tool 10.0.0.5:11211 keys -u 50

    Warning: The sizes command is a development tool that can lock the memcached instance for several minutes if there are millions of items. Use with caution.

    # Connect via TCP
    memcached-tool 10.0.0.5:11211 stats
    
    # Connect via Unix socket
    memcached-tool /var/run/memcached/memcached.sock settings
    
    # Dump keys with limit
    memcached-tool localhost:11211 keys 100

    Sources: scripts/memcached-tool

  7. Manage Memcached Instances with Upstart

    master

    This project includes an Upstart init script (memcached-server.upstart) to manage memcached instances on systems using Upstart (e.g., older Ubuntu releases). The script supports starting multiple instances by specifying a SERVER environment variable, which corresponds to a configuration file at /etc/memcached_<SERVER>.conf.

    Key behaviors:

    • Default Instance: If SERVER is empty, it starts the default memcached instance using /etc/memcached.conf.
    • Named Instances: If SERVER is set (e.g., SERVER=cache1), it starts memcached_cache1 using /etc/memcached_cache1.conf.
    • Validation: The script checks for the existence of the config file before starting. If missing, it exits with an error.
    • Lifecycle: It expects the process to daemonize (expect daemon) and will automatically respawn if the process dies.
    • Triggers: The service stops when the runlevel changes to 0, 1, or 6, or when the stop-memcached-servers event is fired.

    Usage: To start a specific instance, set the SERVER environment variable before starting the job:

    # Start the default instance
    sudo start memcached-server
    
    # Start a named instance (e.g., 'cache1')
    sudo start memcached-server SERVER=cache1

    Ensure the corresponding configuration file exists at /etc/memcached_<SERVER>.conf (or /etc/memcached.conf for the default) before starting.

    Sources: scripts/memcached-server.upstart

  8. Configure Build Options and Feature Flags

    master

    The build system uses configure.ac to detect dependencies and configure feature flags. Run ./configure to detect system capabilities and set up the build environment.

    Key Configuration Options:

    • --with-libevent=PATH: Specify the path to a non-standard libevent installation if it is not found in standard locations.
    • --with-libssl=PATH: Specify the path to a non-standard OpenSSL installation (required if TLS is enabled).
    • --enable-tls: Enable TLS support at build time (requires OpenSSL).
    • --enable-static: Build a static binary (links against -ldl and sets -static flag).

    Dependency Requirements:

    • libevent: Required. The build checks for libevent 2.x. If not found, specify the path using --with-libevent.
    • OpenSSL: Required if --enable-tls is used. Must be version 1.1.0 or higher.
    • POSIX Threads: Required for threading support.

    Error Handling: If dependencies are missing, configure will output an error message indicating the missing library and the URL to download it from (e.g., https://www.monkey.org/~provos/libevent/ for libevent or https://www.openssl.org/ for OpenSSL).

    Sources: configure.ac

  9. Run Multiple Memcached Instances with Systemd

    master

    Memcached supports running multiple instances using systemd's templated service feature. Each instance is identified by a port number.

    To start an instance on a specific port (e.g., 11211):

    systemctl start memcached@11211

    To enable an instance at boot:

    systemctl enable memcached@11211

    Instance-specific configuration can be provided via /etc/sysconfig/memcached.<port>. This file is read before the global /etc/sysconfig/memcached file, allowing per-instance parameter overrides.

    Sources: scripts/memcached@.service

  10. Dependencies

    master

    To build Memcached, you must install the following dependencies:

    • libevent (Required): The core event library. On Debian-based systems, install via libevent-dev.
    • libseccomp (Optional, Linux only): Enables process restrictions for better security. Tested only on x86-64 architectures.
    • OpenSSL (Optional): Enables TLS support. Requires a relatively up-to-date version. pkg-config is needed to locate OpenSSL dependencies (such as -lz).

    Sources: README.md

  11. Configure Proxy Tunables via Lua

    master

    The mcp global table provides access to runtime configuration tunables for the proxy. These functions update the global proxy_ctx_t structure immediately and affect subsequent operations.

    Available configuration functions include:

    • Pool & Backend Management:

      • mcp.pool(name, config) - Configure a backend pool.
      • mcp.backend(name, config) - Configure a specific backend.
      • mcp.init_tls() - Initialize TLS for backends.
      • mcp.backend_use_tls(true/false) - Enable/disable TLS for backends.
      • mcp.backend_use_iothread(true/false) - Enable I/O threads for backends.
    • Timeouts & Limits:

      • mcp.backend_connect_timeout(ms) - Backend connection timeout.
      • mcp.backend_read_timeout(ms) - Backend read timeout.
      • mcp.backend_retry_timeout(ms) - Retry timeout.
      • mcp.backend_retry_waittime(ms) - Wait time between retries.
      • mcp.backend_failure_limit(count) - Failure limit before marking backend down.
      • mcp.backend_depth_limit(count) - Maximum queue depth for backends.
      • mcp.active_req_limit(count) - Maximum active requests.
      • mcp.buffer_memory_limit(bytes) - Memory limit for request buffers.
    • Flap Detection:

      • mcp.backend_flap_time(ms) - Time window for flap detection.
      • mcp.backend_flap_backoff_ramp(factor) - Backoff ramp factor.
      • mcp.backend_flap_backoff_max(ms) - Maximum backoff time.
    • Rate Limiting:

      • mcp.ratelim_global_tbf(name, rate, burst) - Global token bucket rate limiter.
    • GC & Stats:

      • mcp.luagc_ratio(ratio) - Set Lua VM garbage collection ratio.
      • mcp.stat_limit(count) - Limit the number of stats entries.
      • mcp.server_stats() - Retrieve server statistics.
    • Maintenance:

      • mcp.schedule_config_reload() - Trigger a configuration reload.
      • mcp.register_cron(pattern, func) - Register a cron job.
      • mcp.add_stat(name, value) - Add a custom stat.
      • mcp.tcp_keepalive(enable) - Enable TCP keepalive.

    Example:

    -- Set backend connection timeout to 5 seconds
    mcp.backend_connect_timeout(5000)
    
    -- Configure a pool named 'primary'
    mcp.pool('primary', {
        backends = {'127.0.0.1:11211'},
        hash = 'xxhash'
    })
    
    -- Enable TLS for backends
    mcp.backend_use_tls(true)

    Note: The mcp.log function is available in the config thread for logging during configuration.

    Sources: proxy_lua.c

  12. Environment and Safety Warnings

    master

    Be cautious when using the -k (mlockall) option with a large cache. This option can be dangerous if the cache size exceeds available physical memory.

    Key Constraints:

    • Ensure memcached machines do not swap. Swapping negates the performance benefits.
    • Memcached performs non-blocking network I/O but does not perform disk I/O. If data is being written to disk, the configuration is incorrect.

    Sources: README.md