Memcached Distributed Cache System

Memcached Distributed Cache System

Introduction to Memcached

Memcached is a free, open source, high performance, distributed memory object caching system.

Memcached is a software developed by Brad Fitzpatric, a Danga Interactive company owned by LiveJournal. It is now an important factor in improving the extensibility of Web applications in many services, including mixi, hatena, Facebook, Vox, LiveJournal.

Memcached is a memory-based key-value store that stores any piece of data (strings, objects) that can be the result of a database call, API call, or page rendering.

Memcached is simple and powerful. Its concise design facilitates rapid development, eases development difficulties, and solves many problems with large data caches. Its API is compatible with most popular development languages.

Essentially, it is a concise key-value storage system.

The general purpose is to reduce database access times by caching the results of database queries to improve the speed and scalability of dynamic Web applications.

Memcached website:

Differences between Memcached and Redis

As we all know, storing some hot data in a cache can greatly increase the speed, so the question is whether Redis is better or Memcached is better. Here are some simple differences and comparisons between them:

  1. Redis not only supports simple k/v types of data, but also supports the storage of rich data structures such as list, set, zset(sorted set), hash, which gives it a broader application scenario.
  2. Redis's biggest highlight is its support for data persistence, which allows data to be backed up on disk while it is running, and cached data can be loaded back into memory after power failure or restart, so long as Redis configuration is reasonable, data will not be lost.
  3. Redis supports the application of master-slave mode.
  4. Redis has a maximum limit of 1 GB for a single value, whereas Memcached can only hold data within 1 MB.
  5. In concurrent scenarios, Memcache can use cas to ensure consistency, whereas Redis transaction support is weak and can only guarantee the continuous execution of each operation in a transaction.
  6. In terms of performance, Redis is slightly ahead of Memcached in both read and write operations, according to tests provided by netizens.
  7. Memcached's memory management is less complex than Redis's, metadata is smaller, and relatively little extra overhead. The only data type Memcached supports is string, which is ideal for caching read-only data because strings do not require additional processing.

Two ways and differences of redis data persistence

RDB Persistence

  • RDB persistence is the process of saving a snapshot of the current data to the hard disk

RDB is a very compact file that holds redis datasets at a point in time. This file is ideal for backup and disaster recovery. RDB recovers large datasets faster than AOF recovers.

RDB-style data cannot be persisted in real time/seconds. Because bgsave performs a fork operation to create subprocesses every time it runs, it is a heavy operation (data in memory is cloned, roughly twice the expansion needs to be considered), and frequent execution costs are too high (performance is affected).RDB files are saved in a specific binary format, there are multiple formats of RDB versions in the evolution of Redis versions, problems with older versions of Redis services not compatible with newer versions of RDB formats (incompatible versions) and backups are made at regular intervals, so if redis are accidentally down loaded, all modifications made after the last snapshot are lost (data is lost)

AOF Persistence

  • AOF (append only file) persistence: Record each write command in a stand-alone log and re-execute the command in the AOF file on reboot to recover data.
  • AOF solves the real-time problem of data persistence and is now the mainstream method of Redis persistence.

The format of AOF files is readable, which also provides a more flexible way for users to handle them.

For Rediss with the same data, AOF files are usually larger than RDF files. Although AOF provides multiple synchronization frequencies, by default, the frequency of synchronization per second also has higher performance. However, when Redis is heavily loaded, RDB has better performance guarantees than AOF. RDB uses snapshots to persist the entire Redis data, while AOF does.Just append each command executed to the AOF file, so RDB is theoretically more robust than AOF. The official documentation also points out that there are some BUG s in AOF that do not exist in RDB.

Order in which persistent files are loaded at redis startup

  1. When AOF persistence is open and an AOF file exists, load the AOF file first
  2. Load RDB file when AOF is closed or AOF file does not exist
  3. Redis started successfully after loading the AOF/RDB file successfully.
  4. When there is an error in the AOF/RDB file, Redis fails to start and prints the error information.
  • redis recommends rdb


Daemons are special processes that run in the background to perform specific system tasks. Many daemons start when the system boots and run until the system shuts down. Others start only when needed and end automatically when the task is completed.

Will end with the end of the main process

Main Process Create Daemon

  1. The daemon terminates after the main process code executes
  2. The daemon cannot open any more subprocesses or throw an exception

memcached installation

