为什么在同一个实例中使用多个数据库在 Redis 中是个坏主意?

编程入门 行业动态 更新时间:2024-10-23 13:37:13
本文介绍了为什么在同一个实例中使用多个数据库在 Redis 中是个坏主意?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我是 redis 的新手,因此我不太了解它的复杂技术.但是让我把我的场景放在这里:我在同一台服务器上运行两个网站,我希望 redis 在这两个网站上都能工作.在搜索时,我发现我可以通过为同一服务器实例上的不同数据库分配不同的索引来做到这一点,如下所示:

I am new to redis therefore I don't know more about its complex technicalities. But let me put my scenario here: I am running two websites from same server and I wanted redis to work on both. On searching, I found that I can do this by assigning different index to different db on same server instance like below:

//In my first website (development) Idatabase dbOfDev=_conn.GetDatabase(0); //In my second website (production) Idatabase dbOfProd=_conn.GetDatabase(1);

这对我来说非常理想,因为我可以在同一个实例中缓存我的两个数据库.但后来我遇到了多个Redis数据库的意义是什么?和如何在 redis 数据库之间进行更改? 链接说不鼓励在同一服务器实例中使用多个数据库 和已弃用".尽管这些链接确实试图解释其背后的原因,但作为初学者,我仍然无法理解其深层技术方面.

This was ideal for me since I could cache both my database in same instance. But then I bumped into What's the Point of Multiple Redis Databases? and How do I change between redis database? links which says "Use of multiple database in same server instance is discouraged and deprecated". Though these links do try to explain the reason behind it, being a beginner, I am still not able to understand its deep technical aspects.

任何人都可以用更简单的术语解释为什么不鼓励使用同一服务器实例的多个 redis db 的原因.另外,简单来说,如果没有上述方法,我如何管理同一服务器上的两个网站的缓存?

Can anyone explain the reason in simpler terms as why using multiple redis db of same server instance is discouraged. Also, in simpler terms, how can I manage caching of both my websites on same server without the above said approach?

推荐答案

如何在没有上述方法的情况下管理我在同一服务器上的两个网站的缓存?

how can I manage caching of both my websites on same server without the above said approach?

您可以为每个网站使用不同的关键标签.假设将这两个网站命名为 A 和 B.对于网站A的所有key,给每个key一个前缀(key tag):A:.另一方面,为网站 B 的每个键赋予另一个前缀:B:.这样,您就可以为每个网站拥有唯一的密钥命名空间.

You can use different key tag for each website. Say, name the two websites as A and B. For all keys of website A, give each key a prefix(key tag): A:. On the other hand, give each key for website B another prefix: B:. In this way, you can have a unique key namespace for each website.

SET A:key1 val1 SET A:key2 val2 LPUSH B:key1 1 SADD B:key2 val

另请查看这个答案 了解更多解决方案.

Also check this answer for more solutions.

谁能用更简单的术语解释为什么不鼓励使用同一服务器实例的多个 redis 数据库.

Can anyone explain the reason in simpler terms as why using multiple redis db of same server instance is discouraged.

AFAIK,多数据库功能不 不鼓励和 弃用.这是一种为不同应用程序隔离密钥命名空间的方法.然而,Redis的作者考虑Redis 多个数据库错误是我在 Redis 设计中最糟糕的决定全部,因为它使 Redis 内部结构更加复杂.

AFAIK, multiple databases feature is NOT discouraged and deprecated. It's a method to isolated key namespaces for different applications. However, the author of Redis consider Redis multiple database errors my worst decision in Redis design at all, since it makes Redis internals more complex.

Redis 是单线程,因此与多个数据库相比,多个 Redis 实例可以利用多个内核.如果你在一个Redis实例中有多个数据库,你仍然只能使用一个核心.另外Redis实例本身的内存占用很少,不用担心多个Redis实例成本太高.

Redis is single-threaded, so compared to multiple databases, multiple Redis instances can take advantage of multiple cores. If you have multiple databases in one Redis instance, you can still only use one core. Also Redis instance itself has little memory footprint, so you don't need to worry about multiple Redis instance costs you too much.

Redis 非常快,通常瓶颈是网络带宽,不是 CPU.所以通常你不能通过使用多个 Redis 实例获得太多收益.但是,如果您的某个应用程序需要在 Redis 上执行一些慢速命令,并且不希望它阻塞其他应用程序,您可以为 slow 应用程序创建一个单独的 Redis 实例,并拥有另一个 Redis其他快速应用程序的实例.

Redis is very fast, and normally the bottleneck is network bandwidth, NOT CPU. So normally you CANNOT get too much gain by using multiple Redis instances. However, if one of your application needs to do some slow commands on Redis, and don't want it to block other applications, you can have a separate Redis instance for the slow application, and have another Redis instance for other fast applications.

另请注意,Redis Cluster 不支持多个数据库.

Also note that Redis Cluster doesn't support multiple databases.

就我个人而言,我喜欢这种多数据库功能.通常,如果我运行的是 Redis 实例,而不是 Redis Cluster,我会将我的数据放入默认数据库以外的某个数据库中,即数据库 0,以避免偶然登录 Redis 并在默认数据库上做一些可怕的事情.此外,使用多个数据库实现双缓冲区也很容易,例如将数据写入新数据库,完成后,使用 SWAPDB 命令自动高效地交换旧数据库和新数据库.

Personally, I like this multiple database feature. Normally, if I run a Redis instance, not Redis Cluster, I'll put my data into some database other than the default database, i.e. database 0, to avoid incidentally login Redis and do some horrible things on the default database. Also it's very easy to implement a double buffer with multiple databases, e.g. write data to a new database, when it's done, use the SWAPDB command to swap the old DB and new DB automatically and efficiently.

更多推荐

为什么在同一个实例中使用多个数据库在 Redis 中是个坏主意?

本文发布于:2023-10-30 04:09:05,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1541719.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:是个   坏主意   多个   实例   数据库

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!