An open API service indexing awesome lists of open source software.

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

Awesome Lists containing this project

README

          

# atomic_cache Gem
[![Gem Version](https://badge.fury.io/rb/atomic_cache.svg)](https://badge.fury.io/rb/atomic_cache)
[![Test Coverage](https://api.codeclimate.com/v1/badges/790faad5866d2a00ca6c/test_coverage)](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