Home Database Redis What are the common key-value designs of Redis databases?

What are the common key-value designs of Redis databases?

May 29, 2023 am 11:50 AM
redis

 User login system

It is a system that records user login information. After we simplify the business, we only have one table left.

Design of relational database

mysql>select*fromlogin;

  --------- ------------ ---- ------------- ---------------------

|user_id|name|login_times |last_login_time|

  --------- ---------------- ---------------- --- ------------------

|1|kenthompson|5|2011-01-0100:00:00|

|2| dennisritchie|1|2011-02-0100:00:00|

|3|JoeArmstrong|2|2011-03-0100:00:00|

------- ------------------------------------------------------------------ --

The primary key of the user_id table, name represents the user name, and login_times represents the number of logins of the user. After each user logs in, login_times will increase by itself, and last_login_time is updated to the current time.

Design of REDIS

Convert relational data into KV database, my method is as follows:

Key table name: Primary key value: Column name

Value column value

Generally, colon is used as the separator. This is an unwritten rule. For example, in the php-adminforredis system, the keys are separated by colon by default, so user:1user:2 and other keys will be divided into one group. So the above relational data is converted into kv data and recorded as follows:

Setlogin:1:login_times5

Setlogin:2:login_times1

Setlogin:3:login_times2

Setlogin:1:last_login_time2011-1-1

Setlogin:2:last_login_time2011-2-1

Setlogin:3:last_login_time2011-3-1

setlogin:1 :name"kenthompson"

setlogin:2:name"dennisritchie"

setlogin:3:name"JoeArmstrong"

If the primary key is known, you can use the get and set methods Get or modify the user's name, login times, and last login time.

Generally, users cannot know their own ID, they only know their username, so there must be a mapping relationship from name to ID. The design here is different from the above.

 set"login:kenthompson:id"1

 set"login:dennisritchie:id"2

 set"login:JoeArmstrong:id"3

In this way, every time a user logs in, the business logic is as follows (python version), r is the redis object, and name is the known user name.

 #Get the user's id

uid=r.get("login:%s:id"%name)

 #Automatically increase the number of user logins

The following is a possible rephrased sentence: ret = r.incr("login:%s:login_times" % uid)

 #Update the user's last login time

ret=r.set("login:%s:last_login_time "%uid,datetime.datetime.now())

If the requirement is only to know the ID, update or obtain the last login time and number of logins of a user, there is no difference between the relational database and the kv database. One is through btreepk and the other is through hash, both of which work very well.

Assume that there is the following requirement to find the N users who logged in recently. Developers have a look and it is relatively simple and can be done with just one SQL statement.

Please select all columns from the "login" table, sort in descending order by the "last_login_time" column, and limit the result set size to N

After the DBA understands the requirements, consider the future table if It is relatively large, so create an index on last_login_time. By accessing N records starting from the rightmost side of the index, and then performing N table return operations, the execution plan has a significant effect.

What are the designs of common Redis database key values? Two days later, another request came, and I needed to know who has logged in the most times. How to deal with the same relational type? DEV said it is simple

 select*fromloginorderbylogin_timesdesclimitN

The DBA takes a look and needs to create an index on login_time. Do you think there is something wrong? Every field in the table has a prime quote.

The source of the problem is that the data storage of the relational database is not flexible enough, and the data can only be stored using a heap table arranged in rows. A unified data structure means that you must use indexes to change the SQL access path to quickly access a certain column, and the increase in access paths means that you must use statistical information to assist, so a lot of problems arise.

There is no index, no statistical plan, and no execution plan. This is the kv database.

In response to the need to obtain the latest N pieces of data, in Redis, the last-in-first-out feature of the linked list is very suitable. We add a piece of code after the above login code to maintain a login linked list and control its length so that the most recent N logged-in users are always stored in it.

 #Add the current login person to the linked list

ret=r.lpush("login:last_login_times",uid)

 #Keep the linked list with only N bits

ret=redis.ltrim("login:last_login_times",0,N-1)

In this way, you need to get the id of the latest login person, the following code can be used

