-
Key value design
-
1. Key name design
-
2. Value design
-
2. Command usage
-
Three, the use of the client
-
Iv. Relevant tools
-
Delete bigKey
This paper mainly introduces the development specifications of Using Ali Cloud Redis from the following aspects.
-
The key value design
-
Command to use
-
Client use
-
Related tools
With this introduction, you can reduce the problems associated with using the Redis process.
Key value design
1. Key name design
Readability and manageability
Prefixed with the business name (or database name) (to prevent key collisions) and separated by colons, such as business name: table name: ID
ugc:video:1
Copy the code
simplicity
On the premise of ensuring semantics, the length of keys is controlled. When there are too many keys, the memory usage cannot be ignored. For example:
User :{uid}:friends:messages:{mid} simplified to u:{uid}:fr:m:{mid}.Copy the code
Do not include special characters
Counter example: contains Spaces, line breaks, single and double quotation marks, and other escape characters
2. Value design
Refused to bigkey
The number of hash, list, set, and zset elements should not exceed 5000.
Counterexample: a list of 2 million elements.
Use HSCAN, SSCAN, and ZSCAN to gradually delete non-string Bigkeys. At the same time, avoid automatic deletion of bigKey expiration time (for example, if a Zset of 2 million is set to expire in 1 hour, DEL operation will be triggered and block will be caused. This operation does not fail to occur in slow queries, find methods, and delete methods
Select the appropriate data type
For example: entity types (control and use data structure memory encoding to optimize configuration, such as Ziplist, but also pay attention to the balance between memory savings and performance)
Example:
set user:1:name tomset user:1:age 19set user:1:favor football
Copy the code
Is:
hmset user:1 name tom age 19 favor football
Copy the code
Controls the life cycle of keys
Redis is not a garbage bin. It is recommended to use expire to set the expiration time.
2. Command usage
1. The O(N) command concerns the number of N
Examples such as hgetall, lrange, smembers, zrange, and sinter are not unusable, but the value of N needs to be specified. Hscan, SSCAN, and ZSCAN can be used instead of traversal.
2. Disable commands
Prohibit the use of keys, flushall, and flushdb online. Disable the command by using the Redis rename mechanism, or use scan to process the command increments.
3. Use select wisely
Redis multi-database is weak, using numbers to distinguish, many clients support poor, at the same time multi-business multi-database is actually single thread processing, there will be interference.
4, use batch operation to improve efficiency
-
Native commands: for example, mget and mset.
-
Non-native commands: You can use pipelines to improve efficiency.
However, it is important to control the number of elements in a batch operation (for example, up to 500, which also depends on the number of elements in bytes).
Note the difference:
-
A native is an atomic operation; a pipeline is a non-atomic operation.
-
Pipelines can package different commands that native can’t
-
Pipeline needs both client and server support.
5. It is not recommended to use the Redis transaction function too much
Redis has weak transaction functionality (no rollback support), and the clustered version (self-developed and official) requires that the key for a transaction be in one slot (which can be resolved using hashtag).
The Redis cluster version has special requirements for using Lua
1, All KEYS should be passed by the KEYS array, redis. Call /pcall must be the key array, otherwise return error. “-ERR bad lua script for redis cluster, all the keys that the script uses should be passed using the KEYS arrayrn”
-ERR eval/evalsha command keys must be in the same slotrn
7, monitor command
When using the monitor command if necessary, be careful not to use it for a long time.
Three, the use of the client
1. Avoid multiple applications using one Redis instance
Irrelevant business split, public data do servitization.
2. Use connection pools
Can effectively control the connection, while improving efficiency, standard use mode:
Run the following command: Jedis Jedis = null; try { jedis = jedisPool.getResource(); Jedis.executecommand ()} catch (Exception e) {logger.error("op key {} error: "+ LLDB message (), key, e); } finally {// Note that the connection is not closed. In JedisPool mode, Jedis is returned to the resource pool. if (jedis ! = null) jedis.close(); }Copy the code
3. Fuse breaker function
In case of high concurrency, it is recommended that clients add the fuse breaker function (for example, Netflix Hystrix).
4, reasonable encryption
Set a reasonable password, if necessary can use SSL encryption access (Aliyun Redis support)
5. Elimination strategy
Select maxmemory-policy and set the expiration time based on the service type.
The default policy is volatile- LRU, that is, when the maximum memory is exceeded, the lRU algorithm is used to delete expired keys to ensure that the data will not be deleted. However, OOM problems may occur.
Other strategies are as follows:
-
Allkeys-lru: Deletes keys according to the LRU algorithm, regardless of whether the timeout attribute is set, until enough space is made.
-
Allkeys-random: randomly remove allkeys until enough space is left.
-
Volatile -random: Deletes expired keys randomly until sufficient space is available.
-
Volatile – TTL: Deletes the recently expired data based on the TTL attribute of the key value object. If not, retreat to Noeviction strategy.
-
Noeviction: Won’t exclude any data, reject all write operations and return client error message “(error) OOM command not allowed when used memory”, Redis will only respond to read operations.
Iv. Relevant tools
1. Data synchronization
For data synchronization between Redis, run the redis-port command
2. Big Key search
Redis big Key search tool
3. Search for hot keys
The internal implementation uses Monitor, so it is recommended to use Facebook’s Redis-Faina Ali cloud for a short time. Redis has solved the hot key issue at the kernel level
Delete bigKey
-
The following operations can be accelerated using pipeline.
-
Redis 4.0 already supports asynchronous deletion of keys, welcome to use it.
1. Delete Hash: HScan + hdel
public void delBigHash(String host, int port, String password, String bigHashKey) { Jedis jedis = new Jedis(host, port); if (password ! = null && !" ".equals(password)) { jedis.auth(password); } ScanParams scanParams = new ScanParams().count(100); String cursor = "0"; do { ScanResult<Entry<String, String>> scanResult = jedis.hscan(bigHashKey, cursor, scanParams); List<Entry<String, String>> entryList = scanResult.getResult(); if (entryList ! = null && ! entryList.isEmpty()) { for (Entry<String, String> entry : entryList) { jedis.hdel(bigHashKey, entry.getKey()); } } cursor = scanResult.getStringCursor(); } while (!" 0".equals(cursor)); // Delete bigkey jedis.del(bigHashKey); }Copy the code
2, List delete: ltrim
public void delBigList(String host, int port, String password, String bigListKey) { Jedis jedis = new Jedis(host, port); if (password ! = null && !" ".equals(password)) { jedis.auth(password); } long llen = jedis.llen(bigListKey); int counter = 0; int left = 100; While (counter < llen) {// Cut 100 jedis.ltrim(bigListKey, left, llen); counter += left; } // Finally delete key jedis.del(bigListKey); }Copy the code
Set delete: sscan + srem
public void delBigSet(String host, int port, String password, String bigSetKey) { Jedis jedis = new Jedis(host, port); if (password ! = null && !" ".equals(password)) { jedis.auth(password); } ScanParams scanParams = new ScanParams().count(100); String cursor = "0"; do { ScanResult<String> scanResult = jedis.sscan(bigSetKey, cursor, scanParams); List<String> memberList = scanResult.getResult(); if (memberList ! = null && ! memberList.isEmpty()) { for (String member : memberList) { jedis.srem(bigSetKey, member); } } cursor = scanResult.getStringCursor(); } while (!" 0".equals(cursor)); // Delete bigKey jedis.del(bigSetKey); }Copy the code
4, SortedSet delete: zscan + zrem
public void delBigZset(String host, int port, String password, String bigZsetKey) { Jedis jedis = new Jedis(host, port); if (password ! = null && !" ".equals(password)) { jedis.auth(password); } ScanParams scanParams = new ScanParams().count(100); String cursor = "0"; do { ScanResult<Tuple> scanResult = jedis.zscan(bigZsetKey, cursor, scanParams); List<Tuple> tupleList = scanResult.getResult(); if (tupleList ! = null && ! tupleList.isEmpty()) { for (Tuple tuple : tupleList) { jedis.zrem(bigZsetKey, tuple.getElement()); } } cursor = scanResult.getStringCursor(); } while (!" 0".equals(cursor)); // Delete bigKey jedis.del(bigZsetKey); }Copy the code
Some interview questions for 2020 are summarized. The interview questions are divided into 19 modules, which are: Java Basics, Containers, multithreading, reflection, object copy, Java Web, exceptions, Networking, Design patterns, Spring/Spring MVC, Spring Boot/Spring Cloud, Hibernate, MyBatis, RabbitMQ, Kafka, Zookeeper, MySQL, Redis, JVM.
Access to information above information: concern public number: programmers with stories, access to learning materials. Remember to click follow + comment oh ~