Course contentsShow
← System Design
Lesson 243 of 1,9197. Database Scaling: ShardingFree lesson

Geo-Based Sharding

Partition data by geographic region to minimize latency and comply with data residency requirements.

Geo-Based Sharding

What you'll learn: How to partition database data by geographic location to reduce latency and meet legal requirements.

What Is Geo-Based Sharding?

Geo-based sharding means dividing your database so that data from users in different regions lives in different database shards located in those regions. If you have users in Europe, Asia, and North America, you'd maintain three shards—one per region—with each user's data stored in the shard closest to them.

Why Geography Matters

Think of it like opening local branches of a store instead of forcing everyone to travel to headquarters. When a London user accesses your app, their data comes from a London database server, not one halfway across the world. This dramatically reduces network latency because data doesn't need to cross continents.

Beyond speed, many countries enforce data residency laws requiring citizen data to stay within national borders. GDPR in Europe, for example, restricts where European user data can be stored. Geo-based sharding naturally satisfies these legal requirements.

How It Works

The shard key is typically a location identifier—user country, region code, or IP-derived location. Your application routing layer checks where a user is located and directs their requests to the appropriate regional shard.

For example:

  • Users in region=EU → EU database shard
  • Users in region=US → US database shard
  • Users in region=APAC → Asia-Pacific shard

This is essentially range-based sharding where the ranges are geographic boundaries instead of numeric values.

Trade-offs

Cross-region queries become complicated—if you need to aggregate data across all users globally, you must query multiple shards. Also, users who travel internationally may experience inconsistencies if their data doesn't follow them (though you can route by account home region rather than current location).

Key Takeaway: Geo-based sharding stores data near users geographically, optimizing latency and enabling compliance with regional data laws, but complicates global queries.