On the difference and summary of memcache and Redis

Source: Internet
Author: User
Tags data structures flush memcached mysql query redis cpu usage virtual private server
on the difference and summary of memcache and Redis

Redis and memcached Overall comparison

1 Performance comparison: Because Redis only use a single core, and memcached can use multi-core, so on average each kernel Redis in storing small data than memcached performance higher. In more than 100k of data, Memcached performance is higher than Redis, although Redis recently in the storage of large data performance optimization, but compared to Memcached, or slightly inferior.

2 Memory Efficiency comparison: The use of simple key-value storage, memcached memory utilization is higher, and if the redis using a hash structure to do key-value storage, due to its combined compression, Its memory utilization is higher than memcached.

3 Redis supports server-side data operations: Redis has more data structures and supports richer data operations than memcached, usually in memcached, where you need to get the data to the client for similar modifications and set back. This greatly increases the number of network IO and the volume of data. In Redis, these complex operations are often as efficient as the general get/set. So, if caching is needed to support more complex structures and operations, then Redis would be a good choice.
1 2 3 4 5 6

Simply comparing the difference between Redis and memcached

1 Redis not only supports simple k/v types of data, but also provides storage of data structures such as List,set,zset,hash.

2 Redis supports backup of data, that is, data backup in Master-slave mode.

3 Redis Support data persistence, you can keep the data in memory on disk, restart can be loaded again to use.
1 2 3 4 5 6

To compare the differences between Redis and memcached in detail:

<1> What is memcache and what are the business scenarios that apply to memcached. What is the cache mechanism of memcached? When designing an application, you can cache those content by memcached.
1 2
    Memcached is essentially a memory key-value database, but it does not support the persistence of data, and the data is lost after the server shuts down. Memcached is developed using the C language and is available on most POSIX systems, such as Linux, BSD, and Solaris, as long as the libevent is installed.