last_login_list=r .lrange("login:last_login_times",0,N-1)

In addition, to find the person who has logged in the most times, sortedset is very suitable for needs such as sorting and standings. We combine users and login times Stored uniformly in a sortedset.

zaddlogin:login_times51

zaddlogin:login_times12

zaddlogin:login_times23

In this way, if a user logs in, an additional sortedset is maintained, the code is as follows

#The number of login times for this user is increased by 1

ret=r.zincrby("login:login_times",1,uid)

So how to get the user with the most login times, sort in reverse order Just take the Nth ranked user

ret=r.zrevrange("login:login_times",0,N-1)

It can be seen that DEV needs to add 2 lines of code. The DBA does not need to consider indexes or anything.

TAG system

Tags are especially common in Internet applications. If designed with a traditional relational database, it would be a bit nondescript. Let's take the example of searching for a book to see the advantages of redis in this regard.

Design of relational database

Two tables, one for book details and one for tags, indicating the tags of each book. There are multiple tags in a book.

 mysql>select*frombook;

  ------ --------------------------- ---------------------

 |id|name|author|

  ------ ---- ------------------------------------------

 | 1|TheRubyProgrammingLanguage|MarkPilgrim|

 |1|Rubyonrail|DavidFlanagan|

 |1|ProgrammingErlang|JoeArmstrong|

  ------ ------ ------------------------------------------

 mysql>select *fromtag;

  --------- ---------

 |tagname|book_id|

  ------ --- ---------

|ruby|1|

|ruby|2|

|web|2|

 |erlang|3|

  --------- ---------

If you have such a need, the search is both ruby ​​and web. Books, how will they be handled if a relational database is used?

 selectb.name,b.authorfromtagt1,tagt2,bookb

 wheret1.tagname='web'andt2.tagname='ruby'andt1. book_id=t2.book_idandb.id=t1.book_id

The tag table is associated twice and then associated with the book. This SQL is quite complicated. What if the requirement is ruby ​​but not a web book?

Relational data is actually not very suitable for these set operations.

Design of REDIS

First of all, the book data must be stored, the same as above.

Setbook:1:name”TheRubyProgrammingLanguage”

Setbook:2:name”Rubyonrail”

Setbook:3:name”ProgrammingErlang”

setbook: 1:author"MarkPilgrim"

Setbook:2:author"DavidFlanagan"

Setbook:3:author"JoeArmstrong"

tag table We use sets to store data because Sets are good at finding intersections and unions

 saddtag:ruby1

 saddtag:ruby2

 saddtag:web2

 saddtag:erlang3

Then , a book that belongs to ruby ​​but also belongs to the web?

 inter_list=redis.sinter("tag.web","tag:ruby")

 That is, a book that belongs to ruby ​​but does not belong to the web ?

 inter_list=redis.sdiff("tag.ruby","tag:web")

 A collection of books belonging to ruby ​​and web?

 inter_list=redis .sunion("tag.ruby","tag:web")

It’s so simple.

From the above two examples, we can see that in some scenarios, relational databases are not suitable. You may be able to design a system that meets your needs, but it always feels weird and weird. It feels like something is being done mechanically.

Especially in the example of logging in to the system, indexes are frequently created for the business. In a complex system, ddl (create index) may change the execution plan. Since the SQL in the old system with complex business is all kinds of strange, causing other SQL to use different execution plans, this problem is difficult to predict. It is too difficult to require the DBA to understand all the SQL in this system. This problem is particularly serious in Oracle, and every DBA has probably encountered it. Although there are online DDL methods now, DDL is still not very convenient for systems like MySQL. When it comes to big tables, the DBA gets up early in the morning to operate during the low business hours. I have done this often. It is very convenient to use Redis to handle this demand, and only requires the DBA to estimate the capacity.

The above is the detailed content of What are the common key-value designs of Redis databases?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1267
29
C# Tutorial
1239
24
How to build the redis cluster mode How to build the redis cluster mode Apr 10, 2025 pm 10:15 PM

