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.