Home >Backend Development >PHP Tutorial >What is memcached? How to use memcache? _PHP Tutorial
What is Memcached?
Memcached is a distributed memory object caching system developed by danga.com (the technical team that operates LiveJournal), which is used to reduce database load and improve performance in dynamic systems. Many people use it as a storage carrier like SharedMemory. Although memcached uses the same "Key=>Value" method to organize data, it is very different from local caches such as shared memory and APC. . Memcached is distributed, which means it is not local. It completes services based on network connection (of course it can also use localhost). It is an application-independent program or daemon process (Daemon mode).
Memcached uses the libevent library to implement network connection services. In theory, it can handle an unlimited number of connections. However, unlike Apache, it is more often oriented towards stable and continuous connections, so its actual concurrency capabilities are limited. . Under conservative circumstances, the maximum number of simultaneous connections for memcached is 200, which is related to Linux thread capabilities. This value can be adjusted. For information about libevent, please refer to relevant documentation.
Memcached memory usage is also different from APC. APC is based on shared memory and MMAP. Memcachd has its own memory allocation algorithm and management method. It has nothing to do with shared memory and has no restrictions on shared memory. Normally, each memcached process can manage 2GB of memory space. If If more space is needed, the number of processes can be increased.
How to use Memcached?
In many cases, memcached has been abused, which inevitably leads to too many people complaining about it. I often see people posting on forums, similar to "How to improve efficiency", and the reply is "Use memcached". As for how to use it? Where to use it? What is it used for? There was no detailed answer. The author wants to make a point here: memcached is not a panacea, and it is not suitable for all occasions.
Memcached is a "distributed" memory object caching system. That is to say, for applications that do not need to be "distributed", do not need to be shared, or are simply small enough to have only one server, memcached will not bring any On the contrary, it will slow down the system efficiency, because network connections also require resources, even UNIX local connections.
My previous test data showed that the local read and write speed of memcached is dozens of times slower than the direct PHP memory array, while the APC and shared memory methods are similar to the direct array. It can be seen that if it is only a local-level cache, using memcached is very uneconomical.
Memcached is often used as a database front-end cache. Because it has a lot less overhead such as SQL parsing and disk operations than a database, and it uses memory to manage data, it can provide better performance than directly reading the database. In large systems, it is very difficult to access the same data. Frequently, memcached can greatly reduce database pressure and improve system execution efficiency. In addition, memcached is often used as a storage medium for data sharing between servers. For example, data that saves the system's single sign-on status in an SSO system can be saved in memcached and shared by multiple applications.
It should be noted that memcached uses memory to manage data, so it is volatile. When the server is restarted or the memcached process is terminated, the data will be lost, so memcached cannot be used to persist data. Many people misunderstand that memcached's performance is very good, even compared to the comparison between memory and hard disk. In fact, memcached's use of memory will not increase the read and write speed by hundreds or thousands. Its actual bottleneck lies in the network connection, which is related to the use of memory. Compared with the disk database system, the advantage is that it is very "light". Because there is no excessive overhead and direct reading and writing methods, it can easily handle a very large amount of data exchange, so there are often two gigabit network bandwidths. They are all fully loaded, and the memcached process itself does not occupy much CPU resources.