Rails
PostgreSQL
Octopus Gem
Database Replication
Development Environment

Rails Postgresql replication via Octopus gem when in Development env

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

In modern web applications, especially those built with Ruby on Rails, database replication is a powerful technique to improve performance, scale read operations, and ensure high availability. PostgreSQL is a popular choice for the database, providing robust features for replication. When working on a Rails application in the development environment, using database replication effectively can be challenging, yet it offers valuable insights into scaling your application.

The Octopus gem is a versatile tool in the Rails ecosystem that facilitates multi-database operations, including read/write splitting, and can be used for PostgreSQL replication setup. This article explores how to leverage the Octopus gem to handle PostgreSQL replication in a Rails development environment.

Prerequisites

Before diving into implementation, ensure that you have the following:

  • A Rails application set up with PostgreSQL.
  • The Octopus gem included in your Gemfile.
  • Multiple PostgreSQL instances for master and slave configurations.

Setting Up PostgreSQL Replication

PostgreSQL Configuration

Firstly, you'll need to set up PostgreSQL replication. At a high level, this involves configuring one PostgreSQL server as the master and another as the slave. Here's a brief overview of the necessary steps:

  1. Master Configuration:
    • Edit the postgresql.conf file to enable replication settings. Set:
conf
     wal_level = replica
     max_wal_senders = 10
  • In the pg_hba.conf file, allow connections from the slave server(s):
 
     host replication repl_user <slave_ip_address>/32 md5
  1. Slave Configuration:
    • Use pg_basebackup to copy the database cluster from the master.
    • Configure recovery settings in the recovery.conf file:
 
     standby_mode = 'on'
     primary_conninfo = 'host=<master_ip_address> port=5432 user=repl_user password=<password>'

After configuring replication, verify that the slave server is synchronizing data from the master.

Integrating the Octopus Gem with Rails

The Octopus gem provides a simple API for database sharding and replication. In a development environment, this enables you to simulate a replication setup and test read/write operations through Rails models.

Installation

Add the Octopus gem to your Gemfile and run bundle install:

ruby
gem 'ar-octopus'

Configuration

Create an octopus.rb initializer in config/initializers to specify your database environments and replication settings:

ruby
Octopus.setup do |config|
  config.environments = [:development]
end

Modify your database.yml to include slave configurations:

yaml
1development:
2  main:
3    adapter: postgresql
4    database: myapp_development
5    username: user
6    password: password
7    host: master_db
8  slave:
9    adapter: postgresql
10    database: myapp_development
11    username: user
12    password: password
13    host: slave_db
14
15octopus:
16  environments:
17    - development
18  development:
19    main_shard: main
20    replicated: true
21    fully_replicated: true
22    slaves:
23      - slave

Using Octopus in Rails

With Octopus correctly configured, you can manage read/write operations in your application models:

ruby
1# Example Model
2class User < ApplicationRecord
3  # Specify that reads should go to the slave
4  def self.fetch_from_slave
5    Octopus.using(:slave) do
6      User.find_by(name: "John Doe")
7    end
8  end
9  
10  # Writes will automatically go to the master
11  def update_name(new_name)
12    update(name: new_name)
13  end
14end

Additional Considerations

Testing

When developing in a Rails environment, ensure your test suite is configured to account for read/write splitting. This means running tests both against the master and using simulated slave connections.

Migration

Ensure that database migrations target the master database as Octopus routes these operations there. This can be explicitly checked with:

ruby
Octopus.using(:master) do
  # Run migration or other critical operation
end

Performance Monitoring

For larger applications, use monitoring tools like New Relic or Scout to track replication lag and read/write performance across your database instances.

Summary Table

AspectMasterSlave
RolePrimary Data StorageSecondary for Read Ops
Default Write PathYesNo
Replication Settingwal_level = replicastandby_mode = 'on'
Suitable OperationsWrite/ReadRead-Intensive
Configuration Filepostgresql.confrecovery.conf

Conclusion

Setting up PostgreSQL replication with the Rails Octopus gem in a development environment provides a robust framework for preparing your application to scale. It supports separating read and write operations, optimizing database performance, and preparing for high availability. Understanding and implementing these techniques in development ensures your application is prepared for production demands.


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.