# Download dependent packages
[root@server1 ~]# yum install libevent libevent-devel -y
# Download memcached
[root@server1 ~]# yum install memcached -y
  • Manage services using memcached commands, related parameters

    • -d is to start a daemon;
    • -m is the amount of memory allocated to Memcache in MB;
    • -u is the user running Memcache;
    • -l is the IP address of the server being listened on and can have multiple addresses;
    • -p is the port on which Memcache listens, preferably 1024 or more; (Port range: 0-65535)
    • -c is the maximum number of concurrent connections to run, defaulting to 1024;
    • -P is the pid file that sets up to save Memcache.
  • Start the memcached service

[root@server1 ~]# memcached -d -m 1024 -u memcached -l -p 11211 -c
1024 -P /tmp/
  • memcached connection
[root@server1 ~]# yum install telnet -y
[root@server1 ~]# telnet 11211
Connected to
Escape character is '^]'.
set name 0 0 8	 #Save Command
zhangsan 	#data
get name 	#Query Data
VALUE foo 0 8
quit Sign out
Connection closed by foreign host.
  • Shut down services
[root@server1 ~]# pkill memcached
[root@server1 ~]# ss -tuan | grep 11211

memcached memory storage mechanism

slab storage mechanism

1.Item data storage node

item data storage nodes are primarily used to store data

  • item source
typedef struct _stritem {    
	/* Protected by LRU locks */
    //The address of an item, mainly for LRU and freelist chains
    struct _stritem *next;
    //The address of the next item, mainly for LRU and freelist chains
    struct _stritem *prev;

    /* Rest are protected by an item lock */
    //Used to record the address of the next item node in the hash table slot
    struct _stritem *h_next;    /* hash chain next */
    //Recent Visit Time
    rel_time_t      time;       /* least recent access */
    //Cache expiration time
    rel_time_t      exptime;    /* expire time */
    int             nbytes;     /* size of data */
    //The number of times an item is currently referenced to determine if it is being operated on by another thread
    //The node can only be deleted if refcount == 1
    unsigned short  refcount;
    uint8_t         nsuffix;    /* length of flags-and-length string */
    uint8_t         it_flags;   /* ITEM_* above */
    //Record which slabclass_t the item node is located in
    uint8_t         slabs_clsid;/* which slab class we're in */
    uint8_t         nkey;       /* key length, w/terminating null and padding */
    /* this odd type prevents type-punning issues when we do
     * the little shuffle to save space when not using CAS. */
    union {
        uint64_t cas;
        char end;
    } data[];
    /* if it_flags & ITEM_CAS we have 8 bytes CAS */
    /* then null-terminated key */
    /* then " flags length\r\n" (no terminating null) */
    /* then data with terminating \r\n (no terminating null; it's binary!) */
} item;

slab and chunk

A slab is a block of memory space with a default size of 1M. memcached splits a slab into chunks, which are small chunks of memory used primarily to store item s

2.Initialization of slabclass

Each item may be different in size, and the item is stored in a chunk.If the chunk size is insufficient, it is not sufficient to allocate to the item, and if the chunk is too large, it is too wasteful of memory space. Therefore, memcached takes the approach of cutting the slab into chunks of different sizes to satisfy the storage of different sized items. The existing memcached of slabs divided into chunks of different sizes is represented by the structure slabclass

  • slabclass structure source
typedef struct {
    //chunk size
    unsigned int size;      /* sizes of items */
    //How many chunks is the 1M memory size split into
    unsigned int perslab;   /* how many items per slab */

    //Idle chunk list
    void *slots;           /* list of item ptrs */
    //Number of idle chunk s
    unsigned int sl_curr;   /* total free items in list */

    //Currently slabclass has allocated a slab with less than 1M space
    unsigned int slabs;     /* how many slabs were allocated for this class */

    //slab pointer array
    void **slab_list;       /* array of slab pointers */
    //Size of slab pointer array
    unsigned int list_size; /* size of prev array */

    size_t requested; /* The number of requested bytes */
} slabclass_t;

slab_class structure diagram

  1. When the slabclass array is initialized, each slabclass_t is allocated a 1M-sized slab, which is divided into N small blocks of memory, the size of which depends on the size of the size on the slabclass_t structure
  2. Each slabclass_t stores only a certain range of data, and the chunk block cut by the next slabclass is larger than the chunk block cut by the previous slabclass.
  3. The default size of slabclass array in memcached is 64, and the growth factor of slabclass cut block size is 1.25 by default
    For example, slabclass[1] cuts a chunk block size of 100 bytes and slabclass[2] 125. If you need to store a 110-byte cache, you need to get an idle node in the slabclass[2] free chain table to store it.

