search
HomeDatabaseRedisWhat are the common key-value designs of Redis databases?

 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
This article is reproduced at:亿速云. If there is any infringement, please contact admin@php.cn delete
Redis's Role: Exploring the Data Storage and Management CapabilitiesRedis's Role: Exploring the Data Storage and Management CapabilitiesApr 22, 2025 am 12:10 AM

Redis plays a key role in data storage and management, and has become the core of modern applications through its multiple data structures and persistence mechanisms. 1) Redis supports data structures such as strings, lists, collections, ordered collections and hash tables, and is suitable for cache and complex business logic. 2) Through two persistence methods, RDB and AOF, Redis ensures reliable storage and rapid recovery of data.

Redis: Understanding NoSQL ConceptsRedis: Understanding NoSQL ConceptsApr 21, 2025 am 12:04 AM

Redis is a NoSQL database suitable for efficient storage and access of large-scale data. 1.Redis is an open source memory data structure storage system that supports multiple data structures. 2. It provides extremely fast read and write speeds, suitable for caching, session management, etc. 3.Redis supports persistence and ensures data security through RDB and AOF. 4. Usage examples include basic key-value pair operations and advanced collection deduplication functions. 5. Common errors include connection problems, data type mismatch and memory overflow, so you need to pay attention to debugging. 6. Performance optimization suggestions include selecting the appropriate data structure and setting up memory elimination strategies.

Redis: Real-World Use Cases and ExamplesRedis: Real-World Use Cases and ExamplesApr 20, 2025 am 12:06 AM

The applications of Redis in the real world include: 1. As a cache system, accelerate database query, 2. To store the session data of web applications, 3. To implement real-time rankings, 4. To simplify message delivery as a message queue. Redis's versatility and high performance make it shine in these scenarios.

Redis: Exploring Its Features and FunctionalityRedis: Exploring Its Features and FunctionalityApr 19, 2025 am 12:04 AM

Redis stands out because of its high speed, versatility and rich data structure. 1) Redis supports data structures such as strings, lists, collections, hashs and ordered collections. 2) It stores data through memory and supports RDB and AOF persistence. 3) Starting from Redis 6.0, multi-threaded I/O operations have been introduced, which has improved performance in high concurrency scenarios.

Is Redis a SQL or NoSQL Database? The Answer ExplainedIs Redis a SQL or NoSQL Database? The Answer ExplainedApr 18, 2025 am 12:11 AM

RedisisclassifiedasaNoSQLdatabasebecauseitusesakey-valuedatamodelinsteadofthetraditionalrelationaldatabasemodel.Itoffersspeedandflexibility,makingitidealforreal-timeapplicationsandcaching,butitmaynotbesuitableforscenariosrequiringstrictdataintegrityo

Redis: Improving Application Performance and ScalabilityRedis: Improving Application Performance and ScalabilityApr 17, 2025 am 12:16 AM

Redis improves application performance and scalability by caching data, implementing distributed locking and data persistence. 1) Cache data: Use Redis to cache frequently accessed data to improve data access speed. 2) Distributed lock: Use Redis to implement distributed locks to ensure the security of operation in a distributed environment. 3) Data persistence: Ensure data security through RDB and AOF mechanisms to prevent data loss.

Redis: Exploring Its Data Model and StructureRedis: Exploring Its Data Model and StructureApr 16, 2025 am 12:09 AM

Redis's data model and structure include five main types: 1. String: used to store text or binary data, and supports atomic operations. 2. List: Ordered elements collection, suitable for queues and stacks. 3. Set: Unordered unique elements set, supporting set operation. 4. Ordered Set (SortedSet): A unique set of elements with scores, suitable for rankings. 5. Hash table (Hash): a collection of key-value pairs, suitable for storing objects.

Redis: Classifying Its Database ApproachRedis: Classifying Its Database ApproachApr 15, 2025 am 12:06 AM

Redis's database methods include in-memory databases and key-value storage. 1) Redis stores data in memory, and reads and writes fast. 2) It uses key-value pairs to store data, supports complex data structures such as lists, collections, hash tables and ordered collections, suitable for caches and NoSQL databases.

See all articles

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

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools