如何从数据库分区演进到分片来实现横向扩展?

发布于 2024-09-16 04:44:53 字数 764 浏览 4 评论 0原文

假设我有一个 MySQL 表:

CREATE TABLE tweets (
tweet_id INT NOT NULL AUTO_INCREMENT,
author_id INT NOT NULL,
text CHAR(140) NOT NULL,
PRIMARY KEY (tweet_id)
)
PARTITION BY HASH(tweet_id)
PARTITIONS 12;

一切都很好。该表位于单个服务器 - Server1 上。但最终我可能想要横向扩展。因此,我想对表进行分片,并将 12 个分区中的 6 个移动到新服务器 - Server2 上。

我希望:

  • Server1 包含奇数推文:分区 1、3、5、7、9、11
  • Server2 包含偶数推文:分区 2、4、6、8、10、0

1) 什么是将这些分区从 Server1 移动到 Server2 的最佳方法?我需要确保自动增量 tweet_id 的值在迁移过程中保持不变。

2)现在我有2台服务器,如何确保2台服务器生成的自增tweet_id的值不相同?我还需要确保每个分区上的 tweet_id 保持一致,即在分区 k 上,每个 tweet_id 的模 12 等于 k。

3)理想情况下,我想继续这个扩展过程。所以稍后我想添加第三台服务器 - Server3。我想重新平衡分区,以便每台服务器上有 4 个分区。同样,我如何确保 3 个服务器生成的自动增量 tweet_id 是不同的,并且 tweet_id 的模 12 在每个分区内保持一致?

Say I have a MySQL table:

CREATE TABLE tweets (
tweet_id INT NOT NULL AUTO_INCREMENT,
author_id INT NOT NULL,
text CHAR(140) NOT NULL,
PRIMARY KEY (tweet_id)
)
PARTITION BY HASH(tweet_id)
PARTITIONS 12;

All is good. The table lives on a single server - Server1. But eventually I may want to scale out. So I'd want to shard the table and move 6 of the 12 partitions onto a new server - Server2.

I'd want:

  • Server1 to contain odd-numbered tweets: partitions 1, 3, 5, 7, 9, 11
  • Server2 to contain even-numbered tweets: partitions 2, 4, 6, 8, 10, 0

1) What is the best way to move those partitions from Server1 to Server2? I need to make sure the values of the auto-increment tweet_id's remain unchanged during the migration.

2) Now that I have 2 servers, how do I make sure the auto-increment tweet_id's generated by the 2 servers don't have the same value? I'd also need to make sure the tweet_id on each partition stays consistent, i.e. on Partition k every tweet_id's modulo 12 equals to k.

3) Ideally I'd like to continue this scale out process. So later on I'd want to add a 3rd server - Server3. I'd want to re-balance the partitions so that there're 4 partitions on each server. Again how do I make sure the auto-increment tweet_id's generated by the 3 servers are distinct and that the modulo 12 of tweet_id's stay consistent within each partition?

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

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

发布评论

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

评论(2

遇到 2024-09-23 04:44:53

首先,我建议不要对 tweet_id 使用 AUTO_INCRMENT。 Twitter API 为您提供了带有推文的 ID,该 ID 已经保证是唯一的。如果您愿意,您也可以稍后使用它通过 API 引用推文。然而,如果您已经收集了大量数据,那么听起来可能为时已晚。

查看 auto_increment_offsetauto_increment_increment 系统变量。您可以使用它们来确保自动增量 ID 不会相互冲突。基本上,您希望将 auto_increment_offset 设置为大于所有现有 ID 的数字,但在第二个服务器上将其设置得更高。然后,将 auto_increment_increment 设置为 2。这将确保一台服务器生成所有奇数 ID,另一台服务器生成所有偶数 ID。要继续扩大规模,只需相应地调整这些值即可。

一般来说,MySQL 中的分区功能并不是为横向扩展而设计的。如果您需要跨分区查看,您的应用程序将需要处理查询多个服务器的逻辑。

分割数据的最佳选择是选择要放在每个服务器上的推文 ID 范围。在您的情况下,获取推文 ID 的前半部分并将其放在服务器 2 上可能是有意义的。然后服务器 1 可以保持活动状态,直到服务器 2(和您的新应用程序逻辑)准备就绪为止。

First of all, I would suggest not using AUTO_INCREMENT for tweet_id. The Twitter API gives you an ID with the tweet which is already guaranteed to be unique. You can also use this to reference the tweet via the API later if you choose. However, it sounds like it may be too late for that if you already have a lot of data collected.

Look at the auto_increment_offset and auto_increment_increment system variables. You can use those to ensure your autoincrement IDs don't conflict with each other. Basically, you want to set auto_increment_offset to a number greater than all existing IDs, but set it one higher on the second server. Then, set auto_increment_increment to 2. This will ensure that one server generates all odd IDs and the other generates all even IDs. To keep scaling up, just adjust these values accordingly.

Generally speaking, the partitions features in MySQL aren't designed for scaling out. Your application will need to handle the logic of querying multiple servers if you need to look across partitions.

Your best bet to split up the data is to select ranges of tweet IDs to put on each server. It probably makes sense in your case to grab the first half or so of tweet IDs and put them on server 2. Then server 1 can stay live until server 2 (and your new application logic) are ready to go).

花开浅夏 2024-09-23 04:44:53

您可能想看看 dbShards,它可以为您处理这些问题。所有分片中的唯一值都支持自动增量,您可以使用模数将键映射到虚拟分片,而不是将它们直接绑定到物理分片。这使得添加新分片变得更加容易。您可以在 http://www.dbshards.com/dbshards/ 中了解更多信息。

问候,

安迪。

You might want to take a look at dbShards, which handles these issues for you. Auto increment is supported with unique values across all shards and you can use modulus to map keys to virtual shards rather than tying them directly to physical shards. This makes it easier to add new shards. You can read more at http://www.dbshards.com/dbshards/.

Regards,

Andy.

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