String object string

  • Int (long)

    If a string object holds an integer value that can be represented as long, the encoding of the string object is set to int.

  • Embstr (Simple dynamic String, SDS)

    If the string object holds a string value, and the string length is less than 39 (Redis 3.0), the threshold is 45. The string object will then use a simple dynamic string (SDS) to hold the string value. The encoding will be set to EMbstr.

  • Raw (Simple Dynamic String, SDS)

    If the string object holds a string value that is greater than or equal to 39 in length, the string object uses a simple dynamic string (SDS) to hold the string value. The encoding will be set to RAW.

Instead of using C’s traditional string representation directly, Redis built an abstract type called Simple Dynamic String (SDS).

Embstr encoding is an optimized encoding method for saving short strings. Compared with RAW, it has the following advantages:

  • Embstr encoding reduces the number of memory allocations required to create string objects from two raw encoding times to one.
  • Freeing embSTR-encoded string objects requires only one call to the memory freeing function, whereas freeing raw string objects requires two calls to the memory freeing function.
  • Because all the data of embSTR-encoded string objects is stored in a contiguous piece of memory, embSTR-encoded string objects can take advantage of caching better than raw string objects.

Usage scenarios

1. Cache function

Redis serves as the cache layer, MySQL serves as the storage layer, and most of the requested data is obtained from Redis. Because Redis supports high concurrency, caching is often used to speed up reads and writes and reduce back-end stress.

2, counting

Many applications will use Redis as a basic tool for counting, it can achieve fast counting, query caching, and data can be asynchronously landed to other data sources. Specific likes, video plays and so on.

3. Share sessions

A distributed Web service will user Session information (such as login information) stored in each server, this will be a problem in, for the sake of load balance of distributed services will be balanced user’s access to different server, the user refresh one may find that you need to log in, this is user intolerable. To solve this problem, you can use Redis to centrally manage user sessions.

4, the speed limit

For example, to prevent frequent access to the SMS interface, users cannot obtain verification codes within a few minutes after obtaining them. Other sites limit the number of visits to an IP address to n times a second.


List object list

  • Ziplist (Compressed list)

    When the number of list elements is smaller than the list-max-ziplist-entries configuration (512 by default) and the value of each list element is smaller than the list-max-ziplist-value configuration (64 bytes by default), Redis uses Ziplist as an internal implementation of lists to reduce memory usage.

  • linkedList

    Redis uses linkedList as an internal implementation of a list when the list type does not meet Ziplist’s criteria.

After Redis 3.2, the internal encoding of the list is quickList. This encoding is a combination of the above two. The book uses Redis 3.0, so we will look at it later.

Usage scenarios

1. Message queues

The combination of Redis lpush+ BRPOP commands can block queues, the producer client uses LRpush to insert elements from the left side of the list, and multiple consumer clients use BRPOP to block elements from the end of the list. Multiple clients ensure load balancing and high availability of consumption.

2. List of articles

Each user has his or her own article list, which needs to be displayed in pages. Consider using a list at this point, because lists are not only ordered, but also support fetching elements by index range.

· LPUSH + LPOP =Stack · LPUSH + RPOP =Queue · LPSH + LTRIM =Capped Collection · LPUSH + BRPOP =Message Queue


Hash object

  • Ziplist (Compressed list)

    When the number of hash elements is smaller than the hash-max-ziplist-entries configuration (512 by default) and all values are smaller than the hash-max-ziplist-value configuration (64 bytes by default), Redis uses Ziplist as an internal implementation of hashing. Ziplist uses a more compact structure to store multiple elements consecutively, so it’s better than HashTable in terms of memory savings.

  • Hashtable (dictionary)

    When the hash type cannot meet ziplist conditions, Redis will use Hashtable as the internal implementation of the hash, because the ziplist read and write efficiency will be reduced, and the hashtable read and write time complexity is O (1).

Usage scenarios

The user information can be stored. For example, the key is user:1 and the value is ID :1. Name: Tom.


Set object set

  • Intset (Set of integers)

    Redis uses intset as its internal implementation when the collection elements are integers and the number of elements is less than the set-max-intset-entries configuration (512 by default) to reduce memory usage.

  • Hashtable (dictionary)

    Redis uses HashTable as an internal implementation of the collection when the collection type does not satisfy intSet criteria.

Usage scenarios

A typical usage scenario for collection types is tags. For example, one user may be interested in entertainment and sports, while another may be interested in history and news. These points of interest are tags. With this data, we can get people who like the same tag and the tags that users like in common. This data is important for user experience and user stickiness. For example, an e-commerce website will make different types of recommendations to users with different labels. For example, people who are interested in digital products will recommend the latest digital products to them on various pages or through emails, which will usually bring more benefits to the website.


The ordered collection object zset

  • Ziplist (Compressed list)

    When the number of ordered set elements is smaller than the zset-max-ziplist-entries configuration (128 by default) and the value of each element is smaller than the zset-max-ziplist-value configuration (64 bytes by default), Redis uses Ziplist as an internal implementation of ordered collections, which can effectively reduce memory usage.

  • Skiplist

    Ordered collections use skiplist as an internal implementation when ziplist conditions are not met, because ziplist reads and writes become less efficient.

Usage scenarios

A typical use scenario of ordered sets is the leaderboard system. For example, video websites need to make leaderboards for videos uploaded by users. The dimensions of the leaderboards may be in multiple aspects: by time, by the number of plays and by the number of likes obtained.


  • Based on the Redis 3.0
  • Redis Design and Implementation
  • Redis Development and Operation

Take a look at the underlying data structures in Redis Design and Implementation.