Redis cluster mode deploys Redis instances to multiple servers through sharding, improving scalability and availability. The construction steps are as follows: Create odd Redis instances with different ports; Create 3 sentinel instances, monitor Redis instances and failover; configure sentinel configuration files, add monitoring Redis instance information and failover settings; configure Redis instance configuration files, enable cluster mode and specify the cluster information file path; create nodes.conf file, containing information of each Redis instance; start the cluster, execute the create command to create a cluster and specify the number of replicas; log in to the cluster to execute the CLUSTER INFO command to verify the cluster status; make

How to clear redis data How to clear redis data Apr 10, 2025 pm 10:06 PM

How to clear Redis data: Use the FLUSHALL command to clear all key values. Use the FLUSHDB command to clear the key value of the currently selected database. Use SELECT to switch databases, and then use FLUSHDB to clear multiple databases. Use the DEL command to delete a specific key. Use the redis-cli tool to clear the data.

How to read redis queue How to read redis queue Apr 10, 2025 pm 10:12 PM

To read a queue from Redis, you need to get the queue name, read the elements using the LPOP command, and process the empty queue. The specific steps are as follows: Get the queue name: name it with the prefix of "queue:" such as "queue:my-queue". Use the LPOP command: Eject the element from the head of the queue and return its value, such as LPOP queue:my-queue. Processing empty queues: If the queue is empty, LPOP returns nil, and you can check whether the queue exists before reading the element.

How to configure Lua script execution time in centos redis How to configure Lua script execution time in centos redis Apr 14, 2025 pm 02:12 PM

On CentOS systems, you can limit the execution time of Lua scripts by modifying Redis configuration files or using Redis commands to prevent malicious scripts from consuming too much resources. Method 1: Modify the Redis configuration file and locate the Redis configuration file: The Redis configuration file is usually located in /etc/redis/redis.conf. Edit configuration file: Open the configuration file using a text editor (such as vi or nano): sudovi/etc/redis/redis.conf Set the Lua script execution time limit: Add or modify the following lines in the configuration file to set the maximum execution time of the Lua script (unit: milliseconds)

How to use the redis command line How to use the redis command line Apr 10, 2025 pm 10:18 PM

Use the Redis command line tool (redis-cli) to manage and operate Redis through the following steps: Connect to the server, specify the address and port. Send commands to the server using the command name and parameters. Use the HELP command to view help information for a specific command. Use the QUIT command to exit the command line tool.

How to implement redis counter How to implement redis counter Apr 10, 2025 pm 10:21 PM

Redis counter is a mechanism that uses Redis key-value pair storage to implement counting operations, including the following steps: creating counter keys, increasing counts, decreasing counts, resetting counts, and obtaining counts. The advantages of Redis counters include fast speed, high concurrency, durability and simplicity and ease of use. It can be used in scenarios such as user access counting, real-time metric tracking, game scores and rankings, and order processing counting.

How to set the redis expiration policy How to set the redis expiration policy Apr 10, 2025 pm 10:03 PM

There are two types of Redis data expiration strategies: periodic deletion: periodic scan to delete the expired key, which can be set through expired-time-cap-remove-count and expired-time-cap-remove-delay parameters. Lazy Deletion: Check for deletion expired keys only when keys are read or written. They can be set through lazyfree-lazy-eviction, lazyfree-lazy-expire, lazyfree-lazy-user-del parameters.

How to optimize the performance of debian readdir How to optimize the performance of debian readdir Apr 13, 2025 am 08:48 AM

In Debian systems, readdir system calls are used to read directory contents. If its performance is not good, try the following optimization strategy: Simplify the number of directory files: Split large directories into multiple small directories as much as possible, reducing the number of items processed per readdir call. Enable directory content caching: build a cache mechanism, update the cache regularly or when directory content changes, and reduce frequent calls to readdir. Memory caches (such as Memcached or Redis) or local caches (such as files or databases) can be considered. Adopt efficient data structure: If you implement directory traversal by yourself, select more efficient data structures (such as hash tables instead of linear search) to store and access directory information

See all articles