Redis Learning (i)

Source: Internet
Author: User


I. Introduction to Redis


Redis is a memory-based, durable, journaled, Key-value high-performance storage system. The keys are used to identify blocks of data. Values are the actual values associated with the keyword, which can be anything. Sometimes you store strings, sometimes integers, and you store serialized objects (using JSON, XML, and so on). In most cases, Redis will treat values as a sequence of bytes without paying attention to what they are essentially.


redis supports stored value types: String (string), hash (hash), list (linked list), set (set), and Zset (ordered collection). These data types support more operations such as Push/pop, Add/remove, and intersection sets and difference sets, and these operations are atomic in nature. Redis supports sorting in different ways. To ensure efficiency, the data is cached in memory. Redis periodically writes the updated data to disk or writes the operation to the appended record file, and implements the Master-slaver (master-Slave) synchronization on this basis.  


Redis is primarily used for caching, data persistence, Message Queuing, and so on, where data is not always stored in memory, which is different from memcached. Redis caches only all key information, and if the memory usage exceeds a certain threshold, the operation that will trigger the swap will persist the value corresponding to some key to disk and be purged in memory. This feature allows Redis to maintain data that is larger than the memory size of its machine itself. When reading data from Redis, if the value of the read key is not in memory, then Redis needs to load the corresponding data from the swap file and then back to the requester, there is an I/O thread pool problem, the default is blocked. So the best way to use Redis is to have all the data in memory.


two. Data structures supported by Redis1. String (Strings)


We have seen a common string use case where an instance of an object is stored by means of a keyword. Sometimes you will use this kind of operation frequently:


set users:leto "{name: leto, planet: dune, likes: [spice]}"
Redis also has some common operations. For example, it can be used to get the length of thestrlen <key>corresponding value of a keyword, thegetrange <key> <start> <end>corresponding value of the keyword in the specified range will be returned, and value will beappend <key> <value>appended to the existing keyword value (if the keyword does not exist, a new keyword-value pair will be created). 2. Hash (hashes)


Hash data structures are much like string data structures. The significant difference between the two is that the hash data structure provides an additional layer of indirection: a Domain (field). Therefore, the and in the hash datasetstructuregetare:


hset users:goku powerlevel 9000hget users:goku powerlevel


Related actions also include setting up multiple domains at the same time, getting multiple domains at the same time, getting all the fields and values, listing all domains, or deleting a specified domain:


hmset users:goku race saiyan age 737hmget users:goku race powerlevelhgetall users:gokuhkeys users:gokuhdel users:goku age
as you can see, the hash data structure has more operability than the normal string structure. We can use a hash data structure to get a more precise description of what is stored as a user rather than a serialized object. The benefit is the ability to extract, update, and delete specific pieces of data without having to fetch or write the entire value. 3. List (Lists)


For a given keyword, the list data structure allows you to store and manipulate a set of values. You can add a value to the table, get the first or last value of the list, and process the value with the given index. The list data structure maintains the order of values and provides efficient index-based operations. To keep track of the latest users registered on the site, we can maintain anewuserslist of:


lpush newusers gokultrim newusers 0 50
ltrimThe specific composition of the order isLTRIM Key start stop. To understandltrima command, first understand that the value stored by the key is a list, in theory the list can hold any number of values. For the specified list, based on the two range parameters provided, start and stop, theltrimcommand removes the values outside the specified range, leaving only the values in the range. )4. Collection (sets)


Collection data structures are often used to store only unique values and provide many collection-based operations, such as a set. The collection data structure does not sort the values, but it provides efficient value-based operations. A typical use case for using a collection data structure is the implementation of a friend list:


sadd friends:leto ghanima paul chani jessicasadd friends:duncan paul jessica alia


Regardless of how many friends a user has, we can effectively (O (1) Time complexity) identify the user X is a friend of user y:


sismember friends:leto jessicasismember friends:leto vladimir


Also, we can look at two or more people who are not having a common friend:


sinter friends:leto friends:duncan


You can even store the results in a new keyword:


sinterstore friends:leto_duncan friends:leto friends:duncan
5. Classification Set (Sorted sets)A categorical collection data structure is similar to a collection data structure, with the main distinction being the concept of markup (score).


tags provide the ability to sort (sorting) and Rank Division (ranking). If we want a rank-categorized list of friends, you can do this:


zadd friends:duncan 70 ghanima 95 paul 95 chani 75 jessica 1 vladimir


Forduncanfriends, how do you calculate the number of people with a mark (score) of 90 or higher?


zcount friends:duncan 90 100


How do I getchanirank (rank) in the list?


zrevrank friends:duncan chani


Thezrankspecific composition of the command is toZRANK Key menberknow that the sorted set of the key store defaults to the ascending arrangement of the individual menber according to score, which is used to obtain the order of the menber in that arrangement, which is called the rank. )


We used thezrevrankCommand rather thanzrankcommand, which is because the default sort of Redis is from low to high, but in this case our rank division is from high to low. The most common use case for categorical collection data structures is to implement a leaderboard system.three. Redis Application background1. Performance requirements, as the amount of read operations need to be resolved, the process of experience is:database read/write separation (M/s) –> database use multiple slave–> to increase cache (memcache) –> go to Redis
2. Solve the problem of writing: horizontal splitting, splitting the table, there will be users in this table, some users placed in another table;
3. Reliability Requirements: Cache "Avalanche" problem is tangled, the cache is facing the challenge of rapid recovery;
4. Development cost Requirements: cache and DB consistency maintenance costs are getting higher (clear db, then clean up the cache, no, it's too slow!)
5. Development needs to keep up with the constantly influx of product demand: the most expensive hardware cost is the database level of the machine, basically more expensive than the front-end machine several times, mainly IO-intensive, very consumption of hardware;
6. Maintenance Complex: Consistent maintenance costs are increasingly high;
7.BerkeleyDB use B-tree, will always write new, there will not be a file re-organization, which will lead to larger files, the need for file archiving, archive operations to be done on a regular basis;
In this way, it is necessary to have a certain down time;

four. Redis Performance features


1. Rich data support;


2. Mainly used to store a small amount of data, support more than 100k+ per second read and write frequency; 3.Redis Backup of supporting data, i.e. data backup of Master-slave mode (master-slave replication);4.redis supports atomic operations,5. Using distributed Redis to achieve massive data storage, ensure data consistency;6. Rich features: Redis also supports publish/subscribe, notifications, key expiration and other features.


Redis Learning (i)


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.