( One )redis Use of Technology :

redis It's really a great technology , It can solve the concurrency of the website in a moment to a certain extent , For example, the rush to buy goods and other activities ...

redis The reason why high concurrency can be solved is that it can directly access memory , We used to use databases ( Hard disk ), Improved access efficiency , Solved the database server pressure .

Why? redis Is getting higher and higher , Why don't we choose memcache, that is because memcache Only strings can be stored , and redis There are plenty of storage types ( For example, there are strings ,LIST,SET etc. ),memcache Each value can only be stored at the maximum 1M, Storage resources are very limited , It consumes a lot of memory resources , and redis Can be stored 1G, most important of all memcache It's not as good as it is redis security , In case of server failure or accidental shutdown, etc ,redsi The data in memory will be backed up to the hard disk , and memcache All the stored things are lost ; This also explains memcache Not suitable for database , It can be used for caching .

The following is used redis Solve the instant second kill activity to explain :

The following program simulates 20w People rush into this page for seconds , Only those who can succeed in seconds 500 people , We put the advanced users into the redis In line , When the user in the queue reaches 500 Time , Later, the user turned to the end of the second kill page . Here we use random numbers to represent different users .

Here we can see the first successful user of seckill id yes 208522, The last user to succeed in seckilling is 176260, The total number of participants in the second kill is 20w.( The reason for your attention is to verify the accuracy below ).

Next, let's kill the second from the queue in turn 500 Users take it out and observe whether the first user and the last user are the same as the previous record value

We can see that the first user in the seckill queue is successful id yes 208522, The last user is 176260, It can be seen that the result is very accurate .

redis The ability to solve the problem of high concurrency is really good .

( Two )Redis Possible problems of high concurrency , solve :

1,  If redis It's down , Or not linked , What should I do? ?

resolvent :

    ① Configure master slave replication , Configure sentinel mode ( Equivalent to the elder level of ancient sects, you can choose the leader's right ), Once the host is found to be down , Let the next slave be the master .

    ② In the worst case , It can only be closed Redis connect , Go to the database connection . But because of the large amount of data , such SQL The database will crash .

 

2,  If redis The cache will expire at peak time , At this moment the request will be like an avalanche , How to deal with direct access to database ?

   
  Set condition query judgment , judge redis Is there data in the cache , without , Then go to the database connection . Distributed locks, of course , utilize redis Single thread of + multiple IO Reuse technology , Principle of atomicity , Let other threads request to wait , If the first thread goes in and gets the distributed lock, it goes down on the way to query the data , You cannot keep other threads waiting , Set to wait for a certain time to determine whether to retrieve the data , without , Recursively call its own method to let the second thread continue to query the database with the distributed lock . When the second lock gets the data from the database , Set the data value to redis In database cache , Set expiration time , Avoid taking up memory , Easy to use and improve efficiency .

3.    If the user keeps looking for a piece of data that doesn't exist , There is no cache , There is no database , So what happens

    If the data does not exist , No in cache , There is no database , Of course, if you don't set judgment , Will always call the database , Reduce database efficiency , When the number of visits is large, it will even be down .

   
Solution : Query from database , If the database does not , The return value is Null, Determine the value returned by the database , If Null, Then the user defined field will be saved to the Redis in , use key,value Methods ,jedis.setex(key,"empty"), The set failure time depends on the specific situation , Then call String
json=jedis.get(key), Determine whether to get the value "empty".equal(json), If equal , Then a custom exception is thrown , Prompt users , Or directly return
null. In this way, when the user queries again, it is necessary to reids Query in cache ,redis There will be a corresponding Key Gets the previously set value value , This will not call the database again , Affect efficiency and other issues .

The specific codes are as follows :
@Override public SkuInfo getSkuInfo(String skuId) { try {
Thread.sleep(3*1000); // Custom from redis In the tool class jedis object Jedis jedis =
redisUtil.getJedis(); // Concatenated string creation Redis Inside Key value String skuInfoKey=
JedisConst.SKU_PREFIX+skuId+JedisConst.SKU_SUFFIX; // according to key Value acquisition value value String
skuInfoJson = jedis.get(skuInfoKey); // If the return is empty , Call the local database connection if (skuInfoJson==null
|| skuInfoJson.length()==0){
System.out.println(Thread.currentThread().getName()+" No data was found in the current cache "); // Determine if someone has gone to pick up the lock
String skuLockKey=JedisConst.SKU_PREFIX+skuId+JedisConst.SKULOCK_SUFFIX; String
result = jedis.set(skuLockKey, "OK", "NX", "PX", JedisConst.SKULOCK_EXPIRE_PX);
if ("OK".equals(result)){
System.out.println(Thread.currentThread().getName()+" Get distributed lock "); SkuInfo skuInfo=
getSkuInfoDB(skuId); // If there is no value in the database , If someone else attacks maliciously , Set directly to redis In cache if (skuInfo==null){
jedis.setex(skuInfoKey,JedisConst.TIME_OUT,"empty"); return null; } skuInfoJson
= JSON.toJSONString(skuInfo); jedis.setex(skuInfoKey, JedisConst.TIME_OUT,
skuInfoJson); jedis.close(); return skuInfo; } else {
// Suppose someone takes the lock later dang It's gone , Recursively call yourself to find the key .
System.out.println(Thread.currentThread().getName()+" Distributed lock not obtained , Turn on spin mode, commonly known as recursion .");
// wait for 1 second Thread.sleep(1*1000); jedis.close(); return getSkuInfo(skuId); } }else
if (skuInfoJson.equals("empty")){ return null; } else {
System.out.println(Thread.currentThread().getName()+" There is already data in the cache being queried "); SkuInfo
skuInfo = JSON.parseObject(skuInfoJson, SkuInfo.class); jedis.close(); return
skuInfo; } }catch (JedisConnectionException e){ e.printStackTrace(); } catch
(InterruptedException e) { e.printStackTrace(); } return getSkuInfoDB(skuId);
redis What are the disadvantages of ?
     Is the database capacity limited by physical memory , Cannot be used as high-performance read-write for massive data , therefore Redis The suitable scenarios are mainly limited to high-performance operations and operations with small amount of data .

    Redis Difficult to support online expansion , When the cluster capacity reaches the upper limit, online capacity expansion becomes very complex . To avoid this problem , The operation and maintenance personnel must ensure that there is enough space when the system is online , This is a great waste of resources .

Technology
©2020 ioDraw All rights reserved
Feature Engineering Comparison of data extraction tools :Kettle,Datax,Sqoop,StreamSets Nikkei : Sony and Jiaxia are actively applying for Huawei's supply license Self made whole person computer program k8s delete pod2020 Nobel Prize in physiology or medicine announced Interview questions : Handwritten list ( Include reverse linked list ) Some views of a student on Hongmeng system Children programming children's learning route Programmer's promotion 28 Sorting code of Guan 3