Memcache Startup Storage Principle Cluster

Source: Internet
Author: User

first, under Windows installation start

First, add the bin directory of the Memcache to the PATH environment variable to facilitate subsequent use of the command:


  


Then execute the memcached–dinstall command to install the Memcache service:


  


Then go to the calculated services page to see that the installation was successful:


  


To start the Memcache background service program:


  


In the background service, you see that Memcache's background service is started:


  


Then execute, telnet 127.0.0.111211来 Open the Telnet client:


  


It turns out that Telnet is not enabled:


  


Then, on the "Start or shut down Windows features", turn on the Telnet client


  


You will then need to restart your computer to make the changes effective. Then execute telnet 127.0.0.1 11211来 open the Telnet client and open successfully:


  


Test is available, we then Telnet Client to command, stats, the following message appears to indicate normal:


  


Then we save a data and then take it out:


  


This concludes that our memcache has been successfully installed and started.


second, the role
2.1 Distributed Cache

As we move from the IT era to the DT era, our software architecture is also from standalone to cluster and distributed. However, the first step toward the distribution needs to solve the problem of multiple servers sharing login information. It is to solve the problem that multiple servers share common information.

To solve this problem, we can store the public information in the state server, or store it in a database, and then store it in our "distributed cache" and so on. Because the read cache plus network transfer time is much smaller than the time to read the database (IO), so distributed cache is a very good solution to this problem.

  


2.2 Reduce database pressure

All of our data are basically stored in the database, each time the frequent access to the database, resulting in a sharp decline in database performance, unable to service more users at the same time, such as MySQL, especially frequent lock table, then I can memcache to share the pressure of the database, that is, can do data cache, Because Memcache's reading and writing performance can be said to be extremely perfect.


three, storage principle

The memcache uses the key-value pair to store the method. It is essentially a large hash table, with a maximum length of 255 characters for the key, and an expiry time of 30 days.

Its memory model is as follows: Memcache pre-partition the disposable memory space (Slab), each partition is divided into multiple blocks (Chunk) maximum 1M, but the size of the block in the same partition is fixed. Then, when inserting data, it will find the most suitable block according to the data size, then insert, of course, there will be some memory waste, but can reduce the memory fragmentation to some extent, overall, the advantages outweigh the disadvantages. When Memcache's memory is full, it cleans up old data in the following principles: LRU idle > Expire > Minimal access. And it uses the lazy delete, it does not provide the mechanism of monitoring data expiration, but lazy, when the query to a key data, if expired, then discard directly.


Four, the principle of cluster construction

Memcache's cluster-cluster building is super simple and does not require server-side configuration at all. It is the configuration of the cluster through the driver of the client, and the configuration is super simple.

The client implements the cluster principle: first the client configures a list of IP and ports for multiple cluster machines. Then the client driver before writing the data, first processing the key to get the hash value, the total number of machines to take out, and then select the remainder corresponding to the machine.


  


For example, code that saves data to a memcache cluster for a C #:


  


v. Comparison with Redis

Since Redis is also often used as a distributed cache, after having used both of them, a simple experience is as follows (for a detailed comparison of the two, please go online to find the topic):

First, both are Key-value-type storage.

Memcache is multi-threaded (with a corresponding locking mechanism) and requires a thread-safety issue; Redis is single-threaded and does not need to take into account thread safety issues.

Memcache does not provide a master-slave replication mechanism, fault tolerance is not good. (No ha-high availability); Redis provides master-slave replication.

The Memcache cluster configuration is very simple and does not need to be configured on the server side, only need to initialize a serverlist on the client, Redis needs to configure the server side.

Memcache can only be cached and not persisted; Redis is a NoSQL memory database that provides two persistence mechanisms.

Redis provides five types of value, very rich (string/list/hash/set/zset), and memcache data types are relatively single.


Vi. Summary

Due to some of our current problems, the pain of high concurrent access to the database: deadlock, disk IO pain: Very inefficient, multi-service shared data, etc. Memcache with its own advantages: Read and write performance is perfect (no master-slave replication, all the code basically only consider the best performance); Super simple cluster construction, open source, low learning cost, easy to get started, rich success stories, etc., become a popular distributed cache framework.

Memcache Startup Storage Principle Cluster

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.