This is the 7th day of my participation in the August Gwen Challenge.

preface

Redis is a kind of non-relational NoSql database with fast running speed and good concurrency. Because of its excellent read and write performance, persistency, rich data type, single thread, automatic expiration of data, publish and subscribe, and distributed characteristics, it makes many application scenarios in Web development.

basis

Advantages of Redis

  • Excellent read/write performance, Redis can read 110,000 times /s, write 81,000 times /s.
  • Supports data persistence and AOF and RDB persistence modes.
  • Transactions are supported, all Redis operations are atomic, and Redis also supports atomic execution of several combined operations.
  • In addition to string values, it supports hash, set, zset, and list data structures.
  • Supports master/slave replication. The master machine automatically synchronizes data to the slave machine, enabling read/write separation.

Redis shortcomings

  • Redis does not have automatic fault tolerance and recovery functions. The breakdown of the slave host will lead to the failure of some front-end read and write requests, which can be recovered only after the machine restarts or manually changing the front-end IP address.
  • When the host is down, some data cannot be synchronized to the secondary host in a timely manner. After the IP address is switched, data inconsistency may occur, which reduces system availability.
  • Redis is difficult to support online capacity expansion, which can be complicated when the cluster capacity reaches its maximum. To avoid this problem, o&M personnel must ensure sufficient space when the system goes online, which wastes a lot of resources.

application

The cache

Because of its excellent read and write performance, cache is the most common application scenario of Redis, and because Redis supports transactions, it can effectively ensure the consistency of data. For popular data with low update frequency, cache is of great significance. For example, the classification column update frequency is not high, but the vast majority of pages need to access this data, so the reading frequency is quite high, we can consider implementing cache based on Redis.

A distributed lock

The difference between Redis and memcached, in addition to a richer data structure and persistence, is that Redis is single-threaded. Because Redis is single-threaded, distributed locking is also an important application scenario, and because of redis performance, it does not become a bottleneck. Its implementation relies on setnx, EXPIRE, and DEL commands.

timeliness

Redis data can be set automatic expiration time, this feature is also very practical, the automatic expiration feature brings a lot of convenience to development, no need to go to the database to compare the time, for example, verification code only 60 seconds of validity, can not be used after the time, or oAuth2-based Token can only be used once within 5 minutes. It cannot be used over time.

counter

The need for data statistics is very common, keeping counts by atomic increments. For example, the number of applications, resources, likes, favorites, shares and so on. You can write redis first and then periodically write mysql.

Access to the frequency

To reduce the strain on the server or prevent malicious flood attacks, you need to control the frequency of access, such as limiting the maximum number of visits to an IP address at a time. The IP address and other information of the visitor are used as the key, and incR method is used.

The message queue

Redis can be used as a good message queue, relying on the List type to add data to the head of the List using LPUSH and fetching elements from the tail of the List using BRPOP. Meanwhile, there are many mature message queuing products on the market, such as RabbitMQ. Therefore, RabbitMQ is more recommended as the messaging middleware.

Session cache

In cluster mode, the session replication function provided by the container can be generally used when there are not many applications. When the number of applications increases and the system is relatively complex, session services centered on the memory database such as Redis are generally set up. Session is no longer managed by the container. Instead, it is managed by the Session service and an in-memory database.

Social list

Liking, treading, following/being followed, and mutual friends are the basic functions of social networking sites. Generally, the traffic volume of social networking sites is relatively large, and the traditional relational database type is not suitable for storing this type of data. The data structure provided by Redis such as hash and collection can conveniently realize these functions.

Record user decision information

There is also a common need to record user decision information to see if a user has performed an action. For example, whether the user likes, whether the user favorites, whether the user shares and so on.

Intersection, union, and difference sets

In some scenarios, such as social scenarios, it is very convenient to realize social relationships such as common friends, common concerns and common preferences through intersection, union and difference set operations.

Hot lists and leaderboards

Sort by score, for example, showing a list of hottest, most viewed, most active, etc.

The latest development of

The latest trends in chronological order are also a good use for sorting Unix timestamps using the fractional weight of the Sorted Set type.

conclusion

Although Redis has no indexes, no foreign keys, and no schema compared to relational databases, multi-conditional queries need to be implemented indirectly through collection inlines (Sinter, Zinterstore) and joins, making it difficult to operate in some scenarios. But because of its own has a wealth of excellent characteristics (read/write performance, be persistent, rich data types, single thread, automatic expiration, publish-subscribe, distributed data), make it in the web development also has very many application scenarios, and other relational database, a lot of time there will be a surprise effect.

With Redis’s 47 combos, try to see how many common Redis scenarios you can parse