3.Item Node Assignment Process

  1. Find the right slabclass by size
  2. Is there an idle item node in the slabclass idle list, and if there is a direct item allocation to store content
  3. There is no idle item in the idle list to allocate. A slab (default size is 1M) memory block is reopened, the slab is cut, placed in the idle list, and the node is retrieved from the idle list

4.Release of item node

Releasing an item node does not free memory space, but returns the item node to the slabclass free list

memcached storage command

set command

set key flags exptime bytes [noreply]

The parameters are described as follows:

  • Key: The key in the key-value structure used to find cached values.
  • flags: can include integer parameters for key-value pairs, which clients use to store additional information about key-value pairs (which can be understood as serial numbers).
  • exptime: The length of time to save key-value pairs in the cache (in seconds, 0 means forever)
  • Bytes: the number of bytes stored in the cache
  • noreply (optional): This parameter tells the server that no data needs to be returned
  • Value: stored value (always on the second line) (directly understood as value in the key-value structure)


set name 0 900 8
get name
VALUE name 0 8

add command

The Memcached add command stores the value (data value) in a key (key) that does not exist. If the key of add already exists, the data will not be updated (expired keys will be updated), the previous values will remain the same, and you will get a response of NOT_STORED.

add key flags exptime bytes [noreply]

replace command

The Memcached replace command replaces the value (data value) of an existing key. If the key does not exist, the replacement fails and you get a response of NOT_STORED.

replace key flags exptime bytes [noreply]

append command

The Memcached append command appends data to the value of an existing key.

append key flags exptime bytes [noreply]


set key1 0 900 9
get key1
VALUE key1 0 9
append key1 0 900 5
get key1
VALUE key1 0 14

prepend command

The Memcached prepend command appends data to the value (data value) of an existing key.

cas command

The Memcached CAS (Check-And-Set or Compare-And-Swap) command is used to perform an "check and set" operation

It can only write values to the current client if the value corresponding to that key has not been modified by other clients since the last time it was taken.

The check is done with the cas_token parameter, which is a unique 64-bit value that Memcach assigns to elements that already exist.

cas key flags exptime bytes unique_cas_token [noreply]
# unique_cas_token: A unique 64-bit value obtained by the get command.


gets tp
VALUE tp 0 9 1

cas tp 0 900 5 1

get tp
VALUE tp 0 5

Output information description:

  • STORED: Output after successful save.
  • ERROR: Save errors or syntax errors.
  • EXISTS: Another user is updating the data after the last value.
  • NOT_FOUND: The key value does not exist on the Memcached service.

memcached find command

get command

get key
get key1 key2 key3

gets command

The Memcached gets command takes the value (data value) with CAS token memory and returns NULL if the key does not exist.

The basic syntax format of the get command is as follows:

gets key
gets key1 key2 key3

delete command

The Memcached delete command deletes an existing key.

delete key [noreply]

incr and decr commands

The Memcached incr and decr commands are used to increase or decrease the number value of an existing key.

The data for the incr and decr commands must be a decimal 32-bit unsigned integer.

Returns NOT_FOUND if the key does not exist, CLIENT_ERROR if the value of the key is not a number, and ERROR if other errors do.

The basic syntax format of the incr command is as follows:

incr key increment_value	# Self-increasing 
decr key decrement_value	# Self-decreasing
# increment_value: The value added.

memcached statistics command

stat command

The Memcached stats command returns statistics such as PID (process number), version number, number of connections, and so on.

