1. Background information
In most Web applications, data is saved to a relational database, where the WWW server reads the data and displays it in the browser. However, with the increase of data volume and the concentration of access, the burden of relational data, the slow response of database, and the delay of website opening can be seen.
Reduce the number of data accesses by caching database query results in memory to improve the speed of dynamic Web applications and improve the concurrency and scalability of the site architecture
In the traditional development of the database is the most of MySQL, with the data volume tens of millions or billions of levels, its relational database reading speed may not meet our demand for data, so the memory-based cache system appears
2. Knowledge Analysis
Memcache is a high-performance distributed memory object caching system for dynamic Web applications to mitigate database load. It improves the speed of dynamic, database-driven Web sites by caching data and objects in memory to reduce the number of times a database is read.
Memcache is based on a hashmap that stores key/value pairs. Its daemon (daemon) is written in C, but the client can write in any language and communicate with the daemon through the Memcache protocol.
Redis has more complex data structures and provides atomic manipulation of them, which is a different evolutionary path from other databases. The data types of Redis are based on basic data structures and are transparent to programmers without the need for additional abstraction.
Redis runs in memory but can be persisted to disk, so it is necessary to weigh the memory when reading different datasets at high speed, and the amount of application data cannot be greater than hardware memory.
Another advantage of the in-memory database is that it is very simple to operate in memory compared to the same complex data structure on disk, so Redis can do a lot of things that are internally complex.
At the same time, they are compact in the form of disks, because they do not have to be randomly accessed.
3. Frequently Asked Questions
What is the difference between Redis and memcached?
What is atomicity and what is atomic operation?
4. Solution
Memcached
Internal data storage, using slab-based memory management, helps reduce the overhead of memory fragmentation and frequent allocation of memory destruction. Each slab dynamically allocates a page's memory on demand
(unlike the concept of 4Kpage, where the default page is 1M), page internal according to the size of different slab class is divided into memory chunk for the server to store KV key value pair use
The data structures within Redis will eventually be implemented in the form of Key-value, but from the point of view of the structure exposed to the user,
More than memcached, Redis also supports List,set, hashes,sorted Set and other data structures, in addition to the standard common-sense key-value pairs
Basic commands
memcached command or communication protocol is very simple, the command that the server supports is basically the addition, deletion, substitution, atom update, read, etc. of the specific key, including Set, Get, Add, Replace, Append, Inc/dec and so on.
memcached communication protocols include text format and binary format for simple network client tools (such as Telnet) and different requirements for clients with higher performance requirements
The Redis command provides basic operations similar to memcached on KV (string type), supports more data structures on other data structures as well as basic similar operations (and of course, the operations that are unique to these structures, such as set union,list POPs). To a certain extent, it means a wider range of applications.
In addition to the support of multiple data structures,
Redis offers a number of additional features compared to memcached, such as the Subscribe/publish command to support notification mechanisms such as the Publish/subscribe mode, etc. These additional features also help to expand its application scenario Redis client-server communication protocol in full text format (in the future possible inter-server communication will use binary format)
Distributed implementations:
(1) memcached distributed by the client, through the consistent hashing algorithm to ensure access to the cache hit rate; Redis distributed by the server-side implementation, through the service-side configuration to achieve distributed;
(2) transactional, memcached without the concept of transaction, but can be through the CAS protocol to ensure data integrity, consistency. Redis introduces transactional concepts in the database to ensure data integrity and consistency.
(3) Simplicity, memcached is a pure KV cache, the protocol is simple, learning and use cost is much smaller than Redis
Memcached also does not work on data persistence, but there are many memcached protocol-based projects that enable data persistence, such as Memcachedb using BerkeleyDB for data storage, but in essence it is not a cache Server, And just a protocol compatible with memcached Key-valuedata store.
Redis can configure the server in a master-slave manner, the slave node replica backup of the data, and the Slave node can act as a read only node to share the data read work
Redis built-in supports two persistence scenarios, snapshot snapshots and aof incremental log methods. The snapshot, as the name implies, is to dump the complete data in a file over a period of time. The aof increment log is a record of modifications to the data (in effect, each command itself that modifies the data).
5. Code combat 6. Expand Thinking
What is the essence of caching in Java?
The definition of computer cache: The cache is a part of the CPU, it exists in the CPU in this can be explained that the CPU is not likely to hold a large amount of data so Java cache is not the true meaning of the cache Java cache There are two: first, the file cache, refers to the data stored on disk, can be XML format, You can also serialize the file DAT format or other file formats. Second, the memory cache, that is, to implement a static map of a class, the map for general additions and deletions.
7. References
Http://www.redis.net.cn/tutorial/3512.html
http://blog.csdn.net/colorant/article/details/21089057
http://blog.csdn.net/wzqzhq/article/details/64920996
Redis differs from memcached.