Redis Learning: How to jump out of SQL this pit

Source: Internet
Author: User
Tags redis server

Explore One: Redis? What is it?

In short, Redis is a powerful Key-value database with two points of power: fast response (so data memory is stored and written to disk only when necessary), features are rich (supports multiple data types, and complex operations on various types).

In fact, an important feature of Redis is that it is not a database in its usual sense, although it is called a database because it can store and maintain data for you, but it does not provide any SQL dialect like a relational database. But don't worry, Redis is not a black hole that eats data, it just doesn't support SQL and related features, but it provides a robust protocol for interacting with it.

In Redis, there is no concept of a data table, and there is no need to care about operations or functions such as SELECT, join, view, or data fields similar to int or varchar. You are facing a relatively primitive set of data and data types.

Discovery II: Available Datatypes

Let's take a closer look at how this strange database works. As seen above, Redis is based on the Key-value paradigm to store data, so focus first on the concept of "key".

Key is essentially a simple string, such as "username", "password" and so on. When defining a key, you can use ordinary characters, numbers, etc., like ".", ":", "_" when defining key, and so on, except you can't use spaces, so like "user_name", "User:123:age", "User:123:username "is a good way to define the key.

Unlike the field names in the RDBMS, where key is an important part of Redis, we have to be careful when working with keys. In the following narration, Redis does not have the concept of table, so like "Select username from users WHERE user_id=123;" This simple task can only be implemented in a different way, in order to achieve this goal, on Redis, one way is to get the result value by key "User:123:username". As you can see, the definition of key carries a mysterious message (like user IDs). In Redis, the importance of key is evident. (The same is true of key in other Key-value databases.) )

Now you should have a clear understanding of key and take you into the magical world of available data types.

Strings

String is the most basic data type in Redis, which is a normal binary security string that supports a maximum data length of 1Gb.
You can set a string type of data to a key by using the SET command, and the result can be obtained from the key by the GET command. If you want to store digital information like a counter, you can also store it in string data and use INCR and DECR to do self-increment and decrement operations.

Lists

A list is a collection of string data in which items are sorted in the order in which they are inserted. You can think of the list as a chain (chain), so you can add a new link to the left (chain) or rightmost (chain end), but also to the middle of the chain, but break a link.

It is possible to add data (L:left, r:right) to the list via the Lpush and Rpush commands, to eject the element with the Lpop or rpop command (delete the element at the same time), or to get the element of the specified range by Lrange (only the data is returned and no elements are deleted). You can also add elements to a specified location by LSet, but typically this is much slower than a simple lpush or rpush.

Hashes

Hashes stores more tightly connected data in a concise manner. Hashes implements a built-in key-value pair for each stored key to store data, such as the "user" key, whose value can be multiple fields and a data set that corresponds to each character's value pair. If you are familiar with programming languages such as Ruby or JavaScript, the hashes here is similar to the hash concept in those languages.

Sets

Sets is the same as the concept "set" of the same name in mathematics, and is a collection of elements that contain no repetition. In Redis, these objects become string types in Redis. As you would expect, sets differs from lists in that the elements in sets are unordered and cannot be duplicated, and you cannot put two identical data in the sets.

You can add data to the set by Sadd, Srem Delete the data, or return and delete the data through Spop. In addition, the "Sunion", "intersection" and "difference" operations on the set can be implemented by means of the SINTER, Sdiff commands respectively.

Ordered Sets

Ordered sets is similar to sets, except that each element in the Ordered set has a weight that is used to compare and sort with other elements.

Of course ordered set has a similar operation to normal sets, Zadd and Zrem are add and delete elements respectively. Ordered set also has its own unique operation: ZINCR and Zscore, which are used to weigh +1 for an element, and then return the weight value of the element.

Discovery Three: Where is my tables?

Using Redis is completely different from the SQL data tables we used before, there is no language to support you querying data on the server, there are only a few commands to help you manipulate the keys value in the database. The commands in Redis are data type sensitive, that is, you cannot execute a set command on a list, or you will get a hint of execution error. You can send commands to Redis server through REDIS-CLI or other interfaces in the programming language you are using. In the following example, we focus only on the command itself, not on which way you submit it to Redis server.

Imagine a simple SQL database table, like a table in some applications that holds user data:

ID Username password Name surname
1 user1 pass1 Bob Smith
2 User2 pass2 Mario Rossi

Storing data

If we wanted to store the above data in Redis, how would you design the database schema in Redis? It may be more intuitive to look at the app's vision. Using SQL, we obtain a user information in select by specifying a user ID, in other words, a way to differentiate between different data entities, so we can identify and obtain user information through a unique identity. So if the user ID information is added to the Redis key, then our query needs are resolved, and in Redis, the data is stored in the following form:

Key Value
User:1:username user1
User:1:password Pass1
User:1:name Bob
User:1:surname Smith
User:2:username User2
User:2:password Pass2
User:2:name Mario
User:2:surname Rossi

So, to be a user ID, we can read the user information in the form of key user:id:username,user:id:password,user:id:name,user:id:surname.

User Login

The above storage format can also be used for user logins, but requires a way to query the user's ID based on username. That means we also need to establish a connection between username and ID. This can be done by adding another Redis key "User:username:id".

Key value
User:user1:id 1
User:user2:id 2

Now if Mario Rossi wants to log in, we can get all the information from the user by first identifying the username through the key "User:user2:id".

Primary key

