{"id":18323998,"url":"https://github.com/dirk/active_replicas","last_synced_at":"2025-06-17T14:40:28.121Z","repository":{"id":62552889,"uuid":"74251118","full_name":"dirk/active_replicas","owner":"dirk","description":"Smart read replicas in ActiveRecord","archived":false,"fork":false,"pushed_at":"2017-06-29T01:06:46.000Z","size":77,"stargazers_count":3,"open_issues_count":0,"forks_count":1,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-03-21T13:45:39.674Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dirk.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-11-20T03:44:04.000Z","updated_at":"2018-04-09T04:43:25.000Z","dependencies_parsed_at":"2022-11-03T04:15:18.685Z","dependency_job_id":null,"html_url":"https://github.com/dirk/active_replicas","commit_stats":null,"previous_names":[],"tags_count":23,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dirk%2Factive_replicas","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dirk%2Factive_replicas/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dirk%2Factive_replicas/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dirk%2Factive_replicas/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dirk","download_url":"https://codeload.github.com/dirk/active_replicas/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247419597,"owners_count":20936009,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-05T18:30:46.128Z","updated_at":"2025-04-06T00:30:51.239Z","avatar_url":"https://github.com/dirk.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# ActiveReplicas\n\n[![Build Status](https://travis-ci.org/dirk/active_replicas.svg?branch=master)](https://travis-ci.org/dirk/active_replicas)\n\nDrop-in read-replica querying in ActiveRecord. It proxies around ActiveRecord's connection to send read-only queries to replica databases; writes will automatically go to the primary and \"stick\" the request into using the primary for any further queries.\n\nThis is heavily inspired by [Kickstarter's `replica_pools`](https://github.com/kickstarter/replica_pools) gem. It seeks to improve on that gem by better interfacing with ActiveRecord's connection pools.\n\n## Installation \u0026 usage\n\nActiveReplicas injects itself into ActiveRecord. To start you'll want to add it to your application's `Gemfile`:\n\n```ruby\ngem 'active_replicas'\n```\n\nYou then need to instruct it as to which connection to use for the primary and which connection(s) to use for the read replicas:\n\n```ruby\n# config/initializers/active_replicas.rb\nActiveReplicas::Railtie.hijack_active_record primary: { url: 'mysql2://user@primary/my_app' },\n                                             replicas: {\n                                               replica0: { url: 'mysql2://user@replica/my_app' }\n                                             }\n```\n\n**Note**: ActiveReplicas does not do anything automatically. It only injects itself into ActiveRecord when you tell it do so (see above).\n\n## How it works\n\nA few things happen when you call [`hijack_active_record`](http://www.rubydoc.info/github/dirk/active_replicas/master/ActiveReplicas/Railtie#hijack_active_record-class_method):\n\n- It defines delegations for the [connection](http://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters.html) methods that can be sent to replicas and the methods that must be sent to a primary.\n- It sets up a [`ConnectionHandler`](http://www.rubydoc.info/github/dirk/active_replicas/master/ActiveReplicas/Rails4/ConnectionHandler) that will act in place of ActiveRecord's normal connection handler.\n- It replaces ActiveRecord's log subscriber with its own [`LogSubscriber`](http://www.rubydoc.info/github/dirk/active_replicas/master/ActiveReplicas/LogSubscriber) which adds information about replica/primary status to logging messages.\n\nThe new connection handler manages connection pools for each configured primary and replica database(s). Externally it provides instances of [`ProxyingConnectionPool`](http://www.rubydoc.info/github/dirk/active_replicas/master/ActiveReplicas/ProxyingConnectionPool) in place of normal connection pools. Those proxying pools then forward connection methods to primary or replica pools.\n\n### Using it with Ruby on Rails\n\nActiveReplicas works seamlessly within the Rails request lifecycle. It extends the [`clear_active_connections!` called in ActiveRecord's middleware](https://github.com/rails/rails/blob/4-2-stable/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb#L645-L663) to not only clear the active connections (releasing them back to their respective pool) but also resets the primary/replica state. That way the next connection will start out using a replica.\n\n## Contributing\n\nBug reports and pull requests are welcome on [GitHub][]. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.\n\n[GitHub]: https://github.com/dirk/active_replicas\n\n## License\n\nLicensed under the 3-clause BSD license. See [LICENSE](LICENSE) for details.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdirk%2Factive_replicas","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdirk%2Factive_replicas","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdirk%2Factive_replicas/lists"}