Architect's Diary-Introduction and Working Principle of Memcached

What is Memcached

Memcached is an open source, high-performance, distributed memory object caching system

What can Memcached do

The main function is to cache data in memory to reduce database load.
It reduces the number of database reads by caching data and objects in memory, thereby increasing the speed of dynamic, database-driven websites.

Memcached features

1. Store in memory as key/value pairs, with good performance
2. Simple protocol (based on text lines), powerful
3. Libevent-based event processing, non-blocking communication, very fast reading and writing to memory
4. Client-based Distributed, multiple Memcached on the server side do not communicate with each other
5. The server side runs as a daemon process, and the client side can be written in any language

installation

Centos

sudo yum install memcached

Ubuntu

sudo apt-get isntall memcached

start up

memcached -d -m 10 -u root -l 192.168.1.106 -p 9922 -c 256 -P /tmp/memcached.pid

Parsing

  • -d option is to start a daemon
  • -m is the amount of memory allocated to Memcache, the unit is MB, here is 10MB
  • -u is the user running Memcache, here is root
  • -l is the IP address of the listening server, here is the IP address of the server 127.0.0.1
  • -p is the listening port, 9922 is set here, preferably a port above 1024
  • The -c option is the maximum number of concurrent connections running, the default is 1024, here 256 is set
  • -P is the pid file that is set to save Memcache. Here are the
    commonly used ones saved in /tmp/memcached.pid . There are a few more to know:
  • -f block size growth factor, the default is 1.25
  • -n minimum allocation space, key+value+flags default is 48byte
  • -I the size of each slab page
  • -v/-vv Detailed display of various parameters at work

shut down

First enter the following command to find the pid

ps aux|grep memcached

Then kill

kill -9 上面找到的pid

Basic working principle of Memcached

Memcached runs on one or more servers as a wait program, waiting for the client's link at any time. By starting the memcached server, configuring the corresponding monitoring IP, port memory size and other parameters, the client can use the specified server IP to change Data is stored in key-value format

Memcached's two-stage hash

When the client accesses data, it first calculates the hash value of the key (stage one hash) by referring to the node list, and then selects a node; the client sends the request to the selected node, and then the Memcached node uses an internal hash algorithm (Phase 2 hash), for real data (item) access

Memcached communication protocol

Memcached's server-client communication does not use complex XML and other formats, but uses a simple text line-based protocol. Therefore, data can be saved and retrieved on memcached through telnet

Guess you like

Origin blog.csdn.net/qq_32198277/article/details/77699549