当前位置:网站首页>Summary of redis basic knowledge

Summary of redis basic knowledge

2022-06-11 14:54:00 Ant

Redis Basic knowledge summary

What is? redis?

Redis Is a memory based high performance key-value database .

Reids Characteristics

Redis It's essentially a Key-Value Type of in-memory database , It's like memcached, The whole database is loaded in memory for operation , Periodically transfer database data through asynchronous operations flush Save to hard disk .

Because it's a pure memory operation ,Redis Performance is excellent , Can handle over per second 10 Ten thousand read and write operations , Is the fastest known Key-Value DB.

Redis It's not just the performance that's outstanding ,Redis The biggest attraction is to support multiple data structures , In addition, a single value The maximum limit of is 1GB, Unlike memcached Save only 1MB The data of , therefore Redis Can be used to achieve many useful functions .

Let's use his List To do it FIFO Double linked list , Achieve a lightweight high quality Can Message Queuing service , Use him Set Can do high performance tag System, etc. . in addition Redis You can also save Key-Value Set up expire Time , So it can also be treated as one A feature enhanced version of memcached To use .

Redis The main disadvantage is that the database capacity is limited by physical memory , Cannot be used as high-performance read-write of massive data , therefore Redis Suitable scenarios are mainly limited to high-performance operations and operations with small amount of data .

Use redis What are the benefits

  1. Fast , Because the data is in memory , Be similar to HashMap,HashMap The advantage is the time complexity of both the lookup and the operation O(1)
  2. Support for rich data types , Support string,list,set,sorted set,hash
  3. Support transactions , The operations are all atomic , Atomicity means that changes to the data are either performed in full , Or none at all
  4. Rich features : Available for caching , news , Press key Set expiration time , It will be deleted automatically after expiration

redis What data structures are supported

Support multiple types of data structures , Such as a string (String), hash (Hash), list (List), aggregate (Set), Ordered set (Sorted Set Or is it ZSet) And scope query ,Bitmaps,Hyperloglogs And geospatial (Geospatial) Index radius query . The common data structure types are :String、List、Set、Hash、ZSet this 5 Kind of .

Java Chinese vs Redis Data type operations

String
public class StringTest {
    public  Jedis jedis = JedisPoolUtil.getJedis();
    @Test
    // Add and get 
    public void fun(){
        jedis.set("num","1");
        System.out.println(jedis.get("num"));
    }
    @Test
    // Delete value 
    public void fun1(){
        jedis.del("num");
        System.out.println(jedis.get("num"));
    }
    @Test
    // Self subtraction and self subtraction 
    public void fun2(){
        jedis.set("num","1");
        System.out.println(jedis.get("num"));
        jedis.decr("num");
        System.out.println(jedis.get("num"));
        jedis.incr("num");
        jedis.incr("num");
        System.out.println(jedis.get("num"));
    }
    @Test
    // add / subtract   a number 
    //incrBy  The returned value is the modified value. If the original value is a string rather than a number , An exception will be thrown 
    public void fun3(){
        Long num = jedis.incrBy("num", 3);
        System.out.println(num);
        jedis.decrBy("num",10);

        System.out.println(jedis.get("num"));
        jedis.set("name","caopengfei");
        //jedis.decrBy("name",1);
    }
    @Test
    // String splicing 
    public void fun4(){
        Long len = jedis.append("name", "123");
        System.out.println(len);
        System.out.println(jedis.get("name"));
    }
}
Hash
public class HashTest {
    public Jedis jedis = JedisPoolUtil.getJedis();

    //    hash  The operation is map object 
//     Suitable for storing information about key value objects 
    @Test
    // Store value   Name of the first variable of the parameter , map Key name (key), map Key value (value)
//     call hset
    public void fun() {
        Long num = jedis.hset("hash1", "username", "caopengfei");
        System.out.println(num);
        String hget = jedis.hget("hash1", "username");
        System.out.println(hget);
    }

