1

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.

1 Answer 1

1

From what I understand from your question is that you are looking for pros of Redis in a multi-client env.

Some info about Redis which might help you make a decision.

Redis is a key-value store,stores string values against keys.

DATASTRUCTURES There are different storage options:

  1. Key- value
  2. Hashes
  3. list
  4. sets

REDIS documentation provides good examples for them.

Complex structures can also be stored with serialization and shared among different language implementations.

SINGLE THREADED Redis server is single thread in design and performs atomic transactions. Single threaded nature ensures serialized access to shared data. And in no way this nature of Redis restricts multiple clients from connecting the server. Performance stats shared in Redis docs clearly talk about this.

EVAL Redis also provides an option of performing multiple commands using LUA scripts run by EVAL command and a similar option using MULTI/EXE commands. Good feature if application is concerned about number of network calls. Similar to a stored procedure in relational database. Scripts exist in memory and cached only once.

PUB/SUB applications could be created to have OBSERVER design pattern. Using PUB/SUB command channels could be created with a PUBLISHER publishing messages on the channel and received by 'n' number of SUBSCRIBERS.

PERSISTENCE Redis provides support for configurable disk backups and with different levels and frequency.

AVAILABILITY Master-slave replication, with read operations permitted on SLAVE.

Redis docs have very good literature about each and every aspects of Redis (powers and limitations both covered).

You would find difficulties when trying to visualize data in relational sense. Data storage has to be planned or impact analysis of how your application intends to read and write data should be the deciding factor.

You might want to explore all the features of Redis and then decide if this is the right NoSQL for your or you would like to try NoSQL types(Document , column or graph).

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.