ActiveRecord queries execute twice when using Octopus gem configuration

I’m working with a Rails app that uses the Octopus gem for database sharding. I have a User model set up and I’m testing this in my development environment.

octopus:
  environments:
    - development
    - staging  
    - production
  replicated: true
  fully_replicated: true
  development:
    replica1:
      host: 10.0.1.100
      adapter: mysql2
      database: app_dev
      username: dev_user
      password: dev_pass
      reconnect: false
  staging:
    replica1:
      host: 10.0.2.50
      adapter: mysql2
      database: app_staging
      username: staging_user
      password: staging_secret
      reconnect: false
  production:
    replica1:
      host: 10.0.3.25
      adapter: mysql2
      database: app_prod
      username: prod_user
      password: prod_secret
      reconnect: true

Whenever I run User.all or User.count, I notice that the exact same SQL query gets executed twice on the database. Here’s what I see in the logs:

[Shard: replica1]   (0.8ms)  SELECT COUNT(*) FROM `users`
[Shard: replica1]   (1.2ms)  SELECT COUNT(*) FROM `users`
 => 42

Is this expected behavior with the Octopus gem or did I mess up something in my configuration?

maybe octopus is hitting both master and replica even tho replicated: true. try adding slave_groups in your config or see if those queries are being seen as writes. oh, and turn off any rails query caching - that could be causing the double calls.

that’s def weird! have you checked if query caching is on or if some middleware like a logging tool is messing with the calls? does this issue pop up with other models too or just User? by the way, what version of Octopus are you using?

I’ve seen this exact issue with Octopus in distributed setups. Those duplicate queries usually happen because of fully_replicated: true and how Octopus routes connections. It’s probably treating your read queries like they need validation across multiple connection points. Try flipping fully_replicated: false temporarily - bet the duplicates vanish. Also check for any custom middleware or gems hooking into ActiveRecord’s query cycle. Monitoring tools and database profilers love to mess with sharded configs and trigger duplicate queries. Those different reconnect settings between environments could be screwing with connection state too.