{"id":13747280,"url":"https://github.com/ankane/distribute_reads","last_synced_at":"2025-11-17T14:11:14.346Z","repository":{"id":44691660,"uuid":"86281388","full_name":"ankane/distribute_reads","owner":"ankane","description":"Scale database reads to replicas in Rails","archived":false,"fork":false,"pushed_at":"2025-10-22T05:15:47.000Z","size":184,"stargazers_count":368,"open_issues_count":0,"forks_count":40,"subscribers_count":7,"default_branch":"master","last_synced_at":"2025-11-05T07:04:23.816Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ankane.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-03-27T02:15:24.000Z","updated_at":"2025-11-04T09:50:57.000Z","dependencies_parsed_at":"2023-01-31T07:00:37.630Z","dependency_job_id":"6a4fb1b3-8d15-4871-bc47-8ac4bb2272c2","html_url":"https://github.com/ankane/distribute_reads","commit_stats":{"total_commits":232,"total_committers":10,"mean_commits":23.2,"dds":"0.10775862068965514","last_synced_commit":"9c112350593fd9c7ba1a4b423aec32b687a4e8d3"},"previous_names":[],"tags_count":16,"template":false,"template_full_name":null,"purl":"pkg:github/ankane/distribute_reads","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ankane%2Fdistribute_reads","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ankane%2Fdistribute_reads/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ankane%2Fdistribute_reads/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ankane%2Fdistribute_reads/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ankane","download_url":"https://codeload.github.com/ankane/distribute_reads/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ankane%2Fdistribute_reads/sbom","scorecard":{"id":197146,"data":{"date":"2025-08-11","repo":{"name":"github.com/ankane/distribute_reads","commit":"9c112350593fd9c7ba1a4b423aec32b687a4e8d3"},"scorecard":{"version":"v5.2.1-40-gf6ed084d","commit":"f6ed084d17c9236477efd66e5b258b9d4cc7b389"},"score":3.4,"checks":[{"name":"Code-Review","score":0,"reason":"Found 0/30 approved changesets -- score normalized to 0","details":null,"documentation":{"short":"Determines if the project requires human code review before pull requests (aka merge requests) are merged.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#code-review"}},{"name":"Binary-Artifacts","score":10,"reason":"no binaries found in the repo","details":null,"documentation":{"short":"Determines if the project has generated executable (binary) artifacts in the source repository.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#binary-artifacts"}},{"name":"Pinned-Dependencies","score":0,"reason":"dependency not pinned by hash detected -- score normalized to 0","details":["Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/build.yml:21: update your workflow using https://app.stepsecurity.io/secureworkflow/ankane/distribute_reads/build.yml/master?enable=pin","Warn: third-party GitHubAction not pinned by hash: .github/workflows/build.yml:22: update your workflow using https://app.stepsecurity.io/secureworkflow/ankane/distribute_reads/build.yml/master?enable=pin","Warn: third-party GitHubAction not pinned by hash: .github/workflows/build.yml:27: update your workflow using https://app.stepsecurity.io/secureworkflow/ankane/distribute_reads/build.yml/master?enable=pin","Warn: third-party GitHubAction not pinned by hash: .github/workflows/build.yml:33: update your workflow using https://app.stepsecurity.io/secureworkflow/ankane/distribute_reads/build.yml/master?enable=pin","Info:   0 out of   1 GitHub-owned GitHubAction dependencies pinned","Info:   0 out of   3 third-party GitHubAction dependencies pinned"],"documentation":{"short":"Determines if the project has declared and pinned the dependencies of its build process.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#pinned-dependencies"}},{"name":"Packaging","score":-1,"reason":"packaging workflow not detected","details":["Warn: no GitHub/GitLab publishing workflow detected."],"documentation":{"short":"Determines if the project is published as a package that others can easily download, install, easily update, and uninstall.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#packaging"}},{"name":"SAST","score":0,"reason":"no SAST tool detected","details":["Warn: no pull requests merged into dev branch"],"documentation":{"short":"Determines if the project uses static code analysis.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#sast"}},{"name":"Dangerous-Workflow","score":10,"reason":"no dangerous workflow patterns detected","details":null,"documentation":{"short":"Determines if the project's GitHub Action workflows avoid dangerous patterns.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#dangerous-workflow"}},{"name":"Maintained","score":0,"reason":"0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0","details":null,"documentation":{"short":"Determines if the project is \"actively maintained\".","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#maintained"}},{"name":"Token-Permissions","score":0,"reason":"detected GitHub workflow tokens with excessive permissions","details":["Warn: no topLevel permission defined: .github/workflows/build.yml:1","Info: no jobLevel write permissions found"],"documentation":{"short":"Determines if the project's workflows follow the principle of least privilege.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#token-permissions"}},{"name":"CII-Best-Practices","score":0,"reason":"no effort to earn an OpenSSF best practices badge detected","details":null,"documentation":{"short":"Determines if the project has an OpenSSF (formerly CII) Best Practices Badge.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#cii-best-practices"}},{"name":"Fuzzing","score":0,"reason":"project is not fuzzed","details":["Warn: no fuzzer integrations found"],"documentation":{"short":"Determines if the project uses fuzzing.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#fuzzing"}},{"name":"Security-Policy","score":0,"reason":"security policy file not detected","details":["Warn: no security policy file detected","Warn: no security file to analyze","Warn: no security file to analyze","Warn: no security file to analyze"],"documentation":{"short":"Determines if the project has published a security policy.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#security-policy"}},{"name":"Vulnerabilities","score":10,"reason":"0 existing vulnerabilities detected","details":null,"documentation":{"short":"Determines if the project has open, known unfixed vulnerabilities.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#vulnerabilities"}},{"name":"License","score":10,"reason":"license file detected","details":["Info: project has a license file: LICENSE.txt:0","Info: FSF or OSI recognized license: MIT License: LICENSE.txt:0"],"documentation":{"short":"Determines if the project has defined a license.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#license"}},{"name":"Signed-Releases","score":-1,"reason":"no releases found","details":null,"documentation":{"short":"Determines if the project cryptographically signs release artifacts.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#signed-releases"}},{"name":"Branch-Protection","score":0,"reason":"branch protection not enabled on development/release branches","details":["Warn: branch protection not enabled for branch 'master'"],"documentation":{"short":"Determines if the default and release branches are protected with GitHub's branch protection settings.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#branch-protection"}}]},"last_synced_at":"2025-08-16T22:01:05.739Z","repository_id":44691660,"created_at":"2025-08-16T22:01:05.739Z","updated_at":"2025-08-16T22:01:05.739Z"},"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":283028445,"owners_count":26767190,"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","status":"online","status_checked_at":"2025-11-06T02:00:06.180Z","response_time":55,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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-08-03T06:01:23.729Z","updated_at":"2025-11-17T14:11:14.341Z","avatar_url":"https://github.com/ankane.png","language":"Ruby","funding_links":[],"categories":["Ruby"],"sub_categories":[],"readme":"# Distribute Reads\n\nScale database reads to replicas in Rails\n\n**Distribute Reads 1.0 was recently released** - see [how to upgrade](#upgrading)\n\n:tangerine: Battle-tested at [Instacart](https://www.instacart.com/opensource) with [Makara](https://github.com/instacart/makara)\n\n[![Build Status](https://github.com/ankane/distribute_reads/actions/workflows/build.yml/badge.svg)](https://github.com/ankane/distribute_reads/actions)\n\n## Installation\n\nAdd this line to your application’s Gemfile:\n\n```ruby\ngem \"distribute_reads\"\n```\n\n## How to Use\n\n[ActiveRecordProxyAdapters](https://github.com/Nasdaq/active_record_proxy_adapters) does most of the work. First, update `config/database.yml` to use it:\n\n```yml\ndefault: \u0026default\n  primary:\n    adapter: postgresql_proxy\n    url: \u003c%= ENV[\"DATABASE_URL\"] %\u003e\n  replica:\n    adapter: postgresql\n    url: \u003c%= ENV[\"REPLICA_DATABASE_URL\"] %\u003e\n    replica: true\n\ndevelopment:\n  \u003c\u003c: *default\n\nproduction:\n  \u003c\u003c: *default\n```\n\n**Note:** You can use the same instance for the primary and replica in development.\n\nThen add `connects_to` to `app/models/application_record.rb`:\n\n```ruby\nclass ApplicationRecord \u003c ActiveRecord::Base\n  connects_to database: {writing: :primary, reading: :replica}\nend\n```\n\nBy default, all reads go to the primary instance. To use the replica, do:\n\n```ruby\ndistribute_reads { User.count }\n```\n\nWorks with multiple queries as well.\n\n```ruby\ndistribute_reads do\n  User.find_each do |user|                 # replica\n    user.orders_count = user.orders.count  # replica\n    user.save!                             # primary\n  end\nend\n```\n\n## Jobs\n\nDistribute all reads in a job with:\n\n```ruby\nclass TestJob \u003c ApplicationJob\n  distribute_reads\n\n  def perform\n    # ...\n  end\nend\n```\n\nYou can pass any options as well.\n\n## Lazy Evaluation\n\nActive Record uses [lazy evaluation](https://www.theodinproject.com/lessons/ruby-on-rails-active-record-queries), which can delay the execution of a query to outside of a `distribute_reads` block. In this case, the primary will be used.\n\n```ruby\nusers = distribute_reads { User.where(orders_count: 1) } # not executed yet\n```\n\nCall `to_a` or `load` inside the block to ensure the query runs on a replica.\n\n```ruby\nusers = distribute_reads { User.where(orders_count: 1).to_a }\n```\n\nYou can automatically load relations returned from `distribute_reads` blocks by creating an initializer with:\n\n```ruby\nDistributeReads.eager_load = true\n```\n\n## Options\n\n### Replica Lag\n\nRaise an error when replica lag is too high (specified in seconds)\n\n```ruby\ndistribute_reads(max_lag: 3) do\n  # raises DistributeReads::TooMuchLag\nend\n```\n\nInstead of raising an error, you can also use primary\n\n```ruby\ndistribute_reads(max_lag: 3, lag_failover: true) do\n  # ...\nend\n```\n\nIf you have multiple databases, this only checks lag on `ActiveRecord::Base` connection. Specify connections to check with\n\n```ruby\ndistribute_reads(max_lag: 3, lag_on: [ApplicationRecord, LogRecord]) do\n  # ...\nend\n```\n\n**Note:** If lag on any connection exceeds the max lag and lag failover is used, *all connections* will use their primary.\n\n### Availability\n\nIf no replicas are available, primary is used. To prevent this situation from overloading the primary, you can raise an error instead.\n\n```ruby\ndistribute_reads(failover: false) do\n  # ...\nend\n```\n\n### Default Options\n\nChange the defaults for `distribute_reads` blocks\n\n```ruby\nDistributeReads.default_options = {\n  lag_failover: true,\n  failover: false\n}\n```\n\n### Logging\n\nMessages about failover are logged to the Active Record logger by default. Set a different logger with:\n\n```ruby\nDistributeReads.logger = Logger.new(STDERR)\n```\n\nOr use `nil` to disable logging.\n\n## Distribute Reads by Default\n\nAt some point, you may wish to distribute reads by default.\n\n```ruby\nDistributeReads.by_default = true\n```\n\nTo make queries go to primary, use:\n\n```ruby\ndistribute_reads(primary: true) do\n  # ...\nend\n```\n\n## Reference\n\nGet replication lag in seconds\n\n```ruby\nDistributeReads.replication_lag\n```\n\nMost of the time, ActiveRecordProxyAdapters does a great job automatically routing queries to replicas. If it incorrectly routes a query to primary, you can use:\n\n```ruby\ndistribute_reads(replica: true) do\n  # send all queries in block to replica\nend\n```\n\n## Rails\n\nRails 6+ has [native support for replicas](https://guides.rubyonrails.org/active_record_multiple_databases.html) :tada:\n\n```ruby\nActiveRecord::Base.connected_to(role: :reading) do\n  # do reads\nend\n```\n\nHowever, it’s not able to do automatic statement-based routing yet.\n\n## Thanks\n\nThanks to [Nasdaq](https://github.com/Nasdaq) for ActiveRecordProxyAdapters, [TaskRabbit](https://github.com/taskrabbit) for Makara, [Sherin Kurian](https://github.com/sherin) for the max lag option, and [Nick Elser](https://github.com/nickelser) for the write-through cache.\n\n## Upgrading\n\n### 1.0\n\nActiveRecordProxyAdapters is now used instead of Makara. Update `config/database.yml` and `app/models/application_record.rb` to [use it](#how-to-use).\n\n## History\n\nView the [changelog](https://github.com/ankane/distribute_reads/blob/master/CHANGELOG.md)\n\n## Contributing\n\nEveryone is encouraged to help improve this project. Here are a few ways you can help:\n\n- [Report bugs](https://github.com/ankane/distribute_reads/issues)\n- Fix bugs and [submit pull requests](https://github.com/ankane/distribute_reads/pulls)\n- Write, clarify, or fix documentation\n- Suggest or add new features\n\nTo get started with development and testing:\n\n```sh\ngit clone https://github.com/ankane/distribute_reads.git\ncd distribute_reads\ncreatedb distribute_reads_test_primary\ncreatedb distribute_reads_test_replica\nbundle install\nbundle exec rake test\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fankane%2Fdistribute_reads","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fankane%2Fdistribute_reads","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fankane%2Fdistribute_reads/lists"}