Move replicate logic out of db namespace