Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Building a web application with multiple database instances or just a single instance

I am currently designing a web application where I will have customers signing up as companies. Each company will have its own set of users. As I am designing this I am wondering which approach would work best. I see sites like fogbugz or basecamp which use subdomains. In cases with subdomains do you have a database instance per sub domain? I'm wondering if it is recommended to have a database instance per company or if I should have some kind of company table and manage the company and user data/credentials all from one database.

Which approach is best? Is there literature on this subject (i.e. any web or book)?

thanks in advance!

like image 976
ralph Avatar asked Feb 18 '12 18:02

ralph


People also ask

Can we use multiple databases for a single application?

If you can work with single database, working with multiple is no different. You will need a connection string for each database. There rest is, as they say it, history.

Can we connect to two database in a web application?

Yes. Give it a go. "Is it possible to connect multiple database with one application." Yes.

Can a website have more than one database?

Of course you can. You can connect to as many databases as you like but as others have pointed out you probably shouldn't. All you have to do is have another connection string and use that one. They can be named in your web.

Can an app have two databases?

Multiple database technologies can be used with monolithic applications, and can even seem more natural in a microservices environment, where each service would have its own database. This approach, however, is not bulletproof. Far from it, actually.


1 Answers

You have to weigh up your options, as some of this will be a matter of opinion and might not be feasible for your implementation.

That being said, I'd consider the single database approach, for these reasons:

  1. Maintenance: when running a database per registered 'client', you will very easily reach a situation where any changes or upgrades you make to your app's schema have to be applied to every single database instance. This will get ridiculous, fast.

  2. Convenience: You might want analytics and usage stats, or some way to administrate all these databases. Querying a single database is comparatively trivial to trying to aggregate the same query for all your databases. This isn't going to scale.

  3. Scalability *: As mentioned in 2, you're going to require a special sort of aggregation to query things about your clients, and your app as a whole. The bigger your app gets, the more complex your querying. The other issue is, if one client uses the app a lot more than another, what will you be encouraged to optimise? Your app, the bigger client's database, or the smaller client's? Not forgetting anything you do change has to be copied to all databases.

  4. Backups: You can backup one database easily, just by creating a dump and stashing it somewhere. Get a thousand clients and now you have to run 1000 database dumps, and name them well enough to be able to identify them if one single database corrupts. How will you even know if this happens? Database errors will be localised to that specific one, as opposed to your entire app.

  5. UI: A user signs up or is invited to use your app, and belongs to one particular client. Are you going to save that user account to the client's database? If so, see scalability for the issue of working with that data when the user wants to change their password, or you want to email them. So, do you tell the user to let you know which database they're in so you can find them?

  6. Simplification: You have a database per client and want to just use a single one. How do you merge them all together without significantly breaking things? There'll be primary key conflicts if you use auto incremented IDs; bookmarked URLs will break if you decide to just regenerate the keys; foreign keys across tables will no longer point to the right records. Your data integrity will go down the pan.

You mention 'white label' services that offer their product through custom subdomains. I'm not privy to how these work, but the subdomain is only a basic CNAME or A record in their DNS zonefile. The process of adding these can be automated, and the design of the application and a bit of server configuration can deal with linking these subdomains to the correct accounts and data. They're just URLs, so maybe on the backend, the app doesn't differentiate between:

http://client.example.com http://example.com/client 

Overall though, you may decide that all these problems are things you can and would prefer to deal with. Be warned, however, that by doing so you may be shooting yourself in the foot, and you can gain a lot more from crafting a well-designed single database schema and a well-abstracted front-end.

*@xQbert mentions the very real benefit of scalability with multiple databases. I've amended this answer to clarify that I was more concerned with other aspects.

like image 135
mrlee Avatar answered Sep 25 '22 13:09

mrlee