Introduction, data structure and application scenario of redis [1]

You and other students 2022-02-13 08:09:13 阅读数:288

introduction data structure application scenario


This article has been arranged since childhood d Class notes and java Advanced warehouse , any similarity , Most of them are written by others

java Advanced warehouse :https://doocs.github.io/advanced-java/#/

Redis [1] brief introduction 、 data structure 、 Application scenarios

brief introduction :Nosql Introduction and Reidis Introduce

What is? Redis

Belong to NoSQL Of ⼀ Kind of ( Not Only SQL )

It is a general term of database management system different from traditional relational database. The most important difference between the two is NoSQL Don't make ⽤SQL As a query

language ⾔.

NoSQL Data storage may not require fixed table mode keys - Value of storage , Column store ,⽂ File storage , Graphic database

NoSql:redis、memcached、mongodb、Hbase

What is caching

Procedures often need to be adjusted ⽤ The object of exists in memory ,⽅ Let it be ⽤ It can be adjusted quickly ⽤,

There is no need to query in the database or other persistent devices

The main That is to say ⾼ performance DNS cache 、 Front end cache 、 proxy server 、 cache Nginx、 Should be ⽤ Program cache 、 Database cache

Noodles : Why cache

Using cache , It has two main advantages : High performance High concurrency .

High performance

It refers to multiple users accessing at the same time , The program can return the data required by the user as soon as possible , If you check the database every time , It is bound to increase the pressure on the server and database , And after using the cache , Subsequent requests can be returned as soon as possible

High concurrency

mysql Single support to 2000QPS It's getting easier to call the police

Redis The speed at which you can read is 11w Time /s, The speed of writing is 8w Time /s

Cache is memory driven , Memory naturally supports high concurrency .

Noodles : What are the problems with caching ?

There are three main aspects

The cache is inconsistent with the data stored in the database

Cache penetration 、 Cache avalanche 、 Cache breakdown

Cache concurrent contention

make a concrete analysis :

How to ensure the double write consistency between cache and database ?:https://doocs.github.io/advanced-java/#/docs/high-concurrency/redis-consistence

Understand what is Redis The avalanche of 、 Penetration and breakdown ?Redis What happens after the crash ? How should the system deal with this situation ? How to deal with it Redis The penetration ?:https://doocs.github.io/advanced-java/#/docs/high-concurrency/redis-caching-avalanche-and-caching-penetration

Redis What is the concurrent competition problem of ? How to solve this problem ? understand Redis The transaction CAS The plan ?

https://doocs.github.io/advanced-java/#/docs/high-concurrency/redis-cas

hotspot key One of the solutions for

Avoid bandwidth or transmission impact , Local cache hotspot key data , For each read request , Will first check key Whether it exists in the local cache , If it exists, it directly returns , If there is no machine to access the distributed cache

  • Some in the cache Key Corresponding value A machine stored in a cluster , Make all traffic flow to the same machine , Become the bottleneck of the system , Cannot be solved by increasing the capacity of the machine

  • Hot goods 、 hot news 、 Hot reviews 、 Big V Stars get married

  • High performance :Redis The speed at which you can read is 110000 Time /s, The speed of writing is 81000 Time /s

  • Data structure storage system in memory , It can be used as a database 、 Caching and message middleware . It supports many Types of data structures , Such as character string (strings)、 hash (hashes)、 list (lists)、 aggregate (sets)、 Ordered set (sorted sets) etc.

  • characteristic :aof/rdb、 High performance reasons 、key Design 、 hotspot key、 Elimination algorithm

strings: Verification Code 、 Counter

hashes: The shopping cart 、 User information

lists: The list 、 Latest comment page

sets: duplicate removal 、 Common friends

sorted sets : Real time list 、 Priority queue

Data structure and its application scenario

String structure

Introduce

Store string type key-value

Application scenarios

Verification Code

Counter 、 The transmitter

Order duplicate submission token

Hot commodity cards ( serialize json Object storage )

Distributed lock

Be careful

The length of the value cannot exceed 512 MB

key Naming specification , Don't be too long , Colon division , Business name : Table name :ID

List structure

Introduce

String list , Sort by insertion order

Double linked list , Insertion and deletion time complexity O(1) fast , Search for O(n) slow

Application scenarios

Simple queue

The latest comment list

​ Cache only the first page

Non real time Leaderboard : Regularly calculate the list , Such as mobile phone daily sales list

​ Calculate regularly every day , Then store it in the cache , Easy to get results

​ Space for time

Be careful

Usually add an element to the head of the list ( On the left ) Or tail ( On the right )

