Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to analyze why Redis is so fast from the perspective of data storage

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

How to analyze why Redis is so fast from the perspective of data storage is not very clear to many beginners. In order to help you solve this problem, the following editor will explain it in detail. People with this need can come and learn. I hope you can get something.

I. introduction and application

Redis is an in-memory database written by ANSI C, which has excellent performance, network support and persistence, and provides API in multiple languages. The commonly used types are String, List, Hash, Set and ZSet.

Redis generally has the following applications in Internet companies:

String: cache, current limit, counter, distributed lock, distributed Session

Hash: storing user information, user home page visits, combined queries

List: Weibo follower timeline list, simple queue

Set: like, step on, tag, friend relationship

Zset: ranking

For example, when e-commerce is in a big promotion, it will use some special designs to ensure the stability of the system, and the deduction of inventory can be considered as follows:

In the figure above, inventory is deducted directly in Redis and synchronized to the database through Worker after logging. Concurrent processing and repeated processing need to be considered when designing synchronous Worker.

From the above application scenario, we can see that Redis is very efficient and stable, so how is the underlying implementation of Redis?

2. RedisObject, the object of Redis

When we execute the set hello world command, we have the following data model:

DictEntry:Redis assigns a dictEntry to each key-value key-value pair, in which there are pointers of key and val. The next points to the next dictEntry to form a linked list, which can link multiple key-value pairs with the same hash value together to solve the hash conflict problem (chain address method).

Sds: the key key "hello" is stored in SDS (simple dynamic string), which is described in more detail later.

RedisObject: the value val "world" is stored in redisObject. In fact, the types commonly used in redis 5 are stored in redisObject; the type field in redisObject indicates the type of Value object, and the ptr field points to the address where the object is located.

RedisObject objects are very important, and redisObject support is needed for Redis object types, internal coding, memory collection, shared objects and other functions. The advantage of this design is that a variety of different data structures can be set for different usage scenarios, so as to optimize the efficiency of objects in different scenarios.

Whether dictEntry objects or redisObject or SDS objects, memory allocators (such as jemalloc) are required to allocate memory for storage. As the default memory allocator for Redis, jemalloc is relatively good at reducing memory fragmentation. For example, in the 64-bit system, jemalloc divides the memory space into three ranges: small, large and huge; each range is divided into many small memory block units; when Redis stores data, it will choose the most appropriate memory block to store.

As mentioned earlier, each object in Redis is represented by a redisObject structure whose ptr pointer points to the underlying implemented data structure, which is determined by the encoding property. For example, we execute the following command to get the corresponding code for storing "hello":

All the data structure types of redis are as follows (important, used later):

III. String

The underlying implementation of a string object can be int, raw, or embstr (the table above introduces the name). Embstr encoding allocates a contiguous piece of space by calling the memory allocation function once, while raw needs to be called twice.

Int-encoded string objects and embstr-encoded string objects are converted to raw-encoded string objects under certain conditions. Embstr:

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report