Internal implementation of various data structures of the Redis database.

Source: Internet
Author: User

The Redis database is a non-relational database, based on key/value pairs, loaded into memory at runtime, supports virtual memory for value, supports multiple data structures, supports persistence, is known for performance, and can be used for storage, caching, Message Queuing and other scenarios. The data structure maintained by the Redis runtime is mainly described to show how it works.

1. Overall design.

First, Redis does not have an indexing mechanism like MySQL because it has a hash-based dictionary built into it, such as:

Redis computes hash and index values in the following ways:

# using the hash function set by the dictionary, calculate the hash value of key keys hash  = dict, type,hashfunction(key);  # Use the hash table's Sizemask property and hash value to calculate the index value # Depending on the situation, Ht[x] can be ht[0] or ht[1]index = hash & dict
                     
                      ->
                      HT[x].  Sizemask;      
                                
When inserting data, the index is calculated based on the above and then placed in the corresponding position in the table sheet based on the index value.

2. String Type
For example:Set Hello World


3. List Type
For example:lpush List AAAA bbb CCC


4. Hash type
For example:hset test Hello World

Note: when a new hash object is created, it is stored with Zipmap (also known as the small hash). This zipmap is not actually hash table, but zipmap compared to the normal hash implementation can save a lot of the hash itself needs some metadata storage overhead. Although Zipmap's additions, deletions, and lookups are all O (n), there are not too many field numbers for general objects. So the use of Zipmap is also very fast, that is, add delete average or O (1). If the size of field or value exceeds a certain limit, Redis automatically replaces the zipmap with a normal hash implementation (a key corresponding to a hash table).

Reference:
http://www.slideshare.net/iammutex/redis-9948788
Http://blog.nosqlfan.com/html/3525.html?ref=rediszt
Http://redisbook.com/preview/dict/hash_algorithm.html

Internal implementation of various data structures of the Redis database.

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.