Using Memcached for Memory caching

Source: Internet
Author: User
Tags array implement mysql mysql query php example query requires thread
cache| Cache

The usual Web caching method has dynamic caching and static caching, and so on, in the ASP.net has been able to implement the local cache, while the use of memcached cache than asp.net local cache more flexible, you can cache any object, whether or not on the page output. The biggest advantage of memcached is that it can be distributed deployment, which is an essential requirement for large-scale applications.
LiveJournal.com uses the memcached to cache on the front end, and has achieved good results, while wikipedia,sourceforge and so on have adopted or are about to adopt memcached as a caching tool. Memcached can play a huge role in large-scale web applications.

What is memcached?
Memcached is a high-performance, distributed memory object caching system for reducing database load and increasing access speed in dynamic applications.
Memcached is developed by Danga Interactive to enhance the speed of livejournal.com access. LJ's dynamic page hits thousands of times per second, user 7 million. Memcached reduces database load significantly, allocates resources better, and accesses more quickly.


How to use the Memcached-server end?
Run on service side:
#./memcached-d-M 2048-l 10.0.0.40-p 11211
This will start a process that consumes 2G of memory and open port 11211 to receive requests. Because 32-bit systems can handle only 4G of memory addressing, 2-3 processes can be run on 32-bit servers that use PAE greater than 4G memory and are monitored on different ports.


How to use the Memcached-client end?
After the application contains a class that describes the client, it can be used directly, very simply.
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?

Do not consider what kind of database to use (Ms-sql, Oracle, Postgres, Mysql-innodb, etc ...) to implement transactions (acid,atomicity, consistency, isolation, and Durability) requires a lot of overhead, especially when using a hard drive, which means that the query may block. When you use a database that does not contain a transaction, such as Mysql-myisam, the overhead does not exist, but the read thread may be blocked by the write thread.
Memcached never block, very fast.


Why not use shared memory?
The original caching approach was to cache objects online agenda, but such processes could not share the cache, and the hit rate was very low, resulting in extremely low cache efficiency. Later, there is a shared memory cache, many processes or threads share the same block of cache, but after all, but only on a single machine, many machines do the same cache is a waste of resources, but also relatively low hit rate.
Memcached server and clients work together to implement a global cache of distributed across servers. and can work together with Web server, the Web server to CPU requirements, low memory requirements, Memcached server for CPU requirements, high memory requirements, so you can use.


What about the Mysql 4.x cache?
MySQL query caching is not ideal because the following points are:
When the specified table has been updated, the query cache is emptied. On a large load of the system such things happen very frequently, resulting in very low query cache efficiency, in some cases even less open, because it is the management of the cache will still have overhead.
On 32-bit machines, MySQL is still limited to the operation of the memory within 4G, but memcached can be distributed, memory size is theoretically unrestricted.
MySQL is the query cache, not the object cache, if you need a lot of other operations after the query, the query cache is not helpful.
If the data to be cached is small, and the query is not very frequent, such cases can be used to query the MySQL cache, otherwise the words memcached better.


How about database synchronization?
The database synchronization here refers to the MySQL Master-slave mode of synchronization of the database by log synchronous mechanism.
You can distribute read operations, but you cannot distribute writes, but the synchronization of write operations consumes a lot of resources, and this overhead is growing as the slave server grows.
The next step is to split the database horizontally so that different data is distributed to different database server groups to achieve distributed read and write, which requires the implementation of different databases in the application based on different data connections.
When this mode is working (and we recommend doing so), more databases cause more headaches for hardware errors.
Memcached can effectively reduce access to the database, so that the database with the main effort to do infrequent write operations, which is controlled by the database itself, very few will block themselves.


Memcached, is it fast?

Very quickly, it uses libevent to handle any number of open connections (using Epoll, rather than poll), using Non-blocking network IO, distributed hash objects to different servers, and query complexity of O (1).

From:http://www.uuzone.com/blog/janes/92031.htm



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.