    @Test
    // You can also save multiple key
//     call hmset
    public void fun1() {
        Map<String, String> map = new HashMap<String, String>();
        map.put("username", "caopengfei");
        map.put("age", "25");
        map.put("sex", " male ");
        String res = jedis.hmset("hash2", map);
        System.out.println(res);//ok
    }

    @Test
    // obtain hash All the values of 
    public void fun2() {
        Map<String, String> map2 = new HashMap<String, String>();
        map2 = jedis.hgetAll("hash2");
        System.out.println(map2);
    }

    @Test
//     Delete hash The key   You can delete one or more , The number of deleted items is returned 
    public void fun3() {
        Long num = jedis.hdel("hash2", "username", "age");
        System.out.println(num);
        Map<String, String> map2 = new HashMap<String, String>();
        map2 = jedis.hgetAll("hash2");
        System.out.println(map2);
    }

    @Test
    // increase hash Key value pairs in 
    public void fun4() {
        Map<String, String> map2 = new HashMap<String, String>();
        map2 = jedis.hgetAll("hash2");
        System.out.println(map2);
        jedis.hincrBy("hash2", "age", 10);
        map2 = jedis.hgetAll("hash2");
        System.out.println(map2);
    }

    @Test
    // Judge hash Is there a value 
    public void fun5() {
        System.out.println(jedis.hexists("hash2", "username"));
        System.out.println(jedis.hexists("hash2", "age"));
    }

    @Test
    // obtain hash The number of key-value pairs 
    public void fun6() {
        System.out.println(jedis.hlen("hash2"));
    }

    //     Get one hash All of the key value 
    @Test
    public void fun7() {
        Set<String> hash2 = jedis.hkeys("hash2");
        System.out.println(hash2);
    }

    //     Get all value value 
    @Test
    public void fun8() {
        List<String> hash2 = jedis.hvals("hash2");
        System.out.println(hash2);
    }
}
List operation
public void testList()
{
    jedis.flushDB();
    System.out.println("=========== Add one list===========");
    jedis.lpush("collections", "ArrayList", "Vector", "Stack", "HashMap", "WeakHashMap", "LinkedHashMap");
    jedis.lpush("collections", "HashSet");
    jedis.lpush("collections", "TreeSet");
    jedis.lpush("collections", "TreeMap");
    System.out.println("collections The content of :"+jedis.lrange("collections", 0, -1));//-1 For the last element ,-2 Represents the penultimate element 
    System.out.println("collections Section 0-3 The elements of :"+jedis.lrange("collections",0,3));
    System.out.println("===============================");
    //  Delete the value specified in the list  , The second parameter is the number of deletions ( When there are repetitions ), after add The value entered is deleted first , It's like getting out of the stack 
    System.out.println(" Delete the specified number of elements :"+jedis.lrem("collections", 2, "HashMap"));
    System.out.println("collections The content of :"+jedis.lrange("collections", 0, -1));
    System.out.println(" Delete the following table 0-3 Elements outside the interval :"+jedis.ltrim("collections", 0, 3));
    System.out.println("collections The content of :"+jedis.lrange("collections", 0, -1));
    System.out.println("collections List out ( Left side ):"+jedis.lpop("collections"));
    System.out.println("collections The content of :"+jedis.lrange("collections", 0, -1));
    System.out.println("collections Additive elements , From the right end of the list , And lpush Corresponding :"+jedis.rpush("collections", "EnumMap"));
    System.out.println("collections The content of :"+jedis.lrange("collections", 0, -1));
    System.out.println("collections List out ( Right end ):"+jedis.rpop("collections"));
    System.out.println("collections The content of :"+jedis.lrange("collections", 0, -1));
    System.out.println(" modify collections Specify subscript 1 The content of :"+jedis.lset("collections", 1, "LinkedArrayList"));
    System.out.println("collections The content of :"+jedis.lrange("collections", 0, -1));
    System.out.println("===============================");
    System.out.println("collections The length of :"+jedis.llen("collections"));
    System.out.println(" obtain collections Subscript to be 2 The elements of :"+jedis.lindex("collections", 2));
    System.out.println("===============================");
    jedis.lpush("sortedList", "3","6","2","0","7","4");
    System.out.println("sortedList Before ordering :"+jedis.lrange("sortedList", 0, -1));
    System.out.println(jedis.sort("sortedList"));
    System.out.println("sortedList After ordering :"+jedis.lrange("sortedList", 0, -1));
}
Set operation
/*
* Set aggregate , and List The difference between classes is 
* set There will be no duplicate data in the 
*  It can perform aggregation operations with high efficiency 
*  The rest of the operation is basically the same as list identical 
*
* */
public class SetTest {
    public  Jedis jedis = JedisPoolUtil.getJedis();