STAT pid 1162
STAT uptime 5022
STAT time 1415208270
STAT version 1.4.14
STAT libevent 2.0.19-stable
STAT pointer_size 64
STAT rusage_user 0.096006
STAT rusage_system 0.152009
STAT curr_connections 5
STAT total_connections 6
STAT connection_structures 6
STAT reserved_fds 20
STAT cmd_get 6
STAT cmd_set 4
STAT cmd_flush 0
STAT cmd_touch 0
STAT get_hits 4
STAT get_misses 2
STAT delete_misses 1
STAT delete_hits 1
STAT incr_misses 2
STAT incr_hits 1
STAT decr_misses 0
STAT decr_hits 1
STAT cas_misses 0
STAT cas_hits 0
STAT cas_badval 0
STAT touch_hits 0
STAT touch_misses 0
STAT auth_cmds 0
STAT auth_errors 0
STAT bytes_read 262
STAT bytes_written 313
STAT limit_maxbytes 67108864
STAT accepting_conns 1
STAT listen_disabled_num 0
STAT threads 4
STAT conn_yields 0
STAT hash_power_level 16
STAT hash_bytes 524288
STAT hash_is_expanding 0
STAT expired_unfetched 1
STAT evicted_unfetched 0
STAT bytes 142
STAT curr_items 2
STAT total_items 6
STAT evictions 0
STAT reclaimed 1

A lot of status information is shown here, and each status item is explained in detail below:

  • pid:memcache server process ID
  • uptime: number of seconds the server has been running
  • time: Server current Unix timestamp
  • Version:memcache version
  • pointer_size: Operating system pointer size
  • rusage_user: Cumulative user time for the process
  • rusage_system: Process cumulative system time
  • curr_connections: Current number of connections
  • total_connections: Total number of connections since Memcached was run
  • connection_structures: Number of connection structures allocated by Memcached
  • Number of cmd_get:get command requests
  • cmd_set:set Command Request Number
  • Number of cmd_flush:flush command requests
  • get_hits:get command hit count
  • get_misses:get command misses
  • delete_misses:Number of misses for delete command
  • delete_hits:number of delete command hits
  • incr_misses:incr Command Misses
  • incr_hits:incr Command Hits
  • decr_misses:decr command misses
  • decr_hits:decr Command Hits
  • cas_misses:number of cas command misses
  • cas_hits:cas Command Hits
  • cas_badval: Number of wipes used
  • auth_cmds: Number of times authentication commands are processed
  • auth_errors: Number of authentication failures
  • bytes_read: Total number of bytes read
  • bytes_written: Total number of bytes sent
  • limit_maxbytes: Total allocated memory size (bytes)
  • accepting_conns: Has the server reached its maximum connection (0/1)
  • listen_disabled_num: Number of failed listeners
  • Threads: Current number of threads
  • conn_yields: Number of active discards for connection operations
  • Bytes: the number of bytes currently occupied by storage
  • curr_items: Total number of data currently stored
  • total_items: Total number of data stored since startup
  • evictions:Number of objects released by LRU
  • reclaimed: The number of data entries that have expired to store new data

stats items

The Memcached stats items command displays the number of items in each slab and the length of storage time (the last access is now seconds away).


stats items
STAT items:1:number 1
STAT items:1:age 7
STAT items:1:evicted 0
STAT items:1:evicted_nonzero 0
STAT items:1:evicted_time 0
STAT items:1:outofmemory 0
STAT items:1:tailrepairs 0
STAT items:1:reclaimed 0
STAT items:1:expired_unfetched 0
STAT items:1:evicted_unfetched 0

stats slab

The Memcached stats slabs command displays information about each slab, including the size, number, usage, and so on.


stats slabs
STAT 1:chunk_size 96
STAT 1:chunks_per_page 10922
STAT 1:total_pages 1
STAT 1:total_chunks 10922
STAT 1:used_chunks 1
STAT 1:free_chunks 10921
STAT 1:free_chunks_end 0
STAT 1:mem_requested 71
STAT 1:get_hits 0
STAT 1:cmd_set 1
STAT 1:delete_hits 0
STAT 1:incr_hits 0
STAT 1:decr_hits 0
STAT 1:cas_hits 0
STAT 1:cas_badval 0
STAT 1:touch_hits 0
STAT active_slabs 1
STAT total_malloced 1048512

stats sizes

The Memcached stats sizes command displays the size and number of all item s.

This information returns two columns, the first being the size of the item and the second the number of items.


stats sizes
STAT 96 1

flush_all command

The Memcached flush_all command cleans up all key=>value (key=>value) pairs in the cache.

This command provides an optional parameter, time, to perform a cache cleanup operation after the specified time.


flush_all [time] [noreply]


set runoob 0 900 9
get runoob
VALUE runoob 0 9
get runoob

Tags: Database Cache memcached

Posted on Wed, 08 Sep 2021 19:07:17 -0400 by Gregadeath