Rails Web 应用程序:是否为每个打开的帐户创建一个单独的数据库?

发布于 2024-12-02 21:01:00 字数 416 浏览 0 评论 0原文

我即将完成构建一个简单的基于订阅的支持票证 Web 应用程序。我正在设置授权。但由于这将是我自己要部署的 Web 应用程序,所以我对此感到好奇。

您是否为每个开设的帐户创建一个单独的数据库?

假设您有此支持票证 Web 应用程序。您只有一位帐户所有者。帐户所有者可以设置可以响应支持票证的代理。此外,还有一些客户角色可以打开支持票证。

正如您所看到的,数据库将包含用户、支持票证等。

最好的方法是什么?

1)为整个应用程序创建一个数据库?这样,每次有人注册时,所有内容都会与其他票证和用户数据以及其他所有内容一起添加到同一个数据库中,或者...

2) 每次有人注册时,为每个帐户订阅创建一个单独的数据库。

我认为出于安全和数据完整性的目的,选项 2 可能是最佳选择。如果是这样,您是如何解决这个问题的?

I'm about to finish building a simple subscription based support ticket Web app. I'm setting up authorization. But since this it's going to be my very own Web app that I'm going to deploy I'm wondering about this.

Do you create a separate database per account opened?

Let's say you have this support ticket Web app. You have ONE and ONLY ONE account owner. Account owner can setup agents that can respond to support tickets. Also, there are customer roles that open support tickets.

So as you can see the database will contain users, support tickets and more.

What is the best way to go?

1) Create one database for the whole application? That way every time somebody signs up, everything is added to the same database with the other tickets and users data and everything else or...

2) Everytime someone signs up, create a separate database per account subscription.

I'm thinking that maybe option number 2 would be a best choice for security and data integrity purposes. If so, how have you gone about tackling this issue?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冷血 2024-12-09 21:01:00

听起来你想要的是多租户:

多租户是指软件架构中的一项原则,其中
该软件的单个实例在服务器上运行,为多个
客户组织(租户)。多租户与
多实例架构,其中单独的软件实例(或
硬件系统)是为不同的客户组织设置的。和
多租户架构,软件应用程序旨在
虚拟地对其数据和配置进行分区,并且每个客户端
组织使用定制的虚拟应用程序实例。
- 有关多租户的维基百科文章

这篇文章虽然有点过时,但它是我将如何去做的总体思路。 Simple Rails 多租户。它干净、高效,可以让您免于编写不需要的代码。

It sounds like what you want is Multitenancy:

Multitenancy refers to a principle in software architecture where a
single instance of the software runs on a server, serving multiple
client organizations (tenants). Multitenancy is contrasted with a
multi-instance architecture where separate software instances (or
hardware systems) are set up for different client organizations. With
a multitenant architecture, a software application is designed to
virtually partition its data and configuration, and each client
organization works with a customized virtual application instance.
- Wikipedia article on Multitenancy

This article while a little dated is the general idea of how I would go about doing it. Simple Rails Multi-Tenancy. It's clean and efficient and saves you from writing code that you don't need to.

迷迭香的记忆 2024-12-09 21:01:00

你应该选择选项#1。第二个(几乎(可能在某些情况下它是好的,但我目前找不到))永远不是一种选择。

出于安全目的(嗯,在某种意义上),您是正确的,但它也会产生许多您必须考虑的其他问题。

  1. 为每个用户拥有不同的数据库意味着对于每个请求(请记住,HTTP 是无状态的),您将必须打开一个到数据库的新连接,执行任何需要执行的操作,然后再次关闭连接,而不是使用 Rails 中的连接池。这对性能影响很大。

  2. 数据库越多,管理就会越麻烦。此外,在服务器上拥有多个数据库确实需要更多的资源,而不仅仅是一个更大的数据库。

  3. 您必须绕过 Rails 中的整个连接处理,因为通常每个应用程序都有一个数据库。更改特定模型的数据库很容易,但它增加了可能出错的额外位置。

  4. Rails 确实具有在同一数据库中确定范围和处理分离数据的良好功能,仅适用于您提到的那种用例。

You should go for option #1. Number 2 is (almost (there are probably cases where it is good, but I can't find one at the moment)) never an option.

You are right in security purposes (well, in a sense), but it also creates a lot of other problems that you will have to think about.

  1. Having a different database for each user means that for each request (remember, HTTP is state-less) you will have to open up a new connection to the database, do whatever needs to be done and then close the connection again, instead of using the connection pooling that is in Rails. This affects the performance a great deal.

  2. Administration will be a hassle the more databases you have. Also, having multiple databases on a server do require more resources than just a bigger database.

  3. You would have to circumvent the entire connection handling in Rails since there it is usually one database per application. It is easy to change the database for specific models, but it adds additional places where things can go wrong.

  4. Rails do have good functionality for scoping and handling of separating data within the same database, just for that kind of use-case that you are mentioning.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文