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:
?
12 |
# 限定最多使用1.5GB内存 maxmemory 1536mb |
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:
?
12 |
# 设置策略为清理最少使用的key对应的数据 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:
?
12 |
telnet localhost 6379 info |
There are several items in the output information that are related to the state of the cache:
?
12345 |
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
Redis as a cache server configuration