Overview and simple use of Redis (reprint)

Source: Internet
Author: User
Tags redis tutorial value store

Article Source: http://jingyan.baidu.com/article/db55b60996d0124ba30a2f92.html

Redis is a key-value-based cache system, similar to memcached, but supports more complex data structures list, set, Sorted set, and has persistent functionality.

Since the recent work has been used in many places, it took a lot of time to read the article, coding experiments, to understand what Redis can do, what performance.

The first question you'll encounter first is what is Redis?

This question seems ridiculous, but I do agree with Timyang's view that the architect's understanding of Redis differs and the positioning is different, which determines what role Redis will play in the entire system structure. I conclude that there are 3 ways to understand the mainstream:

1.key value Store. is a database stored in key-value form, located directly to MySQL, used as the only storage system.

2.memory cache. is a buffer that stores data in memory to provide buffering between applications and databases, instead of MEMCACHD.

3.data structrue Server. It supports the high-speed operation of complex data structures as a selling point, providing computing and presentation requirements for some special business scenarios. such as leaderboard applications, Top 10 and the like.

More people are still positioning it as a memcached upgrade, providing more data structure operations, still a cache.

Traditional memcached have some drawbacks in a business scenario similar to the SNS community. For example, storing a friend relationship has to be saved with a long string separated by special characters. Under the business requirement that the friend relationship has no upper limit, the operation performance is low and the performance level of the cache system is not reached. and mapping from a relational structure in a database to a long string form in the cache is obviously a crappy part of the architecture.

The list, set, and sorted set provided by Redis can map a good business model to the corresponding data structure with a high degree of fit. As I understand it, the relational database theory can be copied almost to the application of Redis.

The faux Twitter case in the official Redis tutorial is a great starting point. Using the set structure to store follower and following, using the list structure to save everyone's post, plus some common key-value to store user basic information, is very intuitive and clear.

I'll give you a good friend's business scenario to describe my understanding of how the standard relational database structure is mapped to the Redis storage architecture one by one.

There are 3 tables in the database:

1. The user table has two columns: ID, nickname

2. The Friend relationship table has columns: User ID, friend ID, group ID, Friend's note, add friend time

3. Grouping table: Group ID, group name, owning user ID

Add or delete a friend, either insert or delete a record in the Buddy relationship table.

Get a user's group of all friends, and the number of friends in the group:

1 Select G.gid, G.gname,count(f.fuid)23fromgroups G left join Friends F45 ong.gid=f.gid67 where g.uid= #uid # 8 9 GROUP by G.gid, G.gname

Get a list of friends under a group of users:

1 Select F.fuid, U.nickname, F.remark, F.time 2 3 From friends F left Join users u 4 5 onf.fuid=u.id  6  7 where f.uid=#uid #andf.gid= #gid # 8 9 ORDER BY F.time Ten  One Limit #start #, #Count#

Take a look at how Redis implements similar business scenarios.

User nickname: Uid:xxxxx:nickname, stored as a string structure, equivalent to the user table

Group name: gid:yyyyy:gname, stored as a string structure

User-owned groups: Uid:xxxxx:groups, sets the set structure to store the set of GID

Group under Friends: Gid:yyyyy:friends, store in set structure, save Fuid Collection

Friends: Uid:xxxxx:fuid:zzzzz:gid, Uid:xxxxx:fuid:zzzzz:remark, uid:xxxxx:fuid:zzzzz:time each use string structure storage, Equivalent to each field of the Friends table

Add a friend need to put Uid:xxxxx:fuid:zzzzz:gid, Uid:xxxxx:fuid:zzzzz:remark, Uid:xxxxx:fuid:zzzzz:time these three fields set good, and then Sadd GID: Yyyyy:friends zzzzz, add friends to this group's collection

To get a user's group of all friends, and the number of friends in the group, you need to use smembers to get the GID in the Uid:xxxxx:groups collection, and then use these GID to SCard gid:yyyyy:friends to get the number of friends under the group.

Get user 123456 friends list under Group 1001:

1Sort GID:1001: Friends2 3  byUid:123456: Fuid:*: Time4 5Limit0 Ten6 7 Get #8 9Get UID:*: NicknameTen  OneGet UID:123456: Fuid:*: Remark A  -Get UID:123456: Fuid:*: Time

It's interesting, isn't it, like a SQL statement, the * symbol in key is a placeholder that can be replaced with the result of the sort, and get to value inside the dynamic key.

We can conclude that the traditional relational database, dealing with a one-to-many problem, requires the foreign key to be placed on a multi-end, because the RDBMS theory does not have a direct concept of aggregation. With Redis, we can intuitively manage a one-to-many relationship using set.

This is only the use of the difference, and theoretically, the theory of RDBMS can be applied to the Redis, all with relational database theory can be described by the structure, with the data structure of Redis can be achieved.

Most crucially, if you use MySQL, when the data size is very large, the above two query operations need to rely on the table Association technology, and large table joins in large systems is a need to avoid the operation. Instead, each operation of Redis is confined to a smaller dataset, and the Key-value storage form, locating key is just a complex O (1) operation. With the very huge data volume, Redis performance is excellent, which is the advantage of NoSQL!

Overview and simple use of Redis (reprint)

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.