Redis Learning Series, the underlying data structure, bitmaps, distributed lock

A, the underlying data structures redis
redis all data structures are based on a unique key for the name and value data acquired by key, except that different data structures value of
five kinds of data structures:
String (String): the use of very broad, common that store user information.
Dynamic string is a string, the string can be modified using the redundant pre-allocated memory space by allocation within frequent
expansion 1MB is less than the length of a character string is doubled, the expansion is greater than a plus 1MB 1MB. The maximum length is 512MB
List (list): is the list, insert and delete time complexity is O (1), look for the O (n). Each element bidirectional pointers, both before and after may be conveniently
used to do using the asynchronous queue, the queue may be implemented (to ensure sequential access) and stack
substructure: list called fast (QuickList), uses less memory when a continuous list of elements storage, referred ziplist (compressed list)
of data will be allocated a plurality of long ziplist, then bidirectional pointer to string together, referred quicklist.
Advantages: Fast list satisfy both fast insertion and deletion performance, there will not be much of a data redundancy
hash (dictionary): and hash data structure is the same as "array + list", the hash value can only be redis string, rehash way for progressive policy
of progressive rehash: when will retain two hash rehash of old and new structures, queries simultaneously two hash structure queries, and then later in the timing of
tasks and hash operations, gradual old the content hash migrated to the new inside until the move is complete.
set (set): inside the key-value pair is disordered, only (with de-emphasis function)
zset (ordered set): Sort weight both to ensure the value of uniqueness (set), and will assign a score for each value represents the value of
the internal implementation: skip list, zset need to support random insertion and deletion, when a new insert elements the need to ensure an orderly, jumping a list of similar hierarchy
each element may exist with multiple levels, so that you can quickly jump between different levels

General rule (list, set, hash, zset ) container-type data structures:
A, if the container does not exist create one before proceeding
b, if the container element does not, then immediately remove the container, releasing memory
expiration time:
Redis the expiration time may be provided: an object expiration time as a unit, rather than a single key expiration time

Second, distributed lock
using a distributed lock redis:
the SET (seize lock) ---- "setnx (settings lock) -----" expire
under normal circumstances the client to seize the lock, and then use to complete the locks release (del command), so that in order to solve due to abnormal del not performed,
thus adding to lock expiration time, time to automatically execute, but if due to a fault between setnx expire and can not be performed so that expire will result in a deadlock
setnx execution of the former and the latter expire should be performed, similar to a transaction
redis solved was extended in order to set the parameters:
set Lock: EX. 5 to true codehole NX
del Lock: codehole
will expire and the atomic instruction setnx combined together
may be reentrancy: a lock can support the same thread repeatedly locking
three-latency Queuing
redis simple message queue, but there is no guarantee ack, if the message is the reliability of the most demanding, it is not suitable for use
asynchronous message queue:
Redis list is often used for asynchronous message queue, and using rpush lpush operations enqueue, lpop rpop and dequeue operations
support multiple producers and multiple A consumer concurrent incoming and outgoing messages, each consumer will get a list of the different elements of
the processing queue space-time: if the queue is empty when pop will fall into an infinite loop, it will consume resources on the client and server,
typically use sleep to make thread waits
blocking read: sleep can lead to increased delay, using blpop / brpop blocking read, so that the thread goes to sleep, wake up immediately when there is data
When the blocking connection redis client became idle connection time process server will be disconnected, blpop and brpop will throw an exception, the client needs to capture and try again
lock conflict management:
general three ways:
direct throw an exception , try again later notify the user (user experience)
SLEEP for a retry (not suitable for frequent operations)
will be transferred to the request queue delay, again after a while (for asynchronous message processing)

Fourth, the bitmap
practical application records the user's attendance records, if using an ordinary key / value, such that the storage space will increase
redis provides a bitmap, each occupied by a record, i.e., byte array
1, the basic usage:
redis of bit array is automatically extended, again using the bit map memory when the string needs to be converted to ASCII strings, then the corresponding set
value is set to 1 bit, one bit can be set when the re-setting may be carried out once filled
if the acquired element is displayed unprintable hexadecimal digit
2, and statistics to find
bitcount statistical instructions: to count the number of specified within the range of 1
bitpos Find command: used to find the first occurrence of a specified range 0 or 1
specified range [start, end] start and end byte index, i.e., the specified range must be a multiple of 8
3, magic instruction bitfield
the bitset bitget and can only be a single bit operation
bitfield may be a multi-bit operation (up to 64 consecutive bits), there are three sub-instruction set, get, incrby fragment can be designated to read and write
incrby is a specified range of bit increment operation, if The default redis folded, unsigned 8-bit 255 becomes 0, it becomes the symbol -127 -128
user can overflow the overflow policies, the default is folded, can also choose not to perform error saturated cut exceeds the range to stay longer maximum or minimum value

Guess you like

Origin blog.csdn.net/alvin_666/article/details/89068536