All the storage is string String type

Support paging operation , In high concurrency projects , The first page of data is the source list, The second page and more information are loaded through the database

A list can contain at most 2^32 - 1 Elements (4294967295, No more than... Per list 40 100 million elements )

Hash structure

Introduce

It's a string Type of field and value Mapping table ,hash Ideal for storing objects

Application scenarios

The shopping cart

​ hash structure ,cart:userId Corresponding One hashmap

​ key by productId, value by json character string , The content is shopping specifications

User profile

Goods details

Be careful

Every hash Can be stored 2^32 - 1 Key value pair (40 More than )

Set structure

Introduce

Add one or more member elements to the collection , Member elements that already exist in the collection will be ignored

Can do intersection 、 Difference set 、 Combine

Application scenarios

duplicate removal

Social apps focus on 、 fans 、 Common friends

​ Common friends , Friends make intersection

​ Recommendation set can be used to do bad

Statistics website of PV、UV、IP

A collection of user portrait labels in big data

Be careful

Collections are implemented through hash tables

Sorted Set structure

Introduce

Used to add one or more member elements and their fractional values to an ordered set

If a member is already a member of an ordered set , So update the score of this member , Fractional values can be integer values or double precision floating-point numbers .

An ordered set can be seen as Set A sequential value is maintained for each element in the set on the basis of the set : score, It allows the elements in the collection to follow score Sort

Application scenarios

Real time leaderboard : Best selling list 、 Popular sports application teams 、 League table

Priority tasks 、 queue

Circle of friends I like the article - Cancel , Logic : Users can only like or cancel , Count how many times an article has been praised , You can directly get how many members there are

Be careful

The bottom layer is used Ziplist Compress lists and “ Skip list ” Two storage structures

If you add the same data repeatedly ,score Values will be repeatedly overridden , Keep the result of the last modification


General Command

exists

Judge key Whether there is

del

Delete key

type

Judge key type

ttl

see key Survival time

String structure

Introduce

Store string type key-value

Application scenarios

Verification Code

Counter 、 The transmitter

Order duplicate submission token

Hot commodity cards ( serialize json Object storage )

Distributed lock

Be careful

The length of the value cannot exceed 512 MB

key Naming specification , Don't be too long , Colon division , Business name : Table name :ID

Common commands

set/get

Set and get key-value

mget/mset

· Batch set or get multiple key Value

· MGET key [key …]

· MSET key value [key value …]

incr

· incr Yes key Add the corresponding value 1 operation , And return the new value ;

· incr key

incrby

· take key The corresponding number plus increment. If key non-existent , Before the operation ,key It will be set to 0

· incrby key increment

setex

· Set up key Corresponding string value, And set up key In the given seconds Time out after time out , Atomic manipulation

· SETEX key seconds value

setnx

· take key Set the value to value, If key There is no equivalent SET command . When key Do nothing when you exist , yes set if not exists Abbreviation .

· SETNX key value

getset

· Set up key Value , And back to key Old value

· GETSET KEY_NAME VALUE

List structure

Introduce

String list , Sort by insertion order

Double linked list , Insertion and deletion time complexity O(1) fast , Search for O(n) slow

Application scenarios

Simple queue

The latest comment list

​ Cache only the first page

Non real time Leaderboard : Regularly calculate the list , Such as mobile phone daily sales list

​ Calculate regularly every day , Then store it in the cache , Easy to get results

​ Space for time

Be careful

Usually add an element to the head of the list ( On the left ) Or tail ( On the right )

All the storage is string String type

Support paging operation , In high concurrency projects , The first page of data is the source list, The second page and more information are loaded through the database

A list can contain at most 232 - 1 Elements (4294967295, No more than... Per list 40 100 million elements )

Common commands

lpush

· Insert one or more values into the list header

· LPUSH key value1 [value2]

rpop

· Remove and get the last element of the list

· RPOP key

llen

· Get list length

· LLEN key

lindex

· Get the elements in the list by index

· LINDEX key index

lrange

· obtain key Corresponding list The element of the specified subscript range , among 0 Represents the first element of the list , 1 Represents the second element of the list , -1 Means to get all elements ( lrange key 0 -1);

· LRANGE key start stop

rpush

· stay key Corresponding list Add an element to the end of

· RPUSH key value1 [value2]

lpop

· from key Corresponding list Delete an element at the end of , And return the element

· LPOP key

brpop

· Move out and get the last element of the list , If there are no elements in the list, it will block the list until the wait timeout or pop-up elements are found

· BRPOP LIST1 LIST2 … LISTN TIMEOUT

lrem

