This article mainly introduces you to the working principle of Memcached in detail. You can also refer to Memcached Video Tutorial or Memcached Manual.
1. Introduction:
Memcache is a high-performance distributed memory object cache system. By maintaining a unified huge hash table in the memory, it can be used to store various Data in various formats, including images, videos, files, and database search results, etc. Simply put, the data is called into the memory and then read from the memory, thus greatly improving the reading speed. Memcached runs in one or more servers as a daemon and will receive client connections and operations at any time.
2. Features:
1. There is no limit to the amount of item data that can be saved in Memcached, as long as there is enough memory.
2. The maximum memory used by a single Memcached process is 2G. To use more memory, you can open multiple Memcached processes on multiple ports
3. The maximum data expiration time of 30 days is set to permanent It will also expire at this time, constant REALTIME_MAXDELTA
4. The maximum key length is 250 bytes, and it cannot be stored if it is larger than this length. The constant KEY_MAX_LENGTH 250 controls
5. The maximum data for a single item is 1MB, and data exceeding 1MB will not be stored. , controlled by constant POWER_BLOCK 1048576,
6. It is the default slab size
7. The maximum number of simultaneous connections is 200, passed
Controlled by freetotal in conn_init(), the maximum number of soft connections is 1024, through settings.maxconns=1024
Control the parameters related to space occupation: settings.factor=1.25, settings.chunk_size=48,
Affects the slab's data occupancy and stepping method
8.memcached is a non-blocking socket communication service based on the libevent library. Due to non-blocking communication, the memory reading and writing speed is very fast.
9.memcached is divided into server and client. Multiple servers and clients can be configured. It is widely used in distributed services.
10.memcached is very effective as a small-scale data distribution platform.
11.memcached has a one-to-one correspondence between key and value. The default key cannot exceed 128 characters. Section, the default size of value is 1M, which is one slab. If you want to store a value of 2M (continuous), you cannot use two slabs, because the two slabs are not consecutive and cannot be stored in the memory. For storage, it is necessary to modify the size of slabs. When multiple keys and values are stored, even if the slabs are not fully used, other data will not be stored.
12. Put in data of low value, otherwise if the server goes down, all the data will be lost
3. Applicable occasions
1. Distributed applications. Since memcached itself is based on a distributed system, it is especially suitable for large distributed systems.
2. Database front-end cache. Databases are often the bottleneck of website systems. Large concurrent access to the database often causes website memory to overflow. Of course we can also use Hibernate's caching mechanism. However, memcached is based on distribution and can be independent of the website application itself, so it is more suitable for large websites to split applications.
3. Data sharing between servers. For example, we split the login system and query system of the website into two applications, place them on different servers, and cluster them. Then after the user logs in, how does the login information synchronize from the login system server to the query system server? Woolen cloth? At this time, we can use memcached. The login system caches the login information, and the query system can obtain the login information, just like obtaining local information.
4. Inapplicable occasions
Memcached will not bring any benefits to applications that do not require "distribution", do not require sharing, or are simply small enough to have only one server. On the contrary, it will slow down the system efficiency, because the network connection also requires resources
This article has certain reference value, I hope it will be helpful to everyone in need!
The above is the detailed content of A detailed introduction to the operating principle of Memcached [Summary]. For more information, please follow other related articles on the PHP Chinese website!