Introduction to Redis and comparison with memcached

Source: Internet
Author: User
Tags cas key string memcached

I. Redis

(1) Introduction:

Redis is an open source API that is written in ANSI C, supports the web, can be persisted in memory, key-value databases, and provides multiple languages. is one of the NoSQL databases.

Redis is a key-value storage system. Similar to memcached, it supports storing more value types, including string (string), list (linked list), set (set), Zset (sorted set-ordered collection), and hash (hash type). These data types support Push/pop, Add/remove, and intersection-set and difference sets, and richer operations, and these operations are atomic. Based on this, Redis supports sorting in a variety of different ways. As with memcached, data is cached in memory to ensure efficiency.  The difference is that Redis periodically writes the updated data to disk or writes the modified operation to the appended record file, and Master-slave (Master-Slave) synchronization is implemented on this basis. Redis is a high-performance Key-value database. The emergence of Redis, to a large extent, compensates for the lack of memcached such key/value storage, in some cases can be a good complement to the relational database. It provides clients such as Java,c/c++,c#,php,javascript,perl,object-c,python,ruby,erlang, which is convenient to use. Redis supports master-slave synchronization. Data can be synchronized from the primary server to any number of slave servers, from the server to the primary server that is associated with other slave servers. This enables Redis to perform single-layer tree replication. You can write to the data intentionally or unintentionally. Because of the full implementation of the publish/subscribe mechanism, you can subscribe to a channel and receive a complete message release record from the master server when the tree is synchronized anywhere from the database.  Synchronization is helpful for the scalability and data redundancy of read operations. (2) Common commands: In DB, Redis scores have been amazing, and do not say Memcachedb and Tokyo cabinet, said the original memcached, the speed seems to reach only this level.Redis is simply using memory storage, the key to persistence is these three instructions: SAVE BGSAVE lastsave ... When the Save command is received, Redis dumps the data into a file. What is worth saying is its exclusive function: storing lists and collections, which is a more competitive place than MC. Does not introduce the contents of the MC, only the Special: type key-used to get a key of the types keys pattern-match all matching mode key, such as keys * list all keys, of course, the complexity O (n) randomkey- Returns a random Keyrename Oldkeynewkey-key can also rename a list operation, Essence Rpush key string-Add a value to the end of a key list Lpush key string-Add a value to a key list header Llen key-list length Lrange key start end-Returns the value of a range in the list, equivalent to the paging query in MySQL ltrim key start end-keep only a range of values in the list lindex key index-get the list of specific The value of the index number, be aware of the O (n) Complexity LSet key index value-The value of a position in the list Lpop Keyrpop key-and the above Lpop, which is like a stack or a queue of the kind of header take-up instructions, can be used as Message Queuing to use the collection operation Sadd Key member-add element srem key member-Delete element SCard key-return collection size Sismember key member-determine if a value is Sinter key1 Key2 in the collection ... keyn-get more The intersection element of a collection smembers key-lists all elements of the collection and multiple DB commands, which can be replaced by DB, the data can be isolated, and the default is stored in DB 0.

Two. Redis vs. memcached comparison

Many NoSQL blossom, how to choose

In recent years, the industry has been emerging many kinds of nosql products, so how to properly use these products, maximize their strengths, is that we need to further research and think about the problem, in fact, the most important thing is to understand the positioning of these products, and understand the tradeoffs of each product , in the practical application to achieve weaknesses, in general, these nosql mainly used to solve the following problems

1. Small amount of data storage, high-speed read and write access. This kind of product through the data all In-momery way to guarantee the high speed access, simultaneously provides the data landing function, actually this is the Redis most main application scenario.

2. Massive data storage, distributed system support, data consistency guarantee, convenient cluster node Add/delete.

3. The most representative of this is the ideas set out in the 2 essays by Dynamo and BigTable. The former is a completely non-central design, the node through the gossip way to pass the cluster information, the data to ensure the final consistency, the latter is a centralized scheme design, through a similar distributed lock service to ensure strong consistency, data written to write memory and redo log, The periodic compat are then merged onto the disk, and the random write is optimized for sequential writes, which improves write performance.

4.Schema free,auto-sharding and so on. For example, some of the common document databases are support Schema-free, directly store JSON format data, and support functions such as auto-sharding, such as MongoDB.

In the face of these different types of nosql products, we need to choose the most appropriate product based on our business scenario.

Redis application scenario, how to use it correctly

As already analyzed, Redis is best suited for all data in-momory scenarios, although Redis also provides persistence, but actually more of a disk-backed function, compared to the traditional meaning of persistence there is a big difference, then you may have questions, It seems that Redis is more like an enhanced version of memcached, so when to use memcached, when to use Redis?

If you simply compare the differences between Redis and memcached, most of them will get the following ideas:

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

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

3 Redis supports data persistence, which keeps the in-memory data on disk and can be loaded again when it is restarted.

Aside from this, you can delve into the internal structure of Redis to see more essential differences and understand the design of Redis.

In Redis, not all data is stored in memory all the time. This is one of the biggest differences compared to memcached. Redis only caches all key information, and if Redis finds that memory usage exceeds a certain threshold, it will trigger swap operations, and Redis is based on "swappability = Age*log (size_in_memory)" Figure out which key corresponds to the value that requires swap to disk. The value corresponding to these keys is then persisted to disk and purged in memory. This feature allows Redis to maintain data that is larger than the memory size of its machine itself. Of course, the memory of the machine itself must be able to maintain all the keys, after all, the data will not be swap operations. Also, since Redis swaps the in-memory data to disk, the main thread that provides the service and the sub-thread that is doing the swap will share this memory, so if you update the data that needs swap, REDIS will block the operation until the sub-thread completes the swap operation before it can be modified.