    @Test
    /* Add element delete element */
    public void fun(){
        Long num = jedis.sadd("myset", "a", "a", "b","abc");
        System.out.println(num);

    }
    @Test
    /* Get elements */
    public void fun1(){
        Set<String> myset = jedis.smembers("myset");
        System.out.println(myset);
    }
    @Test
    /* Remove elements */
    public void fun2(){
        jedis.srem("myset","a","b");
        Set<String> myset = jedis.smembers("myset");
        System.out.println(myset);
    }
    @Test
    // Judge whether this set There is a value in 
    public void fun3(){
        Boolean sismember = jedis.sismember("myset", "a");
        System.out.println(sismember);
    }
    @Test
    // get A-B  Get the difference set 
    public void fun4(){
        jedis.sadd("myset1","123","32","abc","def","123456","sdfasd");
        jedis.sadd("myset2","abc","345","123","fda");
        Set<String> sdiff = jedis.sdiff("myset1", "myset2");
        System.out.println(sdiff);
    }
    @Test
    // Get intersection 
    public void fun5(){
        Set<String> sinter = jedis.sinter("myset1", "myset2");
        System.out.println(sinter);

    }
    @Test
//     Get and assemble 
    public void fun6(){
        Set<String> sunion = jedis.sunion("myset1", "myset2");
        System.out.println(sunion);
    }
    @Test
//     Number of members 
    public void fun7(){
        System.out.println(jedis.scard("myset1"));
    }
    @Test
//     Get a random member 
    public void fun8(){
        System.out.println(jedis.srandmember("myset1"));
    }
    @Test
//     Put the members of the difference into a new set Both the intersection and union of the same principle in can be followed by 
//     Add a store that will do 
//     And returns the new length 
    public void fun9(){
        System.out.println(jedis.sdiffstore("myset3","myset1","myset2"));
        System.out.println(jedis.smembers("myset3"));
    }
}
SortedSet operation
/*
 and set Very similar , They are a collection of strings , There's no duplicate data 
 The difference is sortedset Each member will have a score (score) Associated with it 
,redis It's the scores that sort the members of a collection from small to large 
sortedset The data in must be single, but his score It can be repeated 
 */
public class SortedsetTest {
    public  Jedis jedis = JedisPoolUtil.getJedis();
//     Additive elements 
    @Test
    public void fun(){
        jedis.zadd("mysort",100.0, "zhangsan");
        jedis.zadd("mysort",200.0,"lisi");
        jedis.zadd("mysort",50.0,"wangwu");
        Map<String ,Double>map = new HashMap<String ,Double>();
        map.put("mutouliu",70.0);

        jedis.zadd("mysort",map);
        Set<String> mysort = jedis.zrange("mysort", 0, -1);
        System.out.println(mysort);
        Set<String> mysort1 = jedis.zrange("mysort", 1, 2);
        System.out.println(mysort1);
    }
} 

redis comparison memcached What are the advantages

  1. memcached All values are simple strings ,redis As a substitute , Support for richer data types
  2. redis Faster than memcached Much faster
  3. redis You can persist its data

