Redis papers distributed before

First: acquaintance Redis

First, what Redis that?

Redis is an open source (BSD license), the use of ANSI C language, a data structure stored in the system memory, which can be used as a database, cache and messaging middleware. It supports multiple types of data structures, such as: strings (strings), hashes (hashes), lists (lists), the set (sets), ordered collection (sorted sets) and range queries, bitmaps, hyperloglogs and geospatial (geospatial) radius of the index query. Redis built-in replication (replication), LUA scripting (Lua scripting), LRU-driven events (LRU eviction), transaction (transactions) and different levels of disk persistence (persistence), and by Redis Sentinel (Sentinel) and automatic partitioning (Cluster ) provides high availability (high availability).

Two, Redis eight properties

1, fast

Redis is written in C-based memory-type single-threaded database compared to disk-based database type much faster data given official support 10W times of read and write operations per second.

2, endurance of

Redis persistence can ensure that the data in memory is saved to disk from time to time, the restart time will be reloaded into memory. There are persistent way RDB and AOF. (Later chapters will detail RDB and AOF)

3, supports a variety of data structures

Redis supports five basic data types: String (String), (Hash) hash, (List) list, (Set) collection, (Sorted Set) ordered set.

We can describe a picture at the image of the underlying data structures supported by Redis

Derived data structure:

BitMaps: Bitmap (for the number of active users, statistics, etc.)

HyperLogLog: ultra-small memory count unique values

GEO: Geographic Information Locator

4, support for multiple programming languages

Support Java, PHP, Python, Ruby, Lua, Nodejs.

5, feature-rich

Supports publish-subscribe, Lua script, things, Pipeline (pipeline, that is, when the instruction reaches a certain number, the client will be performed)

6, simple

Do not rely on external libraries, single-threaded, only Code 23000 rows.

7, master-slave replication support

Make a copy of the data of the master node, which is made highly available cornerstone.

8, high-availability, distributed

Redis-Sentinel (v2.8) supports high availability, Redis-Cluster (v3.0) supports distributed.

Three, Redis typical application scenarios

1, the cache system

Redis cache in general are often used in the development can be divided into two categories, one hot data cache, and second, permission authentication information cache.

Hot data cache: for often questioned, but not frequently modified or deleted data cache, such as: classification of goods, user's basic information, organization information, full page caching.

Session Sharing: PHP Session is saved in the default file server, if the cluster service, the same user may fall over on different machines, which can lead to frequent users logged Redis Save Session adopted, regardless of the user station that fall the machines can obtain the information corresponding to the Session.

API anti-replay: In order to prevent illegal user interface is intercepted for replay attacks, replay attacks What is it? Is to re-send your request intact once, twice ... n times, the normal requests are entered into the normal logic validation to prevent the replay we generally use Redis cache recording the first visit, if the request is intercepted, the second request will be deemed illegal request occurs, generally used with anti-replay and URL signature, subsequent chapters will tell you how to achieve specific.

2, counter / governor

Command: INCRBY

Since self-energizing INCRBY is atomic, concurrency problems can be avoided, to ensure no errors, and 100% millisecond performance.

Counter: you can count the number of similar praise user point user access number, forwarding the article number, the number of video playback, comments and more, if such operations has brought considerable pressure with the MySQL database will give real-time read and write, usually the first place Redis with MySQL script timed to write back;

Governor : typical usage scenario is to limit a user's access to a frequency API

E.g:

1, when to buy, to prevent the user clicks crazy unnecessary pressure;

2, users need to enter the phone code when the user logs in order to determine whether it is the users themselves, but in order not to short message interface is accessed frequently, it will limit the frequency of users to obtain a verification code every minute, for example, no more than five times an hour.

3, leaderboard

Ordered set of typical usage scenario is the ranking system, such as video sites require users to upload video to do list, list of dimensions may be multiple areas: according to time, in accordance with the number of players, according to the number obtained praise.

Top plate scenario key is how the splice Score , S Core type double , we can take advantage of this, assembling a floating-point number, the value of the integer part of the heat, the fractional part of the time , to be noted here that, R & lt EDIS accuracy which it should be a decimal six .

4, the message queue

In addition to its own Redis publish / subscribe model, we can also use List to implement a queuing mechanism, such as: notice of arrival, demand to send the message class, does not require high reliability, but will bring a very large DB pressure, can List accomplished using asynchronous decoupling;

5, social networks

Some commands use a set of features, such as seeking the intersection, union, difference and so on, you can easily get some common friends, common interests such functions;

6, the bit operation (large data processing)

Scenario for millions of the amount of data, such as sign hundreds of millions of users of the system, to re-logins statistics, whether a user is online, and so on.

Think about one billion users, within a few milliseconds to a query to a user is online, how can you do? Never say that the establishment of a key to each user, and then one by one in mind (you can count the required memory will be terrible, and this is similar to a lot of demand, Tencent light this much to spend much money ..) All right. Here in place operational use - use setbit, getbit, bitcount command.

The principle is:

Redis construct within a sufficiently long arrays, each array element is only two values ​​0 and 1, then the subscript index array used to represent our example above which a user id (Ha must be a number), then it is clear this length of hundreds of millions of large arrays will be able to build a memory system via subscript and element values ​​(0 and 1), I said above, several scenarios will be realized.

7, SETNX achieve distributed lock

Now a lot of distributed application scenarios, in order to maintain data consistency, often encounter situations require resources locked by the characteristics of single-threaded implementation of distributed lock Redis is one of them implementations.

SETNX use characteristics, it is easy to think, when needed locked, calls SETNX command, if the returns 1, the setting is successful, access to the current lock, then do something you want, call the DEL command to release the lock after the completion of .

8, avoid spike oversold

To solve the "oversold" The problem, is to ensure that the core operation checking inventory is performed sequentially, said the image is the "multi-threaded" turn into "single-threaded." Even if there are many users arrive simultaneously, it is also a check and give a buy qualifications, once the inventory to usurp the back of the user can not go on.

We need to use Redis atomic operations to achieve this "single-threaded." First we exist stock goods: 1 this list, assuming that there are 10 stocks, go out into the number list push10, this number has no real meaning, represent just one stock. After buying, every arrival of a user, it is the goods: a number of pop 1, means 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, it is performed sequentially.

Four,  the Redis and M emcached Comparative

1, all values ​​are simple strings Memcached, Redis support richer data types

2, Redis can persist its data

3, Redis supports the backup data, i.e., data backup master-slave mode.

4, using different underlying model, communication between the underlying application protocol implemented between the client and the way they are 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, a single maximum limit value Redis is 1GB, the data can be recorded and Memcached in 1MB.

6, Memcache in concurrency scenarios, cas can ensure consistency, and Redis transaction support is relatively weak, only to ensure that each successive execution of operations in the transaction.  

7, Memcached Redis memory management not so complicated, metadata metadata smaller, relatively speaking, very little overhead. Memcached only supported data type is a string string, is ideal for read-only data cache, because the string does not require additional treatment.

 

Guess you like

Origin www.cnblogs.com/xingxia/p/redis_distribute.html