Use the Redis-specific memory model before and after the case comparison:
VM off:300k keys, 4096 bytes values:1.3g used
VM on:300k keys, 4096 bytes values:73m used
VM off:1 million keys, bytes values:430.12m used
VM on:1 million keys, bytes values:160.09m used
VM on:1 million keys, values as large as you want, still:160.09m used

When reading data from Redis, if the value of the key being read is not in memory, then Redis needs to load the data from the swap file before returning it to the requester. There is a problem with the I/O thread pool. By default, Redis will be blocked, that is, all swap files will be loaded before the corresponding. This strategy has a small number of clients and is appropriate for batch operations. However, if you apply Redis to a large web site application, this is obviously not sufficient for large concurrency scenarios. So Redis runs we set the size of the I/O thread pool, and concurrently operates on read requests that need to load the corresponding data from the swap file, reducing blocking time.

If you want to use Redis in an environment of massive data, I believe it is essential to understand the memory design and blocking scenarios of Redis.

Complementary points of knowledge:

Comparison of Memcached and Redis

1 Network IO Model

Memcached is a multi-threaded, non-blocking IO multiplexing network model, divided into the main thread and the worker sub-thread, listening thread listening network connection, after accepting the request, the connection description Word pipe to the worker thread, read/write IO, the network layer using the Libevent encapsulated event Library , multithreading model can play a multi-core role, but the introduction of the cache coherency and lock problem, for example, memcached most commonly used stats command, the actual memcached all operations to the global variable lock, count, etc., resulting in performance loss.

(memcached network IO model)

Redis uses a single-threaded IO multiplexing model, which encapsulates a simple Aeevent event processing framework that implements Epoll, Kqueue, and select, which can be used to maximize the speed advantage for purely IO operations. However, Redis also provides some simple computing functions, such as sorting, aggregation, etc., for these operations, the single-threaded model can actually seriously affect the overall throughput, CPU calculation process, the entire IO schedule is blocked.

2. Memory management aspects

Memcached uses a pre-allocated pool of memory to manage memory using slab and chunk of different sizes, item selects the appropriate chunk storage based on size, the way memory pools can save the cost of requesting/freeing memory, and can reduce memory fragmentation, But this approach also leads to a certain amount of wasted space, and when memory is still large, new data may be rejected for reference to Timyang's article: http://timyang.net/data/Memcached-lru-evictions/

Redis uses on-site memory storage to store data, and rarely uses free-list to optimize memory allocation, and there is a degree of memory fragmentation, and the Redis data store command parameters, which store the time-to-date information separately, and call them temporary data. Non-temporary data is never removed, even if there is not enough physical memory, so that swap will not eliminate any non-temporal data (but will attempt to eliminate some temporary data), which is more appropriate for Redis as storage instead of the cache.

3. Data consistency issues

Memcached provides a CAS command that guarantees consistency of the same data for multiple concurrent access operations. Redis does not provide CAS commands, and this is not guaranteed, but Redis provides the functionality of a transaction that guarantees the atomicity of a sequence of commands and is not interrupted by any action.

4. Storage methods and other aspects

Memcached basically only supports simple key-value storage, does not support enumeration, and does not support persistence and replication functions.

In addition to Key/value, Redis supports numerous data structures such as list,set,sorted Set,hash, which provides the keys

enumeration, but not on-line, if you need to enumerate online data, Redis provides tools to scan its dump files, enumerate all the data, and Redis also provides the functionality of persistence and replication.

5. Client support for different languages

Memcached and Redis have rich third-party clients to choose from for different language clients, but because memcached has been developing for a longer period of time, many of Memcached's clients are more mature and stable in terms of client support at present. and Redis because its protocol itself is more complex than memcached, plus the author constantly add new features, and so on, the corresponding third-party client tracking speed may not catch up, sometimes you may need to make some changes on the basis of third-party clients to better use.

According to the above comparisons it is not difficult to see that when we do not want the data to be kicked out, or need more data types other than Key/value, or need to use the landing function, using Redis is more appropriate than using memcached.

Some of the peripheral features of Redis

In addition to being stored as storage, Redis also provides some other functions, such as aggregation calculation, pubsub, scripting, etc., for such functions need to understand its implementation principle, clearly understand its limitations, can be used correctly, such as pubsub function, This is actually not supported by any persistence, the consumer connection between the flash or the reconnection between the message is all lost, and such as aggregation calculation and scripting and other features are limited by the Redis single-threaded model, it is impossible to achieve high throughput, need to use caution.

In general, the Redis author is a very diligent developer who can often see that the author is experimenting with a variety of new ideas and ideas, and that the functionality of these areas requires that we need to know more about them before using them.

Summarize:

The best way to use 1.Redis is to in-memory all data.

2.Redis more scenes are used as substitutes for memcached.

3. It is more appropriate to use Redis when more data type support is required other than key/value.

4. Using Redis is more appropriate when the stored data cannot be excluded.

The above information is transferred from: http://gnucto.blog.51cto.com/3391516/998509

Introduction to Redis and comparison with memcached

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.