CodeBlanks Logo Icon CodeBlanks

Master-slave

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

query.sql
-- master-slave
CREATE SUBSCRIPTION sub_orders

Sample solution

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

Question
CREATE SUBSCRIPTION sub_orders CONNECTION 'host=master dbname=shop' PUBLICATION ____
Answer
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

All lessons in Expert