Memcached is the Holy one?
In data-driven web development, it is often repeated to remove the same data from the database, which greatly increases the database load. Caching is a good way to solve this problem. But httpruntime.cache in ASP. Although it can be implemented to cache the page locally, but still not flexible, at this time memcached may be what you want.
Memcached is a high-performance distributed memory object caching system for dynamic Web applications to mitigate database load. It improves the speed of dynamic, database-driven Web sites by caching data and objects in memory to reduce the number of times a database is read. After years of development, there are many well-known Internet applications have been used to memcached, such as: Wikipedia, Flickr, Youtube, WordPress and so on.
Second, why use memcached?
Shows the general purpose of memcached: distributed caching . When the browser first requests access to the data, the application server returns to the user from the database server and deposits it into the memcached server as a Key/value key-value pair. When the user accesses the last requested data for the second time, the application server looks through the memcached server for a cache and, if any, reads directly from the memcached. Because the memcached server is deployed in the intranet of the Web site, and the data is stored in the server's memory space, the access speed is faster than the database's disk IO, which improves the service response speed and reduces the load pressure on the database server during peak hours.
(1) memcached, as a distributed cache server with high-speed operation, has the following features:
- The protocol is simple : Use simple text-line-based protocols without using complex XML protocols. Therefore, through Telnet can also be stored on the memcached data, access to data;
- Libevent-based event processing : Memcached uses this libevent library, so it can perform its high performance on Linux, BSD, Solaris and other operating systems;
libevent is a library that encapsulates event-handling functions such as Linux's Epoll, BSD-class operating system kqueue, and so on as a unified interface. The Performance of O (1) can be played even if the number of connections to the server increases.
libevent: http://www.monkey.org/~provos/libevent/
- built-in memory storage : In order to improve performance, the data stored in the memcached is saved in the memcached internal memory storage space ;
Because the data exists only in memory, restarting the memcached and restarting the operating system will cause all data to disappear . Additionally, when the content capacity reaches the specified value, the unused cache is automatically deleted based on the LRU (Least recently used) algorithm. The memcached itself is a server designed for caching, so there is not too much consideration for permanent data issues .
One of the better products in NoSQL:Redis, which solves the problem of data persistence, and restarting Redis does not result in data loss.
- distributed without communication : although it is a "distributed" cache server, there is no distributed functionality on the server side, which is entirely dependent on the client 's implementation. We will be surprised to find that the memcached cluster is very easy and simple to add only the server IP and port number in the client's configuration file; in other words, our application only needs to request the data to the memcached client. In the memcached client, a distributed algorithm (consistent hash algorithm) is used to compute the address of a memcached server from the memcached server list (if it is a read request, The memcached server information is cached with the key in the distributed algorithm according to key, then the client passes the data (Key/value pair) to the computed memcached server (if it is a read request, The data that contains the specified key is read from the computed memcached server);
PS: because the memcached cache server is not communicating with each other, so the cluster size can be easily expanded, with good scalability .
(2) Comparison of memcached and Redis
There is no need for ① to be overly concerned with performance, because both are already high enough . Since Redis uses only single cores , and memcached can use multicore , on average, Redis has a higher performance than memcached for storing small data on a per-core scale. In more than 100k of data, memcached performance is higher than Redis, although Redis has recently been optimized for the performance of storing big data, but it is slightly inferior to memcached. Having said so much, the conclusion is that no matter which one you use, the number of requests per second will not be a bottleneck. (such as bottlenecks may be in the network card)
② If you want to say memory usage efficiency, using simple key-value storage, memcached memory utilization is higher, and if Redis uses hash structure to do key-value storage, because of its combined compression, Its memory utilization is higher than memcached. Of course, this is related to your application scenario and data characteristics.
③ If you have requirements for data persistence and data synchronization, it is recommended that you choose Redisbecause neither of these features memcached. Choosing Redis is also wise, even if you just want the cached data to be not lost after upgrading or rebooting the system.
Thus, we can conclude that, in simple key/value scenarios (such as caching), memcached has higher read and write performance, while Redis has more powerful capabilities and richer data types in data persistence and data synchronization scenarios;
Third, the installation and simple operation of memcached
The memcached is simple to install and supports multiple platforms including: The most classic Linux, FreeBSD, Solaris (memcached 1.2, 5 +), Mac OS x, and our most familiar windows. Here we use the most familiar Windows platform of our day to install the memcached service and make simple configuration and operation.
(1) Download memcached for Windows
Url:http://code.jellycan.com/files/memcached-1.2.6-win32-bin.zip
Its corresponding source address: Http://code.jellycan.com/files/memcached-1.2.6-win32-src.zip
(2) Install Windows Server 2003 Enterprise Edition in the virtual machine ( not necessary step-up, also can be done in this machine ), named: Memcachedserver. Also, set the IP address for Windows Server in the virtual machine to ensure that the host and virtual machines can ping each other.
(3) Copy the downloaded memcached package to a specified folder in Windows, such as c:/memcachedserver/in my Windows Server
(4) ① install memcached service in Windows:memcached.exe-dInstall (then, the corresponding uninstall command is:memcached.exe-d uninstall)
② after the installation is complete, you can view the services of Memcached server in the list of Windows services, in general we want to set it as a boot entry: Setting the Startup type to Automatic in the properties
③ after installing the service, you can start the memcached service, there are two ways to start: One is to select the Memcached service click Start directly in the Windows Services list, and the second is to enter at the command line:memcached-d start (the corresponding stop service command is:memcached-d stop)
(5) Detect if the Memcached service is successfully started:
① using the Telnet command to connect to the login console: Telnet Server IP address 11211 (11211 is the default memcached service port number), I enter here:telnet 192.168.80.10 11211 (192.168.80.10 is the IP address of my Windows Server virtual machine)
② Printing current memcache Server Status:stats
As you can see, the stats command lists a series of memcached service status information, and what does that mean? We can know by:
(6) Initial learning memcached data read and write command operation:
① Add or change a command:set KeyName additional information survival time Storage bytes [carriage return] data block specifically stored
The additional information here is 0, represents none, the survival time is 0, represents the permanent, and the specific data block is what we call the value in the Key/value;
PS: It is also possible to use the add KeyName Extra information to store the number of bytes [carriage return] specific stored data blocks. However, the set is obviously more powerful, it will determine whether KeyName exists, does not exist, is added, the existence is modified;
② Read command:get KeyName, here we have just added a key1 of data, so we use: Get Key1 to obtain the Key1 this key cache data information;
③ Update command:replace KeyName additional information survival time Storage bytes [carriage return] data block specifically stored
④ Delete command:delete KeyName
Iv. Summary of Learning
In this article we have a preliminary understanding of what is memcached, download and install the memcached, and set it as the Windows Self-boot service, through telnet to the memcached login using commands for data read and write operations. Overall, memcached is a high-performance Key/value cache system that can accommodate the distributed cache service needs of large Web sites by building memcached clusters. Next article, we will be in. NET to connect and manipulate memcached, finally, a comprehensive case to reflect memcached as a distributed cache of the important role.
Finally, if the pro feel my article is good or useful to you, trouble to help me point a "recommendation", let me more motivated to continue to write, thank you!
Reference documents
(1) Preach intelligence podcast Mullen, "Memcached Open Class", http://bbs.itcast.cn/thread-14836-1-1.html
(2) Charlee, "memcached Complete Analysis", http://kb.cnblogs.com/page/42731/
(3) Migrations, "memcached Introduction", http://blog.csdn.net/zz198808/article/details/8032571
(4) Toxic, "memcached", http://www.cnblogs.com/lost-1987/articles/3069460.html
Zhou Xurong
Source: http://www.cnblogs.com/edisonchou/
The copyright of this article is owned by the author and the blog Park, welcome reprint, but without the consent of the author must retain this paragraph, and in the article page obvious location to give the original link.
Key/value King memcached: First, set off Memcached's hijab to