Memcached Features, Benefits and Limitations

It should be noted here that many developers think that Memcached is a distributed Cache, but in fact, the Memcached server itself is a single instance, but in the process of client implementation, it can be partitioned according to the stored primary key, and this area is the Memcached server. One or more instances, if the client is also included in Memcached, can be partially centralized in concept. Centralized architecture, nothing more than two cases: 1. Node balanced mesh (JBoss Tree Cache), using JGroup's multicast communication mechanism to synchronize data. 2. Master-Slaves mode (distributed file system), Master manages Slave, how to choose Slave, how to migrate data, are all done by Master, but Master itself also has a single problem.

Features, Benefits and Limitations

Memory  : Memory storage, fast speed, high memory requirements, and non-persistent cached content. The CPU requirements are very low, so the Memcached server is often deployed together with some CPU-intensive and memory-intensive applications. (Otherwise, it will squeeze resources from each other)

Centralized Cache  : It avoids the propagation problem of distributed Cache, but it needs non-single point to ensure its reliability. This requires the work of cluster. Multiple Memcached can be used as a virtual cluster. There is no difference in read and write performance of memcached.

Distributed expansion : One of the outstanding advantages of Memcached is that it adopts a distributed expansion mode. Multiple Memcached servers deployed on one machine or Memcached servers deployed on multiple machines can be formed into a virtual server, which is completely shielded and transparent to the caller. Improved memory utilization per machine.

Socket communication : Pay attention to the size of the transmitted content and serialization. Although Memcached is usually placed in the intranet as a Cache, the Socket transmission rate should be relatively high (currently supports Tcp and udp modes, and can be selected according to different clients) Use nio's synchronous or asynchronous calling method), but the serialization cost and bandwidth cost still need to be paid attention to. Serialization is also mentioned here. The performance of object serialization is often a headache for everyone, but if the serialization of Class objects of the same class is transmitted, the first serialization time is relatively long, and subsequent optimization will be carried out. In fact, serialization The biggest cost is not object serialization, but class serialization. It's best if all you're going through is strings, eliminating the need for serialization, so you tend to keep smaller content in Memcached.

Special memory allocation mechanism : The first thing to note is that the  maximum storage object supported by Memcached is 1M  (page). Its memory allocation is special, but this allocation method is also for performance considerations. A simple allocation mechanism can make it easier to recycle and re-allocate, saving CPU usage (described in the previous article).

The Cache mechanism is simple : First of all, it has no synchronization, message distribution, two-phase commit, etc. It is a very simple Cache, put things in, and then take them out. If you find that the provided Key is not hit, then it is very straightforward. Let me tell you, your key does not have any corresponding thing in the cache. Go to the database or other places to get it. When you get it from an external data source, you can directly put the content in the Cache, so that you can get it next time. hit. Here we will mention how to synchronize these data. There are two ways. One is to update the Cache content immediately after you modify it, so that it will take effect immediately. The other is to allow an invalidation time. When the invalidation time is reached, the content will be deleted naturally. At this time, it will fail to hit, and then the content will be placed in the Cache again to update the content. The latter is used in some occasions where the requirements are not high and the writing is infrequent.

Importance of the client: The rational design of the client is very important, and it also provides users with a lot of space to expand and design the client to meet the needs of various scenarios, including fault tolerance, weight, efficiency, and special functional requirements. , embedded frames, etc.

Several application points : caching of small objects (user's token, permission information, resource information). Small static resource cache. Cache of Sql results (this part is used well, the performance improvement is quite large.)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324440374&siteId=291194637