Under Windows, it also has an unofficial version available (http://code.jellycan.com/memcached/). 1. Basic configuration of Memcache 1) Start memcache Server Side #/usr/local/bin/memcached-d-M 10-u root-l 192.168.0.200-p 12000-c 256-p The memcached.pid-d option is to start a daemon, M is the amount of memory allocated to Memcache, in MB, I am here 10MB,-U is the user running memcache, I am here root, and-L is the server IP address of the listener, If there are more than one address, I specify the server's IP address 192.168.0.200, p is set memcache listening port, I set up here 12000, preferably more than 1024 of the port,-C option is the maximum number of concurrent connections, the default is 1024, I set 256 here, according to your server load to set, p is set to save the memcache pid file, I am here to save in/tmp/memcached.pid 2) If you want to end the memcache process, execute: # kill ' cat/tmp/ The Memcached.pid ' hash algorithm maps the binary value of any length to a smaller binary value of a fixed length, which is called a hash value. A hash value is a unique and extremely compact numeric representation of a piece of data. If you hash a clear text and even change only one letter of the paragraph, subsequent hashes will produce different values.

It is computationally impossible to find two different inputs that hash the same value.

2, the purpose of the consistent hash algorithm is two points: first, the node changes after the other nodes are affected as small as possible; second, the data redistribution is as balanced as possible after the node changes.

3, why to run memcached.

If the site's high traffic and most of the access will result in high database load situation, the use of memcached can reduce the pressure on the database.

4, the application of memcached business scene. 1 If the site contains a large number of dynamic Web pages, so the load of the database will be veryHigh.

Since most database requests are read, memcached can significantly reduce the database load.

2 If the load on the database server is low but CPU usage is high, then the computed results (computed objects) and the rendered page template (enderred templates) can be cached.

3 The use of memcached can cache session data, temporary data to reduce their database write operations.

4 cache Some files that are small but frequently accessed.

5 Caching Web ' services ' (non-IBM-promoted Web services, translator's note) or RSS feeds results ...

5, does not apply the memcached business scene.

1 The size of the cached object is larger than the 1MB memcached itself is not designed to handle large multimedia (large media) and large binary blocks (streaming huge blobs). 2 The length of the key is greater than 250 characters 3 the virtual host does not allow the memcached service to run if the application itself is hosted on a low-end virtual private server, virtualization technologies such as VMware and Xen are not suitable for running memcached.

Memcached needs to take over and control large chunks of memory, and memcached performance will be compromised if the memcached managed memory is swapped out by the OS or hypervisor. 4 The application runs in an insecure environment memcached to provide any security policy and can access memcached only by Telnet.

If your application is running on a shared system, you need to focus on security issues.

    5 The business itself needs to be persistent data or the need is how the database 6.memcached work. The high performance of memcached originates from a two-phase hash (two-stage hash) structure. Memcached is like a huge hash table that stores a lot of <key,value> pairs. With key, you can store or query arbitrary data. The client can store the data on multiple sets of memcached. When querying the data, the client first calculates the hash value (phase one hash) of the key by referring to the node list, and then selects a node; The client sends the request to the selected node, and then the Memcached node passes through an internal hashing algorithm (phase two hash), Find the real data (item) and return it to the client.

From an implementation point of view, Memcached is a non-blocking, event-based server program. 7, MEMcached's biggest advantage is what. The biggest advantage of memcached is that it brings excellent level scalability, especially in a huge system. Because the client makes a hash of itself, it's easy to add a lot of memcached to the cluster.

Memcached do not communicate with each other, so there is no increase in memcached load, no multicast protocol, no network traffic explosion (implode).

8, memcached and MySQL query cache compared to what are the advantages and disadvantages. Disadvantage: 1 compared to the MySQL query cache, the memcached into the application requires a lot of work.

The MySQL query cache, which automatically caches the results of SQL queries, can be executed repeatedly and quickly by cached SQL queries. Advantages: 1 When the table is modified, the MySQL query cache will be refreshed immediately (flush).

When write operations are frequent, the MySQL query cache often invalidates all cached data. 2 on multi-core CPUs, the MySQL query cache will encounter extended problems (scalability issues).

On multi-core CPUs, query cache adds a global lock, which can become slower because of the need to flush more cached data. 3 in the MySQL query cache, it is not possible to store arbitrary data (only SQL query results). Using memcached, we can build a variety of efficient caching. For example, you can execute multiple independent queries, build a user object, and then cache the user object in memcached. and query cache is SQL statement level, it is impossible to do this.

In a small web site, query cache will help, but as the size of the site, query cache will do more harm than good. 4 the memory capacity that query cache can utilize is limited by the free memory space of the MySQL server. It is good to add more memory to the database server to cache the data.

However, with memcached, as long as you have free memory, can be used to increase the size of the memcached cluster, and then you can cache more data.

9, memcached and the server's local cache (such as the PHP APC, mmap files, etc.) compared to what the pros and cons.

1 First, the local cache faces a severe memory limit, and the amount of memory that can be exploited is limited by the free memory space of the (single) server. 2) Local Cache is a bit better than memcached and query cache, that is, it can store arbitrary data, and there is no network access latency. As a result, local cache data queries are faster. Consider putting the highly common data in the local cache.

If each page needs to load a small amount of data, consider placing them in local cached. 3 The local cache lacks the characteristics of collective failure (group invalidation). In the memcached cluster, deleting or updating a key will make all observers aware of it.

But in the local cache, we can only notify all servers to flush cache (slow, not extensible) or simply rely on the caching timeout mechanism.

10, memcached the cache mechanism is how. The main cache mechanism of memcached is the LRU (least recently used) algorithm + timeout failure. When you save data to memcached, you can specify how long the data can stay in the cache which is forever, or some time in the future.

If memcached memory is not available, the expired slabs will be replaced first, then the oldest unused slabs.

11, memcached How to implement redundancy mechanism. Not implemented. Memcached should be an application cache layer, from the design itself to Beijing without any redundancy mechanism. If a memcached node loses all data, it should be possible to retrieve the data from a data source, such as a database. The application system should tolerate the failure of nodes. If you are concerned that the failure of the node will greatly increase the burden on the database, then there are some options.

For example, you can add more nodes (to reduce the impact of losing a node), hot standby nodes (take over IP when other nodes are down), and so on.

12, memcached How to deal with fault-tolerant. In the case of node failure, the cluster does not need to do any fault-tolerant processing. If a node failure occurs, the response depends entirely on the user. When a node fails, several scenarios are listed below for your choice: 1 ignore it.

Before the failed node is restored or replaced, there are many other nodes that can cope with the effect of node failure. 2 Remove the failed node from the list of nodes. You must be careful in doing this operation. By default (the remainder hash algorithm), the client adds or removes nodes, causing all cached data to be unavailable.

Because the list of nodes for a hash reference changes, most keys are mapped to a different node (from the original) because of a change in the hash value. 3 Start hot standby node, take over the IP occupied by the failure node. This will preventStop hash disorder (hashing chaos).

4 If you want to add and remove nodes without affecting the original hash result, you can use the consistent hashing algorithm (consistent hashing). 5) two times hash (reshing). When the client accesses the data, if you find a node down, do a hash again (the hash algorithm is different from the previous one), and then select another node (note that the client does not remove the down node from the list of nodes, and then it is possible to hash it first).

