PostgreSQL
Slony
Database Replication
Data Synchronization
Scheduled Sync

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.

Practice system design

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

  1. Slon Daemons: Separate processes (slon) run on each participating database, handling replication tasks.
  2. Schema Changes: Slony can replicate schema changes, but these must be meticulously managed to ensure consistency across the databases.
  3. Communication: Data and updates are transferred through a series of Slony-generated SQL commands.

Setting Up Slony

Initial Setup

  1. Install Slony: Ensure slony1-2.x is compatible with your PostgreSQL version.
  2. Create Slony Schema: This is done in the PostgreSQL database to house all replication-related tables and functions.
  3. 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.

  1. Cron Jobs: Leverage cron jobs on Unix-like systems to trigger replication tasks using the slon command.
  2. 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.

  1. Create a Shell Script: This script will start the slon process:
bash
1    #!/bin/bash
2    
3    SLON_BIN="/usr/local/bin/slon"
4    CLUSTER_NAME="mycluster"
5    CONFIG_FILE="/path/to/slon.conf"
6
7    $SLON_BIN $CLUSTER_NAME $CONFIG_FILE
  1. Schedule with Cron:
    Edit the cron table using crontab -e and add an entry for the cron job:
plaintext
    # Run Slony sync every day at 2am
    0 2 * * * /path/to/your/script.sh

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:

  1. Low-latency Environments:
    • Minimizes frequent updates, reducing strain on network resources.
  2. Batch Processing:
    • Ideal for end-of-business-day transactions where real-time data isn't critical.

Advantages and Disadvantages

FeatureAdvantagesDisadvantages
FlexibilityCustomizable schedules are possible.Complex configuration.
Performance OptimizationAlleviates network load during peak times.Increased lag between data changes.
Cost-EffectiveLeverages existing infrastructure.Potential for human error in scheduler setup.
System ScalabilityExpand 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
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track 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.

Practice system design

All Rights Reserved.