Redis 的10个技巧

原文:http://objectrocket.com/blog/how-to/10-quick-tips-about-redis/

译文:https://www.jb51.net/article/73376.htm

Redis 在当前的技术社区里是非常热门的。从来自 Antirez 一个小小的个人项目到成为内存数据存储行业的标准,Redis已经走过了很长的一段路。

随之而来的一系列最佳实践,使得大多数人可以正确地使用 Redis。

下面我们将探索正确使用 Redis 的10个技巧。

1、停止使用 KEYS

Okay,以挑战这个命令开始这篇文章,或许并不是一个好的方式,但其确实可能是最重要的一点。

很多时候当我们关注一个redis实例的统计数据,我们会快速地输入”KEYS *”命令,这样key的信息会很明显地展示出来。平心而论,从程序化的角度出发往往倾向于写出下面这样的伪代码:

for key in 'keys *':
 doAllTheThings()

但是当你有1300万个key时,执行速度将会变慢。因为KEYS命令的时间复杂度是O(n),其中n是要返回的keys的个数,这样这个命令的复杂度就取决于数据库的大小了。并且在这个操作执行期间,其它任何命令在你的实例中都无法执行。

作为一个替代命令,看一下 SCAN 吧,其允许你以一种更友好的方式来执行… SCAN 通过增量迭代的方式来扫描数据库。这一操作基于游标的迭代器来完成的,因此只要你觉得合适,你可以随时停止或继续。

推荐:一个致命的 Redis 命令,损失 400 万!

2、找出拖慢 Redis 的罪魁祸首

由于 Redis 没有非常详细的日志,要想知道在 Redis 实例内部都做了些什么是非常困难的。幸运的是 Redis 提供了一个下面这样的命令统计工具:

127.0.0.1:6379> INFO commandstats
# Commandstats
cmdstat_get:calls=78,usec=608,usec_per_call=7.79 
cmdstat_setex:calls=5,usec=71,usec_per_call=14.20 
cmdstat_keys:calls=2,usec=42,usec_per_call=21.00 
cmdstat_info:calls=10,usec=1931,usec_per_call=193.10

通过这个工具可以查看所有命令统计的快照,比如命令执行了多少次,执行命令所耗费的毫秒数(每个命令的总时间和平均时间)

只需要简单地执行 CONFIG RESETSTAT 命令就可以重置,这样你就可以得到一个全新的统计结果。

3、 将 Redis-Benchmark 结果作为参考,而不要一概而论

Redis 之父 Salvatore 就说过:“通过执行GET/SET命令来测试Redis就像在雨天检测法拉利的雨刷清洁镜子的效果”。很多时候人们跑到我这里,他们想知道为什么自己的Redis-Benchmark统计的结果低于最优结果 。但我们必须要把各种不同的真实情况考虑进来,例如:

  • 可能受到哪些客户端运行环境的限制?
  • 是同一个版本号吗?
  • 测试环境中的表现与应用将要运行的环境是否一致?

Redis-Benchmark的测试结果提供了一个保证你的 Redis-Server 不会运行在非正常状态下的基准点,但是你永远不要把它作为一个真实的“压力测试”。压力测试需要反应出应用的运行方式,并且需要一个尽可能的和生产相似的环境。

4、Hashes 是你的最佳选择

以一种优雅的方式引入 hashes 吧。hashes 将会带给你一种前所未有的体验。之前我曾看到过许多类似于下面这样的key结构:

foo:first_name
foo:last_name
foo:address

上面的例子中,foo 可能是一个用户的用户名,其中的每一项都是一个单独的 key。这就增加了 犯错的空间,和一些不必要的 key。使用 hash 代替吧,你会惊奇地发现竟然只需要一个 key :

127.0.0.1:6379> HSET foo first_name "Joe" 
(integer) 1 
127.0.0.1:6379> HSET foo last_name "Engel" 
(integer) 1 
127.0.0.1:6379> HSET foo address "1 Fanatical Pl" 
(integer) 1 
127.0.0.1:6379> HGETALL foo
1) "first_name" 
2) "Joe" 
3) "last_name" 
4) "Engel" 
5) "address" 
6) "1 Fanatical Pl" 
127.0.0.1:6379> HGET foo first_name
"Joe"

