Summary - Replication and sharding
- SQL course
- Expert
- Lesson 29/45
- 5 min
Shard a users table by shard key.
Summary - Replication and sharding
Replication copies data (publication/subscription), sharding splits it (PARTITION BY HASH/RANGE). Master writes, slaves read. Synchronous is consistent, asynchronous is fast.
-- CREATE PUBLICATION -> CREATE SUBSCRIPTION
-- PARTITION BY HASH / RANGEReplication copies, sharding splits.
Practice Summary - Replication and sharding
Summary task: shard the users table with PARTITION BY HASH (user_id).
Starter code
-- summary sharding
CREATE TABLE users (user_id INT, name TEXT)Sample solution
CREATE TABLE users (user_id INT, name TEXT) PARTITION BY HASH (user_id);Expected output
CREATE TABLE
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Replication publication
CREATE PUBLICATION pub_users FOR TABLE ____
CREATE PUBLICATION pub_users FOR TABLE users
Explanation: Changes to the users table added to the publication are copied to subscribers.
2 How does sharding split data?
- Vertically
- By type
- Horizontally Correct answer
- By column
Explanation: Sharding splits data horizontally.
3 Summary - Replication and sharding: match the terms
| Term | Meaning |
|---|---|
replication |
copy |
sharding |
split |
master |
master |
sync |
sync |