Making 768 servers look like 1
147 points
• 1 day ago
• Article
Link
管理拥有数百万用户的应用时,基础设施常常需要扩展到远超单台数据库服务器的容量。虽然垂直扩展和只读副本能在短期内缓解压力,但最终会遇到主服务器写入瓶颈、固定的数据容量上限以及执行大规模备份的后勤难题。为了解决这些限制,组织必须采用数据库分片(Database Sharding),把数据和查询流量分散到多台服务器上。
分片通过将大型数据集切分到多个主节点来实现,从而支持 PB 级存储和高查询吞吐量。举例来说,可以把 768 台服务器划分为 256 个分片(shard),每个分片由一个主节点和两个副本组成。虽然这种做法能有效缓解性能瓶颈,但也显著增加了应用与数据交互的复杂性,需要一套能管理路由、备份和健康监控的全局系统来维护整个集群。
为了让数百个分片对外表现为一个统一的数据库,架构师通常会引入专门的代理层(proxy)。这些路由器位于应用与数据库之间,作为处理连接池、请求排队和查询解析的高级中间件运行。当应用提交查询时,路由器会查阅内部的拓扑映射以定位所需数据所在的分片。对于简单查询,路由器会直接将请求转发到相应节点;对于跨分片的复杂查询,路由器则会规划跨节点执行、聚合结果并返回最终数据集。
这类系统的配置通常通过定义数据拓扑的元数据文件来管理,例如 Vitess 的 VSchema 或 Neki 的数据映射文件。这些文件告诉路由器表和行如何分布,常用基于 ID 列的哈希策略。由于这些配置本质上是结构化文本,越来越多的 AI 代理被用来微调和优化复杂的分片方案,从而实现更高效的数据布局和查询规划。
当规模增长到每秒数百万次查询时,单个路由器自身也会成为瓶颈。工程师通常通过部署多个代理实例并由 Network Load Balancer (NLB) 协调来解决这一问题。 NLB 为应用提供单一入口点,将传入连接分发到路由器池中。这种部署彻底屏蔽了底层基础设施的复杂性,使应用可以像访问单个简单的数据库实例一样与大规模分布式集群交互。
一般来说,当数据库规模超过几 TB 时,采用分片架构是推荐做法——在这种规模下,增加的复杂性远小于性能和可靠性带来的收益。像 Neki for Postgres 和 Vitess for MySQL 这样的工具,已成为业界管理此类大规模系统的标准平台。借助这些专用平台,组织能够有效维护海量分布式数据库,同时保持应用逻辑清晰简洁,与底层硬件架构解耦。
Managing infrastructure for applications with millions of users often requires scaling far beyond the capacity of a single database server. While vertical scaling and read replicas offer short-term relief, they eventually succumb to bottlenecks such as write limitations on the primary server, fixed data capacity, and the logistical burden of performing massive backups. To overcome these constraints, organizations must turn to database sharding, which involves distributing both data and query traffic across many distinct servers.
Sharding works by partitioning a large dataset across multiple primary nodes, allowing for petabyte-scale storage and high query throughput. For instance, a system might scale to 768 servers by grouping them into 256 shards, with each shard consisting of one primary and two replicas. While this effectively solves performance bottlenecks, it introduces significant complexity in how the application interacts with the data, requiring a system that can manage routing, backups, and health monitoring across the entire fleet.
To make hundreds of shards appear as a single, cohesive database, architects implement a specialized proxy layer. These routers sit between the application and the database, functioning as advanced middleware that handles connection pooling, request queuing, and query parsing. When an application submits a query, the router consults its internal topology map to identify which specific shard holds the required data. For simple queries, the router forwards the request directly to the correct node. For complex queries spanning multiple shards, the router plans the execution across several nodes, aggregates the results, and returns the final set to the application.
Configuration for these systems is typically managed through metadata files that define the data topology, such as Vitess's VSchema or Neki's data mapping. These files tell the router exactly how tables and rows are distributed, often using hashing strategies based on an ID column. Because this configuration is essentially structured text, AI agents are increasingly being leveraged to help fine-tune and optimize these complex sharding schemes, allowing for more efficient data placement and query planning.
As the scale grows to millions of queries per second, a single router can become a bottleneck itself. Engineers solve this by deploying many proxy instances, often coordinated by a Network Load Balancer (NLB). The NLB provides a single entry point for the application, distributing incoming connections across the available pool of routers. This setup hides the underlying infrastructure complexity entirely, allowing the application to interact with a massive, distributed cluster as if it were a single, simple database instance.
Adopting a sharded architecture is generally recommended for databases exceeding a few terabytes, where the trade-off in complexity is far outweighed by the gains in performance and reliability. Tools like Neki for Postgres and Vitess for MySQL have become industry standards for managing this scale. By utilizing these specialized platforms, organizations can effectively maintain massive, distributed databases while keeping their application logic clean, simple, and abstracted from the underlying hardware architecture.
82 comments • Comments Link
• 大规模分片引入了显著的技术权衡,尤其在跨分片连接、序列与外键完整性方面,通常要求应用对查询路由和数据拓扑有深入理解。
• 在让分布式系统貌似像单一数据库与强迫其按单一数据库的方式表现之间存在根本性张力。试图在高层抽象中隐藏分片常常会"泄露"细节,当开发人员无意中执行跨分片操作时,会带来意想不到的复杂性和性能问题。
• 有人主张应用应显式处理针对特定分片的路由。这种做法把分片视为一等的架构考量,可以避免意外的跨分片事务,并确保开发者始终意识到系统的内在局限。
• 是否需要分片常常与垂直扩展的可行性一并讨论。虽有人认为多数组织可以通过单一的大型主库配合只读副本实现显著扩展,但也有人指出,在备份、恢复和网络存储等实际操作上存在瓶颈,使得在极端规模下不得不进行水平分区。
• 分片带来了巨大的"复杂性税"。团队必须在更高的故障率和维护负担与可扩展性收益之间权衡,许多人建议在物理上或经济上无法再进行垂直扩展之前,应优先考虑垂直扩展。
• 像 Oracle RAC 这样的数据库解决方案提供了一种模型:多个节点以非分片的集群方式运行,保持完整的 ACID 特性并支持标准的关系型功能。这与常见的分片架构形成对比,后者通常需要牺牲某些 SQL 能力或施加严格的查询限制。
• 分片策略的选择(例如基于范围的分片与一致性哈希)深刻影响随着时间扩展集群的能力。正确的设计可以减少频繁的数据重平衡需求,但也增加了对数据库如何应对不断增长负载的依赖。
• 对复杂数据库系统做基准测试本质上存在争议,因为配置各异并且常受厂商限制(例如 DeWitt clauses)。这些限制阻碍了对性能声明的独立验证,使人们质疑性能提升究竟是源于优秀工程设计,还是特定测试环境下的约束。
• 架构决策常常受组织文化和业务增长压力影响,而不仅仅是技术需求。有时在基础设施上"用钱堆砌"或过早采用分片,是为了满足快速交付功能的需求,而非应对真实的硬件容量上限。
• 可视化和文档在让复杂基础设施概念易于理解方面至关重要。使用定制动画和清晰、简化的概念模型,有助于弥合分布式系统理论的抽象与工程团队实际实现之间的鸿沟。
此次讨论反映出传统单体关系型数据库的支持者与拥护水平分片架构者之间的深刻分歧。尽管通过分片扩展被视为大型全球服务的技术必需,但许多参与者警告称,这种做法常被过早采用,导致不必要的运维复杂性和难以调试的"泄漏"抽象。大家达成的共识是,分布式系统从根本上改变了开发者体验,要求在管理查询和数据约束的方式上做出调整。最终,在垂直扩展与水平扩展之间的选择,往往更多地受制于组织约束、维护成本和增长时间表,而非单一数据库服务器的绝对性能极限。 • Sharding at scale introduces significant technical trade-offs, particularly regarding cross-shard joins, sequences, and foreign key integrity, often requiring the application to have a deep understanding of query routing and data topology.
• A fundamental tension exists between making a distributed system look like a single database and forcing it to behave like one. High-level abstractions that hide sharding can become "leaky," leading to unexpected complexity and performance issues when developers inadvertently execute cross-shard operations.
• Some argue that applications should explicitly handle routing to specific shards. This approach treats sharding as a first-class architectural concern, preventing accidental cross-shard transactions and ensuring that developers remain aware of the system's inherent limitations.
• The necessity of sharding is often debated against the viability of scaling vertically. While some maintain that most organizations can scale significantly using a single, high-capacity primary database with read replicas, others point to real-world operational bottlenecks in backup, recovery, and network storage that mandate horizontal partitioning at extreme scales.
• Sharding involves a substantial "complexity tax." Teams must weigh the potential for increased failure rates and maintenance burdens against the benefits of scalability, with many suggesting that vertical scaling should be prioritized until it is no longer physically or economically feasible.
• Database solutions like Oracle RAC provide a model where multiple nodes act as a unified, non-sharded cluster with full ACID compliance and support for standard relational features. This contrasts with common sharded architectures that often require sacrificing certain SQL capabilities or imposing strict query limitations.
• The choice of sharding strategy, such as range-based versus consistent hashing, profoundly impacts the ability to scale the cluster over time. Proper design can mitigate the need for frequent data rebalancing but introduces dependencies on how the database handles growing workloads.
• Benchmarking complex database systems is inherently controversial due to varying configurations and vendor restrictions (such as "DeWitt clauses"). These prevent independent verification of claims, leading to skepticism about whether performance gains are due to superior engineering or the specific constraints of the test environment.
• Architectural decisions are frequently influenced by organizational culture and business growth pressures rather than purely technical requirements. "Throwing money" at infrastructure or adopting sharding prematurely can sometimes be a response to the need for rapid feature delivery rather than a response to actual hardware capacity limits.
• Visualization and documentation play a critical role in making complex infrastructure concepts accessible. The use of custom animations and clear, simplified conceptual models helps bridge the gap between abstract distributed systems theory and practical implementation for engineering teams.
The discussion reflects a deep divide between proponents of traditional monolithic relational databases and those championing horizontally sharded architectures. While scaling through sharding is recognized as a technical necessity for massive global services, many participants caution that it is often adopted prematurely, creating unnecessary operational complexity and "leaky" abstractions that are difficult to debug. There is a consensus that distributed systems fundamentally change the developer experience, requiring a shift in how queries and data constraints are managed. Ultimately, the choice between vertical and horizontal scaling is often dictated as much by organizational constraints, maintenance costs, and growth timelines as it is by the absolute performance limits of a single database server.