If a node is good or bad, the two hashing methods are risky, and dirty data may exist on both good nodes and bad nodes (stale).

13, how to memcached the item in bulk Import export. This should not be done. Memcached is a non-blocking server. Any operation that may cause memcached pauses or instantaneous denial of service should be well thought-out. Importing data into a memcached is often not what you really want.
Imagine that if the cached data changes between export imports, you need to deal with dirty data, and if the cached data expires between export imports, how do you deal with the data? Therefore, bulk export import data is not as useful as you might think. But it's useful in a scene.

If you have a large amount of data that never changes and you want the cache to be hot (warm), it is helpful to bulk import cached data.

    14, but I really need to memcached the item in bulk export import, how to do ...

    If you need to bulk export and import, the most likely reason is that rebuilding the cached data takes a long time or the database is bad for you to suffer. If a memcached node down makes you miserable, you must do some optimization work on the database. For example, dealing with the "surprise group" problem (memcached nodes are ineffective, repeated queries to make the database overwhelmed) or the existence of poor optimization queries.

Memcached is not an excuse and a solution to avoid optimizing queries. Here are some tips: use MogileFS (or similar software like COUCHDB) to store item, calculate it and dump it onto disk. MogileFS can easily overwrite the item and provide quick access. You can even cache the item in the MogileFS in memcached, which speeds up reading.

    The combination of mogilefs+memcached can speed up the response of cache misses and improve the usability of the Web site. Re-use MySQL. MySQL's InnoDB primary key query speed is very fast. If most of the cached data isCan be placed in the varchar field, the performance of the primary key query is better. Query by key from Memcached is almost equivalent to MySQL's primary key query: Hash the key to a 64-bit integer, and then store the data in MySQL.

You can make the original (not hash) key stored in the normal fields, and then establish a level two index to speed up the query ... key passive, delete the invalid key, and so on.

    15, memcached is how to do authentication. There is no identity authentication mechanism. Memcached is software that runs on the application's lower level (authentication should be the responsibility of the upper application). The memcached client and server side is lightweight, in part because the authentication mechanism is not implemented at all. In this way, memcached can quickly create a new connection, and the server side does not need any configuration.

If you want to restrict access, you can use a firewall or have memcached listen for UNIX domain sockets. 16, memcached of multithreading is what.

    How to use them. A thread is a law (threads rule). With the efforts of Steven Grimm and Facebook, Memcached 1.2 and later have multithreaded mode. Multithreaded mode allows memcached to take full advantage of multiple CPUs and share all cached data between CPUs. Memcached uses a simple locking mechanism to guarantee the mutual exclusion of data update operations. This approach can handle multi gets more efficiently than running multiple memcached instances on the same physical machine. If your system does not load heavily, you do not need to enable multithreaded mode of operation. If you are running a huge web site with large hardware, you will experience the benefits of seeing multithreading. For more information, see: Simply Summarize: Command parsing (Memcached spends most of the time here) can be run in multithreaded mode. memcached internal operations on data are based on a number of global locks (so this part of the work is not multithreaded).

Future improvements to multithreaded mode will remove a large number of global locks and improve the performance of memcached in highly loaded scenarios.

17. When designing applications, you can cache those content through memcached.

1 Caching Simple query results: The query cache stores the entire result set corresponding to a given query statement, which is most appropriate for caching the resulting set of queries to the query, such as loading specific filtered content, that are often used but not changed by SQL statements.
$key = MD5 (' SELECT * from Rest_of_sql_statement_goes_here ');
if ($memcache->get ($key)) {' Return $memcache->get ($key); '}else {'//Run ' query and transform T He result data into your final dataset form ' $result = $query _results_mangled_into_most_likely_an_array ' $m Emcache->set ($key, $result, TRUE, 86400); Store The result of the "query for a" ' Return $result '} Remember that if a query statement corresponds to a result set change, the resulting set does not show up.
    This method is not always useful, but it does make the work faster. 2 caching simple row based query results: A row-based cache checks the list of cached data keys, those in the cache can be fetched directly, rows that are not in the cache are fetched from the database and cached with a unique key, and finally joined to the final data set. Over time, most of the data will be cached, which means that the query statement will get more data rows from the memcached than with the database.
    If the data is fairly static, we can set a longer cache time.
    A row-based caching pattern is particularly useful for this search scenario where the dataset itself is large or the dataset is obtained from multiple tables, and the dataset depends on the query's input parameters but there is a duplicate portion between the result sets of the query. For example, if you have a data set of user a,b,c,d,e. You go to a page that shows the user a,b, E information. First, memcached gets 3 different keys, each corresponding to a user to go to the cache to find, all misses.
    You then go to the database and use SQL queries to get the data rows of 3 users and cache them. Now you're going to click on another page that shows c,d,e information. When you go to find memcached, the c,d data is not hit, but we hit E's data. Then the c,d row data are obtained from the database, and the memcached is slowed down.

