Scott Ringwelski's Blog

Knockoff: A poorly named read-replica gem

Our application at Handshake does a lot of database reads. Eventually, we needed to set up read-replica, and chose Octopus to do so.

Octopus worked — but it had its downsides. The configuration file was a bit verbose and complex (https://gist.github.com/catsby/6923840). Because it was more than just read-replicas, it supported other types of sharding strategies which sometimes conflicted with replica functionality. For example, equality of records was overwritten to be false if not from the same shard/replica. This was something we could work around (https://github.com/thiagopradi/octopus/compare/master…strydercorp:master), but maintaining a fork is not always the best use of time.

We wanted something simpler and focused on read-replicas. There are a host of libraries including replica_pools, makara, active_replica, active_replicas, and more, but all fell short in one area or another.

Knockoff

Knockoff is a simple gem for read-replicas (https://github.com/sgringwe/knockoff) . It’s top goals are

  • Minimal ActiveRecord monkey-patching
  • Easy run-time configuration using ENV variables
  • Opt-in usage of replicas
  • No need to change code when adding/removing replicas
  • Thread safe (we run it on Puma and Sidekiq)

we’ve been running it in production at Handshake for a while now, and it works great.