Understanding Redis (1)

Source: Internet
Author: User
The official Redis website defines Redis as "Redisisanopensource, BSDlicensed, and advancedkey-valuecacheandstore". We can see that Redis is a key system that can be used to cache or store data. Redis is short for "RemoteDictionaryServer" (Remote dictionary service) and provides a string)

The official Redis website defines Redis as "Redis is an open source, BSD licensed, advanced key-value cache and store". We can see that Redis is a key system, it can be used to cache or store data. Redis is short for "Remote Dictionary Server", which provides a string)

The official Redis website defines Redis as "Redis is an open source, BSD licensed, advanced key-value cache and store". We can see that Redis is a key-value system, it can be used to cache or store data. Redis is short for "Remote Dictionary Server". It provides string, list, hash, and set) and sorted set. These data structures make it an easy-to-use key-value system.

As the simplest data structure provided by Redis, strings and Common commands are shown in:


It can be seen that the Redis string type is actually similar to the map in C ++, and a key corresponds to a value. The list type is a relatively advanced data structure, as shown in Common commands:

A list is actually implemented by a two-way linked list. Therefore, it is highly efficient to insert or delete data at both ends of the list. It can be used to implement the "new" function similar to Weibo and Renren. A more interesting structure provided by Redis is called hash. Its Common commands are shown in:


A hash is actually a set of key-value pairs. The hash key itself is a "parent key", and several fields contained in the hash are "subkeys ", each "subkey" has a corresponding value. There is no relationship between subkeys, but they constitute a complete hash structure. We can regard a hash as a row in a relational database. Each subkey in the hash corresponds to a field in the row. Therefore, when data in a relational database is cached in Redis, it may be convenient to use the hash structure. Note that there is no sequential relationship between subkeys in the hash structure. Another unordered structure provided by Redis is a set, as shown in Common commands:


The data in the set is unique and unordered. The collection is implemented in a hash table in Redis, so the time complexity of the insert and delete operations is O (1 ). Redis provides computation commands for the set, such as intersection and other operations, which are very convenient to use. Another data structure closely related to a set is an ordered set, as shown in Common commands:


The biggest difference between an ordered set and a set is that each data in an ordered set has a corresponding score, and the elements in the set are sorted by these scores. From the perspective of structural functions and usage, ordered sets may be the most advanced of the five data structures provided by Redis.

Redis is a memory database, which makes it outstanding in terms of data access efficiency. Because data in the memory is at risk of loss at all times, Redis provides two persistence mechanisms to promptly write data in the memory to the hard disk. The first mechanism is RDB, which regularly writes memory data to the hard disk by using snapshot storage; the second mechanism is AOF. Every time you execute a command to change Redis data by writing logs, add the command to the log and save it to the hard disk. By default, apsaradb for Redis enables the RDB mode and the AOF mode. This is because AOF is more time-consuming. However, if data security requirements are extremely high and cannot bear any consequences of data loss, the AOF mode becomes the first choice for persistence.

Of course, even if Redis has a persistence mechanism, once the local hard disk is damaged, data loss is inevitable. Therefore, Redis also provides the replication function to automatically synchronize data from a master database to multiple slave databases to prevent data loss as much as possible. The master-slave mechanism of Redis can be described as follows: after the database is started, the SYNC command is sent to the master database; after the master database receives the SYNC command, the snapshot is saved. During this period, all commands sent to the master database are cached. After the snapshots are saved, the master database sends the snapshots and the cached commands together to the slave database. The master database saves the snapshot files sent from the master database, execute the cache commands sent from the primary database in sequence. In the synchronization process, the slave database will not be blocked. By default, it uses the data before synchronization to continue responding to commands sent from the client.

Transactions are one of the features provided by Redis. First, use the MULTI command to notify Redis. the following commands belong to the same transaction. Then, enter several commands, which are put into the command queue and not executed immediately. Finally, use the EXEC command to notify Redis that all commands belonging to the same transaction have been input and are now executed. All or all valid commands of the same transaction are executed, or are not executed, and other commands are not inserted during execution. Pipeline is another feature provided by Redis. This feature enables Redis to receive multiple commands at a time and then return results at a time after execution. This can reduce the number of communications between the client and the Redis server, thus reducing the round-trip latency.

In addition to transactions and pipelines, Redis also provides many other features, such as setting the key expiration time and using the BLPOP/BRPOP command to implement priority queues, use the PUBLISH/SUBSCRIBE command to SUBSCRIBE to and PUBLISH messages.

Redis is written by ansi c and runs on Most POSIX systems including Linux and OS X. the officially recommended operating system is Linux. Redis does not have an official Windows version, but Microsoft has ported and maintained a Redis that can run on Windows and put it on github (https://github.com/MSOpenTech/redis ).

Redis programming interfaces are called clients. Most mainstream programming languages have officially recommended clients. :

Databases and Redis have been used in practice by many companies including Sina Weibo, zhihu, Stackoverflow, blizzard, and Flickr.

Note:-5 are from Redis in Action, Josiah L. Carlson, Manning Publications, 2013

References:

1. Li Zihua, Redis Getting Started Guide, people's post and telecommunications press, May 2013 version 1st

2. Josiah L. Carlson, Redis in Action, Manning Publications, 2013

3. Redis official website http://www.redis.io

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.