{"id":19725359,"url":"https://github.com/bodacious/query-cache-demo","last_synced_at":"2025-04-29T23:30:40.814Z","repository":{"id":66871731,"uuid":"525445118","full_name":"Bodacious/query-cache-demo","owner":"Bodacious","description":"Demonstrates the cost of using ActiveRecord Query cache in a simple Rails app","archived":false,"fork":false,"pushed_at":"2022-08-22T15:00:42.000Z","size":44,"stargazers_count":6,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-04-05T20:11:12.380Z","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/Bodacious.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"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":"2022-08-16T15:46:23.000Z","updated_at":"2025-01-07T03:22:24.000Z","dependencies_parsed_at":"2023-02-27T10:46:36.541Z","dependency_job_id":null,"html_url":"https://github.com/Bodacious/query-cache-demo","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Bodacious%2Fquery-cache-demo","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Bodacious%2Fquery-cache-demo/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Bodacious%2Fquery-cache-demo/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Bodacious%2Fquery-cache-demo/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Bodacious","download_url":"https://codeload.github.com/Bodacious/query-cache-demo/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251599710,"owners_count":21615571,"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-11T23:29:29.411Z","updated_at":"2025-04-29T23:30:40.788Z","avatar_url":"https://github.com/Bodacious.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# README\n\nThis simple Rails application demonstrates the impact that ActiveRecord's query cache has on web application performance.\n\n## Setting up the app\n\nAfter you have downloaded the app, run the following commands:\n\n```\n$ bin/setup\n```\n\n## Start the application\n\nThe application can be started at localhost:3000 by running the following:\n\n```\n$ rails server\n```\n\n## Understanding this app\n\nThis simple application shows a list of recipes that belong to a user.\n\nThe application will automatically sign you in as a random user (to force the calling of a few DB requests). You may manually control which user you are signed in as, by providing a `current_user_id` query param.\n\nThere are two main pages that are relevant to this demonstration. The _cached_ page and the _memoized_ page. The cached page follows a conventional Ruby on Rails pattern, where records are loaded once from the database, and then subsequent DB queries are loaded from the ActiveRecord query cache. The _memoized_ page avoids using ActiveRecord's query cache, and instead caches records in memory using a so-called _memo-ized_ pattern.\n\n## Running the benchmarks\n\nYou can compare the performance of each approach by running a benchmark against the code:\n\n```\n$ ab -c2 -n2000 http://127.0.0.1:3000/cached/users/1/recipes\\?current_user_id\\=5\n\n# Then...\n\n$ ab -c2 -n2000 http://127.0.0.1:3000/memoized/users/1/recipes\\?current_user_id\\=5\n```\n\n## Results\n\n### Results with query-cache\n\n```\nConcurrency Level:      2\nTime taken for tests:   19.733 seconds\nComplete requests:      2000\nFailed requests:        0\nTotal transferred:      136566298 bytes\nHTML transferred:       134538000 bytes\nRequests per second:    101.36 [#/sec] (mean)\nTime per request:       19.733 [ms] (mean)\nTime per request:       9.866 [ms] (mean, across all concurrent requests)\nTransfer rate:          6758.66 [Kbytes/sec] received\n\nConnection Times (ms)\n              min  mean[+/-sd] median   max\nConnect:        0    0   0.0      0       0\nProcessing:     9   20   5.3     19      74\nWaiting:        9   18   5.1     18      74\nTotal:          9   20   5.3     19      74\n\nPercentage of the requests served within a certain time (ms)\n  50%     19\n  66%     19\n  75%     20\n  80%     21\n  90%     25\n  95%     27\n  98%     36\n  99%     45\n 100%     74 (longest request)\n```\n\n### Results with no query-cache (memoized)\n\n```\nConcurrency Level:      2\nTime taken for tests:   7.850 seconds\nComplete requests:      2000\nFailed requests:        0\nTotal transferred:      136566652 bytes\nHTML transferred:       134538000 bytes\nRequests per second:    254.79 [#/sec] (mean)\nTime per request:       7.850 [ms] (mean)\nTime per request:       3.925 [ms] (mean, across all concurrent requests)\nTransfer rate:          16990.05 [Kbytes/sec] received\n\nConnection Times (ms)\n              min  mean[+/-sd] median   max\nConnect:        0    0   0.0      0       0\nProcessing:     4    8   2.1      7      32\nWaiting:        4    7   2.1      7      32\nTotal:          4    8   2.1      7      32\n\nPercentage of the requests served within a certain time (ms)\n  50%      7\n  66%      8\n  75%      8\n  80%      8\n  90%      9\n  95%     10\n  98%     11\n  99%     16\n 100%     32 (longest request)\n```\n\n## Conclusion\n\nThis simple application demonstrates that relying on ActiveRecord's query cache to return records is not as fast as storing the records in memory (via a variable or constant). Developers should be mindful of which strategy they are using when interacting with ActiveRecord objects.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbodacious%2Fquery-cache-demo","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbodacious%2Fquery-cache-demo","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbodacious%2Fquery-cache-demo/lists"}