Redis beginners (1) -- Understanding redis

Source: Internet
Author: User

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:


Figure 1

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:

Figure 2

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:


Figure 3

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:


Figure 4

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:


Figure 5

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. :


Figure 6

Redis does not have a C ++ client officially recommended, so it can be replaced by a C client. The officially recommended c client is hiredis, which can be found on GitHub (https://github.com/redis/hiredis ).

As a powerful and highly efficient nosql database, redis has been successfully used in practice by many companies including Sina Weibo, zhihu, stackoverflow, blizzard, and Flickr.

Note: Figure 1-5 is 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

Redis beginners (1) -- Understanding redis

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.