1

I am looking for best method to create application which are connected to different databases based on client

So basically it is a multi tenancy model for an android app (enterprise use)

Deploy the same app to different customers but connect to different database for different customer.

The things I thought about is making a key and for each customer the key varies and a correct webservice will be contacted based on the key ( individual webservices for each client)?

Has anyone done something similar? What is the best method to approach this. Thanks

3

In any case you will require a server API (webservice, REST or similar) to connect your Android app to the database. Assuming your database is available through such an API, there are two options.

  1. Design the database & server to serve multiple customers (multi-tenancy). With this your server API will expect the tenancy selection to happen on a per-request basis (e.g. a HTTP header or a query attribute), or at session initialization (e.g. when the user logs in).

    REST example:

    # get customers for tenant ABC
    GET example.com/api/customer/?tenant=ABC
    # create a new customer for tenant ABC
    POST example.com/api/customer/?tenant=ABC
    

    Obviously, your server will have to make sure clients can only access data that they are allowed to. Instead of passing the tenant as a query parameter, it can also be passed as an HTTP header or set as part of the session.

  2. Create different API endpoints, with each endpoint connecting to a different database. In this case your Android app needs to be configurable in some manner, e.g. by requesting the API endpoint to use from (say) the central login server.

    Example:

    # Request API endpoint to use
    # we assume the configuration response to contain the apiUrl parameter
    apiUrl = GET login.example.com/configuration/?user=XY
    GET <apiUrl>/customer/
    ...
    

    The server will run multiple endpoints, e.g. abc.example.com or xyz.example.com for tenants ABC and XYZ, respectively. So the actual API calls will look like this:

    # get customers for tenant ABC 
    GET abc.example.com/customer
    # get customers for tenant XYZ
    GET xyz.example.com/customer
    

As always with engineering decisions, there are a couple of trade-offs to consider:

  1. Data secrecy/segregation v.s. operation efficiency. Multitenancy is relatively simple to solve in a database (add a tenantfield or similar to at least the core tables of your data model, or every table for that matter). Operating one database is easier than operating many. However, in some industries you may need to keep data in separate databases.

  2. Declarative v.s. explicit client code. In the first example, your app client needs to be aware of the actual tenant it runs for to be able to send the right parameters to the server. In the second example, all that the app client needs to know is the endpoint's URL to connect to.

Note that the first and second example could also be mixed:

  • In the second example, the same database can be used for different customers. To do so, run a front-end webserver that adds a tenant header to the HTTP request depending on the API endpoint.

  • In the first example, the server could route the request to a different database depending on the tenant selection. Some frameworks make such routing trivial, e.g. Django has a database router built-in.

  • thanks for the insight :). In my case, each customer has a specific database and so I think it is the second approach suits the purpose. What I am confused about is how configurable is the endpoint . Say customer A downloads app for Client A and customer B downloads app for Client B. How will I recognize it as from Client A and Client B? [Can you elaborate on that configuring android app for customer to customer] based on some App setting? – Rohith Nair Nov 21 '14 at 12:28
  • I've updated the second example, hopefully making it more clear. I'm presuming clients A and B will download the same app, and that it is their login credentials that will redirect the app to the correct database. I.e. the login server knows which <apiUrl> to apply to each user. – miraculixx Nov 21 '14 at 12:33
  • The problem though is central login server is not feasible, because each customer has multiple users (segregated). Is there some way I can put in some key into the android app when a user downloads it from some location?. But actually you have some how captured all parameters I was thinking :) – Rohith Nair Nov 21 '14 at 12:41
  • Well if you distribute the app from an appstore (Google or Apple), I don't think you can determine a client association, but I'm not sure. Another option is to ask the user's client association before starting the login, so your central server would simply handout the apiUrl and the actual login happens there. Or you can store the various apiUrls in the app itself, but I wouldn't recommend that (new client => new app version, very tedious). – miraculixx Nov 21 '14 at 13:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.