You and other students 2022-02-13 08:09:13 阅读数:288
java Advanced warehouse :https://doocs.github.io/advanced-java/#/
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
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
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 .
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
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
Store string type key-value
Verification Code
Counter 、 The transmitter
Order duplicate submission token
Hot commodity cards ( serialize json Object storage )
Distributed lock
The length of the value cannot exceed 512 MB
key Naming specification , Don't be too long , Colon division , Business name : Table name :ID
String list , Sort by insertion order
Double linked list , Insertion and deletion time complexity O(1) fast , Search for O(n) slow
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
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 )
It's a string Type of field and value Mapping table ,hash Ideal for storing objects
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
Every hash Can be stored 2^32 - 1 Key value pair (40 More than )
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
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
Collections are implemented through hash tables
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
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
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
Judge key Whether there is
Delete key
Judge key type
see key Survival time
Store string type key-value
Verification Code
Counter 、 The transmitter
Order duplicate submission token
Hot commodity cards ( serialize json Object storage )
Distributed lock
The length of the value cannot exceed 512 MB
key Naming specification , Don't be too long , Colon division , Business name : Table name :ID
Set and get key-value
· Batch set or get multiple key Value
· MGET key [key …]
· MSET key value [key value …]
· incr Yes key Add the corresponding value 1 operation , And return the new value ;
· incr key
· take key The corresponding number plus increment. If key non-existent , Before the operation ,key It will be set to 0
· incrby key increment
· 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
· 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
· Set up key Value , And back to key Old value
· GETSET KEY_NAME VALUE
String list , Sort by insertion order
Double linked list , Insertion and deletion time complexity O(1) fast , Search for O(n) slow
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
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 )
· Insert one or more values into the list header
· LPUSH key value1 [value2]
· Remove and get the last element of the list
· RPOP key
· Get list length
· LLEN key
· Get the elements in the list by index
· LINDEX key index
· 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
· stay key Corresponding list Add an element to the end of
· RPUSH key value1 [value2]
· from key Corresponding list Delete an element at the end of , And return the element
· LPOP key
· 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
· Remove elements , You can specify how many to remove
· LREM KEY COUNT VALUE
It's a string Type of field and value Mapping table ,hash Ideal for storing objects
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
Every hash Can be stored 2^32 - 1 Key value pair (40 More than )
· Set up key The value of the specified field in the specified hash set
· HSET key field value
· return key The value associated with the field in the specified hash set
· HGET key field
· return key All fields and values in the specified hash set
· HGETALL key
· from key Remove the specified domain from the specified hash set
· HDEL key field [field …]
· return hash Inside field Whether there is
· HEXISTS key field
· increase key The value of the specified field in the specified hash set , If it is -1 Is decreasing
· HINCRBY key field increment
· Set up key The value of the specified field in the specified hash set
· HMSET key field value [field value …]
· return key The value of the specified field in the specified hash set
· HMGET key field [field …]
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
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
Collections are implemented through hash tables
· 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 …]
· Returns the... Stored in the collection key Base number of ( The number of collection elements ).
· SCARD key
· 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 …]
· Returns the intersection of all members of the specified set .
· SINTER key [key …]
· Return members member Is it a collection of storage key Members of .
· SISMEMBER key member
· 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 …]
· Returns all members of the union set of a given set .
· SUNION key [key …]
· return key Gather all the elements .
· SMEMBERS key
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
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
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
· Add one or more members... To an ordered collection , Or update scores of existing members
· ZADD key score1 member1 [score2 member2]
· Get the number of members of the ordered set
· ZCARD key
· Calculates the number of members of a specified interval fraction in an ordered set
· ZCOUNT key min max
· ZINCRBY key increment member
· Add the increment... To the score of the specified member in the ordered set increment
· 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]
· 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]
· 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
· 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
· Remove one or more members of an ordered collection
· ZREM key member [member …]
· 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