# Separate RC database for multiple tenants

**URL:** <https://forums.rocket.chat/t/separate-rc-database-for-multiple-tenants/6694>\
**Category:** Community Support\
**Created:** [April 24, 2020, 9:20am UTC](https://forums.rocket.chat/t/separate-rc-database-for-multiple-tenants/6694 "2020-04-24T09:20:31Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Prasanna](https://avatars.discourse-cdn.com/v4/letter/p/57b2e6/32.png) [@Prasanna](https://forums.rocket.chat/u/Prasanna)\
**Post date:** [April 24, 2020, 9:20am UTC](https://forums.rocket.chat/t/separate-rc-database-for-multiple-tenants/6694/1 "2020-04-24T09:20:31Z")

</div>

### Description

I am trying rocketchat in a multitenant architecture. I came across a problem wherein I had to have all 60 collections of rocketchat inside each tenant db. When the server is starting, the rocketchat db is configured based on the env variable MONGO\_URL. By this way I was able to have chats of multiple tenants in a single database. At the end of the day, having a single common database for chat is not what I want. I am trying to isolate each tenant chat from one another. i.e., either by having separate chat db for each tenant or dumping rocketchat collections inside each tenant.  
Is there a way to do either of those?

### Server Setup Information

- Version of Rocket.Chat Server: 3.1.0-develop
- Operating System: Linux
- Deployment Method: Kubernetes (Docker)
- Number of Running Instances: 1
- DB Replicaset Oplog: Enabled
- NodeJS Version: 12.14.1 - x64
- MongoDB Version: 4.2.5

---

<div class="post-metadata">

**Author:** ![aaron.ogle](https://sea1.discourse-cdn.com/flex021/user_avatar/forums.rocket.chat/aaron.ogle/32/15_2.png) [@aaron.ogle](https://forums.rocket.chat/u/aaron.ogle)\
**Post date:** [April 24, 2020, 8:21pm UTC](https://forums.rocket.chat/t/separate-rc-database-for-multiple-tenants/6694/2 "2020-04-24T20:21:16Z")

</div>

Multi-tenant we’d do something like:

Tenant1:  
MONGO\_URL=mongodb://db1,db2:27017/tenant1?replicaSet=rs01  
MONGO\_OPLOG\_URL=mongodb://db1,db2:27017/local?replicaSet=rs01

Tenant2:  
MONGO\_URL=mongodb://db1,db2:27017/tenant2?replicaSet=rs01  
MONGO\_OPLOG\_URL=mongodb://db1,db2:27017/local?replicaSet=rs01

Tenant3:  
MONGO\_URL=mongodb://db1,db2:27017/tenant3?replicaSet=rs01  
MONGO\_OPLOG\_URL=mongodb://db1,db2:27017/local?replicaSet=rs01

Etc. Each of course getting own address and rocket.chat instance running

---

<div class="post-metadata">

**Author:** ![Prasanna](https://avatars.discourse-cdn.com/v4/letter/p/57b2e6/32.png) [@Prasanna](https://forums.rocket.chat/u/Prasanna)\
**Post date:** [April 24, 2020, 8:48pm UTC](https://forums.rocket.chat/t/separate-rc-database-for-multiple-tenants/6694/3 "2020-04-24T20:48:43Z")

</div>

But this has to be defined before server running right?! in the deployment file.  
In my case I won’t be knowing the tenant\_id before the tenant makes its first connection  
Is there a way for me to define like this

> MONGO\_URL=mongodb://db1,db2:27017/_ **TENANT\_ID** _?replicaSet=rs01  
> MONGO\_OPLOG\_URL=mongodb://db1,db2:27017/local?replicaSet=rs01

where value for TENANT\_ID would be set after the tenant make its connection

---

<div class="post-metadata">

**Author:** ![Prasanna](https://avatars.discourse-cdn.com/v4/letter/p/57b2e6/32.png) [@Prasanna](https://forums.rocket.chat/u/Prasanna)\
**Post date:** [April 24, 2020, 8:51pm UTC](https://forums.rocket.chat/t/separate-rc-database-for-multiple-tenants/6694/4 "2020-04-24T20:51:12Z")

</div>

> [@aaron.ogle](#):
>
> Etc. Each of course getting own address and rocket.chat instance running

By this you mean separate rocket.chat instance for each and every tenant.  
Am I right?

---

<div class="post-metadata">

**Author:** ![aaron.ogle](https://sea1.discourse-cdn.com/flex021/user_avatar/forums.rocket.chat/aaron.ogle/32/15_2.png) [@aaron.ogle](https://forums.rocket.chat/u/aaron.ogle)\
**Post date:** [April 24, 2020, 10:08pm UTC](https://forums.rocket.chat/t/separate-rc-database-for-multiple-tenants/6694/5 "2020-04-24T22:08:58Z")

</div>

If it were me and I was building something like this to support multiple customers. I would not even start the resources until you know the tenant id. Starting a new Rocket.Chat instance doesn’t really take that much time.

So i’d put something in your process that is hit first that allows you to get the tenant\_id and then trigger resource creation that way you never even start Rocket.Chat instance for that tenant until you know who it is.

Wether this is some simple landing page that is hit and says “Preparing for first use” or a registration process.

---

<div class="post-metadata">

**Author:** ![Rakesh96](https://avatars.discourse-cdn.com/v4/letter/r/a587f6/32.png) [@Rakesh96](https://forums.rocket.chat/u/Rakesh96)\
**Post date:** [April 25, 2020, 7:36am UTC](https://forums.rocket.chat/t/separate-rc-database-for-multiple-tenants/6694/6 "2020-04-25T07:36:12Z")

</div>

> [@aaron.ogle](#):
>
> then trigger resource creation

so do you mean to start the server once the tenant\_id gets available. Is that correct?

Also in terms of kubernetes context, for scaling will that be good if we have 1 tenant for each pod? Since deployment config is common for all pods, there won’t be any pod specific config so we can’t have different mongo\_url for each pod(tenant).

---

<div class="post-metadata">

**Author:** ![aaron.ogle](https://sea1.discourse-cdn.com/flex021/user_avatar/forums.rocket.chat/aaron.ogle/32/15_2.png) [@aaron.ogle](https://forums.rocket.chat/u/aaron.ogle)\
**Post date:** [April 26, 2020, 3:58am UTC](https://forums.rocket.chat/t/separate-rc-database-for-multiple-tenants/6694/7 "2020-04-26T03:58:05Z")

</div>

I’m not sure I understand why you want / need to share deployment config and then want to change per pod?

Speaking from experience this is how I would do a multi tenant setup on k8s.

1. Some system to establish the tenant.
2. Create new deployment(in k8s terms) with its own mongo\_url pointing to the tenant I’d
3. Have your system check for it to come up.
4. Take the user there. 🙂

---

<div class="post-metadata">

**Author:** ![jitsi](https://avatars.discourse-cdn.com/v4/letter/j/3e96dc/32.png) [@jitsi](https://forums.rocket.chat/u/jitsi)\
**Post date:** [June 25, 2020, 4:38am UTC](https://forums.rocket.chat/t/separate-rc-database-for-multiple-tenants/6694/8 "2020-06-25T04:38:12Z")

</div>

> [@aaron.ogle](#):
>
> process that is hit first that allows you to get the tenant\_id and then trigger resource creation that way you never even start Rocket.Chat instance for that tenant until you kn

i think we only need get settings base on tenant\_id.  
when start app, will get all settings of all tenants, and then after user login we will have tenant\_id that can use to get settings from DB/Cache.