5、设置 key 值的存活时间

无论什么时候,只要有可能就利用key超时的优势。一个很好的例子就是储存一些诸如临时认证key之类的东西。

当你去查找一个授权key时——以OAUTH为例——通常会得到一个超时时间。这样在设置key的时候,设成同样的超时时间,Redis就会自动为你清除!而不再需要使用KEYS *来遍历所有的key了,怎么样很方便吧?

6、 选择合适的回收策略

既然谈到了清除key这个话题,那我们就来聊聊回收策略。当 Redis 的实例空间被填满了之后,将会尝试回收一部分key。根据你的使用方式,我强烈建议使用 volatile-lru 策略——前提是你对key已经设置了超时。

但如果你运行的是一些类似于 cache 的东西,并且没有对 key 设置超时机制,可以考虑使用 allkeys-lru 回收机制。我的建议是先在这里查看一下可行的方案。

7、如果你的数据很重要,请使用 Try/Except

如果必须确保关键性的数据可以被放入到 Redis 的实例中,我强烈建议将其放入 try/except 块中。几乎所有的Redis客户端采用的都是“发送即忘”策略,因此经常需要考虑一个 key 是否真正被放到 Redis 数据库中了。至于将 try/expect 放到 Redis 命令中的复杂性并不是本文要讲的,你只需要知道这样做可以确保重要的数据放到该放的地方就可以了。

8、不要耗尽一个实例

无论什么时候,只要有可能就分散多redis实例的工作量。从3.0.0版本开始,Redis就支持集群了。Redis集群允许你基于key范围分离出部分包含主/从模式的key。完整的集群背后的“魔法”可以在这里找到。

但如果你是在找教程,那这里是一个再适合不过的地方了。如果不能选择集群,考虑一下命名空间吧,然后将你的key分散到多个实例之中。关于怎样分配数据,在redis.io网站上有这篇精彩的评论。

9、内核越多越好吗?

当然是错的。Redis 是一个单线程进程,为什么 Redis 单线程能支撑高并发?即使启用了持久化最多也只会消耗两个内核。除非你计划在一台主机上运行多个实例——希望只会是在开发测试的环境下!——否则的话对于一个 Redis 实例是不需要2个以上内核的。

10、高可用

到目前为止 Redis Sentinel 已经经过了很全面的测试,很多用户已经将其应用到了生产环境中(包括 ObjectRocket )。如果你的应用重度依赖于 Redis ,那就需要想出一个高可用方案来保证其不会掉线。

Redis is hot in the tech community right now. It’s come a long way from being a small personal project from Antirez, to being an industry standard for in memory data storage. With that comes a set of best practices that most people can agree upon for using Redis properly. Below we’ll explore 10 quick tips on using Redis correctly.

1. STOP USING KEYS *

Okay, so maybe shouting at you isn’t a great way to start this article. But it’s quite possibly the most important point. Too often do I look at an redis instance, pull up a quick commandstats, and see a glaring KEYS staring right back at me. In all fairness, coming from a programmatical standpoint it would make sense to have a psuedocode that does something like:

for key in 'keys *':
  doAllTheThings()

But when you have, say, 13 million keys, things are going to slowdown. Since KEYS is O(n) where n is the number of keys returned, your complexity is bound by the dbsize. Also, during this whole operation, nothing else can be run against your instance.

As a substitute, check out SCAN which allows you to well… scan through your database in increments instead. This operates on an interator so you can stop and go as you see fit.

2. Find Out What’s Slowing Down Redis

Since Redis doesn’t have the most verbose of logs, it’s often hard to trackdown what exactly is going on inside your instance. Luckily Redis provides you with the commandstat utility to show you this:

127.0.0.1:6379> INFO commandstats
# Commandstats
cmdstat_get:calls=78,usec=608,usec_per_call=7.79
cmdstat_setex:calls=5,usec=71,usec_per_call=14.20
cmdstat_keys:calls=2,usec=42,usec_per_call=21.00
cmdstat_info:calls=10,usec=1931,usec_per_call=193.10

