-1

How can I handle instances of an application seeking to become software as a service? I want to make an app that can handle patients of a doctor and that patients can consult easily.

The question I have is regarding how to handle different doctors with their clients so that in the future can scale the app , and customers of a doctor are not mixed with other doctor clients in a database .

Is there any way to separate instances of an application to handle this, with separate databases by doctor?

Can I accomplish this with ruby on rails?

What would be the best way to do this?, I have no idea and I feel a little lost on this issue.

Cheers

  • 1
    This is called multitenancy. It's not something for the inexperienced developer to attempt. I would recommend https://leanpub.com/multi-tenancy-rails. – fylooi Jan 22 '16 at 16:54
  • Possible duplicate of [Best way to handle multitenancy in Rails 3](http://stackoverflow.com/questions/3776593/best-way-to-handle-multitenancy-in-rails-3) – Mike Szyndel Jan 22 '16 at 17:08
  • fylooi, Michal Szyndel, thank you for your answer I will look more about it – Juan Carlos Jan 22 '16 at 18:40

2 Answers2

1

Step one is not to worry about it. A reasonable database configuration will allow you to work with tables containing millions of rows before you even need to think about sharding your data or application. You're going to hit a lot of other bottlenecks limiting your scale before you get to that point. Spend your time and energy on those (and on building a product with actual users) first and avoid prematurely optimizing to solve performance problems you don't have.

Once you do need to consider sharding you absolutely do have options for a Rails app (https://blog.engineyard.com/2009/a-quick-primer-on-sharding-for-ruby-on-rails is a good introduction). Keeping sharding in mind when designing your application and data schema will help too. Look out for dependencies which would prevent you from isolating clusters of users and their data from each other. When you do have shared dependencies look for opportunities to extract services which could scale independently from the rest of the system.

At the database level I think https://github.com/thiagopradi/octopus is one of the most popular ways to shard Active Record models but certainly not the only option for a Rails app.

Jonah
  • 17,918
  • 1
  • 43
  • 70
  • The problem at hand is not sharing but multi tenancy. – Mike Szyndel Jan 22 '16 at 16:54
  • @MichalSzyndel multitenency may also be a concern but the question seems to me to be asking about both data isolation and horizontal scaling. It's not clear to me which is more relevant in this case given the content of the question. If you have thoughts on multitenecy as a solution please add an answer. – Jonah Jan 22 '16 at 17:01
0

Since your question was quite broad, I'll have to answer accordingly.

--

What you're asking is something called "multi tenancy":

Software Multitenancy refers to a software architecture in which a single instance of a software runs on a server and serves multiple tenants. A tenant is a group of users who share a common access with specific privileges to the software instance.

This is definitely the realm of development teams; you need several components to get it working, which Rails is not really equipped for.

Having said that, there is a popular way of achieving it with Apartment & PGSQL schemas.

--

Real multi-tenancy should have separate computing configurations, with their own resources and data-pools; Rails can only run on one server and - without massive hacking - one database.

If you wanted to create a system which handles doctors individually, you'll want to look at scoping your data. This is what PGSQL schemas do:

#config/routes.rb
scope constraints: SubDomain do
   resources :patients
end

#lib/sub_domain.rb
module SubDomain
    def initializer(router)
        @router = router
    end

    def self.matches?(request)
        Doctor.exists? request.subdomain
    end
end

The above gives you subdomains (the most base level of scoping), which will allow you to use the following:

#app/models/doctor.rb
class Doctor < ActiveRecord::Base
   has_many :patients
end

#app/models/patient.rb
class Patient < ActiveRecord::Base
   belongs_to :doctor
end

#app/controllers/patients_controller.rb
class PatientsController < ApplicationController
   before_action :set_doctor

   def index
      @patients = @doctor.patients
   end

   def show
      @patient = @doctor.patients.find params[:id]
   end

   private

   def set_doctor
      @doctor = Doctor.find request.subdomain
   end
end 

The above will allow you to access http://1.doctor.com/patients to see all the patients for that doctor:

#app/views/patients/index.html.erb
<% @patients.each do |patient| %>
   <%= patient.name %>
<% end %>

--

Of course, the above is a rudimentary example, with neither the database or application level security in place to maintain data integrity.

The main challenge of multi tenancy (with Rails) is creating as water-tight system as possible.

Richard Peck
  • 76,116
  • 9
  • 93
  • 147