ArticleDirectory
- Use memcached for memory cache
Use memcached for memory cache
Resend old text
2005.8.9
Generally, the webpage cache methods include dynamic cache and static cache. net can be used to cache partial pages, while memcached cache is better than ASP. the local cache of net is more flexible and can cache any object, regardless of whether it is output on the page or not. The biggest advantage of memcached is that it can be deployed in a distributed manner, which is also essential for large-scale applications.
Livejournal.com uses memcached for caching on the front end and achieves good results. However, Wikipedia and SourceForge also adopt or will soon use memcached as the cache tool. Memcached can play a huge role in large-scale website applications.
What is memcached?
Memcached is a high-performance, distributed memory object cache system that reduces database loads in dynamic applications and improves access speeds.
Developed by danga interactive, memcached improves the access speed of livejournal.com. The number of dynamic page views per second on the LJ is several thousand, and the number of users is 7 million. Memcached greatly reduces the database load and makes it easier to allocate resources for faster access.
How to Use memcached-server?
Run on the server:
#./Memcached-D-M 2048-l 10.0.0.40-P 11211
This starts a process that occupies 2 GB of memory and opens port 11211 to receive requests. Because the 32-bit system can only handle 4G memory addressing, 2-3 processes can be run on 32-bit servers with 4G memory and monitored on different ports.
How to Use memcached-client?
After the application end contains a class used to describe the client, it can be used directly, which is very simple.
PHP example:
$ Options ["servers"] = array ("192.168.1.41: 11211", "192.168.1.42: 11212 ");
$ Options ["debug"] = false;
$ MEMC = new memcachedclient ($ options );
$ Myarr = array ("one", "two", 3 );
$ MEMC-> set ("key_one", $ myarr );
$ Val = $ MEMC-> get ("key_one ");
Print $ Val [0]. "\ n"; // prints 'one'
Print $ Val [1]. "\ n"; // prints 'two'
Print $ Val [2]. "\ n"; // prints 3
Why not use a database to do this?
What kind of database (MS-SQL, Oracle, S, MySQL-InnoDB, Etc ..), implementing acid, atomicity, consistency, isolation, and durability requires a lot of overhead. Especially when using hard disks, this means that the query may be blocked. When a database that does not contain transactions (such as MySQL-MyISAM) is used, the preceding overhead does not exist, but the read thread may be blocked by the write thread.
Memcached never blocks and is very fast.
Why not use shared memory?
The original caching method was to cache objects in the thread, but the cache could not be shared between processes, and the hit rate was very low, resulting in extremely low cache efficiency. Later, there was a cache for shared memory. Multiple processes or threads shared the same cache, but after all, it was limited to only one machine, the same cache on multiple machines is also a waste of resources, and the hit rate is relatively low.
Memcached server and clients work together to implement cross-Server Distributed Global cache. It can work with the Web server. The web server has high CPU requirements and low memory requirements. The memcached server has low CPU requirements and high memory requirements, so it can be used together.
What about MySQL 4.x cache?
MySQL query cache is not ideal because of the following:
When the specified table is updated, the query cache is cleared. In a system with a large load, such events occur frequently, resulting in low query cache efficiency. In some cases, the query cache may not be available, because it still has overhead for Cache Management.
On 32-bit machines, MySQL still limits memory operations to 4 GB, but memcached can be distributed out, so the memory size is theoretically unlimited.
MySQL queries the cache instead of the object cache. If a large number of other operations are required after the query, the query cache will not help.
If the data to be cached is small and not frequently queried, you can use MySQL to query the cache. Otherwise, memcached is better.
How about Database Synchronization?
Here, Database Synchronization refers to the database synchronization mechanism similar to the MySQL master-slave mode through log synchronization.
You can distribute read operations but cannot distribute write operations. However, synchronization of write operations consumes a large amount of resources, and the overhead increases with the growth of the slave server.
The next step is to split the database horizontally so that different data is distributed to different database server groups to achieve distributed read/write, this requires connecting different databases based on different data in the application.
When this mode works (we also recommend this), more databases lead to more headaches and hardware errors.
Memcached can effectively reduce access to the database, so that the database uses the primary energy to do less frequent write operations, which is controlled by the database itself and rarely blocks itself.
Is memcached fast?
It uses libevent to handle any number of open connections (using epoll instead of poll), and uses non-blocking network I/O, distributed Hash objects to different servers, the query complexity is O (1 ).