With the development of Redis, more and more architectures have replaced memcached as the cache server, which has several outstanding features:
1. In addition to hash, sorted Set, list and other data structures are provided.
2. Can be persisted to disk
3. Support Cluster (3.0)
It has the same performance and memcached, and with the popular other components (such as queues) will also use Redis, from a simple architecture, there is no need to mix redis and memcached.
Write a short article about how to use Redis as a cache server configuration you need to pay attention to several points.
Redis Configuration
As a cache server, if you do not limit memory, it is very likely that the entire server memory consumption, can be set in the Redis configuration file:
If the memory reaches the specified upper limit and adds more cache content to the Redis, the policy for cleaning up the content needs to be set:
1 2 |
maxmemory-policy allkeys-lru
|
There are a number of cleanup strategies, and the official Redis documentation has a very detailed description: Http://redis.io/topics/lru-cache
Redis Monitoring
Redis provides the info command to monitor the status of the server at any time, using only telnet to the port of the corresponding server and executing the command:
1 2 |
telnet localhost 6379
info
|
There are several items in the output information that are related to the state of the cache:
1 2 3 4 5 |
keyspace_hits:14414110
keyspace_misses:3228654
used_memory:433264648
expired_keys:1333536
evicted_keys:1547380
|
By calculating the hits and miss, we can get the cache hit ratio: 14414110/(14414110 + 3228654) = 81%, a cache invalidation mechanism, and expiration time well-designed system, hit rate can be more than 95%, for the overall performance improvement is very large.
Used_memory,expired_keys,evicted_keys the specific meaning of these 3 messages, the official Redis also has a very detailed description: Http://redis.io/commands/info
A ruby gem called Redis-stat, which uses the info command to show a more intuitive information report, recommends:
Https://github.com/junegunn/redis-stat
Optimize your Rails cache configuration
While rails is using Redis as a cache, the configuration is simple, and the official documents are written in schema form:
1 |
config.cache_store = :redis_store, "redis://localhost:6379/0/cache"
|
Because it is too simple, many people directly use the default settings, but in fact there are some useful parameters can be written in the form of hash options, such as compressing more than 32K of data compression and then into the cache, such as setting the default all key failure time is 8 hours:
1 |
config.cache_store = :redis_store, {:host => ‘redis.server‘ , :port => 6379, :compress => true , :expires_in => 8.hours, :compress_threshold => 32.kilobytes}
|
As an example, a Redis cache server consumes about 4.2G of memory before optimizing configuration, with cache hits of around 70%.
We first execute the bgsave command on the server, dump the memory, and then use the Https://github.com/sripathikrishnan/redis-rdb-tools tool to parse the dump data into a CSV file:
1 |
rdb -c memory /var/redis/6379/dump .rdb > memory.csv
|
Using Excel to open analysis, sort and count by key, we can see which types of caches are most on the server, adjust the expiration time and failure mechanism of this type of cache, and then use Redis-stat to observe and fine tune the overall hit rate.
By analyzing the large memory-intensive key, we found that there are about 30% of the same type key, using 95% of the memory, these caches are mostly HTML fragment caches, by setting the Compress_threshold and compress parameters, Let the overall memory footprint from 4.2G, down to 1.3G.
From: https://ruby-china.org/topics/22761
Redis as a cache server configuration