Just recently started reading about Redis and how it is an extremely fast key-value store. It also does a lot under the hood in that it keeps a lot in-memory for caching, but does also write to disk when needed. This is great, but when exactly should it be used. I feel like it might be overkill to use Redis everywhere, but where does it make sense?
http://jimneath.org/2011/03/24/using-redis-with-ruby-on-rails.html
The link above displayed a great example of a user model using Redis to facilitate User friendships. Now, it seems to me that if I only had 10 users, then I could write
class User < ActiveRecord::Base # follow a user def follow!(user) $redis.multi do $redis.sadd(self.redis_key(:following), user.id) $redis.sadd(user.redis_key(:followers), self.id) end end end as follows
class User < ActiveRecord::Base # follow a user def follow!(user) UserFollower.create(:user_id => self.id, :following => user.id) #UserFollower is join-table end end I imagine that if I have a few users there is virtually no difference. I'm assuming if I had 1,000,000 I might start to notice the difference. I don't know exactly how Redis knows when to persist this data, but are the benefits essentially its cache? So I don't have to query the DB anymore and could just have Redis fetch it for me because it is in-memory?
If user 1 follows someone and it gets stored in Redis, after which their session with the server is finished until their next request, does that information stay in Redis so that if another user, say user 2, does something that gets this info, will it be a query to the DB or would it be from Redis?
I could definitely see the plus side of this as a key-value store that is caching and being used among multiple users at once through their various requests and also to reduce queries in general, but is that it? I'm not exactly sure how Redis works, but from what I've read that is the impression I got.