1

I am working on setting up multiple sites all part of one company. I'm using nitrous.io to development in. I have read that you can use one DB in production. Do I need to do that in the development side as well? Not sure how to do that.

File structure on nitrious box/domains to be used

  • Accounts/ (user.site1.com & user.site2.com)
  • Admin/ (admin.site1.com)
  • Shop/ (shop.site1.com & shop.site2.com)
  • Site1/ (site1.com)
  • Site2/ (site2.com)

I'll be using devise for the accounts, and want to implement cross site login. Looked into some of that already.

From what I have read, it kinda sounds like the other posts cover a bit more than I want and make it out to be a PITA. It sounds like if more than one site needs to modify the same tables it gets complicate. That's not the case. One site may edit a given table, but others will need access to the table.

Any tips would be greatly appreciated.

Ian

2 Answers 2

2

You definitely do not want to have multiple sites all talking to the same database. That's a recipe for a mess. Here are a few examples of things that can go wrong...

  1. Site 1 updates table at the same time as site 2, and there is a conflict as to what data should be saved.
  2. You update the code on site 1 to add a new column to the table. You do the same for site 2, but site 2's update crashes because the new column has already been added (by site 1).
  3. One site tries to read a record just after another has deleted it, and there is a conflict.
  4. etc. (there are many more cases I'm sure you can think of)

But all is not lost!!!

What you DO need to do is create a single server that is an API for the other sites. All your sites will call this single server for all updates, and that single server will be the only thing that talks to the database.

Your API will have a few basic actions - one to create a new record, one to get a record, one to update a record, and one to delete a record.

You might start by checking out the Rails resources documentation at http://guides.rubyonrails.org/routing.html#resource-routing-the-rails-default - it goes into how you can automatically generate a basic CRUD API. CRUD btw stands for Create, Read, Update and Delete - the basic functions you'll want to do.

Later, you might get more creative with your API and expand to add more functions. Suppose every time you create an order, there are several steps to be taken. What you can do is make one route in your API that you call, and the controller for that route makes all the updates necessary. Basically any time you find yourself writing code for one of your sites that makes the same set of API calls every time, you should consider creating a new route in the API that makes that call.

Another advantage of an API is that it lets you do some good thinking about what you want your database and actions to be.

You might start by first putting together the basic API, then write just one of your sites. Get it working, then add another. Consider Googling around for "Rails restful API" for some good references, and good luck!

Sign up to request clarification or add additional context in comments.

7 Comments

This is a recipe for horrifying performance, though, and lots of re-implementation of logic application-side that should be expressed as queries. It's a fast-path to n+1 SELECT territory. There's certainly a place for this approach - but it should rarely be built on a simple "CRUD" style API.
Not at ALL a recipe for bad performance. That's what APIs are for - allowing multiple applications to access a shared system of data and business logic.
I'm really confused - are you saying even a basic CRUD API is a bad approach?
no - I'm saying that just a CRUD API is generally bad for performance. A proper API with app-specific logic and queries is of course fine if well designed. Though posssibly overkill for this use case.
Yes, if the apps are independently updated and versioned, for sure. Whether it's a web API, a library API, stored procs, views, or whatever, a stable interface is a necessity. I was working on the assumption that the apps are all versioned and managed together as part of a shared codebase; will amend answer.
|
0

Rails users typically use multiple schemas in a single DB for this, in cases where the "multiple apps" are really one app with multiple versions - e.g. "customer2.mycompany.com", "customer3.mycompany.com", etc. This is usually called "multi-tenant" hosting.

You can query shared tables and user/site/product specific tables, controlling which is found using the search_path.


Note:

This approach can also work where multiple apps access shared data, but it only really works well if all the apps share the same code for accessing the shared data. That can be a web API (per Tim's answer), a library providing functions or objects, or it can be a database-level API with views and procedures. (For Rails only a web API or a library will work well, Rails tends to deal very poorly with view and procedure based interfaces).

If each app has its own code for accessing the shared data you'll be wasting time and effort - and you'll also make maintenance into a nightmare. Every time you have to change the structure of the shared data in any way, you'll have to update all the apps.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.