Memcached practice foundation

Time:2019-12-15

I. memcached practice foundation

1. Introduction to memcached

Free and open source, high performance, distributed memory object caching system

Effect:
Data cache
Storage session
SMS verification code storage

install
Memcached is based on libevent, so libevent needs to be installed before memcached is installed

2. Memcached common commands

Set command
Store the value (data value) in the specified key (key). If the specified key already exists, update the value corresponding to the key
Syntax:
set key flags expire bytes
Parameters:
The key in the key value structure, used to find the cache
Flags can include integer parameters of key value pairs, which are used by clients to store additional information about key value pairs
Expire the length of time to save a key value pair in the cache in seconds 0 is permanent
Bytes the length of bytes stored in the cache
Value stored value
Output information
Stored indicates successful storage
Error error saving information error

Get command
Get the value stored in the key
grammar
get key1 key2 key3

Gets command
Get the value with CAS token store (data value)
If the key does not exist, return null
grammar
gets key1 key2 key3
In the output using the gets command, the number in the last column represents the CAS token of the current key

CAS command
Check and set. This command can write the value to the current client only when the value corresponding to the key has not been modified by other clients after the last value of the current client
grammar
cas key flags expire bytes unique_cas_token
The key in the key value structure, used to find the cache value
Flags can include integer parameters for key value pairs, which clients use to store additional information about key values
Expire the length of time to save a key value pair in the cache in seconds 0 is permanent
Bytes the number of bytes stored in the cache
Unique CAS token a unique 64 bit value obtained through the gets command
Value stored value

Delete command
Delete existing key
delete key
Syntax:
delete key
Parameter description
Key value pair: the key in the key value structure, used to find the cache value
Output value description
Deleted successfully
Error syntax error or deletion failure
Not_found key does not exist

Prepend / append command
Append before
prepend key flags expire bytes

Append after
append key flage expire bytes
Parameter description
The key in the key value structure, used to find the cache value
Flags can include integer parameters of key value pairs
Expires the length of time to save the key value pair in the cache in seconds 0 means it is permanently valid
Bytes length in cache
Value stored value
Output information
Stored success
Not stored storage failed
Client error

Incr / decr command
Add or subtract the number value of the existing key
The data of incr and decr command operation must be decimal 32-bit unsigned integer
If the key does not exist, return not found. If the value of the key is not a number, return client error. Other errors return error
grammar
incr key increment_value
decr key decrement_value
Parameter description
The key in the key value structure, used to find the cache value
Increment_value: the increased value
Increment “value: the value of decrement

Add command

Store value in the specified key
If the key of add already exists, the data will not be updated, the previous values remain the same, and not stored will be obtained
grammar
add key flags expire bytes
Parameter description
The key in the key value structure is used to find the cache value
Flags can include integer parameters of key value pairs
Expire the length of time to save the key value pair in the cache in seconds 0 means permanent
Bytes the length of bytes stored in the cache

3. Memcached extended installation and storage session

Install libmemcached = > install memcached = > install PHP memcached extension
Cookies and sessions

1、http request =》 
2、http reponse+set cookie (sessionId) =》 
3、http request+cookie =>
4、http response

Disadvantages of file storing session:

1. Slow reading and writing of file IO
2. Restricted distribution

Storage session mode

  • Modify php.ini
  • Set file in code
4. Use of memcached class

File cache redis cache memcahce cache

  1. Linked server
Reset server address
$cache->resetServerList();
$cache->addServers(
array(127.0.0.1,11211,60),
array(127.0.0.1,11212,40),
)

The third parameter corresponds to the weight in the server to control the probability of the server being selected

  1. Set get change data
$cache->setMulti(
array(
key1=>value1,
key2=>value2,
key3=>value3
),time()+900
);
$cache->get(key1);
$cache->getMulti(key1,key2,...);
//CAS data uniqueness multi server concurrent scenario
$cache->get('k1',null,$cas);
  1. common method
Grab seats and buy tickets
$cache->add(key,value,time()+900);
$cache->increment(key,incr);
$cache->decrement(key,decr);

The data operated by incr and decr commands must be a 32-bit unsigned integer in decimal system, and no negative value will appear in the result

II. Memcached usage scenarios

  1. Session storage
  2. Relieve database pressure and improve interaction speed
  3. Database master slave synchronous relay

Matters needing attention

value < 1M key < 512
Cache practice < 30 days
MySQL data synchronization update

III. introduction to memcached

advantage

  • A memory database is a database that operates directly by putting data in memory
  • Solve the problem of data efficiency and reduce IO consumption
  • It can be divided into relational memory database and non relational memory database

Features

  • Simple protocol, using simple text line based protocol
  • Flexible adjustment of server connections based on libevent event processing
  • Memory storage, fast memory reading speed
  • Distributed without mutual communication, each server only manages its own data
  • Lack of certification and security controls

Using memcached to implement distributed algorithm

  • Servers do not communicate with each other
  • Because the program is distributed
  • Consider algorithm to disperse storage pressure
  • Consider the hit rate of the algorithm

The residual calculation and dispersion method of distributed algorithm
The CRC is calculated according to the key, and then the memcached server node is obtained by modulus of the number of servers. When the server is unable to link, add the number of attempted links to the key for recalculation
Disadvantages: when adding or removing servers, almost all caches will be rebuilt, and avalanche crash is also considered

Consistency hash algorithm of distributed algorithm

  • Find out that the hash value of the server node is allocated to the circle of 0-2 ^ 32
  • Find out the hash value of the stored data key and map it to the circle
  • Start the clockwise search from the location to which the data is mapped, and save the data to the first server found

Advantages: redundant less load balancing, over smooth storage balancing

Using memcached to realize session sharing

Session is stored in public memcached to realize multi server sharing
Disadvantages: the cluster error will cause the user unable to log in, and the recovery mechanism may cause the user to drop the line