https://github.com/ibotta/atomic_cache
a gem which prevents the thundering herd problem
https://github.com/ibotta/atomic_cache
atomic-cache cache rubygem thundering-herd
Last synced: about 1 year ago
JSON representation
a gem which prevents the thundering herd problem
- Host: GitHub
- URL: https://github.com/ibotta/atomic_cache
- Owner: Ibotta
- License: apache-2.0
- Created: 2018-01-29T17:42:52.000Z (over 8 years ago)
- Default Branch: main
- Last Pushed: 2023-05-05T16:00:39.000Z (about 3 years ago)
- Last Synced: 2025-03-21T12:06:28.971Z (about 1 year ago)
- Topics: atomic-cache, cache, rubygem, thundering-herd
- Language: Ruby
- Homepage:
- Size: 208 KB
- Stars: 8
- Watchers: 90
- Forks: 6
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# atomic_cache Gem
[](https://badge.fury.io/rb/atomic_cache)
[](https://codeclimate.com/github/Ibotta/atomic_cache/test_coverage)
## User Documentation
* [Installation & Project Setup](docs/PROJECT_SETUP.md)
* [Model Setup](docs/MODEL_SETUP.md)
* [Usage & Testing](docs/USAGE.md)
* [Custom Clients/Adapters](docs/INTERFACES.md)
#### atomic_cache is a gem which prevents the [thundering herd problem](https://en.wikipedia.org/wiki/Thundering_herd_problem)
In a nutshell:
* The key of every cached value includes a timestamp
* Once a cache key is written to, it is never written over
* When a newer version of a cached value is available, it's written to a new key (e.g. same key with a newer timestamp)
* When a new value is being generated for a new key only 1 process is allowed to do so at a time
* While the new value is being generated, other processes read one key older than most recent (last known value)
* If the last known value isn't available other processes wait for the new value, polling periodically for it
#### Quick Reference
```ruby
class Foo < ActiveRecord::Base
include AtomicCache::GlobalLMTCacheConcern
force_cache_class(:custom_foo) # optional
cache_version(5) # optional
def active_foos(ids)
keyspace = cache_keyspace(:activeids, ids)
atomic_cache.fetch(keyspace, expires_in: 5.minutes) do
Foo.active.where(id: ids.uniq)
end
# value stored at 'company:custom_foo:activeids::1270643035.04671'
# last mod time stored at 'company:custom_foo:lmt'
end
end
```
For further details and examples see [Usage & Testing](docs/USAGE.md)
## Development
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
## Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/ibotta/atomic_cache
## Releasing
Releases are automatically handled via the Travis CI build. When a version greater than
the version published on rubygems.org is pushed to the `main` branch, Travis will:
- re-generate the CHANGELOG file
- tag the release with GitHub
- release to rubygems.org