This gives you a breakdown of all the commands, how many times they’ve been run, the number of microseconds it took to execute (total and avg per call)

To reset this simply run CONFIG RESETSTAT, and you’ve got a brand new slate.

3. Use Redis-Benchmark as a Baseline, Not the Gospel Truth

Salvatore, the creator of Redis put it geniously: “To test Redis doing GET/SET is like testing a Ferrari checking how good it is at cleaning the mirror when it rains.” A lot of times people come to me wondering why their Redis-Benchmark results are less than optimal. But we have to take into account many different factors, such as:

  • What client-side limitations could we have run into?
  • Was there a difference in versioning?
  • Are the tests being performed relevant to what the application will be performing?

Redis-Benchmark provides an awesome baseline to make sure your redis-server isn’t behaving abnormally, but it should never be taken as a true “load test”. Load tests need to be reflective of how your application behaves, and from an environment as close to production as possible.

4. Hashes Are Your Best Friend

Invite hashes over for dinner. Wine and dine hashes. You’ll be amazed at what happiness they can bring if you just give them the chance. I’ve seen one too many key structures like this before:

foo:first_name
foo:last_name
foo:address

In the above example, foo would be maybe a username for a user, and each one of those is a separate key. This adds room for errors, and adds unnecessary keys to the fold. Instead, consider a hash. Suddenly you’ve only got one key:

127.0.0.1:6379> HSET foo first_name "Joe"
(integer) 1
127.0.0.1:6379> HSET foo last_name "Engel"
(integer) 1
127.0.0.1:6379> HSET foo address "1 Fanatical Pl"
(integer) 1
127.0.0.1:6379> HGETALL foo
1) "first_name"
2) "Joe"
3) "last_name"
4) "Engel"
5) "address"
6) "1 Fanatical Pl"
127.0.0.1:6379> HGET foo first_name
"Joe"

5. Set That TTL!

Whenever possible, take advantage of expiring keys. A perfect example is storing something like temporary authentication keys. When you retrieve the auth key—let’s use OAUTH as an example—you often are given an expiration time. When you set the key, set it with the same expiration, and Redis will clean up for you! No more need for KEYS * to iterate through all those keys, eh?

6. Choosing the Proper Eviction Policy

While we’re on the topic of cleaning up keys, let’s touch on eviction. When your Redis instance fills up, Redis will attempt to evict keys. Depending on your use case, I highly recommend volatile-lru—assuming you have expiring keys. If you’re running something like a cache and don’t have an expiry set, you could consider allkeys-lru. I’d recommend checking out the available options here.

7. If Your Data is Important, Try/Except

If it’s absolutely critical for data to make it to your Redis instance, I heavily recommend putting in a try/except. Since almost all Redis clients are configured to “fire-and-forget,” there should always be consideration for when a key doesn’t actually make it to the database. The complexity added to your redis call is next to nothing in this case, and you can ensure your important data makes it to where it should be.

8. Don’t Flood One Instance

Whenever possible, split up the workload amongst multiple Redis instances. As of version 3.0.0, Redis Cluster is now available. Redis Cluster allows you to break apart keys amongst sets of given masters/slaves based on key ranges. A full breakdown of the magic behind Cluster can be found here. And if you’re looking for a tutorial, then look no further. If clustering is not an option, consider namespacing and distributing your keys among multiple instances. An amazing write-up on partitioning your data can be found on the redis.io website here.

9. More Cores = More Better, Right?!

Wrong. Redis is a single threaded process and will, at most, consume two cores if you have persistence enabled. Unless you plan on running multiple instances on the same host—hopefully only for dev testing in that case!—you shouldn’t need more than two cores for a Redis instance.

10. HA All the Things!

Redis Sentinel is now very well tested, and many users have it running in production (ObjectRocket included!). If you’re relying heavily on Redis for your application, then you need to consider an HA (high availability) solution to keep you online. Of course, if you don’t want to manage all of that yourself, ObjectRocket offers our HA platform with 24×7 support for your consumption, give it a shot.

这些信息有用吗?
Do you have any suggestions for improvement?

Thanks for your feedback!