Redis beginners (1) -- Understanding Redis and redis beginners

Source: Internet
Author: User
Tags map data structure redis server

Redis beginners (1) -- Understanding Redis and redis beginners

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 ( ).

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 ( ).

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


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

How to make the Internet access to the redis cache service, now only 127001 access is allowed

Comment out the bind in the configuration. Don't forget to restart redis.

Java Development 20: Redis in the Real World: How does Redis defeat memcached in applications that contain a large number of read operations?

In addition, I have discussed common server-based data storage, such as MongoDB and CouchDB. Each data storage has its own advantages and disadvantages, especially when used in a specific field. This Java Development 2.0 focuses on Redis, a lightweight key-value pair data storage. Most NoSQL implementations are essentially key-value pairs, but Redis supports a wide range of value sets, including strings, lists, sets, and hashes. Therefore, Redis is generally called a Data Structure server. Redis is also known for its exceptional speed, making it the best choice for a specific type of use cases. When we want to know something new, it may be helpful to compare things we are familiar with. Therefore, we will start Redis exploration by comparing them with memcached. Next we will introduce the main functions of Redis, which can make it better than memcached in some application scenarios. Finally, I will show you how to use Redis as a traditional data storage for Model objects. Redis and memcached Memcached are well-known Memory Object cache systems that run by importing target keys and values into the memory cache. Therefore, Memcached can avoid I/O costs when reading a disk. Pasting memcached between a Web application and a database produces better read performance. Therefore, Memcached is a good choice for applications that require fast data query. One example is the stock query service. You need to access the database to obtain relatively static data, such as the stock name or price information. MemcacheDB is unfair to Redis and memcached. It is much better than MemcacheDB. MemcacheDB is a Distributed Key-Value Pair storage system designed for data persistence. MemcacheDB is similar to Redis. Its new advantages allow it to easily communicate with clients implemented by memcached. However, memcached also has its limitations. one fact is that all its values are simple strings. Redis, as a substitute for memcached, supports a richer set of functions. Some benchmarks also indicate that Redis is much faster than memcached. Redis provides a wide range of data types that allow it to store more complex data in the memory, which cannot be achieved using memcached. Unlike memcached, Redis can persist its data. Redis solves a major cache problem, and its rich feature set finds other uses for it. Because Redis can store data on disks and replicate data across nodes, it can be used as a data warehouse in traditional data mode (that is, you can use Redis, just like using RDBMS ). Redis is also often used as a queue system. In this case, Redis is the basis for backup and work queue persistent storage (using the Redis list type. GitHub uses this method to prepare Redis for a large-scale infrastructure example using Redis. Start now! To start using Redis, you need to access it through local installation or hosting vendor. If you use a MAC, the installation process may not be that simple. If you are using Windows ??, Install Cygwin first. If you are looking for a hosting vendor, Redis4You has a free plan. Regardless of the access method, you can perform operations according to the following examples in this article. However, it may not be a good cache solution to use a hosting vendor for caching, network latency may offset any performance advantage. You need to use commands to interact with Redis. That is to say, there is no SQL query language. Working with Redis is very similar to using a traditional map data structure, that is, all the other full texts>

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