· Remove elements , You can specify how many to remove

· LREM KEY COUNT VALUE

Hash structure

Introduce

It's a string Type of field and value Mapping table ,hash Ideal for storing objects

Application scenarios

The shopping cart

​ hash structure ,cart:userId Corresponding One hashmap

​ key by productId, value by json character string , The content is shopping specifications

User profile

Goods details

Be careful

Every hash Can be stored 2^32 - 1 Key value pair (40 More than )

Common commands

hset

· Set up key The value of the specified field in the specified hash set

· HSET key field value

hget

· return key The value associated with the field in the specified hash set

· HGET key field

hgetall

· return key All fields and values in the specified hash set

· HGETALL key

hdel

· from key Remove the specified domain from the specified hash set

· HDEL key field [field …]

hexists

· return hash Inside field Whether there is

· HEXISTS key field

hincrby

· increase key The value of the specified field in the specified hash set , If it is -1 Is decreasing

· HINCRBY key field increment

hmset

· Set up key The value of the specified field in the specified hash set

· HMSET key field value [field value …]

hmget

· return key The value of the specified field in the specified hash set

· HMGET key field [field …]

Set structure

Introduce

Add one or more member elements to the collection , Member elements that already exist in the collection will be ignored

Can do intersection 、 Difference set 、 Combine

Application scenarios

duplicate removal

Social apps focus on 、 fans 、 Common friends

​ Common friends , Friends make intersection

​ Recommendation set can be used to do bad

Statistics website of PV、UV、IP

A collection of user portrait labels in big data

Be careful

Collections are implemented through hash tables

Common commands

sadd

· Add one or more specified member Element to set key in . One or more elements specified member If it's already assembled key If it exists in, it ignores

· SADD key member [member …]

scard

· Returns the... Stored in the collection key Base number of ( The number of collection elements ).

· SCARD key

sdiff

· The returned collection element is the first key The set of is the same as all the following key The difference set of the set of

· SDIFF key [key …]

sinter

· Returns the intersection of all members of the specified set .

· SINTER key [key …]

sismember

· Return members member Is it a collection of storage key Members of .

· SISMEMBER key member

srem

· stay key Remove the specified element from the collection . If the specified element is not key Elements in the collection are ignored

· SREM key member [member …]

sunion

· Returns all members of the union set of a given set .

· SUNION key [key …]

smembers

· return key Gather all the elements .

· SMEMBERS key

Sorted Set structure

Introduce

Used to add one or more member elements and their fractional values to an ordered set

If a member is already a member of an ordered set , So update the score of this member , Fractional values can be integer values or double precision floating-point numbers .

An ordered set can be seen as Set A sequential value is maintained for each element in the set on the basis of the set : score, It allows the elements in the collection to follow score Sort

Application scenarios

Real time leaderboard : Best selling list 、 Popular sports application teams 、 League table

Priority tasks 、 queue

Circle of friends I like the article - Cancel , Logic : Users can only like or cancel , Count how many times an article has been praised , You can directly get how many members there are

Be careful

The bottom layer is used Ziplist Compress lists and “ Skip list ” Two storage structures

If you add the same data repeatedly ,score Values will be repeatedly overridden , Keep the result of the last modification

Common commands

zadd

· Add one or more members... To an ordered collection , Or update scores of existing members

· ZADD key score1 member1 [score2 member2]

zcard

· Get the number of members of the ordered set

· ZCARD key

zcount

· Calculates the number of members of a specified interval fraction in an ordered set

· ZCOUNT key min max

zincrby

· ZINCRBY key increment member

· Add the increment... To the score of the specified member in the ordered set increment

zrange

· Return the members in the specified interval of the ordered set through the index interval , The position of members is incremented by the value of points ( From small to large ) Sort by

· ZRANGE key start stop [WITHSCORES]

zrevrange

· Return the members in the specified interval of the ordered set through the index interval , The position of members is incremented by the value of points ( From big to small ) Sort by

· ZREVRANGE key start stop [WITHSCORES]

zrevrank

· Returns the rank of a specified member in an ordered set , Members of an ordered set are decremented by fractions ( From big to small ) Sort

· ZREVRANK key member

zrank

· Return to ordered set key Member of the member Ranking . The members of the ordered set press score Value increment ( From small to large ) Sequential arrangement

· ZRANK key member

zrem

· Remove one or more members of an ordered collection

· ZREM key member [member …]

zscore

· Back to the ordered set , The score of a member

· ZSCORE key member

copyright:author[You and other students],Please bring the original link to reprint, thank you. https://en.javamana.com/2022/02/202202130809093104.html