Master-Slave Replication
In Master-Slave replication, there are two types of servers:
- Master Server: The primary server that accepts writes and updates.
- Slave Servers: One or more secondary servers that replicate the data from the Master Server.
The process works as follows:
- All write operations (e.g., INSERTs, UPDATEs, DELETEs) go to the Master Server.
- The Slave Servers are set to "read-only" mode and receive a copy of the transactions from the Master Server through a log-based replication method (e.g., MySQL's binary logging).
- When a Slave Server receives a transaction from the Master Server, it applies that transaction to its local database, ensuring data consistency.
Example:
Suppose you have an e-commerce website with a MySQL database. You want to ensure high availability and performance for your customers. In this scenario:
- The Master Server is the primary database server responsible for handling all write operations (e.g., new orders, user registrations).
- Two Slave Servers are set up as read-only replicas of the Master Server, which replicate the data in real-time.
This setup allows you to:
- Ensure high availability: If the Master Server goes down, one of the Slave Servers can be promoted to become the new Master Server.
- Improve performance: Customer reads (e.g., retrieving order status) can be directed to the Slave Servers, reducing the load on the Master Server.
Clustering
In clustering, multiple servers work together as a single unit, providing high availability and scalability. There are two types of clustering:
- Active-Passive Clustering: One server (the passive node) is idle until the primary server fails.
- Active-Active Clustering: Multiple servers share the workload, with each node active at the same time.
The process works as follows:
- All nodes in a cluster share access to a common storage system (e.g., shared file system or SAN).
- When one node fails, another node can take over its responsibilities.
- Clusters can be configured to use load balancing and failover techniques to ensure high availability.
Example:
Suppose you have an online banking platform with a PostgreSQL database. You want to ensure that your customers' financial data is always available. In this scenario:
- Three cluster nodes are set up, each running the same PostgreSQL instance.
- The cluster uses a shared storage system (e.g., SAN) to store customer data.
- Load balancing and failover techniques are configured so that if one node fails, another node can take over its responsibilities.
This setup allows you to:
- Ensure high availability: If one node fails, another node can take over its responsibilities, ensuring minimal downtime.
- Improve scalability: As the number of customers grows, new nodes can be added to the cluster, providing more resources for handling increased traffic.