ankane / distribute_reads

Scale database reads to replicas in Rails

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Is it possible to disallow writes?

cjlarose opened this issue · comments

I'm interested in making specific blocks of code in my application perform reads from the slave pool, but they shouldn't try to write. I'd like to prevent application developers from accidentally writing code that doesn't correctly account for replication lag and other impacts brought on by reading from a slave, then writing to the primary.

Is something like this already possible with this gem? If not, I'm curious on your thoughts of a allow_writes option that's on by default, but can be disabled. Alternatively, there could be a raise_on_attempted_writes option that's false by default.

distribute_reads(allow_writes: false) do
  User.find_each do |user|                 # replica
    user.orders_count = user.orders.count  # replica
    user.save!                             # raise an exception
  end
end

Hey @cjlarose, interesting idea. You can probably fork and add it pretty easily. The main logic would go here. It seems like a pretty specialized use case, so I'm hesitant to add it to the gem.

@ankane Thanks for responding! I understand that my use case might be pretty niche. Thanks for pointing me in the right direction as far as where code is we need to modify. I opened a separate issue (#3) for adding a license to the project to make sure we can use a fork.