Redis really easy to use it

 

What is Redis

Redis is an open source use the underlying C language key-value store database. Can be used for caching, event publication subscriptions, high-speed queues scene. And support for rich data types: string (string), hash (hash), list (list), set (unordered collections), zset (sorted set: an ordered collection)

Redis application scenarios in the project

1, cache data

The most commonly used, often need to query the data and the changes are not very frequent often called hot data.

2, the message queue

Corresponds to the message subscribe system, such as ActiveMQ, RocketMQ. If there is a higher demand for data consistency, it is recommended to use MQ)

3, counter

Such as statistical hits, thumbs rate, redis be atomic, concurrency problems can be avoided

4, the electricity supplier website information

Large electronic business platform initialization data cached page. For example, where the network will be different when buying tickets and your home's price point into the price.

5, hot data

News sites such as real-time hot spots, the hot microblogging search, etc., require frequent updating. There is always a large amount of data from the database directly affect the performance of queries

A reason to love

  In single-node server we usually like this

  With the development of enterprises, business expansion. The face of vast amounts of data, the direct use MySql result in reduced performance, read and write data will be very slow. So we can deal with massive amounts of data to cache.

  So now we have this:

  FIG only briefly on the role of the cache, we need to continue to increase when the data from the copy by the main technologies to achieve separate read and write

  Database layer interact directly with the cache, if there is data in the cache is directly returned to the client, if not will go to the query from MySql. Thereby reducing the pressure on the database, increases efficiency.

  Usually released a new phone, there will be buying activity. The same time period, the server will receive a lot of orders request.

  We need to use atomic operations redis to achieve this "single-threaded." First, we have a list of inventory exists, it is assumed there are 10 stocks, go out into the number list push10, this number has no real meaning, just on behalf of 10 stocks. After buying, every arrival of a user, you pop a number from the list, indicating that the user buy success. When the list is empty, it said it had taken away. Because a pop operation is a list of atoms, even though many users simultaneously arrive, is performed sequentially

  Off-topic: There is a limit of buying directly from the front page request, these requests are intercepted directly to the front and not the back-end server

Why so fast Redis

1, Redis is a pure memory operation, we need to manually when needed persisted to hard drive

2, Redis is single-threaded, thus avoiding the frequent switching operation of the multithreaded context.

3, Redis simple data structures, the data operation is relatively simple

4, using different underlying model, application protocol for communication between the underlying implementation methods and between them and the client is not the same, Redis VM build their own direct mechanism, because most of the system call system function, it will waste some time to move and requests

5, the use of multiple I / O multiplexing model, non-blocking I / O

Multiple I / O multiplexing

  I / O multiplexing technology to solve technical process or thread is blocked to an I / O system calls that occur, can monitor multiple descriptors, once a descriptor is ready (usually ready for reading or writing is ready, before that file descriptor read and write operations), the program can be notified accordingly read and write

Data type scenarios Redis

  We mentioned earlier Redis supports five rich data types, then under different scenarios how can we choose?

String

  String is the most common data types, he can store any type of string, of course, including binary, JSON object oriented, even after the picture is base64 encoded. In Redis in a string of maximum capacity is 512MB, it can be said to be omnipotent.

Hash

  Commonly used for data storage structure, such as forums system can be used to store user information Id, nickname, avatar, integral like. To change the information therein, only a value of an item deserialization modified Value taken by Key, and then stored in the sequence of the Redis, Hash storage structure, since the structure of Hash be insufficient in a certain number of elements in a single Hash compression and storage, so you can save a lot of memory. This structure in the String does not exist.

List

  List is implemented as a doubly linked list that can support the reverse lookup and traversal, more convenient operation, but bring some extra memory overhead, a lot of internal implementation Redis, including the transmit buffer queue data structure, etc. are also used . In addition, you can use lrange command, do the Redis-based paging functionality, excellent performance, good user experience.

Set

  Functions and features list set provided externally is similar to a list special is that is can be set to automatically de-duplication, when you need to store a list of data, do not want to duplicate data, this time you can choose to use the set.

Sorted Set

  Can be re-sorted by a right conditions, you can make data applications such as rankings by the number of clicks.

Redis cache data consistency

  Data and caching the true sense of the database data is impossible consistent, final data has been divided into two categories consistent and strong. If the business requirements for data must have been so strong you can not use the cache. Cache can do only ensure that the final data consistency.

  We can do is ensure data consistency as possible. Whether to delete the library and then delete the cache or to delete the cache and then delete the database, all data inconsistencies may occur because of concurrent read and write operations, we can not guarantee their order. Specific coping strategies still have to be determined according to business needs, not go into here.

Redis outdated and out of memory

  We can set an expiration time when his Redis data store. But the key is how to delete it?

  I think it is the beginning of a regular delete, and later found not the case, because if the timing delete, you need a timer continuously monitors the key, although memory freed, but very cpu consuming resources.

  Redis is used regularly delete expired deleted, the default is tested once every 100ms encountered expired key to delete the detected sequence is not detected here, but random testing. That it will not slip through the net? Obviously Redis also takes into account that, when we read / write a key has expired, it will trigger inert Redis deletion policy, directly back to get rid of outdated key

  Out of memory means is part of the key user store Redis can be automatically deleted, so that the situation can not find the data from the cache will appear. Join our server memory to 2G, but with the development of business data cache has exceeded the 2G. But this does not affect the operation of our program, because the operating system is visible memory is not limited physical memory. It does not matter the physical memory is not enough, the computer will draw a space from the hard drive as virtual memory. This is the scene of two applications designed Redis mind: caching, persistent storage

Cache breakdown

  A protective layer cache database just to relieve the pressure and added, when the query from the cache data we need not going to query the database. If hackers, frequent access to data in the cache is not, then the cache will lose the meaning of existence, the pressure moment for all requests fell on the database, this will result in the database connection exception.

solution:

  1, set the timer background task, the initiative to update the cache data. This program is easy to understand, but scattered when the key when the operation is quite complex

  2, hierarchical cache. For example two layers of protective buffer layer, level 1 cache invalidation time is short, level 2 cache invalidation for a long time. There is a request coming from the priority level 1 cache to find, if there is no corresponding data found in the level 1 cache, lock the thread, the thread and then take the data from the database is updated to level 1 and level 2 cache. Other threads are available directly from level 2 thread

  3, to provide a mechanism for interception, a series of internal safeguard the legitimate key value. When requested key is not legitimate, direct return.

Cache avalanche

  Refers avalanche cache buffer for some reason (such as down, cache service hung up or does not respond) integral crash out, led to a large number of requests reaches the back-end database, resulting in the collapse of the database, the system crashes, a disaster, which is above cache mentioned breakdown.

How to avoid an avalanche:

  1, to cache plus a random interval within a certain time to take effect, different key set different expiration time, avoid the same time a collective failure.

  2, and caching solutions similar breakdown, so secondary cache, a cache miss when reading data from the original copy of the cache.

  3, or using locking manner to avoid excessive queue server requests simultaneously read and write operations.

Epilogue

  Redis high performance, read speed is 110 000 times / s, write speed is 81000 times / s, support services, backup support, rich data types.

  All things are two sides, Redis is flawed:

  1, as is the in-memory database, a single machine so the amount of data storage is limited, developers need to advance estimates, the need for timely delete unnecessary data.

  2, when the modified Redis data needs to be persisted to hard drive data to rejoin the content, relatively long time, this time Redis is not working properly.

Guess you like

Origin www.cnblogs.com/heqiyoujing/p/11223519.html