Redis resolve common scenario

I. Introduction

Redis is a key-value storage systems, now used in a variety of systems more and more, in most cases because of its high-performance characteristics, are treated as cache use, introduced at Redis usage scenarios often encountered here.

Two characteristics Redis

A product usage scenario certainly need to characteristics of the product, the first list what features Redis:

  • Good read and write performance
  • Endurance of
  • Rich data types
  • Single-threaded
  • Data is automatically expired
  • Publish and subscribe
  • distributed

Here we are a few scenarios, different dimensions of said application under Redis

Three scenarios

Suitable as high-performance cache

Redis cache is the most common scenarios, so use it all, mainly because of excellent Redis read and write performance. And there is a growing replace memcached, as the preferred server-side caching components. Moreover, the internal affairs of Redis is supported in the use of time can effectively ensure data consistency. Use as a cache, there are two ways to save data:

  • 1, before reading, reading the Redis go to, if there is no data, reading the database, the pull-up data into Redis.
  • 2, insert the data, while write Redis.

Option One: simple to implement, but there are two caveats:
1, avoid caching breakdown. (No need to hit the database data has not led to Redis data, and has been hitting the database.)
2, real-time data will be relatively narrowly.

Option Two: real-time data is strong, but not easy to develop a unified processing. .

Of course, two ways to apply the actual situation. Such as: a program suitable for real-time data requirements are not particularly high scene. Option II applies to dictionary table, the amount of data stored data.

More rich data format, feature-rich application scenarios

Redis compared to other caches, there is a very big advantage, it is to support multiple data types.

Description string data type string, the simplest kv hashhash storage format, value for the field and value, for such a scenario ID-Detail. list simple list, order list, or the first end support insert data set unordered list, fast lookup for intersection and union, difference processing the sorted set ordered set

In fact, by the above data types of features, the basic can think of a suitable application scenarios.

  • Suitable string-- simplest kv storage memcached similar storage structure, and message authentication code, configuration information, etc., to use this type of memory.
  • hash-- general key for the ID or unique identifier, value that corresponds to the details. Such as commodity information, personal information details, news and other details.
  • Because list-- list are ordered, and more suitable for storing data corresponding fixed number of ordered data. As provinces tables, dictionary tables and so on. Because the list is ordered, sorted according to the time for writing, such as: *** The latest, message queues, and so on.
  • set-- can be simply understood as ID-List mode, such as a micro-blog in which man friends, where the cattle is that SET, SET may provide the intersection of the two, and union, difference operation. For example: Find two people together friends and so on.
  • Sorted Set-- is an enhanced version of the set, and adds a score parameters will be automatically sorted based on the value of the score. Not similar to other more suitable to sort data according to the time of insertion top 10.

As mentioned above, although not as a relational database Redis less complex data structures, but also for a lot of scenes, to more than the average cache data structure. Understanding of data structures for each business scenario, not only help enhance the development efficiency can be effectively utilized in the performance of Redis.

It can be used as single-threaded distributed lock

Speaking difference Redis and Memcached, we are talking about more data structures and persistence of these two characteristics, in fact, there is a relatively large difference is:

  • Redis is single-threaded, improve the processing efficiency multiplexing.
  • Memcached is multi-threaded, CPU processing efficiencies through a thread switch.

So this feature Redis single-threaded, it is actually very important application scenarios, the most commonly used is a distributed lock.
Deal with highly concurrent systems, all with multi-server deployment, each technical framework for data lock has a very good approach, such as .net's lock, java's synchronized, by locking an object can respond to the thread lead the data pollution problems. But after all, this thread can only control server, distributed deployment of data pollution problems in the future, it is more difficult to handle the. Redis is single-threaded this feature, it is very consistent with this requirement, the following pseudo-code

// generate lock
 the while Lock! = 1
    // expiration time in order to avoid deadlock
    now = int(time.time())
    lock_timeout = now + LOCK_TIMEOUT + 1
    lock = redis_client.setnx(lock_key, lock_timeout)

// really want to deal with traffic
doing() 

// release the lock
now = int(time.time())
if now < lock_timeout:
    redis_client.delete(lock_key)

The above is only a pseudo-code description of the process, in fact, the whole logic is very simple, as long as the case when considering the deadlock, it is better to deal with. Redis as a distributed lock because of its performance advantages, and will not become a bottleneck, the bottleneck is usually true business process content, or try to narrow the scope of the lock to ensure system performance.

Automatic expiration can effectively enhance the development efficiency

Redis data can be set for the expiration time, this feature is we are more and more applications, without the use of outdated data cleaning party to pay attention, so development efficiency is relatively high, of course, performance is relatively high. The most common is: SMS verification code, with time-sensitive merchandise display and so on. Like database without the need to check the time even compare. Because the use of relatively simple, not go into details.

Distributed persistence and effective response to huge amounts of data and high concurrency

The official version is just the beginning of Redis supports stand-alone or simple master, most of the applications are to develop their own cluster middleware, but as more and more widely, the user calls about distributed more and more, so Redis 3.0 when the official version added support for distributed, mainly two aspects:

  • Redis hot standby server from the master, to ensure system stability
  • Redis fragmentation mass data and respond to high concurrency

And although Redis is a memory cache, data in memory, but supports a variety of ways Redis data persistence, written to disk, all, stability Redis data is very secure, with Redis cluster program, some systems have the Redis data store as a kind NoSql applicable.

 

Guess you like

Origin www.cnblogs.com/liuqingzheng/p/11080539.html