Go Redis usage scenarios and usage experience

Source: Internet
Author: User
Tags set set

Redis is an open source (BSD licensed), In-memory data structure store!

Just ended a game activity project, due to the estimated number of participants, the amount of data will be very large, in order to achieve better game results, so decided to abandon the previous MySQL as a secondary redis storage, but mainly Redis, MySQL as a supplement (responsible for the landing of some important user data), The goods received a lot during the period.

I. Data structures in Redis and common usage scenarios

Common REDIS Data Structures:

    • String # Key-value pairs
    • List # Lists
    • Hash # Hashes
    • Set # Set
    • Sorted Set # ordered set

Each data structure has its own appropriate usage scenario, but remember not to hold a hammer in your hand and see what is a nail . In addition, when the amount of data stored is large, pay attention to the time complexity of each operation command

1, String

Common commands:  set,get,decr,incr,mget and so on. 

Scenario: The string type is the simplest and most commonly used REDIS data structure, the Key/value format can completely replace memcached as a cache server, and the single-machine test results show that Redis works better.

Set, get: The simplest data cache

Mset, Mget: Batch operation, unified data back to the client, save network IO time

DECR, INCR: Counter

Append command: Can be used as a time series, with GetRange, SetRange, the operation of the string, the current Redis also wood pruning operations

Setbit, Getbit: A good command of memory, can be used as a simple Boolean filter to determine whether the user has performed certain actions

2. List

Common commands: Lpush,rpush,lpop,rpop,lrange and so on.

Application Scenario: List has a lot of application scenarios, the application is quite extensive

Lpush, Lpop: Natural queue Operation , easy to implement queue tasks, celery storage containers we chose Redis.

Lpush, LTrim: show the latest data, very useful! For example: The top of the game on the marquee, you can use these two commands to store the latest 50 records

There are other operations: plug-in Blpop, Lrange (O (n)), Lindex (O (n)), Linsert (O (n)), Llen (O (1)), Lrem (O (n)), LSet (O (n))

3, Hash

Common commands: Hget,hSet,hgetall, etc. 

Application scenario: Previously in memcached if you save a large data, often with serialization after the save, removed to use after deserialization, that is not economical, in high concurrency there is also an atomicity problem, in Redis, with a hash, so easy!

Hget, Hset: implements a key corresponding to a set of data sets, the dataset contains multiple individual Key/value key-value pairs, the operation is still atomic

Hmget, Hmset, Hgetall: Batch operation, save network IO time OH

Hincrby: atomically adds 1 to the value of the hash field

Other operations: Hdel (O (n)), Hkeys (O (n)), Hexits (O (1)), Hvals (O (n)), Hscan (O (n))

4. Set

Common commands: Sadd,spop,smembers,sunion and so on.

Application scenario: Set is similar to a list, except that set is a set of deduplication and requires a data structure that is not duplicated, it is necessary to consider set

Sadd: Storing a data collection that does not duplicate data

Sunion, Sdiff, sinter: for collection processing , such as Weibo, a user's attention to the set set of everyone, through the set, intersection, difference set operation, to achieve ' common concern ', ' common preferences ', ' two friends ' and other functions

Other operations: Srem, Spop, SCard, Sismember, Smove, Srandmember

5. Sorted Set

Common commands: Zadd,zrange,zrem,zcard, etc.

Application scenario: Set is unordered, and sorted set as the name implies, it is ordered, composed of key, member and score, need an orderly and non-repeating data structure, it is necessary to consider sorted set

Zadd: Stores a collection of data sorted by score, automatically sorted when added, for example: priority queue , score for normal messages, 1 for important messages, and 2 for critical message, and worker threads can choose to get work tasks in reverse order of score. Let important tasks take precedence.

Zrange, Zrangebyscore, etc.: Get the data set in score order , for example, the time stream information of Weibo, and publish time as score. It can also be used to process outdated data. background tasks using Zrange ... Scores queries the sorted collection and takes out the latest 10 items. If the Unix time is found to have expired, delete the entry in the database.

Zrank: leaderboard function, score as poll result

Other operations: Zcard, Zcount, Zincrby, Zrem, Zscore, and set operations

Get here first, then record the deployment in this project and the Redis experience in the business

Go Redis usage scenarios and usage experience

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.