所以,我来到了一个地方,我想把我存储在redis的数据分割成单独的数据库,因为我有时需要在一种特定的数据上使用键命令,并想把它分开,以使其更快。

If I segment into multiple databases, everything is still single threaded, and I still only get to use one core. If I just launch another instance of Redis on the same box, I get to use an extra core. On top of that, I can't name Redis databases, or give them any sort of more logical identifier. So, with all of that said, why/when would I ever want to use multiple Redis databases instead of just spinning up an extra instance of Redis for each extra database I want? And relatedly, why doesn't Redis try to utilize an extra core for each extra database I add? What's the advantage of being single threaded across databases?


当前回答

在极少数情况下,Redis数据库可以用于部署新版本的应用程序,其中新版本需要使用不同的实体。

其他回答

上面没有提到我们的动机。我们使用多个数据库,因为我们经常需要删除一组特定类型的数据,而FLUSHDB可以简化这一操作。例如,我们可以在数据库0上使用FLUSHDB清除所有缓存的网页,而不影响我们对Redis的所有其他使用。

这里有一些讨论,但我没有找到关于这vs扫描和删除性能的明确信息:

https://github.com/StackExchange/StackExchange.Redis/issues/873

I don't really know any benefits of having multiple databases on a single instance. I guess it's useful if multiple services use the same database server(s), so you can avoid key collisions. I would not recommend building around using the KEYS command, since it's O(n) and that doesn't scale well. What are you using it for that you can accomplish in another way? Maybe redis isn't the best match for you if functionality like KEYS is vital. I think they mention the benefits of a single threaded server in their FAQ, but the main thing is simplicity - you don't have to bother with concurrency in any real way. Every action is blocking, so no two things can alter the database at the same time. Ideally you would have one (or more) instances per core of each server, and use a consistent hashing algorithm (or a proxy) to divide the keys among them. Of course, you'll loose some functionality - piping will only work for things on the same server, sorts become harder etc.

在一个实例中使用多个数据库可能在以下场景中有用:

同一数据库的不同副本可以使用实时数据用于生产、开发或测试。人们可以使用replica来克隆一个redis实例来达到同样的目的。然而,前一种方法更容易使现有的正在运行的程序选择正确的数据库切换到预期的模式。

我正在使用redis来实现电子邮件地址的黑名单,并且我对不同级别的黑名单有不同的TTL值,因此在同一实例上使用不同的db对我有很大帮助。

在极少数情况下,Redis数据库可以用于部署新版本的应用程序,其中新版本需要使用不同的实体。