Course contentsShow
← Databases & SQL
Lesson 456 of 1,9535. Relational Schema ArchitectureFree lesson

Database Sharding Fundamentals

Evaluate architectural requirements for distributing a single schema across multiple physical nodes.

In this lesson, you will learn how to evaluate the architectural requirements for distributing a single schema across multiple physical nodes through the practice of sharding.

The Concept of Sharding

When a single database node reaches its physical limits—whether in terms of storage capacity, CPU, or I/O throughput—we transition from simple partitioning to sharding. While partitioning usually keeps data within a single instance, sharding involves horizontally slicing your data and distributing those slices (shards) across different servers or physical clusters.

Architectural Requirements

Before moving to a multi-node architecture, you must evaluate your schema against three critical requirements:

  1. Shard Key Selection: You must choose a column that provides uniform distribution. If you use user_id as a shard key, all data associated with a specific user stays together. If you choose a poorly distributed key (like status_code), you risk a "hot shard" where one node carries 90% of the traffic.
  2. Cross-Shard Query Complexity: In a single node, you can join any two tables easily. Once sharded, a JOIN between a users table on Node A and an orders table on Node B is impossible at the database level. You must evaluate if your application architecture can handle "scatter-gather" queries, where the application fetches data from multiple nodes and joins them in memory.
  3. Transactional Integrity: Because individual nodes operate independently, enforcing UNIQUE constraints or FOREIGN KEY relationships across physical boundaries becomes a massive hurdle. You often have to move these constraints from the database layer into the application logic.

Illustrative Example

Consider a Global_Users table. If we shard by region, our architecture looks like this:

-- Node 1: North America
INSERT INTO users_na (user_id, name) VALUES (101, 'Alice');

-- Node 2: Europe
INSERT INTO users_eu (user_id, name) VALUES (202, 'Bob');

When you need to look up a user, your application logic must identify the region first, then route the query to the correct connection string. This adds complexity to your connection pooling and maintenance routines, requiring robust sharding middleware or careful application-level routing.

Key Takeaway: Sharding trades the simplicity of ACID compliance and local joins for the ability to scale horizontally, requiring you to carefully select a distribution key and manage cross-node data integrity within your application.