Today I Learned

TIL, 2018-06-03, Sharding, Read/Write Databases

Visit Grade Tecko and its accompanying Heroku app!

Research on Sharding, Reading Octopus


  • If you have HashWithIndifferentAccess, just use it?
  • ActiveRecord::VERSION::MAJOR and MINOR is a thing to check versions.
  • Logger method:
def self.logger
  if defined?(Rails.logger)
    @logger ||= Rails.logger
    @logger ||=$stderr)
  • Hooks into ActiveRecord::Base.connection, creates an Octopus::Proxy
  • Inclusion method:
def self.included(base)

How would I implement separate databases for reading and writing operations?


  • Read/write master databases and read-only slaves are a common pattern, especially for big applications doing mostly read accesses/data warehouses.
  • It allows you to scale and to tune the databases differently, for either efficient reads or efficient writes.
  • It really depends on what you are trying to achieve by having two databases.
    • If it is for performance reasons (which i suspect it may be) i would suggest you look into denormalizing the read-only database as needed for performance.
    • If performance isn’t an issue then I wouldn’t mess with the read-only schema.
  • I’ve worked on similar systems where there would be a read/write database that was only lightly used by administrative users. That database would then be replicated to the read only database during a nightly process.

Slow Rails Queries


  • New Relic, PgHero to track down query bottlenecks in the applications.
  • Seq Scans: Means PG needs to scan a table row by row to find matching entries.
  • Seed local database to mimic production. Perform benchmarks on a local computer, with settings resembling a real production site.
  • Siege for local benchmarks.


This project is maintained by daryllxd