猫鼬在node.js中创建连接以支持多租户 [英] Mongoose create connection for multi-tenancy support in node.js

查看:166
本文介绍了猫鼬在node.js中创建连接以支持多租户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究一种使用node.js + mongoose和mongodb实现多个数据库以实现多租户支持的好方法.

I'm researching a good way to implement multiple database for multi-tenant support using node.js + mongoose and mongodb.

我发现猫鼬支持称为createConnection()的方法,我想知道使用该方法的最佳实践.实际上,我将所有这些连接存储在一个由租户分隔的数组中.就像是这样:

I've found out that mongoose supports a method called createConnection() and I'm wondering the best practice to use that. Actually I am storing all of those connection in an array, separated by tenant. It'd be like:

var connections = [
   { tenant: 'TenantA', connection: mongoose.createConnection('tenant-a') },
   { tenant: 'TenantB', connection: mongoose.createConnection('tenant-b') }
];

假设用户向租户发送了他将通过请求标头登录的租户,而我在早期的中间件Express中就得到了它.

Let's say the user send the tenant he will be logged in by request headers, and I get it in a very early middleware in express.

app.use(function (req, res, next) {
    req.mongoConnection = connections.find({tenant: req.get('tenant')});
});

问题是,可以静态地存储这些连接,还是可以在每次发出请求时创建一个更好的连接?

The question is, is it OK to store those connections statically or a better move would be create that connection every time a request is made ?

编辑2014-09-09-有关软件要求的更多信息

最初,我们将有大约3个租户,但是我们的计划是在一两年内将这个租户数量增加到40个.读操作多于写操作,它基本上是一个具有机器学习功能的大数据系统.它不是免费增值软件.由于历史数据量很大,所以数据库很大,但是将非常旧的数据移动到另一个位置并不是问题(我们已经考虑过).我们计划稍后在数据库计算机上的可用资源用尽时进行分片,也可以将一些租户分配到不同的计算机上.

At first we are going to have around 3 tenants, but our plan is to increase that number to 40 in a year or two. There are more read operations than write ones, it's basically a big data system with machine learning. It is not a freemium software. The databases are quite big because the amount of historical data, but it is not a problem to move very old data to another location (we already thought about that). We plan to shard it later if we run out of available resources on our database machine, we could also separate some tenants in different machines.

最让我着迷的是,有人说为多租户添加前缀集合不是一个好主意,但原因很简短.

The thing that most intrigues me is that some people say it's not a good idea to have prefixed collections for multitenancy but the reasons for that are very short.

https://docs.compose.io/use-cases/multi- tenant.html

http://themongodba.wordpress.com/2014/04/20/building-fast-scalable-multi-tenant-apps-with-mongodb/

推荐答案

我不建议手动创建和管理这些单独的连接.我不知道您的多租户要求的详细信息(租户数量,数据库大小,预期交易数量等),但是我认为最好使用

I would not recommend manually creating and managing those separate connections. I don't know the details of your multi-tenant requirements (number of tenants, size of databases, expected number transactions, etc), but I think it would be better to go with something like Mongoose's useDb function. Then Mongoose can handle all the connection pool details.

我探索的第一个方向是在单独的节点进程上设置每个租户.在单独的节点进程中运行租户有一些有趣的好处.从安全角度(隔离内存)和稳定性角度来看(一个租户进程崩溃不会影响其他进程)是有道理的.

The first direction I would explore is to setup each tenant on a separate node process. There are some interesting benefits to running your tenants in separate node processes. It makes sense from a security standpoint (isolated memory) and from a stability standpoint (one tenant process crash doesn't effect others).

假设您要基于URL建立租约,则需要在实际租户服务器之前设置代理服务器.工作是查看URL并根据该信息路由到正确的过程.这是非常简单的节点http代理设置.每个租户实例可以是完全相同的代码库,但是启动时使用不同的配置(该配置告诉他们要使用的mongo连接字符串).

Assuming you're basing the tenancy off of the URL, you would setup a proxy server in front of the actual tenant servers. It's job would be to look at the URL and route to the correct process based on that information. This is a very straightforward node http proxy setup. Each tenant instance could be the exact same code base, but launched with a different config (which tells them what mongo connection string to use).

这意味着您可以设计实际的应用程序,就好像它不是多租户一样.每个进程仅知道一个mongo数据库,并且不需要多租户逻辑.它还使您可以稍后根据负载轻松拆分流量.如果出于性能原因需要拆分租户,则可以在代理级别透明地进行. DNS都可以保持不变,并且您只需将实例所在的服务器移到幕后即可.您甚至可以让代理在多个服务器之间平衡对租户的请求.

This means you're able to design your actual application as if it wasn't multi-tenant. Each process only knows about one mongo database, and there is no multi-tenant logic necessary. It also enables you to easily split up traffic later based on load. If you need split up the tenants for performance reasons, you can do it transparently at the proxy level. The DNS can all stay the same, and you can just move the server that the instances are on behind the scenes. You can even have the proxy balance the requests for a tenant between multiple servers.

这篇关于猫鼬在node.js中创建连接以支持多租户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