Key/value King memcached: I. Starting the MC Header

Source: Internet
Author: User
1. What is memcached?

In data-driven web development, the same data is often retrieved from the database repeatedly, which greatly increases the database load. Caching is a good solution to this problem. However, although httpruntime. cache in ASP. NET can cache partial pages, it is still not flexible enough. In this case, memcached may be what you want.

Memcached isHigh-performance distributed memory object Cache SystemUsed for dynamic web applications to reduce database load. It caches data and objects in the memory to reduce the number of reads to the database, thus improving the speed of dynamic and database-driven websites. After years of development, many well-known Internet applications have used memcached, such as Wikipedia, Flickr, YouTube, and Wordpress.

2. Why memcached?

   

Demonstrate General Usage of memcached:Distributed cache. When the browser requests data for the first time, the application server first retrieves the data from the database server and returns the data to the user, and stores the data in the memcached server as a key/value pair. When the user accesses the data of the last request for the second time, the application server checks whether the cache exists on the memcached server. If yes, the application server reads the data directly from memcached. Because the memcached server is deployed in the Intranet of the website data center and the data is stored in the memory space of the server, the access speed is faster than the disk Io of the database, this increases the service response speed and reduces the load pressure on the database server during peak hours.

(1) As a high-speed distributed cache server, memcached has the following features:

  • Simple Protocol: UseSimple text line-basedAnd does not use complex XML protocols. Therefore, you can use Telnet to save and retrieve data on memcached;
  • Libevent-based event processing: Memcached uses thisLibeventLibrary, so it can make full use of its high performance in Linux, BSD, Solaris and other operating systems;
LibeventIs a library that encapsulates the epoll and kqueue of BSD operating systems in Linux into a unified interface. O (1) performance can be used even if the number of connections to the server increases.

Libevent: Http://www.monkey.org /~ Provos/libevent/

  • Built-in memory storage: To improve performance, data stored in memcached is stored in memcached.Built-in memory storage spaceMedium;

Because the data only exists in the memoryRestarting memcached and the operating system will cause all data to disappear.. In addition, when the content capacity reaches the specified value, the unused cache is automatically deleted based on the LRU (least recently used) algorithm. Memcached is a server designed for caching, soThe data permanent issue is not considered too much..

A good nosql product:RedisTo better solve the problem of data persistence. Restarting redis will not cause data loss.

  • Non-Communication distributed: Although it is a "distributed" cache server, the server does not have a distributed function.Depends entirely on the client. We are surprised to find that the memcached cluster is very easy. Simply add the Server IP address and port number to the client configuration file. In other words, our application only needs to send data requests to the memcached client. In the memcached client, a distributed algorithm is used to calculate the address of a memcached server from the memcached server list (for read requests, the memcached server information with the key cached in the distributed algorithm is obtained based on the key );

  

(2) Comparison between memcached and redis

① There is no need to worry too much about performance becauseBoth are high enough.. BecauseRedis only uses single-core, AndMemcached can use multiple coresTherefore, in comparison, redis has higher performance than memcached in storing small data on average per core. Memcached has higher performance than redis in data of more than kb. Although redis has recently optimized its performance in storing big data, it is inferior to memcached. The conclusion is that no matter which one you use, the number of requests processed per second will not become a bottleneck. (For example, the bottleneck may be on the NIC)

② If we want to talk about memory usage efficiency,Memcached memory usage is higher if simple key-value storage is used.If redis uses the hash structure for key-value storage, its memory usage will be higher than memcached due to its combined compression. Of course, this is related to your application scenarios and data features.

③ If youIf you have requirements on data persistence and data synchronization, we recommend that you choose redisBecause memcached does not have these two features. Even if you just want to upgrade or restart the system and the cache data will not be lost, it is wise to choose redis.

Therefore, we can draw a conclusion: In simple key/value application scenarios (such as cache), memcached has higher read/write performance, while in data persistence and Data Synchronization scenarios, redis has more powerful functions and richer data types;

Iii. memcached installation and simple operations

Memcached is easy to install and supports multiple platforms, including the most classic Linux, fressbsd, Solaris (memcached 1.2.5 or later), Mac OS X, and windows. Here we use the most familiar Windows platform to install the memcached service and perform simple configuration and operations.

(1) download memcached for Windows

URL: http://code.jellycan.com/files/memcached-1.2.6-win32-bin.zip

Its corresponding Source Code address: http://code.jellycan.com/files/memcached-1.2.6-win32-src.zip

(2) install Windows Server 2003 Enterprise Edition (You can also perform this operation on the local machine.), Named memcachedserver. In addition, you must set an IP address for the Windows server in the virtual machine to ensure that the host machine and the virtual machine can ping each other.

(3) copy the downloaded memcached package to a specified folder in windows, for example, C:/memcachedserver/

(4) ① install memcached in Windows:Memcached.exe-D install(Then, the corresponding uninstall command is:Memcached.exe-D uninstall)

② After the installation is complete, you can view the memcached Server Service in the Windows Service list. Generally, we need to set it as the boot item: Set the start type to automatic in the attribute.

③ After installing the service, you can start the memcached service. There are two ways to start it: 1. Directly select the memcached service in the Windows Service list and click Start; 2. input the following in the command line:Memcached-d start(The corresponding stop service command is:Memcached-d stop)

(5) check whether the memcached service is successfully started:

① Use the Telnet command to connect to the logon server: Telnet Server IP address 11211 (11211 is the default memcached service port number). Here I enter:Telnet 192.168.80.10 11211(192.168.80.10 is the IP address of my Windows Server Virtual Machine)

② Print the current memcache server status:Stats

We can see that the stats command lists a series of memcached service status information. What does this information mean? We can know through:

(6) get started with the data read/write commands of memcached:

① Add or change the command:SetKeyname additional information survival time storage Byte Count [Press enter] specific stored data block

 

Here, the additional information is 0, indicating none. The survival time is 0, indicating permanent. The specific data block is the value in key/value;

PS:You can also useAddKeyname additional information survival time storage Byte Count [Press enter] specific stored data block. However, set is obviously more powerful. It determines whether the keyname exists. If it does not exist, it is added. If it exists, it is modified;

② READ command:GetKeyname. Here we have just added a key1 data, so we use: Get key1 to get the cached data of the key key1;

③ Update command:ReplaceKeyname additional information survival time storage Byte Count [Press enter] specific stored data block

④ DELETE command:DeleteKeyname

Iv. Learning Summary

This article describes what memcached is, downloads and installs memcached, sets it as a Windows self-starting service, and connects to the memcached logon station through telnet to use commands for data read and write operations. In general, memcached is a high-performance Key/value cache system. By building a memcached cluster, it can meet the needs of distributed cache services for large websites. Next, we will connect to and operate memcached in. net. Finally, we will use a comprehensive case to demonstrate the important role of memcached as a distributed cache.

References

(1) Chuan Zhi podcast Maron, "memcached Open Class", http://bbs.itcast.cn/thread-14836-1-1.html

(2) charlee, "memcached full analysis", http://kb.cnblogs.com/page/42731/

(3) Zhang Zhao, memcached introduction, http://blog.csdn.net/zz198808/article/details/8032571

(4) Toxic, memcached, http://www.cnblogs.com/lost-1987/articles/3069460.html

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.