Memcache Cache System Knowledge Point Comb _ server Other

Source: Internet
Author: User
Tags memcached port number
Memcached Concept:
Memcached is a free and open source, high-performance, distributed object caching system, which can be used to save a number of frequently accessed objects or data, the saved data like a huge hash table, the table in the way of key-value in memory.
Website Download Address: http://www.memcached.org/
Operating environment: Linux,bsd,windows can run
Protocol theory: Http://code.sixapart.com/svn/memcached/trunk/server/doc/protocol.txt (English)

Operation Process:


1, check the client's request data is in the memcached, if there is, directly to the request data returned, no longer to do any operation of the database, path operation for ①②③⑦.

2, if the requested data is not in the memcached, to check the database, the data obtained from the database back to the client, while the data cache to the memcached (memcached client is not responsible, the need for a clear implementation of the program), the path operation for ①②④⑤⑦⑥.
3, each update the database at the same time update the data in memcached, to ensure consistency.

4. When allocated to memcached memory space, the LRU (least recently Used, least recently used) policy plus expiration policy is used, and the expiration data is replaced first and then replaced with the most recent unused data.

Memcached Features:

Simple protocol
It is a text line based protocol that allows access to data operations directly via Telnet on the memcached server
Based on Libevent event processing
Libevent is a set of libraries developed using C, which encapsulates event handling functions such as Epoll of the kqueue,linux system of BSD systems into an interface that improves performance compared to traditional select.
The built-in memory management method
All data are stored in memory, access data than the hard disk, when the memory is full, through the LRU algorithm automatically delete unused cache, but did not consider the data disaster tolerance, restart services, all data will be lost.
Distributed
Each memcached server does not communicate with each other, independently accesses data and does not share any information. The server does not have distributed functionality, and distributed deployment depends on the Memcache client.

Memcached installation and start-up:
Installation memcached need to be installed first libevent

Copy Code code as follows:

Shell>tar ZXVF libevent-1.4.14b-stable.tar.gz
SHELL>CD libevent-1.4.14b-stable
Shell>./configure
Shell>make && make Install

Install memcached
Copy Code code as follows:

Shell>tar ZXVF Memcached-1.2.5.tar.tar
SHELL>CD memcached-1.2.5
Shell>./configure–prefix=/usr/local/memcached
Shell>make && make Install

Start memcached
Shell>/usr/local/memcached/bin/memcached–p 11211–d–u Root–p/tmp/memcached.pid

Copy Code code as follows:

-P is used for TCP, the default port is 11211
-D indicates that a daemon is started in the background (daemon)
-U indicates that the root user is specified to start and the default cannot be started with root
-P represents the PID storage location for the process where "P" is the uppercase "P"
-L, followed by IP address, manually specify the listening IP address, the default all IP is listening
-M followed by the allocated memory size, in megabytes, by default of 64M
-C Maximum Run concurrent connection number, default is 1024
-F Block size growth factor, default is 1.25
-I return an error when I run out of memory, not delete the item, that is, no LRU algorithm


In a 64-bit system, the Libevent-1.4.so.2 file cannot be found by linking the file with the same name in a 32-bit directory to a 64-bit directory, which creates a shortcut like Windows.
Shell >/usr/local/lib/libevent-1.4.so.2/usr/lib64/libevent-1.4.so.2
If you find that there is no port on the monitor after startup, because the "P" with the PID parameter is the capital "P" of the command, you may write in lowercase.

Command line Direct Action command

save, there are six command items.
Copy Code code as follows:

Set: Add a new entry to the memcached or replace the existing entry with the new data replacement
ADD: When key does not exist, it memcached the data, otherwise, return not_stored response
Replace: When key exists, it will deposit data to memcached, otherwise return not_stored response
Cas: Change an existing key value, but it also has checked functionality
Append: Inserts a new value after this value
Prepend: Inserts a new value before this value


take, there are two command items:

Copy Code code as follows:

Get: Take a single value, return data from the cache, the first line will be the name of the key, flag value and the return value length, the real data in the second row, the last return end, such as key does not exist, the first line directly back to end
Get_multi: Take multiple values at once


Delete, a command:
Copy Code code as follows:
Delete


Instance action:
Connect to Memcache First
Copy Code code as follows:
Shell>telnet 127.0.0.1 11211



in the Enter data

Copy Code code as follows:
Set Liuguohua 0 0 21
369369.blog.51cto.com


The system automatically saves after carriage return and returns the normal value stored
In the command format
<command> <key> <flags> <exptime> <bytes> \ r \ n
Note that after you set the bytes byte, the length of the stored value must match, otherwise you cannot save success, like the Liuguohua field is 21 bytes, so the 369369.blog.51cto.com length is 21 (totaling 21 digits or letters).


Fetch Data
Copy Code code as follows:
Get Liuguohua

After carriage return, the first line returns value Liuguohua 0 21
Second line, return true value 369369.blog.51cto.com



memcached Memory algorithm:

Memcached uses the slab allocation mechanism to allocate and manage memory, which, according to the predetermined size, divides the allocated memory into a specific length of memory block, then divides the memory blocks of the same size into groups, and when the data is stored, it matches the slab size according to the key value size, Find the nearest slab storage, so there is a waste of space phenomenon.

The traditional way of memory management is to reclaim memory by free after using the memory allocated via malloc, which is easy to generate memory fragmentation and reduce the operating system's efficiency in memory management.



memcached Caching Policy:

The memcached cache policy is LRU (least recently used) plus expiration failure policy. When you store data items in a memcached, you may specify the expiration time of the cache, which defaults to permanent. When the memcached server has run out of allocations, the invalidated data is replaced first, and then the most recently unused data. In LRU, Memcached uses a lazy expiration policy that does not monitor the expiration of the saved Key/vlue, but instead examines the record timestamp when the key value is obtained, and checks that the key/value expires on the space, which reduces the load on the server.

memcached distributed algorithm:
When depositing/removing Key/value to the memcached cluster, the memcached client program calculates which server is stored according to a certain algorithm and then saves the Key/value value to this server. In other words, access to data two steps, the first step, select the server, the second access to data.



distributed algorithm (consistent hashing):

There are two ways to select a server, one is to compute the distribution based on the remainder, the other is to compute the distribution according to the hashing algorithm.
remainder algorithm:
By first obtaining the integer hash value of the key, divided by the number of servers and determining the access server based on the remainder, this method is computationally simple and efficient, but almost all caches will fail when the memcached server increases or decreases.
Hash Algorithm:
The hash value of the memcached server is first calculated and distributed to the 0 to 2 32-square circle. The same method is used to calculate the hash value of the key that stores the data and map it to the circle, and then start looking clockwise from where the data maps to, and save the data to the first server found. If the server is still not found with more than 2 32, the data is saved to the first memcached server. If you add a memcached server, the keys on the first server that only increase the server's counter-clockwise direction on the circle are affected.



memcache Management and performance monitoring:

Direct management and monitoring via command line can also be monitored via web software such as Nagios,cacti
Command line:
Copy Code code as follows:

Shell>telnet 127.0.0.1 1211///If IP and port number are specified at startup, make a corresponding change here
Command after successful connection
Stats: A variety of information on statistical memcached
Stats Reset: Re-statistical data
Stats Slabs, display slabs information, you can see in detail the segmented storage of data
Stats items: Displays the item count in slab
Stats cachedump 1 0: List key values stored in the first paragraph of slabs
Set|get: saving or fetching data
STAT Evictions 0: Number of legitimate item to make room for new item

Other common software uses:
Copy Code code as follows:

Shell>./memcached-tool 127.0.0.1:11211
Shell>./memcached-tool 127.0.0.1:11211 Display

Web software:
Copy Code code as follows:

memcache.php
Nagios Plugin
Cacti module


Memcached and Redis comparison:



This article is from the "System Network Operation Dimension" blog, please be sure to keep this source http://369369.blog.51cto.com/319630/833234

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.