Memcache And Redis What are the differences

  1. storage Memecache Store all the data in memory , It hangs when the power goes out , Data cannot exceed memory size . Redis Some of it is stored on the hard drive , This ensures data persistence .
  2. Data support type Memcache Support for data types is relatively simple . Redis There are complex data types .
  3. Using the underlying model is different The underlying implementation between them And the application protocol of communication with the client is different . Redis Build it yourself VM Mechanism , Because the normal system calls system functions , It's a waste of time moving and requesting .

redis Common performance problems and solutions :

  1. Master Write memory snapshot ,save Command scheduling rdbSave function , Blocks the work of the main thread , The impact on performance is significant when snapshots are large , Intermittent suspension of service , therefore Master It is best not to write a memory snapshot .
  2. Master AOF Persistence , If you do not override AOF file , This persistence has minimal impact on performance , however AOF The file gets bigger and bigger ,AOF Too much paper will affect you Master The recovery rate of a restart .Master It's best not to do any persistence work , Includes memory snapshots and AOF Log files , In particular, do not enable memory snapshots for persistence , If the data is critical , Some Slave Turn on AOF The backup data , The policy is to synchronize once per second .
  3. Master call BGREWRITEAOF rewrite AOF file ,AOF It's going to take up a lot of when you rewrite it CPU And memory resources , Lead to service load Too high , Temporary suspension of service .
  4. Redis Performance issues with master-slave replication , For master-slave replication speed and connection stability ,Slave and Master Preferably on the same LAN

mySQL Are there in 2000w data ,redis The only known 20w The data of , How to ensure redis The data in is hot data

redis When the size of the memory data set rises to a certain size , Data elimination strategies will be implemented ( Recovery strategy ).

redis Provide 6 A data culling strategy :

  1. volatile-lru: From the set of data for which the expiration time has been set (server.db[i].expires) Select the least recently used data in
  2. volatile-ttl: From the set of data for which the expiration time has been set (server.db[i].expires) To select the data to be expired
  3. volatile-random: From the set of data for which the expiration time has been set (server.db[i].expires) In the arbitrary selection of data elimination
  4. allkeys-lru: From the data set (server.db[i].dict) Select the least recently used data in
  5. allkeys-random: From the data set (server.db[i].dict) In the arbitrary selection of data elimination
  6. no-enviction( deportation ): Exclusion data

Why? redis You need to put all the data in memory

Redis In order to achieve the fastest read-write speed, read all data into memory , And write data to disk asynchronously . therefore redis It has the characteristics of fast and data persistence . If you don't put the data in memory , disk I/O Speed is a serious effect redis Performance of . Today, memory is getting cheaper ,redis Will be more and more popular .

If the maximum memory usage is set , After the number of existing records reaches the memory limit, the new value cannot be inserted .

Redis Single process, single thread

redis Use queue technology to change concurrent access into serial access , Eliminate the cost of traditional database serial control

redis How to solve the problem of concurrent competition

Redis Single process single thread mode , Use queue mode to change concurrent access to serial access .Redis There is no concept of lock in itself ,Redis There is no competition for multiple client connections , But in Jedis The client to Redis Connection timeout occurs when concurrent access is performed 、 Data conversion error 、 Blocking 、 The client closes the connection and so on , These problems are caused by the confusion of client connection .

There is 2 A solution :

1. Client angle , In order to ensure the normal order between each client and Redis communicate , Pool connections , At the same time, read and write to the client Redis Internal lock is used for operation synchronized.

2. Server angle , utilize setnx Implementation lock .

notes : For the first , The application itself is required to handle the synchronization of resources , The methods that can be used are more popular , have access to synchronized You can also use lock; The second one needs to use Redis Of setnx command , But we need to pay attention to some problems .

redis Several ways of persistence

snapshot (snapshots)

By default ,Redis The snapshot of the data is stored in a binary file on disk , The file named dump.rdb. You can configure Redis Persistence strategy of , For example, every data set N Seconds have exceeded M Secondary update , Just write the data to disk ; Or you can call the command manually SAVE or BGSAVE.

