Memcached (1)-----Installation and Basic introduction

Source: Internet
Author: User
Tags cas delete key memcached

Memcachedmemcached Overview

Memcached is a software developed by LiveJournal Danga Interactive Company. Memcached is a high performance , distributed Memory object caching system with versatility,

The goal is to speed up the Dynamic Web program and reduce the burden on the database.

    mem->memory memory + Cachedcache = memcached

memcached Installation Detailed

1. Installing Libevent

Enter Libevent official website http://libevent.org/

    

Now the latest version of Libevent is 2.0.20

W     Egt https://github.com/libevent/libevent/releases/download/release-2.0.22-stable/libevent-2.0.22-stable.tar.gz

    TAR-ZXVF libevent-2.0.22-stable.tar.gz

    CD libevent-2.0.22-stable

    ./configure    


2. Installing memcached

Enter Memcached official website http://www.memcached.org/

    

Can see now memcached the latest stable version is 1.4.29

    Wegt http://www.memcached.org/files/memcached-1.4.29.tar.gz

TAR-ZXVF memcached-1.4.29.tar.gz

    CD libevent-2.0.22-stable

    ./configure

memcached Open Service

Enter memcached-h to view Help

    

The main parameters

-P <num> Listening TCP port (default: 11211)
-D Run as daemon memcached
-U <username> run memcached account, non-root user
-M <num> maximum memory usage, in MB, default of MB
C <num> Soft connection number, default is 1024
-V Output warnings and error messages
-VV request and return information for the print client
-H Printing Help information
-I print copyright information for memcached and libevent

Use the command to turn on the memcached service (memory size 64M, port use 11211)

    Memcached-d-M 32-p 11211

    

memcached Connection

Use the command telnet 127.0.0.1 11211 where the second parameter is an IP address, and the third parameter is the port number

memcached Command

1.add add k-v cache data, the cache cannot exist.

Add key flag expire length

Key: Value

Flag: Flags are initially 16-bit unsigned integers, and the current version is typically 32-bit. The user Client stores the custom tag data.

Expire: The validity period, in seconds, 0 means no expiration (relative does not expire, if the storage is full, it may be deleted), if set to one minute expires, the parameter is set to 60.

Another is to set the expired Unix time ( The number of seconds to calculatefrom 1970-1-1).

Length: How long to cache data

    Add Name 0 0 5

Lihua

STORED

    Where stored indicates a successful storage and some response commands

"STORED": Indicates the storage was successful.

"not_stored": Indicates no storage, but is not an error. For example, use Add for an existing key.

"EXISTS": Indicates that the data was not successfully set using the CAS command, and the data has been modified by others since the last time you obtained the data.

"not_found": Indicates that key is not stored when using CAs to store data.

2.get using key to take value

Get key

    Get Name

VALUE Name 0 5

    Lihua

END

3.replace update a k-v cache, the cache must be present .

Replace key flag expire length

    Replace name 0 0 5

Zhang

STORED

4.set sets a k-v cache, the difference between set and add is that when a key does not exist, the set and add functions are the same as the new data, but when the key is present,

set functions As with replace, you can modify the data .

Set key flag expire length

    Set Gender 0 0 4

Male

STORED

    Set name 0 0 6

Liuxin

STORED

    5.delete Delete a k-v cache

Delete key [noreply]

    noreply : Optional Parameters. Notifies the server not to send an answer message.

Delete name

DELETED

    6.INCR/DECR value-added and impairment operations

INCR/DECR <key> <value>

Value: You need to increase or decrease the number of small values.

    Add Count 0 0 1

5

STORED

INCR Count 3

8

DECR Count 5

3

7.flush_all clears all cached data.

8.stats queries the running state of the server and other internal data.

    

     

    

memcached memory storage mechanismSlab allocation mechanism: Defragment memory for reuse

The most recent memcached by default uses a mechanism called slab allocator to allocate and manage memory. Prior to the advent of this mechanism, the allocation of memory was done by simply

For malloc and free. However, this approach can lead to memory fragmentation, aggravating the burden of the operating system memory manager, and in the worst case, cause the operating system to be more than the memcached process

itself is slow. Slab Allocator was born to solve the problem.

Let's look at the principle of slab allocator. The following is the goal of slab allocator in the memcached documentation:

The primary goal of the slabs subsystem in memcached is to eliminate memory fragmentation issues totally by using

Fixed-size memory chunks coming from a few predetermined size classes.

In other words, the basic principle of the Slab allocator is to divide the allocated memory into blocks of a specific length in a predetermined size to completely resolve the memory fragmentation problem.