How is the uniqueness of the ID value guaranteed in Redis? In SQL, you can define the self-increment key by "ID int primary KEY auto_increment", and now we need a similar way to generate a different ID for each user. Based on the numeric data mentioned in the previously available data types, the scheme in Redis is to create a key "user:next_id" and use it as a counter, executing the INCR command on key "user:next_id" whenever a new user is added.

SELECT * from users;

The next challenge is to query the list of users. Perhaps you think the data store above us is enough to query the list of users: You can get the current value of "user:next_id" counter, and then walk through 0 to counter to get user data in one or more steps. But if a user is removed from the system (the deletion is described below), and we traverse all the IDs from 0 to counter, some IDs will not be queried for any data.

Although this is usually not a problem, we do not want to waste time on nonexistent user data, so we need to create another key "User:list" whose value is a list or set type that stores each new user ID and, if necessary, from "User:list" The ID is removed from the I prefer to use list because it can be paged through the Lrange command.

Delete User

Another problem to be faced with is "data integrity" and see what happens when we delete a user. We need to remove every reference to this user, that is to say, delete all of the following key "user:id:*", "User:username:id", and the user ID in "user:list".

Explore FOUR: A simple use case

To learn how to do this, we try to design a virtual library and group books according to the subject. The following example is slightly more complicated than the user table above, but you will learn how to handle correlation relationships in Redis.

In the application, we need to collect books and store their title,author (s), topic (s), pages, Price, ISBN and description. Obviously some books have more than one author, and it may cover different topics (for example, a book can be a programming theme, or it can be a description of Ruby programming). Another author may have written a lot of books, and a subject will inevitably contain many books. As you can see, there are many-to-many relationships between authors and books, themes, and books.

SQL scenario

First, we tried to use SQL data tables to build a data model for this scenario so that we could simulate it more intuitively in the Redis domain:

Books

ID title pages Price ISBN description
1 Programming Ruby 829 $26 0974514055 Ruby programming language
2 Erlang Programming 496 $42 0596518188 An introduction to Erlang

Authors

ID Name Surname
1 Dave Thomas
2 Chad Fowler
3 Andy Hunt
4 Francesco Cesarini
5 Simon Thompson

Topics

ID Name Description
1 Programming Books about programming
2 Ruby Books about Ruby
3 Erlang Books about Erlang

Books-authors

book_id author_id
1 1
1 2
1 3
2 4
2 5

Books-topics

book_id topic_id
1 1
1 2
2 1
2 3

Redis scene

The previous section has described how to store data in Redis, so it's not a problem to understand the three tables of books,authors and topics here. But when confronted with the many-to-many associations between Books-authors and Book-topics, the problem becomes complicated. Below take topic as an example to see how to solve the relationship between book and topic, once it is clear, the relationship between book and author will be solved.

For each book, we need to know which topics it belongs to, and for each topic, it also deals with each book it contains. In other words, for each book, you need a list of IDs that store the topic associated with it, and for each topic, you also need a list of the IDs that store its associated books. That's where set is. We will create two sets: "Book:id:topic" and "Topic:id:books", which save the book's Topics ' ID list, which stores topic's Books ' ID list. Taking the data in the previous SQL scenario, for example, the book "Programming Erlang" (ID 2 in the Books table), there will be a key of "book:2:topics", value is the set type and data is (1,3) data, and the subject " Programming "There will be a data set with a key of" topic:1:books "and a value of ().

After analysis, the data model of Redis scenario is obtained:

Authors

Strings
-Author:id
-Author:id:name
-Author:id:surname

Sets
-Author:id:books

Lists
-Authorlist

Books

Strings
-Book:id
-Book:id:title
-Book:id:pages
-Book:id:price
-BOOK:ID:ISBN
-Book:id:description

Sets
-Books:id:authors-books:id:topics

Lists
-Book:list

Topics

String
-Topic:id
-Topic:id:name
-Topic:id:description

Sets
-Topic:id:books

Lists
-Topic:list

As you can see, many-to-many associations in SQL can be implemented with two sets in Redis. You'll find this kind of implementation quite useful, and it gives us the ability to get other information freely: you can get books that belong to multiple topics by working on the intersection of all the "topic:id:books" collections of interest. For example, the intersection of the collection "Topic:1:books" (programming Theme) and the "Topic:2:books" (Ruby theme) will result in a collection of only one element (1) for the Id=1 book: Programming Ruby.

For this implementation, you must pay particular attention to the deletion of the data. Because there is a reference to the books in the topics, the same books has a reference to the topics, how does the deletion work? To delete the data in books, the first thought is to delete the data for each key "book:id:*", but before you do this, you need to walk through the collection of all key "Topic:id:books" in topics and remove the ID of the book you want to delete from it. Of course, you should also remove this ID from the list of key "Book:list" in books. If you want to delete a topic, the operation is very similar: before removing all the key "topic:id:*" information from topics, you need to traverse the Books:id:topics ID set of the key "topic" in books. and remove the ID of the topic that you want to delete, and also remove the ID from the Topic:list list. The same operation applies to authors as well.

Discovery Five: Back home

For Redis's exploration into a paragraph, now look back and see what we've learned in our travel bags.
We learned about data types and operations commands in Redis, as well as some other interesting things. Is there a few memorable stories:

    • Resolve unique self-key problems by executing the INCR command on string data
    • Handle user login scenarios with the meaning rich key: "User:username:id"
    • Multi-to-many correlation between data by set

So far, the Redis journey is over and hope has not brought you unhappiness. Finally put on a pair of Liang Ji: Have fun coding free software!

Redis Learning: How to jump out of SQL this pit

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.