working principle

  • Redis forks.
  • The child process begins to write data to temporary RDB In file .
  • When the child process finishes writing RDB file , Replace the old file with a new one .
  • This way can make Redis Use copy-on-write technology .

AOF

Snapshot mode is not very robust , When the system stops , Or unintentionally Redis By kill fall , Finally write Redis The data will be lost .

This may not be a big problem for some applications , But for applications that require high reliability ,Redis It's not a good choice .Append-only File mode is another option . You can open... In the configuration file AOF Pattern

Virtual memory mode

When your key Very small and value When a large , Use VM The effect will be better . Because it saves a lot of memory .

When your key No hours , Consider using some unusual methods that will be great key Become big value, For example, you can consider key,value Combine into a new value.

vm-max-threads This parameter , Access can be set up swap Number of threads in the file , It is better not to exceed the number of cores of the machine , If set to 0, So all right swap The operation of the file is serial . May cause a relatively long delay , But there is a good guarantee for data integrity .

When I test myself, I found that the performance of virtual memory is also good . If the amount of data is large , Consider distributed or other databases .

redis Cache invalidation policy and primary key invalidation mechanism

As a caching system, we should clean up invalid data regularly , We need a primary key invalidation and elimination strategy .

stay Redis among , Having a life span key go by the name of volatile. When creating the cache , For the given key Set lifetime , When key When it expires ( The life span is 0), It may be deleted .

1、 Some operations that affect survival time

Time to live can be achieved by using DEL Command to delete the whole key To remove , Or be SET and GETSET The command overwrites the original data , in other words , modify key Corresponding value Same as using the other key and value To cover later , The current data has a different lifetime .

for instance , To a key perform INCR command , Do... On a list LPUSH command , Or for a hash table HSET command , None of these operations will change key Time to live . On the other hand , If you use RENAME To a key Change the name , So after the change of name key Life time is the same as before .

RENAME Another possibility of command is , Try to put a key Change the name to another one with a lifetime another_key , This is the old one another_key ( And its lifetime ) Will be deleted , And then the old key It will be renamed another_key , therefore , new another_key Life time is the same as the original key equally . Use PERSIST The command can be deleted without key Under the circumstances , remove key Survival time , Give Way key To be a persistent key .

How to update the lifetime

Can be applied to a key perform EXPIRE command , The new specified lifetime will replace the old one . The accuracy of the expiration time has been controlled at 1ms within , The time complexity of primary key invalidation is O(1),EXPIRE and TTL Command with ,TTL You can see key The current lifetime of . Set successfully returned 1; When key Does not exist or cannot be for key When setting the lifetime , return 0 .

Maximum cache configuration :

stay redis in , Allows the user to set the maximum memory size to use ,server.maxmemory The default is 0, No maximum cache specified , If there is new data added , Maximum memory exceeded , Will make redis collapse , So it must be set .redis When the size of the memory data set rises to a certain size , We will implement data elimination strategy .

redis Provide 6 A data culling strategy :

  1. volatile-lru: From the set of data for which the expiration time has been set (server.db[i].expires) Select the least recently used data in
  2. volatile-ttl: From the set of data for which the expiration time has been set (server.db[i].expires) To select the data to be expired
  3. volatile-random: From the set of data for which the expiration time has been set (server.db[i].expires) In the arbitrary selection of data elimination
  4. allkeys-lru: From the data set (server.db[i].dict) Select the least recently used data in
  5. allkeys-random: From the data set (server.db[i].dict) In the arbitrary selection of data elimination
  6. no-enviction( deportation ): Exclusion data

Notice the 6 Species mechanism ,volatile and allkeys Specifies whether to phase out data from a data set that has set an expiration time or from all data sets , hinder lru、ttl as well as random There are three different elimination strategies , Add a kind of no-enviction The strategy of never recycling .

