Consider using `Set` to store credential sets on the client instance and to keep track of which credentials have been applied to each socket instance in the pool. This would allow you to just do a quick set difference between what's been applied to the socket and the authentications that have been applied to the client.

In most of the MongoDB drivers we take this approach and in while synchronizing in Pool#checkout we validate that each socket has applied the correct logins/logouts with a simple set comparison:

https://github.com/mongodb/mongo-ruby-driver/blob/c947c93c9b2d131dbb3162ea61cc743e06e82622/lib/mongo/connection/pool.rb#L314-L335

This also has the added benefit of making your DB#logout method very simple. Just remove the credential from the client set and let the pool issue the logout when the socket gets checked out.