The principle of Slab allocation is quite simple. Divide the allocated memory into blocks of various sizes (chunk) and divide the same size blocks into groups (the Chunk collection) (Figure 1).

    

Fig. 1 structure diagram of Slab allocation

Also, slab allocator has the purpose of reusing allocated memory. In other words, the allocated memory is not freed, but reused.

Main terms of Slab allocation

Page

The memory space allocated to slab, which is 1MB by default. After assigning to slab, the slab is divided into chunk according to the size of the.

Chunk

The memory space used to cache records.

Slab Class

A group of chunk of a specific size.

The principle of caching records in slab

The following shows how memcached selects slab and caches the data sent by the client to chunk.

Memcached based on the size of the data received, select the slab that best fits the data size (Figure 2). A list of idle chunk in slab is kept in memcached, according to the

List, select Chunk, and then cache the data in it.

    

Figure 2 How to select a group to store records

In fact, Slab allocator also has advantages and disadvantages. Here's a look at its drawbacks.

Disadvantages of Slab Allocator

Slab allocator solved the original memory fragmentation problem, but the new mechanism also brought new problems to memcached.

The problem is that the allocated memory cannot be effectively exploited because it allocates memory of a specific length. For example, cache 100 bytes of data into a 128-byte chunk,

The remaining 28 bytes are wasted (Figure 3).

    

Figure 3 Use of chunk space

There is no perfect solution for this problem, but the more effective solution is documented in the documentation.

The most efficient-on-the-waste-to-use-a list of size classes that closely matches (if that's at all Possibl e) Common sizes of

Objects the clients of this particular installation of memcached is likely to store.

That is, if you know in advance the common size of the data sent by the client, or if you only cache data of the same size, you can reduce waste if you use a list of groups that fit the data size.

Unfortunately, it is not possible to perform any tuning at this time, only to look forward to future versions. However, we can adjust the difference in the size of the slab class. Next, the growth factor option is described.

Tuning with growth Factor (growth factor)

memcached specifies the growth factor factor at startup (with the-f option) to control the difference between slab to some extent. The default value is 1.25. However, when this option appears

Previously, this factor was once fixed to 2, known as the "Powers of 2" strategy.

Let's try using the previous settings to start memcached in verbose mode:

  $ memcached-f 2-VV

The following is the verbose output after startup:

Slab class 1:chunk size Perslab 8192
Slab class 2:chunk size Perslab 4096
Slab class 3:chunk size Perslab 2048
Slab class 4:chunk size 1024x768 perslab 1024x768
Slab class 5:chunk size 2048 Perslab
Slab class 6:chunk size 4096 Perslab
Slab class 7:chunk size 8192 perslab
Slab class 8:chunk size 16384 perslab
Slab class 9:chunk size 32768 perslab
Slab class 10:chunk size 65536 perslab
Slab class 11:chunk size 131072 Perslab 8
Slab class 12:chunk size 262144 Perslab 4
Slab class 13:chunk size 524288 Perslab 2

As you can see, starting with a 128-byte group, the size of the group increases to twice times the original. The problem with this setting is that the difference between slab is large, and in some cases it is quite a waste of memory.

Therefore, to minimize memory waste, two years ago this option was appended with growth factor.

Take a look at the current default settings (f=1.25) when the output (space limit, this is only written to the 10th group):

Slab class   1:chunk size     Perslab 11915
Slab class 2:chunk size perslab 9362
Slab class 3:chunk size 144 Perslab 7281
Slab class 4:chunk size 184 perslab 5698
Slab class 5:chunk size 232 Perslab 4519
Slab class 6:chunk size 296 perslab 3542
Slab class 7:chunk size 376 Perslab 2788
Slab class 8:chunk size 472 perslab 2221
Slab class 9:chunk size 592 perslab 1771
Slab class 10:chunk size 744 perslab 1409

As can be seen, the gap between groups is much smaller than the factor of 2 o'clock, which is more suitable for caching hundreds of-byte records. From the above output, there may be some calculation errors, these errors are

Deliberately set in order to keep the number of bytes aligned.

When introducing memcached into a product or deploying it directly using default values, it is best to recalculate the expected average length of the data and adjust the growth factor to get the most

The appropriate settings. Memory is a precious resource, and a waste of it is too bad.

References

http://kb.cnblogs.com/page/42732/

http://acooly.iteye.com/blog/1120346/

http://blog.csdn.net/ado1986/article/details/41927767

Memcached (1)-----Installation and Basic introduction

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.