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

What are the five things you must know before using Redis

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces what you must know before using Redis. The content is very detailed. Interested friends can use it for reference. I hope it will be helpful to you.

Developing applications using Redis is a pleasant process, but like other technologies, there are a few things you need to keep in mind for Redis-based application design. In the past, you may already know the whole routine of relational database development, and there are many similarities in Redis-based application development, but you must keep in mind the following two points-Redis is an in-memory database, and it is single-threaded. Therefore, when using Redis, you need to pay attention to the following points:

1. Control all keys stored in Redis

The main function of a database is to store data, but it is normal for developers to ignore some data stored in the database because of changes in application requirements or data usage, as well as in Redis. You may ignore some expired keys, or you may forget the data because a module of the application is deprecated.

In either case, Redis stores some data that is no longer used and takes up some space for no reason. Redis's weakly structured data schema makes centrally stored content difficult to figure out unless you use a very mature naming convention for keys. Using appropriate naming methods will simplify your database management, when you use your application or service to make key namespaces (usually using colons to divide key names). You can easily identify when data is migrated, transformed, or deleted.

Another common use case for Redis is the second data store as a hot data item, where most of the data is stored in other databases, such as PostgreSQL or MongoDB. In these use cases, developers often forget to delete the corresponding data in the Redis when the data is removed from the main storage. In the case of cross-data storage, cascading deletion is usually required. In this case, it can be achieved by saving all identifiers of specific data items in the Redis configuration, so as to ensure that after the data is deleted in the master database, the system will call a cleanup program to delete all relevant copies and information.

two。 Controls the length of all key names

As we mentioned above, we need to use the appropriate naming rules and add prefixes to identify the direction of the data, so this seems to be contrary to it. But don't forget that Redis is an in-memory database, and the shorter the keys, the less space you need. Of course, when there are millions or billions of keys in the database, the length of the key name will make a big difference.

For example, on a 32-bit Redis server, if you store a million keys, each of which is 32-character, you will consume about 96MB space when using 6-character length keys, but if you use 12-character length keys, the space consumption will increase to about 111MB. With the increase in the number of keys, 15% of the extra overhead will have a significant impact.

3. Use appropriate data structures

Whether it's memory usage or performance, sometimes data structures can have a big impact. Here are some best practices to refer to:

Instead of storing data as thousands (or millions) of separate strings, consider using hash data structures to group related data. Hash tables are very efficient and can reduce your memory usage; at the same time, hashes are more beneficial to detail abstraction and code readability.

Use list instead of set when appropriate. If you don't need to use the set feature, List can provide faster speeds than set with less memory.

Sorted sets is the most expensive data structure, regardless of memory consumption or the complexity of basic operations. If you just need a way to query records and do not care about attributes such as sorting, it is recommended to use a hash table.

An often overlooked feature in Redis is bitmaps or bitsets (after V2.2). Bitsets allows you to perform multiple bit-level operations on the Redis value, such as some lightweight analysis.

4. Do not use keys when using SCAN

Starting with Redis v2.8, the SCAN command is available, which allows you to retrieve keys from keyspace using cursors. Compared to the KEYS command, although SCAN cannot return all matches at once, it avoids the high risk of blocking the system, thus allowing some operations to be performed on the primary node.

It should be noted that the SCAN command is a cursor-based iterator. Each time the SCAN command is called, a new cursor is returned to the user, and the user needs to use this new cursor as the cursor parameter of the SCAN command in the next iteration to continue the previous iteration. At the same time, with SCAN, you can also use keyname mode and the count option to adjust the command.

SCAN related commands also include SSCAN command, HSCAN command and ZSCAN command, which are used for collections, hashes and sequels, respectively.

5. Use server-side Lua scripts

In the process of using Redis, the support of Lua script undoubtedly provides a very friendly development environment for developers, thus greatly liberating the creativity of users. When used properly, Lua scripts can greatly improve performance and resource consumption. Instead of sending data to CPU, scripts allow you to execute logic closest to the data, thereby reducing network latency and redundant data transmission.

In Redis, a very classic use case for Lua is data filtering or aggregating data into applications. By encapsulating the processing workflow into a script, you can use fewer resources to get a smaller answer in less time by simply calling it.

About the use of Redis before the 5 must know which things to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

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

Internet Technology

Wechat

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

12
Report