Shard Key Immutability
What you'll learn: Why shard keys must remain unchanged after a record is created, and what happens if they don't.
The Core Problem
When you shard a database, each record lives on a specific shard determined by its shard key. For example, if you shard users by user_id, user 12345 always lives on the same shard calculated from that ID.
Now imagine a user changes their primary identifier (the shard key). Suddenly, the system needs to:
- Calculate which shard the record should now live on
- Move the entire record from the old shard to the new shard
- Update all references and maintain consistency during the move
This cross-shard data movement is expensive, complex, and error-prone. It requires coordinated transactions across shards, network transfers, and potential downtime or locks.
The Library Book Analogy
Think of a library where books are shelved by the author's last name (the "shard key"). If you suddenly decide to re-file all books by publication year instead, you'd have to:
- Find every book
- Move it to a completely different shelf
- Update the catalog
- Hope no one tries to check out the book mid-move
It's chaos. Better to pick a filing system and stick with it.
Why Immutability Matters
When choosing a shard key (like user_id, customer_id, or order_id), you must select something that will never change. Don't use:
- Email addresses (users change them)
- Usernames (users rename accounts)
- Geographic regions (users relocate)
Use stable, permanent identifiers instead.
Key Takeaway: Choose shard keys that are immutable from creation—changing a shard key requires expensive cross-shard data movement that undermines the entire purpose of sharding.