Use policy rules :

  1. If the data presents a power-law distribution , That is to say, some data access frequency is high , Some data access frequency is low , Then use allkeys-lru
  2. If the data is equally distributed , That is, all data access frequencies are the same , Then use allkeys-random

Three data elimination strategies :

ttl and random Easier to understand , The implementation will also be simpler . Mainly Lru Use elimination strategy at least recently , Design will be right key Sort by expiration time , Then take the first failure key To eliminate

redis Suitable scene

Redis Best for all data in-momory Scene , although Redis Persistence is also provided , But it's more of one disk-backed The function of , Persistence is quite different from traditional persistence , Then you might have questions , Seems to be Redis More like an enhanced version Memcached, So when to use Memcached, When to use Redis Well ?

If you compare it simply Redis And Memcached The difference between , Most of you will get the following points :

  1. Redis It's not just about supporting simple k/v Data of type , It also provides list,set,zset,hash Such as data structure storage .
  2. Redis Support data backup , namely master-slave Mode data backup .
  3. Redis Support data persistence , Data in memory can be kept on disk , When you restart, you can load it again for use .

Session cache (Session Cache)

One of the most common USES Redis Is the session cache (session cache). use Redis Cache sessions than other stores ( Such as Memcached) The advantage is that :Redis Provide persistence . When maintaining a cache that is not strictly consistent , If all of the user's cart information is lost , Most people won't be happy , Now? , Will they do that again ?

Fortunately, , With Redis Years of improvement , It's easy to figure out how to use it properly Redis To cache the documents for the session . Even well-known business platforms Magento Also provide Redis Plug in for .

Full page caching (FPC)

Except for basic conversations token outside ,Redis It's also very simple FPC platform . Back to consistency , Even if it restarts Redis example , Because there is disk persistence , Users won't see the page load speed drop , This is a huge improvement , similar PHP Local FPC.

Once again to Magento For example ,Magento Provides a plug-in to use Redis As the full page cache backend .

Besides , Yes WordPress For users of ,Pantheon There is a very good plug-in wp-redis, This plugin will help you load the pages you have visited as quickly as possible .

queue

Reids A big advantage in the field of memory storage engines is that it provides list and set operation , This makes Redis Can be used as a good message queue platform .Redis Operations used as queues , It's like a native programming language ( Such as Python) Yes list Of push/pop operation .

If you quickly in Google Mid search “Redis queues”, You'll find plenty of open source projects right away , The purpose of these projects is to leverage Redis Create great back-end tools , To meet various queue requirements . for example ,Celery There is a background that is used Redis As broker, You can check it out here .

Ranking List / Counter

Redis Incrementing or decrementing Numbers in memory works very well . aggregate (Set) And ordered set (Sorted Set) It also makes it very easy for us to perform these operations ,Redis It just happens to provide both data structures .

therefore , We want to get the highest ranked from the sorted set 10 Users – We call it “user_scores”, We just need to execute like this :

Of course , This assumes that you are doing an incremental sort based on your users' scores . If you want to return the user and the user's score , You need to do it this way :

ZRANGE user_scores 0 10 WITHSCORES

Agora Games This is a good example , use Ruby Realized , Its leaderboard is to use Redis To store data , You can see it here .

Release / subscribe

Last ( But certainly not the least ) yes Redis Release / Subscribe to the function . Release / There are a lot of usage scenarios for subscriptions . I've seen people using it in social networking connections , It can also be used as a release-based publication / Subscribe to script triggers , Even with Redis Release / Subscribe to set up a chat system !

Redis Of all the features provided , I feel that this is the one who likes the least , Although it provides users with this functionality .

reference

Sharing plans

Blog content will be synchronized to Tencent cloud + Community , Invite everyone to join us :https://cloud.tencent.com/

license agreement

In this paper A signature - Noncommercial use - Share in the same way 4.0 The international license agreement , Reprint please indicate the source .

原网站

版权声明
本文为[Ant]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203012040060723.html