Promote secondary to primary from secondary node
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
In the world of distributed databases, maintaining high availability and data integrity across multiple nodes is paramount. In setups involving master-slave architectures, a common operation is to promote a secondary (or slave) node to a primary (or master) node. This is particularly relevant in scenarios where the primary node fails or requires maintenance, and the system needs to continue functioning seamlessly. This article explores the technical procedures and considerations involved in making a secondary node primary, including both manual and automated approaches.
Understanding Node Roles
Primary Node
- Purpose: Handles all write operations and coordinates data across secondary nodes.
- Responsibilities: Maintains the latest and most complete view of the data.
Secondary Node
- Purpose: Receives read operations and asynchronously fetches updates from the primary node.
- Responsibilities: Acts as a backup for failover scenarios and enhances read scalability.
Scenarios for Promotion
- Primary Node Failure: The most common trigger for promoting a secondary node is the failure of the primary node. A quick promotion ensures minimal downtime and data loss.
- Maintenance Tasks: Sometimes a primary node needs to be taken down for maintenance or upgrades. Promoting a secondary node can keep the system operational during this period.
- Geographic Considerations: In geodistributed setups, promoting a secondary node closer to a particular user base can reduce latency for local users.
Technical Steps for Promoting a Secondary Node
Manual Promotion
In a manual promotion setup, administrators are responsible for the switch:
- Identify Potential Candidates: Choose a candidate secondary node. It should be the most up-to-date in terms of replication lag.
- Assess Replication Lag: Check the replication status to ensure the secondary node has the latest data.
- Stop Applications: Temporarily halt applications or set them to read-only mode to prevent discrepancies during promotion.
- Promote the Secondary Node: Configurations vary depending on the database system (e.g., PostgreSQL or MySQL), but typically involve commands or config changes like:
- For MySQL: Use
RESET MASTERandSET GLOBAL read_only=0to allow write operations. - For PostgreSQL: Modify
recovery.confto remove recovery settings.
- Reconfigure Old Primary and Other Secondaries: Once promoted, adjust configurations on the old primary and other nodes to realign them with the new primary.
Automated Promotion
Many modern databases have built-in support for automated failover processes that handle node promotion:
- Heartbeat Monitoring: Utilizes a watcher service to monitor the health of the primary node.
- Automatic Election: Upon primary failure detection, a process often aided by a consensus protocol (like Raft or Paxos) will elect a new primary.
- Notification Services: Alerts and logging to notify administrators of the changeover, maintaining transparency and records of the event.
Practical Example
Consider a PostgreSQL setup with synchronous replication between nodes. Here's a simplified manual process to promote a secondary node:
After this, reconfigure other secondary nodes to follow the newly promoted primary node.
Considerations and Challenges
- Replication Consistency: Ensure all transactions are committed across nodes.
- Network Partitioning: Protect against split-brain scenarios by confirming network stability.
- Data Integrity: Utilize tools to verify data consistency post-promotion.
Summary Table
| Topic | Key Points |
| Primary Node Role | Handles writes, coordinates replication. |
| Secondary Node Role | Manages reads, backup for failover. |
| Promotion Scenarios | Node failure, maintenance, and geography. |
| Manual Promotion | Involves direct admin intervention using database commands. |
| Automated Promotion | Uses heartbeat, election protocols, and often integrated failover solutions. |
| Challenges Ahead | Managing replication lag, network reliability, and ensuring data integrity. |
Conclusion
Promoting a secondary node to primary is an essential procedure for maintaining database availability and reliability. Whether manually or automatically triggered, understanding the nuances of this process helps administrators ensure smooth transitions with minimal impact on users. Careful monitoring and testing are fundamental to mastering this aspect of database management.
Related reading
- promoting a master in replication
- Propagate Sleuth baggage on parallel streams
- Proper way to cache results TaskT with IMemoryCache
- Pros and cons of multi-leader vs leaderless replication in databases?
- Proper Realm usage patterns/best practices?
- Properly close mongoose's connection once you're done
- Pros and Cons of Amazon SageMaker VS. Amazon EMR, for deploying TensorFlow-based deep learning models?
- Pros and cons on utilizing Azure Service Fabric vs Custom Azure Cloud?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.