ExplainThis 全端開發雙週報 #90 分片 (Sharding) 是什麼?使用時有哪些利弊?
ExplainThis released the 90th issue of its full-stack development biweekly newsletter, approaching its upcoming 100th issue milestone. The introduction highlights recent growth in the E+ membership tier, where several new members enrolled using corporate education subsidies, with over half choosing lifetime memberships. The E+ program provides technical articles, videos on full-stack development and AI engineering, a Discord community, and tax-compliant invoicing for corporate reimbursement. Following this announcement, the newsletter transitions into its core technical subject of database sharding. Sharding is a database architecture strategy that distributes different subsets of data across multiple machines, contrasting with replication which copies identical data. By partitioning datasets into smaller segments, systems can route queries directly to the specific machine responsible for that data without searching all nodes. In production environments, sharding and replication are often combined to maintain partitioned data boundaries while providing high availability and fault tolerance when nodes fail. While replication distributes data across multiple machines to improve availability and lower latency, sharding primarily provides scalability. Systems can scale through vertical scaling by upgrading individual machine hardware or through horizontal scaling by adding more machines. Single-machine hardware limits make vertical scaling insufficient for massive data volumes. Sharding implements horizontal scaling by splitting data across multiple nodes, enabling system capacity and throughput to grow through adding nodes or splitting and migrating shards.
Read the original ↗Contents
分片 (Sharding) 是什麼?使用時有哪些利弊?
ExplainThis released the 90th issue of its full-stack development biweekly newsletter, approaching its upcoming 100th issue milestone. The introduction highlights recent growth in the E+ membership tier, where several new members enrolled using corporate education subsidies, with over half choosing lifetime memberships. The E+ program provides technical articles, videos on full-stack development and AI engineering, a Discord community, and tax-compliant invoicing for corporate reimbursement. Following this announcement, the newsletter transitions into its core technical subject of database sharding.
- The ExplainThis Full-Stack Biweekly newsletter has reached its 90th edition, nearing its 100th issue.
- Multiple new members recently enrolled in the E+ membership program utilizing corporate training subsidies.
- Over half of the new E+ subscribers opted for the lifetime membership option.
- The E+ membership offers technical articles, videos covering full-stack and AI engineering, and an exclusive Discord community.
- E+ issues corporate-friendly tax invoices to facilitate reimbursement through employer education stipends.
分片 (Sharding) 是什麼?使用時有哪些利弊?
Sharding is a database architecture strategy that distributes different subsets of data across multiple machines, contrasting with replication which copies identical data. By partitioning datasets into smaller segments, systems can route queries directly to the specific machine responsible for that data without searching all nodes. In production environments, sharding and replication are often combined to maintain partitioned data boundaries while providing high availability and fault tolerance when nodes fail.
- Unlike replication which stores identical data on multiple nodes, sharding splits distinct subsets of data across different machines.
- Sharding allows queries to directly target the designated shard rather than scanning all machines in the cluster.
- Sharding and replication are not mutually exclusive and are commonly deployed together in practice.
- Combining sharding with replication ensures that if a machine holding a specific shard fails, replica nodes can continue serving traffic.
分片有哪些好處與成本?
While replication distributes data across multiple machines to improve availability and lower latency, sharding primarily provides scalability. Systems can scale through vertical scaling by upgrading individual machine hardware or through horizontal scaling by adding more machines. Single-machine hardware limits make vertical scaling insufficient for massive data volumes. Sharding implements horizontal scaling by splitting data across multiple nodes, enabling system capacity and throughput to grow through adding nodes or splitting and migrating shards.
- Replication improves system availability and reduces latency, whereas sharding primarily delivers scalability.
- Vertical scaling upgrades single-machine resources (such as RAM and disk space) but eventually hits physical hardware limits.
- Horizontal scaling expands system capacity by adding more machines rather than upgrading single-machine specifications.
- Sharding is a horizontal scaling technique that partitions data across multiple machines.
- Systems using sharding can handle growing data volumes and workloads by adding nodes, splitting shards, or migrating shards.
每個系統都該用分片嗎?
Although sharding offers theoretical scalability advantages, it is practically not recommended for all data systems, particularly those in early stages or with limited scale. While replication improves system availability even for small datasets by handling node failures, sharding provides marginal benefits when data volume and read/write loads remain manageable on a single node. Modern hardware significantly increases single-machine capacity limits, reducing the urgency to adopt sharding. Consequently, organizations should avoid the development and operational overhead of sharding unless growth genuinely surpasses single-node boundaries.
- Sharding is not recommended for early-stage or small-scale systems where loads fit within a single node.
- Replication provides availability benefits regardless of data size by protecting against node failure with failover mechanisms.
- Sharding introduces development and maintenance overhead that may outweigh benefits for workloads lacking multi-tenant isolation or high-scale requirements.
- Advancements in modern hardware allow single machines to handle much higher workloads than in the past, reducing the need for premature sharding.
分片有哪些實務上的困難與成本?
Implementing database sharding introduces significant practical challenges and operational costs, primarily centered on choosing an appropriate partition key. The choice of partition key directly dictates query performance; suboptimal selection can lead to costly cross-shard scans, especially within relational data models involving entities like users and orders. Additionally, managing database transactions across shards introduces substantial complexity and latency, often necessitating protocols like two-phase commit to ensure atomicity during updates across different shards.
- Selecting a partition key is the primary challenge in sharding because it determines which shard holds specific data and heavily impacts query latency.
- Queries that do not align with the partition key must scan multiple shards and aggregate results, significantly degrading system performance.
- Relational data models with interdependent entities (e.g., users, orders, products) force trade-offs between different query patterns when selecting a partition key.
- Handling transactions across shards is much more complex and slower than single-database transactions, requiring coordination mechanisms like two-phase commit to preserve atomicity.
- Designing partition keys requires deep architectural understanding of data relationships, common query paths, and update patterns to minimize cross-shard transactions.
閱讀更多
This section concludes the initial part of a discussion on the technical concept of sharding based on the book Designing Data-Intensive Applications (DDIA). It directs readers interested in a more comprehensive exploration of sharding to the E+ Growth Plan. Members of the program can access extended and deeper coverage of the topic.
- The text concludes the first part of a technical discussion on sharding derived from the book DDIA.
- Sharding is the primary technical concept addressed in this series.
- Further and more in-depth exploration of sharding is available through the E+ Growth Plan membership.
本期推薦
This newsletter edition highlights several engineering articles and open-source tools spanning frontend optimization, AI agents, language compilers, and systems programming. Featured topics include Size Limit for tracking JavaScript bundle sizes in CI, Cloudflare's Rust-based TypeScript compiler port tsrs, and LangChain's practical guide on evaluating AI agents. Additional essays explore the relevance of frontend frameworks in the age of AI coding assistants, common O(n²) performance pitfalls in seemingly clean JavaScript code, the historical influence of libraries on native Web APIs, and how Linux namespaces underpin Docker containers.
- Size Limit is an open-source tool that monitors JavaScript bundle sizes in CI and comments changes on pull requests to prevent performance regression.
- Cloudflare introduced tsrs, a TypeScript-to-Rust port that achieves up to 8x performance improvements and reduces memory usage by around 70% compared to TypeScript 7.0.
- LangChain outlined core practices for evaluating AI agents, addressing dataset creation, LLM-as-a-judge usage, and multi-step process evaluation.
- Nesting find() inside map() in JavaScript can degrade performance to O(n²), increasing API p99 latency significantly, which can be mitigated using Map or Set data structures.
- Building a basic container from scratch in Go demonstrates how Docker leverages Linux namespaces and cgroups for process, filesystem, and hostname isolation.