Master-slave
- SQL course
- Expert
- Lesson 25/45
- 5 min
Learn the primary and replica structure.
Master-slave
In the master-slave (primary-replica) model, writes go only to the master. Slaves copy from the master and serve read queries.
CREATE SUBSCRIPTION sub_orders
CONNECTION 'host=master dbname=shop'
PUBLICATION pub_orders;This query writes to master, reads from slave.
Master-slave — details
If the read load is heavy, you add more slaves; each shares read traffic. But write speed is capped by the master alone.
Practice Master-slave
Connect the sub_orders subscription to the pub_orders publication with CONNECTION 'host=master dbname=shop'.
Starter code
-- master-slave
CREATE SUBSCRIPTION sub_ordersSample solution
CREATE SUBSCRIPTION sub_orders CONNECTION 'host=master dbname=shop' PUBLICATION pub_orders;Expected output
CREATE SUBSCRIPTION
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Create a subscription
CREATE SUBSCRIPTION sub_orders CONNECTION 'host=master dbname=shop' PUBLICATION ____
CREATE SUBSCRIPTION sub_orders CONNECTION 'host=master dbname=shop' PUBLICATION pub_orders
Explanation: pub_orders is the right answer.
2 Which node receives writes?
- The replica (slave)
- A reader
- The primary (master) Correct answer
- Both
Explanation: Writes go only to the master.
3 Master-slave: match the terms
| Term | Meaning |
|---|---|
master |
master |
slave |
slave |
write |
write |
read |
read |