The design of Redis index

Source: Internet
Author: User
Tags lua


Redis does not directly support indexing and needs to be maintained by itself.


For a range-only index, we can simply save the index as a kv pair, and V save the main key,

A range retrieval, or a Redis index, is implemented using the Zset of the other.



Example of a traditional user system example
UID User ID
Name User name
Credit User points
Type types

Can be placed directly into a hashset.
Hmset usr:1 UID 1 name AAA credit Type 0
Hmset usr:2 UID 2 name BBB credit Type 1


Retrieving by UID is quick, but if you want to query type=1 users, you can scan them all.
In a relational database, we can simply index on type
select * from USR where type=1

Such SQL can be executed efficiently. Redis we need to maintain a zset ourselves.
Zadd Usr.index.type 0 0:1
Zadd Usr.index.type 0 1:2

Note that all weights are set to 0 so that you can retrieve them directly by value, and then you can
Zrangebylex Usr.index.type [1: (1;

(Here's a little trick, character: Behind that, so >=1: and <1; is 1: The beginning of the
than [1: [1:\xff this form looks simpler. )

Obviously the result is 1:2, but also to deal with their own user id=2, and then you can get other fields through Usr:2.
If there are few updates here, you can also consider getting the other fields directly after 1:2, which is equal to the index with the redundant fields in SQL.

To keep the index consistent, a lot of extra work needs to be done

More simple to add:
Hmset usr:3 UID 3 name CCC credit Type 3
Zadd Usr.index.type 0 3:3


In order to maintain consistency, it is best to use the script to achieve. And the modification is somewhat complicated,
Must first read the original value, or use LUA bar

Local usr=keys[1]--usr:1 all key keys used are supplied with parameters
Local Index=keys[2]--usr.index.type
Local Uid=argv[1]--1
Local Newtype=argv[2]--9

--Save original values
Local Oldtype=redis.call (' Hget ', usr, ' type ')
--type modified to new value
Redis.call (' Hset ', usr, ' type ', Newtype)

--delete old index
Redis.call (' Zrem ', Index,oldtype ... ":".) Uid
--Add New Index
Redis.call (' Zadd ', index,0, Newtype. ":" .. Uid


To maintain consistency, use this LUA script for all of the modified type places,
When you delete a USR record, you also want to read out the original type and delete the index, similar to the script.

Obviously, the SQL database index is very good, but he helped you to achieve the internal, the implementation of the principle is similar.
Similarly Redis can also build multiple indexes, even composite indexes, such as:

Zadd Usr.index.type_credit 0 3:20:3

Type:credit:uid format, it is important to note that the number field participating in the index must be left up to 0 alignment (a few are based on the length of the word), such as
003:00020:3

Unless you do not need range retrieval, you only need to search accurately, for example:
Zrangebylex Usr.index.type_credit [3:20: (3:20;
Only need to retrieve type=. and credit=. Users, otherwise you have to use

Zrangebylex Usr.index.type_credit [3:00020: (3:00030;
Retrieves the type=. and credit between. and.


Obviously Redis use of the index is still more trouble, whether it really needs.
Whether you can use the SQL database directly and let Redis cache the query results. Needs to be carefully weighed by the architect.

But it's true that when you build indexing skills, Redis becomes a lot more powerful.


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.