Redis as a single machine cache usage recommendation

Posted by MK27 on Tue, 03 Mar 2020 02:44:07 +0100

 

Preface

Prepare to introduce redis as a single machine cache in the project because the cache middleware used in the original project cannot be used in the native Kirin operating system.

      

Configuration optimization recommendations

Configure the redis service to start the daemon

Redis does not run as a daemon by default, it can run in the background automatically when redis-server is started by modifying the configuration item daemonize to yes.

Security Configuration

Configuring bind to 127.0.0.1 prevents redis from being attacked externally.Additionally, using the requirepass configuration item, you can set the password to be entered first when accessing redis server data.

A minor pity is that redis only supports saving access passwords in clear text in a configuration file, which provides an idea to generate configuration files dynamically to increase security:

First, backup redis.conf to redis.conf.tml, where requirepass is configured with ciphertext.

Use other programs to read the requirepass configuration item of redis.conf.tml before starting the redis service, decrypt the ciphertext, replace the requirepass value to generate the redis configuration file redis.conf., delete redis.conf after starting the redis service, which will achieve confidentiality.

Set Maximum Memory and Memory Elimination Policy

To prevent redis from consuming unlimited memory and causing the system to run out of memory, it is recommended to set maxmemory to 1024 mb.(Actually, if you look at it with the ps command, you'll find that redis-server uses at most a little more memory than maxmemory)

Also configure the memory elimination policy maxmemory-policy to be allkeys-lru, allowing redis to use the LRU algorithm to eliminate data in all keys when memory is full.

Log File Configuration

Redis provides a logfile configuration entry to specify the log output location.By default, redis outputs all logs to the same file, which grows over time.

It is recommended to modify the source code of redis and change the redisLog function inside to output the log by day.

Persistence Configuration

Since we only need to use redis for database caching, no persistence is required.You don't need to worry about the "cache avalanche" phenomenon when redis restarts, because we have many business servers and won't restart at the same time.

* Single business server tps are limited, and cache zeroing will not put too much pressure on the database.

Turn off persistence methods to mask the original save configuration and increase save''

Slow Query Configuration

Set the slowlog-log-slower-than 5000 to record all requests with response times greater than 5ms to facilitate problem locating in case of failure.

High Risk Command Configuration

There are some redis commands that consume more resources on the redis server, resulting in inefficient query caching.To prevent novices from misusing these commands, we can rename them and configure them as follows:

rename-command MONITOR "DANGEROUS_CMD_MONITOR"

rename-command FLUSHALL "DANGEROUS_CMD_FLUSHALL"

rename-command FLUSHDB  "DANGEROUS_CMD_FLUSHDB"

rename-command CONFIG   "DANGEROUS_CMD_CONFIG"

rename-command KEYS     "DANGEROUS_CMD_KEYS"

 

Performance test comparison before and after optimization

 

Before optimization:

 

[huangcihui:/home/huangcihui] redis-benchmark -h 127.0.0.1 -p 6379 -c 100 -n 100000  -k 1 -e  -q -r 10000  -d 512

PING_INLINE: 56148.23 requests per second

PING_BULK: 55617.35 requests per second

SET: 57570.52 requests per second

GET: 56085.25 requests per second

INCR: 55309.73 requests per second

LPUSH: 54764.51 requests per second

RPUSH: 57570.52 requests per second

LPOP: 54644.81 requests per second

RPOP: 54884.74 requests per second

SADD: 50327.12 requests per second

HSET: 58445.36 requests per second

SPOP: 53191.49 requests per second

LPUSH (needed to benchmark LRANGE): 54945.05 requests per second

LRANGE_100 (first 100 elements): 11693.17 requests per second

LRANGE_300 (first 300 elements): 3824.09 requests per second

LRANGE_500 (first 450 elements): 2342.19 requests per second

LRANGE_600 (first 600 elements): 1671.12 requests per second

MSET (10 keys): 40192.93 requests per second

 

[huangcihui:/home/huangcihui] redis-benchmark -h 127.0.0.1 -p 6379 -c 100 -n 100000  -k 1 -e  -q -r 10000  -d 512

PING_INLINE: 55340.34 requests per second

PING_BULK: 54854.64 requests per second

SET: 53937.43 requests per second

GET: 54347.82 requests per second

INCR: 52910.05 requests per second

LPUSH: 54674.69 requests per second

RPUSH: 51894.13 requests per second

LPOP: 53676.86 requests per second

RPOP: 53022.27 requests per second

SADD: 53676.86 requests per second

HSET: 55401.66 requests per second

SPOP: 56085.25 requests per second

LPUSH (needed to benchmark LRANGE): 54347.82 requests per second

LRANGE_100 (first 100 elements): 11160.71 requests per second

LRANGE_300 (first 300 elements): 3383.98 requests per second

LRANGE_500 (first 450 elements): 2246.33 requests per second

LRANGE_600 (first 600 elements): 1592.66 requests per second

MSET (10 keys): 37622.27 requests per second

 

[huangcihui:/home/huangcihui] redis-benchmark -h 127.0.0.1 -p 6379 -c 100 -n 100000  -k 1 -e  -q -r 10000  -d 512

PING_INLINE: 54734.54 requests per second

PING_BULK: 54024.85 requests per second

SET: 54854.64 requests per second

GET: 52798.31 requests per second

INCR: 55463.12 requests per second

LPUSH: 55432.37 requests per second

RPUSH: 55834.73 requests per second

LPOP: 54495.91 requests per second

RPOP: 53705.69 requests per second

SADD: 52521.01 requests per second

HSET: 54229.93 requests per second

SPOP: 54585.15 requests per second

LPUSH (needed to benchmark LRANGE): 55648.30 requests per second

LRANGE_100 (first 100 elements): 11225.86 requests per second

LRANGE_300 (first 300 elements): 3598.29 requests per second

LRANGE_500 (first 450 elements): 2222.77 requests per second

LRANGE_600 (first 600 elements): 1620.25 requests per second

MSET (10 keys): 38684.72 requests per second

 

 

 

 

After optimization:

 

[huangcihui:/home/huangcihui] redis-benchmark -h 127.0.0.1 -p 6379 -c 100 -n 100000  -k 1 -e  -q -r 10000  -d 512 -a pass123

PING_INLINE: 51361.07 requests per second

PING_BULK: 46838.41 requests per second

SET: 49043.65 requests per second

GET: 50150.45 requests per second

INCR: 51786.64 requests per second

LPUSH: 55493.89 requests per second

RPUSH: 50150.45 requests per second

LPOP: 56915.20 requests per second

RPOP: 55928.41 requests per second

SADD: 56369.79 requests per second

HSET: 58651.02 requests per second

SPOP: 57703.40 requests per second

LPUSH (needed to benchmark LRANGE): 56593.10 requests per second

LRANGE_100 (first 100 elements): 11723.33 requests per second

LRANGE_300 (first 300 elements): 3954.76 requests per second

LRANGE_500 (first 450 elements): 2504.95 requests per second

LRANGE_600 (first 600 elements): 1733.61 requests per second

MSET (10 keys): 44444.45 requests per second

 

[huangcihui:/home/huangcihui] redis-benchmark -h 127.0.0.1 -p 6379 -c 100 -n 100000  -k 1 -e  -q -r 10000  -d 512 -a pass123

PING_INLINE: 57339.45 requests per second

PING_BULK: 56561.09 requests per second

SET: 56116.72 requests per second

GET: 56625.14 requests per second

INCR: 57142.86 requests per second

LPUSH: 59880.24 requests per second

RPUSH: 51387.46 requests per second

LPOP: 51599.59 requests per second

RPOP: 51334.70 requests per second

SADD: 55865.92 requests per second

HSET: 57937.43 requests per second

SPOP: 58719.91 requests per second

LPUSH (needed to benchmark LRANGE): 56625.14 requests per second

LRANGE_100 (first 100 elements): 11845.53 requests per second

LRANGE_300 (first 300 elements): 3999.20 requests per second

LRANGE_500 (first 450 elements): 2414.70 requests per second

LRANGE_600 (first 600 elements): 1702.16 requests per second

MSET (10 keys): 39494.47 requests per second

 

[huangcihui:/home/huangcihui] redis-benchmark -h 127.0.0.1 -p 6379 -c 100 -n 100000  -k 1 -e  -q -r 10000  -d 512 -a pass123

PING_INLINE: 51786.64 requests per second

PING_BULK: 38417.21 requests per second

SET: 55524.71 requests per second

GET: 39047.25 requests per second

INCR: 44822.95 requests per second

LPUSH: 53276.50 requests per second

RPUSH: 58582.31 requests per second

LPOP: 57208.24 requests per second

RPOP: 55066.08 requests per second

SADD: 52910.05 requests per second

HSET: 55187.64 requests per second

SPOP: 57405.28 requests per second

LPUSH (needed to benchmark LRANGE): 57570.52 requests per second

LRANGE_100 (first 100 elements): 10960.11 requests per second

LRANGE_300 (first 300 elements): 3794.20 requests per second

LRANGE_500 (first 450 elements): 2355.44 requests per second

LRANGE_600 (first 600 elements): 1705.41 requests per second

MSET (10 keys): 44130.62 requests per second

 

 

Operating efficiency will be improved slightly after optimization

Usage conventions

colony

Redis provides three cluster modes, master-slave, sentry and fragmented.But since we're only going to be a single machine cache, we don't need to configure it.

Use database and key name prefix reasonably to differentiate business

Redis provides multiple database configurations and supports up to 256 databases.We can specify that different business modules use different databases to avoid database primary key name conflicts.

However, even in the same business module, it is often easy to have the same primary key name, so there are some specifications for key names: uniform use of the prefix: "+specific values.

For example, set uname:13560453764 huangcihui

Prefixes can be managed using excel, which essentially resolves key name conflicts.

Failure Time

It is best to set an expiration time for all keys, preferably a random number within a range, to avoid concurrent cache failures.

 

Manometry

It is best to execute a manometer command before deploying redis to see if there are any performance exceptions.Consider adjusting operating system parameters if there are exceptions.

redis-benchmark -h 127.0.0.1 -p 6379 -c 100 -n 100000  -k 1 -e -a pass123 -q

Note that you need to use the -k parameter to set up a long connection or the test results will perform much worse.(Tests on my machine are five times different)

Topics: Linux Redis Database Excel