A few days ago I implemented a Cache mechanism in ruby, initially I was about to use Redis, it allows you to create keys with values and also set an expiration time.
The caveat is that by using Redis this tiny app was depending in another 3rd party software to run and I didn’t wanted to end up depending in anything else for this particular case. By the way, I did thought about memcached but then again I didn’t wanted extra dependencies.
The purpose of this caching mechanism is to prevent calling the database every time we need to count how many records are in different tables. Instead it will read the value from the cache until that value expires.
So a few minutes later I had a tested working solution:
Note: You can find the spec file in this gist.
There was just a problem, this will run in a Threaded Web Server, and guess what? This code is not thread-safe.
There’s a few things we need to know:
- Ruby core classes are not thread-safe
- It will work as intended under MRI because of GIL (Global Interpreter Lock)
- It will not work as intended under Rubinius or Jruby
### How can we solve this?
We can use a Mutex, which is a shorthand for “mutual exclusion”, it implements a simple semaphore to coordinate access to shared data from multiple concurrent threads.
Now you could argue that I should also lock while reading, but in reality for the purpose of the code it really doesn’t matter, the only thing it may happen is to have an earlier count and we are a few numbers behind than we were suppose to.
How can you make sure it is thread safe?
You can spawn a few threads and increase the value of the key, so if we create 400 threads each adding 10 times you should expect the last value to be 4000.
We expect the value to be always 4000 when running this code.
I hope you enjoyed