Abstract: this paper introduces the use of ali cloud Redis development specification, from the key design, command is used, the client use, related tools, etc, through the introduction of this paper can reduce use Redis process problems,
One, the key value design
1. The key of design
(1) [suggestion] : readability and manageability
In the business name (or the database name) as a prefix (to prevent the key conflict), use a colon separated, such as business name: name of the table: id
The ugc: video: 1
(2) [suggestion] : simplicity
Guarantee under the premise of semantics, control the length of key, when the key is large, memory footprint also nots allow to ignore, such as:
User: {uid} : friends: messages: {mid} is simplified to u: {uid} : fr: m: {mid},
(3) [force] :
don't contain special characters
Counter-examples: contain Spaces, newlines, single and double quotes, and other escape character
Value design
2.
(1) [force] : refuse bigkey (prevent network flow, slow query)
String type control within the 10 KB, hash, list, set, zset element number not more than 5000,
Example: a list containing 2 million elements,
The string bigkey, don't use del delete, use hscan sscan, progressive delete zscan way, at the same time attention should be paid to prevent bigkey expiration time automatically deleted problems (such as a 2 million zset set 1 hour expired, will trigger del operation, block, and the operation will not appear in the slow query can check (latency)), search and delete methods
(2) [recommend] : select suitable data type,
For example: the entity type (to reasonable control and use of optimal allocation of memory encoding data structure, such as ziplist, but also should pay attention to the balance between save memory and performance)
Example:
Set the user: 1: the name Tom
Set the user: 1: the age 19
Set the user: 1: favor "
Is:
Hmset user: 1 the name Tom age 19 favor "
(3) [recommend] : control key life cycle, redis not trash can,
Suggest using the expire set the expiration time (condition allowed to break up the expiration time, prevent concentrated expired), not out of date data focus on idletime,
Second, command USES the
1. [recommend] O (N) command attention to the amount of N
Hgetall, for example, lrange smembers, zrange, sinter, etc are not unusable, but it is necessary to define the value of N, have a need to traverse can use hscan, sscan, zscan instead,
2. [recommend] : disable command
Prohibit online using keys, flushall flushdb etc., through redis rename mechanism banned command, or use processes progressive scan,
3.] [recommend reasonable use the select
Redis database more weaker, use Numbers to distinguish, many client support is poorer, at the same time, many business with many actual database or a single thread processing, there will be interference,
4.] [recommend use batch operation efficiency
Native command: for example, mget mset,
Non-native command: you can use the pipeline efficiency,
But should pay attention to the number of elements in the control of a batch operation (for example, within 500 actual are also associated elements and the number of bytes),
Note the different:
1. Native is atomic operation, pipeline is atomic,
2. The pipeline can be packaged in different orders, native do
3. The client and the server at the same time is required for pipeline,
5. [suggestion] Redis transaction function is weak, too much is not recommended to use
A weak function to Redis transactions (does not support the rollback), and cluster version (from research and official) requires a transaction key must be on a slot (can be solved using the hashtag function)
Version 6. [suggestion] Redis cluster have special requirements on use Lua:
1. All the key should be array by the KEYS to deliver, redis. Call/pcall calls inside redis command, the key position, must be the KEYS array, otherwise the error returned directly, "- ERR bad lua script for redis cluster, all the KEYS that the script USES should be passed using the KEYS array", "
2. All the key, must be in one slot, otherwise the error returned directly, "- ERR eval/evalsha command keys must in same slot"
7. [suggestion] if necessary to use monitor command, be careful not to use for a long time,
Three, the client use the
1. [recommend]
To avoid multiple applications use a Redis instance
Is exemple: unrelated business, public service data do,
2. [recommend]
Use a database connection pool, and can effectively control connection, improve efficiency at the same time, the standard method of use:
Execute the following command:
Jedis Jedis=null;
Try {
Jedis=jedisPool. GetResource ();
//the specific command
Jedis. ExecuteCommand ()
} the catch (Exception e) {
Op key logger. The error (" {} the error: "+ um participant etMessage (), key, e);
} the finally {
//note that this is not close the connection, in JedisPool mode, Jedis will be returned to the resource pool,
If (jedis!=null)
jedis.close();
}
Below is JedisPool optimization method of the article:
Jedis common abnormal summary
JedisPool resource pool optimization
3. [suggestion]
Suggest the client to add fusing under high concurrency functions (such as netflix hystrix)
4. [recommend]
Set up reasonable password, if necessary you can use SSL encryption access (ali cloud Redis support)
5. [suggestion]
According to their own business types, choose good maxmemory - policy (maximum memory elimination strategy), set expiration time,
The default policy is volatile - lru, namely after the maximum amount of memory, using the lru algorithm was the key in the expired keys, guarantee not expired data not be deleted, but may appear OOM,
Other strategies are as follows:
Delete key allkeys - lru: according to lru algorithm, regardless of the data to have set the timeout property, until enough room,
Allkeys - random: random delete all keys, until enough room,
Volatile - random: random delete expired keys, until enough room,
Volatile - TTL: according to the key value object attributes of TTL, deletes data recently will be expired, if not, back to the noeviction strategy,
Noeviction: won't eliminate any data, reject all write operations and returns the client error message "(error) OOM command not allowed the when, informs the memory", then Redis only response to a read operation,
4, related tools
1. [recommend] : data synchronization
Data synchronization between redis can use: redis - port
2. [recommend] : big key search
Redis big key search tool
3. [recommend] : hot key search (internal implementation using the monitor, so suggest use short time)
Facebook redis - faina
Ali cloud Redis is the hot key at the kernel level to solve the problem, welcome to use the
Five appendix: delete bigkey
1. The following can use a pipeline acceleration,
2. Redis 4.0 is support asynchronous delete key, welcome to use the
1. The Hash delete: hscan + hdel
Public void delBigHash (String host, int port, String password, String bigHashKey) {
Jedis Jedis=new Jedis (host, port);
If (password!=null & amp; & !" ". The equals (password)) {
Jedis. Auth (password);
}
ScanParams ScanParams=new ScanParams (). The count (100);
String cursor="0";
Do {
ScanResult
List
If (entryList!=null & amp; & ! EntryList. IsEmpty ()) {
For (Entry
Jedis. Hdel (bigHashKey, entry getKey ());
}
}
Cursor=scanResult. GetStringCursor ();
} while (!" 0 "equals (cursor));
//delete bigkey
Jedis. Del (bigHashKey);
}
2. Delete the List: ltrim
Public void delBigList (String host, int port, String password, String bigListKey) {
Jedis Jedis=new Jedis (host, port);
If (password!=null & amp; & !" ". The equals (password)) {
Jedis. Auth (password);
}
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull