Should I use a single or multiple database setup for a multi-client application?

前端 未结 10 730
囚心锁ツ
囚心锁ツ 2020-11-30 17:50

I am working on a PHP application that intends to ease company workflow and project management, let\'s say something like Basecamp and GoPlan.

I am not sure on what

相关标签:
10条回答
  • 2020-11-30 18:08

    I usually add ClientID to all tables and go with one database. But since the database is usually hard to scale I will also make it possible to run on different database instances for some or all clients.

    That way you can have a bunch of small clients in one database and the big ones on separate servers.

    A key factor for maintainability though, is that you keep the schema identical in all databases. There will be headache enough to manage the versioning without introducing client specific schemas.

    0 讨论(0)
  • 2020-11-30 18:14

    When you're designing a multi-tenant database, you generally have three options:

    1. Have one database per tenant
    2. Have one schema per tenant
    3. Have all tenants share the same table(s)

    The option you pick has implications on scalability, extensibility and isolation. These implications have been widely discussed across different StackOverflow questions and database articles.

    In practice, each of the three design options -with enough effort- can address questions around scale, data that varies across tenants, and isolation. The decision depends on the primary dimension you’re building for. The summary:

    • If you're building for scale: Have all tenants share the same table(s)
    • If you're building for isolation: Create one database per tenant

    For example, Google and Salesforce follow the first pattern and have their tenants share the same tables. Stackoverflow on the other hand follows the second pattern and keeps one database per tenant. The second approach is also more commonplace in regulated industries, such as healthcare.

    The decision comes down to the primary dimension you're optimizing your database design for. This article on designing your SaaS database for scale talks about the trade-offs and provides a summary in the context of PostgreSQL.

    0 讨论(0)
  • 2020-11-30 18:16

    For multitenancy, performance will typically increase the more resources you manage to share across tenants, see

    http://en.wikipedia.org/wiki/Multitenancy

    So if you can, go with the single database. I agree that security problems would only occur due to bugs, as you can implement all access control in the application. In some databases, you can still use the database access control by careful use of views (so that each authenticated user gets a different view).

    There are ways to provide extensibility also. For example, you could create a single table with extension attributes (keyed by tenant, base record, and extension attribute id). Or you can create per-tenant extension tables, so that each tenant has his own extension schema.

    0 讨论(0)
  • 2020-11-30 18:16

    You can start with a single database and partition it as the application grows. If you do this, there a few things I would recommend:

    1) Design the database in a way that it can be easily partitioned. For example, if customers are going to share data, make sure that data is easily replicated across each database.

    2) When you have only one database, make sure it is being backed up to another physical server. In the event of a failover you can revert traffic to this other server and still have your data intact.

    0 讨论(0)
  • 2020-11-30 18:18
    • DEVELOPMENT For rapid development, use a database per customer. Think how easy it will be to backup, restore, or delete a customer's data. Or to measure/monitor/bill usage. You won't need to write code to do it by yourself, just use your database primitives.

    • PERFORMANCE For performance, use a database for all. Think about connection pooling, shared memory, caching, etc.

    • BUSINESS If your business plan is to have lots of small customers (think hotmail) you should probably work on a single DB. And have all administrative tasks such registration, deletion, data migration, etc. fully automated and exposed in a friendly interface. If you plan to have dozens or up to a few hundreds of big customers then you can work in one DB per customer and have system administration scripts in place that can be operated by your customer support staff.

    0 讨论(0)
  • 2020-11-30 18:20

    Listen to the Stackoverflow podcast where Joel and Jeff talk about the very same question. Joel is talking about their experience offering a hosted version of their software. He points out that adding client ids all over your DB complicates the design and code (are you sure you didn't accidentally forget to add it to some WHERE clause?) and complicates hosting feature, such as client-specific backups.

    It was in episode #20 or #21 (check the transcripts for details).

    0 讨论(0)
提交回复
热议问题