Since then, no matter how these user information to arrange the combination, any information on the a,b,c,d,e of the page can be obtained from the memcached data. 3 caching is not just SQL data, you can cache the mostThe final part of the display page, to save CPU computing time such as making a page that displays user information, you may get a piece of information about the user (name, birthday, home address, profile), and then you may be able to convert the profile information in XML format into HTML format or do some other work. Instead of storing these properties separately, you might prefer to store the rendered blocks of data.

Then you can simply remove the preprocessed HTML directly to populate the page, which saves valuable CPU time. 18. Using a tiered cache memcached can handle a large amount of cached data at a high speed, but it is still considered to maintain a multi-tiered cache structure based on the system's circumstances. For example, in addition to the memcached cache, multilevel caching can be established through local caching (such as Ehcache, Oscache, etc.). For example, you can cache some basic data with a local cache, for example, small but frequently accessed data (such as product classification, connection information, server state variables, application configuration variables, etc.), it makes sense to cache the data and keep them as close to the processor as possible, which can help reduce the time to generate the page and

Reliability can be increased by memcached failure. 19. When updating the data, you need to update the cached user to edit their own information, when saving information to the database, you need to update the data in the cache or simply delete the old data. If you update the data immediately, prevent reading from the database of the data that you have just updated.

When a user habitually reload his or her user information to confirm that the modification succeeds, the data is taken out of the cache, and they get the latest data.

What are the advantages of memcache:1 and memcached? 1) distributed. 2 A single point of access relative to the memory of the application server.

3) Strong performance.

2, not very suitable for the use of memcached cache. 1 if the value is particularly large, it is not suitable. Memcache only supports 1M value under the default compilation. In fact, because of the process of serialization deserialization, it is not recommended to store very large data in memcache from a practical standpoint.

Memcache is suitable for output-oriented content caching, rather than processing-oriented data caching, which is not appropriate to put large chunks of data into the processing and then put back, but it is suitable to take out directly to the output or take out without the need to deal with direct use. 2 If it is not allowed to expire, it is not suitable.

Memcache has a maximum expiration of 30 days by default, and it reclaims the least recently used data after memory has reached the limit of usage.

    3, the process of clearing part of the cached data. Namespaces can be used (key prefixes can be used in memcache context)Instead, such as setting the key to "subsystem name + entity name + Entity ID" to achieve, to distinguish between different types of cached content, so that when necessary to clear a certain type of cache.

    4, the organization of value. Mainly involves the granularity of the data being cached, such as to save a data table, a row of data stored in a key value or unified to save as a key value.

If the data is stored in a small size, it is best to obtain the time to bulk acquisition, while saving can also be stored in bulk, that is, the number of calls across the network the less the better.

    5, the memcache of the key in the Convention and naming norms? The first: Generally the project name + character constants (Entity name or table name, etc.) + return PO ID (or the only mark can be). This method code is usually embedded into the service, which destroys the service logic and has high coupling.

    Consider adding a layer between the action layer and the service layer to reduce coupling. The second type: You can use spring AOP to intercept the service you want to cache, the unique key can be composed of class name + method name + parameter name, etc. this method is suitable for sub module development because all the methods in the same class are invoked, but interceptors can also affect performance to some extent.

    But it can improve the efficiency of development, there is not to break the service layer business logic. Third: +id (or query conditions) with SQL statements. This method is not very good.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26-27--28 29---30 31--32 33 34 35 36 37 38-39 40 41 42 4 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 85, 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 1 19 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148-149 150 151 152 153 154 155 156 157 158 159 160 161 162 163-164 165
Related Article

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.