CodeBlanks Logo Icon CodeBlanks

Summary - Replication and sharding

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 / RANGE

Replication copies, sharding splits.

Practice Summary - Replication and sharding

Summary task: shard the users table with PARTITION BY HASH (user_id).

Starter code

query.sql
-- summary sharding
CREATE TABLE users (user_id INT, name TEXT)

Sample solution

query.sql
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

Question
CREATE PUBLICATION pub_users FOR TABLE ____
Answer
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

All lessons in Expert