Redis Usage Basics (iv)--redis Sorting and message notification

Source: Internet
Author: User
Tags message queue

Redis Usage Basics (iv)

--redis Sorting and message notification

(Reproduced please attach this article link--linhxx)

First, sort

1. Command

Sort key [ALPHA] [DESC] [LIMIT start end], sorts the list, collection, and ordered collection, and when the ALPHA parameter is added, it can be sorted in dictionary order, with DESC in reverse order, plus LIMIT supporting paging.

2. Key parameters

By parameter: By Key:*->val, you can specify criteria for sorting, you can pass in a list yourself, or you can specify a column to sort on.

Get parameter: Get Key:*->val, you can specify the return result of sort sorting instead of returning the entire collection, multiple get for multiple arguments, and get# returns the key value of the element itself.

Store parameter: Store Key2, you can save the results of the previous sort in another key.

3, performance issues

The time complexity O (n+mlog m) of sort, n is the cardinality to be sorted, and M is the sorted value. In addition, Redis uses a container with a space of n before sorting to store temporary data during sorting.

Therefore, several issues need to be noted:

1) Minimize the number of collections to be sorted to reduce n

2) If all results are not required, limit is used to reduce m

3) If the result you want to sort is large, store it in store.

Second, Message Queuing

Redis Message Queuing can be divided into two categories, producers and consumers, when the producer of the data will be placed in the message queue, the consumer to monitor the message in the queue when there is data, can be processed later.

1. Command

Redis provides a command called Brpop, which differs from rpop in that when a key is manipulated with a command, if key does not have a value, it blocks the wait until the value is removed and the operation is taken. In addition, similar to Brpop, there are also blpop commands.

2. Priority level

Because the Brpop command can support multiple keys, and when each key has unhandled data, it starts processing from the leftmost key. For example, there are two e-mail reminders, one is the authentication of open account, and the other is a new message reminder. If too many do not take precedence, the new message alerts the business to block the opening of the account. Because the timeliness of account opening is higher, it needs to be placed on the left.

3. Publish Subscription Mode

The Publish subscription mode has a special command, the command that is issued is the Publish channel message, and the subscribed command is the Subscribe channel message, but because the Redis Publish command does not persist the message, That is, the message that was published before the publisher could not be seen by a later subscription.

After the subscribe command, the client will enter the subscription status, then only four commands can be entered: subscribe, unsubscribe, Psubscribe, Punsubscribe, other commands will be error.

After the subscription state, the client receives 3 types of replies, each with three values, the first value being the type of reply, and 232 values different depending on the type. The message types are as follows:

1) Subscribe indicates the feedback of a successful subscription, at which point the second return value is the channel name of the subscription, and the third value is the number of channels subscribed to by the current client.

2) message indicates the received subscription message, which is also the core of this pattern, the second value is the name of the channel, and the third value is the content of the message.

3) Unsubscribe means the successful unsubscribe of a channel, the second value is the name of the canceled channel, the third is the number of remaining subscription channels, if it is 0, then the subscription mode will be canceled, you can continue to enter other non-subscribed commands.

4. Batch subscription mode

Command Psubscribe, which supports BLOB mode, which is similar to a regular pattern, such as Psubscribe channel.*, subscribes to all channels at the beginning of the channel.

Accordingly, the Punsubscribe command supports bulk unsubscribe.

Third, pipeline

Redis and the client are connections made with TCP, so sending messages back and forth across the network is a time-consuming term called message latency. When executing multiple commands, each command needs to be executed and returned, and the next one executes.

When it is necessary to execute together, the Redis underlying communication provides support for pipelines that, when each command in a set of commands does not depend on the previous one, can send the request together and return together to reduce the number of network traffic.

Iv. space consumption

1, the complexity of

Redis provides two encodings for each data type, such as hash, which is stored in a hash table when the element is very large, and the time complexity is only O (1), but when there are few elements, O (n) and O (1) are not very wide, and in order to save memory, Redis uses an internal encoding method. Use time to change space.

Redis can use the object ENCODING key to see the internal encoding type of each key.

2. Coding

Redis uses a struct type in internal encoding, as follows:

typedef struct redisobject{

unsigned type:4;

unsigned notuse:2;

unsigned encoding:4;

unsigned lru:22;

int refcount;

void *ptr;

}

The type represents types, with the number 0-4 representing five types, Notuse is reserved, unused, and the PTR pointer points to the data stored, encoding 9, 0-8, including native encoding, integer, hash table, Zipmap, doubly linked list, ziplist, Skiplist, String. There are different encoding methods for the five types of data for Redis, as shown in: (from the network)

3. String optimization

Strings are stored in a struct, including string lengths, specific content, and remaining space. When the set command is executed, it takes 30 bytes, and when the key value is a 64-bit integer, the PTR pointer points directly to the value instead of to the struct, which can be saved to 16 bytes.

When the storage is 0-9999, Redis, which stores these numbers by default, directly points to the reference, occupying 0 bytes of space.

4. Hash optimization

Setting Hash-max-ziplist-entries and Hash-max-ziplist-value in the configuration file, when the number of keys in the hash is less than the entiries value, and each key value is less than the value, the Ziplist encoding is used. Otherwise, use a hash table to encode. Ziplist Sacrifice time for space, the hash table sacrificed space for time, so the data less with ziplist, more time with a hash table.

Therefore, the two parameters should not be set too large.

5. List optimization

Lists and hashes are similar, with list-max-ziplist-entries and List-max-ziplist-value to configure. Coding methods include Ziplist, doubly linked lists, quicklist,quicklist combined ziplist and doubly linked lists, to reduce space and reduce time appropriately.

6. Set optimization

Configuration file configuration set-max-intset-entries, less than when using intset encoding, otherwise with a hash table. Intset makes the set interior orderly arrangement, easy to use the dichotomy method to find, but the addition and deletion needs to be ordered, the element is more slow.

7, ordered set optimization

Configuration file configuration zset-max-ziplist-entries and Zset-max-ziplist-value. including Ziplist and skiplist encoding, skiplist is stored using a hash table and a skip list structure, a hash table to store the mapping of fractions, and a jump List to store fractions and the mapping of element values.

--written by Linhxx

More recent articles, please pay attention to the public number "machine learning", or scan the right QR code.

Redis Usage Basics (iv)--redis Sorting and message notification

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.