One app, multiple databases

Hi,

how would you set up one app which connects to a database depending on the subdomain?

Example:

subdomain1.example.com
MONGO_URL=mongodb://localhost:27017/subdomain1

subdomain2.example.com
MONGO_URL=mongodb://localhost:27017/subdomain2

The database connection (MONGO_URL) ist set in the environment variables. But i dont know how to work with variables on that level.

At the moment we use nginx. But as far as i know, we have to start one instance of the app for every different customer.

thanks
benjamin

1 Like

Actually, if you are talking about multi tenant apps.

May be you can use a single DB with using the DB Schema to isolate each tenant.

If you need to provide a sub domain for each tenant, okay then you need work bit hard for that. But it’s doable. But it’s hard to explain here. Need some research.

On the otherside, you can deploy a new meteor instances on the fly with docker. May be Galaxy is a good solution for you when it’s came.

For right now, you can Check ReactionCommerce project. They are also doing something similar.

1 Like

thank you for your input. We´re unsure which road to take. Perhaps someone reading this took one of the roads you mentioned and share their experience.

Someone please jump in if I’m wrong, but isn’t it node.js that you want to see the correct environment variable? Nginx just reverse proxies for node.js and I don’t think it cares what MONGO_URL is set to.

How you accomplish what you want to do, I think, depends on how you’re deploying your applications.

Are you deploying with Meteor-up? I’ve used it to put 2 applications on the same server in different subdomains, like you’re doing, and I’m pretty sure I was able to have each app use a different db (one local, one remote). MUP allows you to set the environment variables for each application under the “env” key in mup.json.

If you’re deploying to Modulus.io or something like that, then your problem is automatically solved, since you enter the env variables straight in.

yes, it works like you describe. But when you have hundreds of subdomains it´s not a clean solution in my option. I thought, perhaps, i´m missing something …

We decided to go for 1db. We don´t have millions of records, so performance shouldn´t be a problem. At least i hope so. We will do a couple of more tests to see how it goes. I will post my experience here.

How are you doing this with Kadira? Do you run one app and one DB, or many apps and many DBs?

It’s many apps (for some services, not only in Meteor) and two main DBs.