PostgreSQL Slony replication - scheduled sync
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
Slony is a powerful open-source replication system designed for PostgreSQL, offering a wealth of configurations suited for a range of replication needs. Unlike PostgreSQL's built-in streaming replication, which is synchronous, Slony is generally asynchronous, making it ideal for replication over wide area networks where latency could be significant. One compelling feature it supports is scheduled synchronization, where data replication is configured to occur at predefined times.
How Slony Replication Works
Slony operates on a publish/subscribe model. The primary server acts as the origin, where the original data is stored, and one or more secondary servers, known as subscribers, are synchronized with the origin.
Key Components
- Slon Daemons: Separate processes (
slon) run on each participating database, handling replication tasks. - Schema Changes: Slony can replicate schema changes, but these must be meticulously managed to ensure consistency across the databases.
- Communication: Data and updates are transferred through a series of Slony-generated SQL commands.
Setting Up Slony
Initial Setup
- Install Slony: Ensure
slony1-2.xis compatible with your PostgreSQL version. - Create Slony Schema: This is done in the PostgreSQL database to house all replication-related tables and functions.
- Configure Node Network: Define nodes using Slony configuration scripts, specifying origins and subscribers.
Scheduled Synchronization
Scheduled sync provides flexibility to balance load and performance by controlling when data is replicated.
- Cron Jobs: Leverage cron jobs on Unix-like systems to trigger replication tasks using the
sloncommand. - Event Management: Specify event horizons and frequency to manage event generation and consumption.
Example: Scheduled Sync
Below is an example of setting up a scheduled sync for environments that demand specific replication timings.
- Create a Shell Script: This script will start the
slonprocess:
- Schedule with Cron:Edit the cron table using
crontab -eand add an entry for the cron job:
Performance Considerations
The delayed and scheduled nature of this replication method can lead to divergent data between updates. Hence, it's often not used for scenarios requiring real-time synchronization. However, it is advantageous for:
- Low-latency Environments:
- Minimizes frequent updates, reducing strain on network resources.
- Batch Processing:
- Ideal for end-of-business-day transactions where real-time data isn't critical.
Advantages and Disadvantages
| Feature | Advantages | Disadvantages |
| Flexibility | Customizable schedules are possible. | Complex configuration. |
| Performance Optimization | Alleviates network load during peak times. | Increased lag between data changes. |
| Cost-Effective | Leverages existing infrastructure. | Potential for human error in scheduler setup. |
| System Scalability | Expand subscriber nodes effortlessly. | Lacks the efficiency of built-in streaming. |
FAQs
How does Slony handle conflicts?
Slony presumes a "trusted" master copy of the data from which replicas are made and doesn’t inherently resolve conflicts automatically. Proper application logic and constraints must be in place to prevent conflicting transactions.
Is Slony suitable for high-availability setups?
Primarily, Slony handles data replication and not failover. For high-availability setups, combining Slony with other tools such as Pacemaker or PostgreSQL's built-in mechanisms is recommended.
Conclusion
Slony's support for scheduled synchronization provides a convenient approach for environments willing to trade-off immediacy for controlled data consistency. When correctly configured, it can achieve efficient data dissemination while reducing loads during peak operational hours. Although it may not be the best fit for applications requiring real-time updates, its open-source nature and configurability make it a valuable tool in PostgreSQL's replication landscape.
Related reading
- postgresql streaming replication -- continuous archiving?
- PouchDB - start local, replicate later
- Prevent Caching in ASP.NET MVC for specific actions using an attribute
- Prevent FLUSH TABLES query from being replicated
- PostgreSQL V9.4 - Logical Decoding - SQL interface Starting at a specific LSN?
- pq could not resize shared memory segment. No space left on device
- Practical uses for AtomicInteger
- PRECONDITION_FAILED Delivery Acknowledge Timeout on Celery & RabbitMQ with Gevent and concurrency

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.