In-depth study of memcache characteristics and limitations

Source: Internet
Author: User

In-depth study of Memcache

Features and limitations
There is no limit to the amount of item data that can be saved in memcached, as long as the memory is sufficient.
memcached single process maximum use of memory 2G, to use more memory, can be divided into multiple ports to open more than one memcached process maximum 30 days of data expiration, set to permanent will also expire at this time, constant Realtime_maxdelta 60*60*24* 30 control maximum key length of 250 bytes, greater than the length can not be stored, constant key_max_length 250 control single item maximum data is 1MB, more than 1MB data is not stored, constant Power_block 1048576 control, it is the default slab size The maximum number of simultaneous connections is 200, controlled by the freetotal in Conn_init (), the maximum number of soft connections is 1024, and the parameters related to space occupancy are controlled by settings.maxconns=1024: settings.factor= 1.25, settings.chunk_size=48, the impact of slab data consumption and stepping mode memcached is a non-blocking socket communication mode service, based on the Libevent library, because of non-blocking communication, memory read and write speed is very fast.
Memcached sub-server and client, can be configured for multiple server-side and client, applied to distributed services is very extensive.
Memcached as a small-scale data distributed platform is very effective.
Memcached is the key value one by one corresponding, key default maximum can not exceed 128 bytes, value default size is 1M, that is, a slabs, if you want to save 2M value (continuous), can not use two slabs, because two slabs is not continuous, can not be stored in memory, It is necessary to modify the size of the slabs, when multiple keys and value are stored, even if the slabs is not exhausted, no other data will be stored.


#/usr/local/bin/memcached-d-M 10-u root-l 192.168.0.200-p 12000-c 256-p/tmp/memcached.pid

The-D option is to start a daemon,
-M is the amount of memory allocated to Memcache, in megabytes, I'm 10MB,
-U is the user running memcache, I am root here,
-L is the server IP address of the listener, if there are multiple addresses, I specify the server IP address 192.168.0.200,
-P is the port that sets Memcache listening, I set here 12000, preferably more than 1024 ports,
The-c option is the maximum number of concurrent connections to run, the default is 1024, I set the 256 here, according to the load of your server to set,
-P is set to save memcache PID file, I am here to save in/tmp/memcached.pid,

2) If you want to end the memcache process, execute:

# Kill ' Cat/tmp/memcached.pid '


Memcache uses the memory allocation mechanism of the slab allocator: divides allocated memory into blocks of a specific length in a predetermined size to completely resolve memory fragmentation issues. Memcache storage involves slab,page,chunk three concepts
1. Chunk is a fixed-size memory space, which defaults to 96Byte.
2. The page corresponds to the actual physical space, with 1 page 1M.
3. The same size of chunk is also called slab.
Memcached the start time, a number of slab are generated according to the-N and-f parameters. The application of Memcache each application 1page, and this 1M space divided into several chunk, these chunk have the same size, belong to the same slab.

Add

When adding item via Memcache:
1. Memcache calculate the size of the item (key+value+flags), select the appropriate slab (just to drop the item's slab)
2. If the item corresponds to a slab that does not appear, then apply 1 page (note that this 1M space whether or not the memcached use memory can be applied successfully) and add the item to slab in the chunk
3. If the item corresponding to the slab appears, then in the Slab preference expired (free_ chunks) and delete (the chunk of the delete in 1.2.2 have problems that cannot be reused) are stored chunk, followed by the selection of unused chunk (free_chunks_end) for storage.
4. If the item corresponding to the slab appears, but the corresponding slab has been stored full, then will apply for a new page, the page is divided into the corresponding size of chunk, continue to store.
5. If the item corresponding to the slab appears, but the corresponding slab has been stored full and memcache also reached the maximum memory usage. The LRU algorithm will be used to clear the item (May erase the item that has not expired) and there will be eviction++

"Delete":
1. The delete operation simply resets the chunk to the delete state, so that the next use will take precedence over such chunk.
"Flush"
1. The flush operation is equivalent to an action that invalidates all the item. Does not change the memcache memory allocation situation.
"Some attention."
1. Memcache already allocated memory will no longer be actively cleaned up.
2. Memcache the memory page assigned to a slab can no longer be assigned to another slab.
3. Flush_all cannot reset the layout of memcache allocated memory pages, just to expire all of the item.
4. Memcache Maximum stored item (key+value) size limit is 1M, which is limited by page size 1M
5. Because the memcache distributed is the client program through the hash algorithm to get the key to achieve, different languages may be different hash algorithm, the same client program may also use dissimilar methods, so in the multi-language, multi-module sharing the same set of memcached services, It is important to note that the same hash algorithm is selected on the client
6. You can disable LRU substitution with the-m parameter when starting memcached, and add and set will return failure when memory runs out
7. Memcached starts by specifying the amount of data storage, does not include the memory that it occupies, and the administrative space that is set up to save the data. As a result, it consumes more memory than the memory allocation specified at startup, which requires attention.
8. Memcache storage is limited to the length of the key, PHP and C maximum length is 250

In-depth study of memcache characteristics and limitations

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.