>_
EngineeringNotes
Back to DBMS Topics
Lecture 21

The Master-Slave Database Concept

Master-Slave replication is the foundational building block of database scaling. By dedicating one node to write commands and secondary nodes to read queries, we split the I/O bottleneck and gain system resilience.

1

Master-Slave Architecture Topology

Here is how data flows in a Master-Slave setup. Note that write requests go exclusively to the Master database, which asynchronously propagates changes to the Slaves, while read queries are load-balanced across the Slaves:

App ServiceBackend API Servers(Connection Pooled)Master DatabasePrimary Node (Postgres)Writes & UpdatesSlave Database ASecondary Node (NoSQL/MongoDB)Reads OnlySlave Database BSecondary Node (NoSQL/MongoDB)Reads OnlyWrite commands (INSERT / UPDATE)ReadReadReplicateReplicate
2

Core Architectural Pillars

1. IO Optimisation under High Load

When database read/write request velocity is extremely high, a single server experiences heavy disk and socket I/O bottlenecks. Master-Slave splits this work: the Master server handles writing locks, while lightweight read-oriented secondary servers handle queries, preventing transaction starvation.

2. Connection to CQRS Pattern

This setup is the physical implementation of CQRS (Command Query Responsibility Segregation). Write command queries (INSERT, UPDATE, DELETE) target the Master (command), and read queries (SELECT) target Slaves (query).

3. Safeguarding Site Availability

By offloading queries to multiple replicas, we ensure that if one replica fails, requests fail over to another replica. Furthermore, read queries are served locally near users (reducing round-trip latency) without taxing the transactional master.

4. Replication Synchronicity

Database replication handles transferring transaction logs from Master to Slaves. This runs in either synchronous (strong consistency but high write latency) or asynchronous (eventual consistency with replication lag) configuration.

3

Synchronous vs. Asynchronous Replication

Depending on your database requirements (e.g. banking transactions vs. social media feeds), you must configure replication carefully:

Replication TypeHow it WorksProsConsTypical Use Case
SynchronousMaster waits for confirmation from all (or a quorum of) slaves that the write log is written on their disk before acknowledging success to the client app.Zero Replication Lag. Perfect data consistency across all nodes.Slow write latency. If a single slave node blocks or crashes, all writes to the Master block as well.Banking ledgers, financial accounts.
AsynchronousMaster writes data locally and immediately sends success acknowledgment to the app. Replicates logs to slaves in the background.Fast writes (low latency). Master remains healthy even if slaves fail.Replication lag occurs. For a few seconds, slaves contain stale data. Potential data loss if master crashes before logs sync.Cab driver coordinates, social media posts, search indexes.
4

Connection to Database Clustering Concepts

Master-Slave replication is the foundational mechanism for Database Clustering.

Primary/Secondary Failover Routing

In database clustering configurations (Active-Passive or Active-Active), a cluster manager checks node health. If the Master node suffers a physical disk failure, the clustering engine triggers an automatic failover election. One of the Slaves is promoted to Master to begin receiving write requests, ensuring high availability.

Load Balancing Read Queries

True clustering uses a load-balancing layer (like PgBouncer or HAProxy) that intercepts client calls. The clustering controller checks read loads on the Slave nodes and distributes incoming queries symmetrically, ensuring no single slave gets overwhelmed.

5

Original Lecture Slide Reference