Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB vs Redis for user sessions?

I need to load a few fields of information that changes infrequently for validation of routes that a user might access.

Currently, I query mongodb and store those fields in redis alongside any particular state information in a single hash that is keyed by 'user:' + mongodb_user_objectid when a user logs in.

Then I create a session for the http cookie and store this key as a string in redis as well keyed by 'sess:' + session_id.

Would it be better to not copy over the fields from mongodb and deal with updating both redis and mongodb when one of those validation fields may change?

Is there a significant performance difference from reading and writing this session information directly from/to mongodb without using redis as a middleman?

like image 464
paulkon Avatar asked Apr 06 '14 00:04

paulkon


People also ask

Should I use Redis or MongoDB?

Redis handles large volumes of workload more comfortably as compared to MongoDB. Redis is single-threaded which means it runs on a single core. So, in terms of performance, Redis is slightly better than MongoDB. MongoDB is also known to respond slowly once it is bound by the CPU.

Is Redis good for session store?

Redis Enterprise is a popular database ideal for both cache and session store use cases, delivering both the high-availability required for caching and session store scenarios as well as the durability needed for session store with in-memory replication.

Why MongoDB is better than Redis?

MongoDB is schemaless, which means that the database does not have a fixed data structure. This means that as the data stored in the database gets larger and larger, MongoDB is able to operate much faster than Redis. Redis is only significantly faster when the stored data is relatively small in size.

Can Redis replace MongoDB?

Redis is often used in combination with an on-disk database such as MongoDB. While the on-disk database is the primary storage solution, Redis may be used as a caching layer, or for performing real-time analytics. If you're already using MongoDB, you can achieve similar results without adding Redis to your system.


1 Answers

Unfortunately, a definite answer will require a measurement using your setup because there are so many potential factors.

However, my guess is that the overhead of using two datastores outweighs any potential advantages because the reads should be incredibly fast on both DBs:

  • Since the sessions will be used often, the collection keeps pretty 'hot' so it will probably remain in RAM if any possible in MongoDB, too
  • Losing a session wouldn't be great, but it's not a disaster, so you can write to MongoDB without waiting for a journal commit (which is pretty much the same reliability redis has)
  • In either case, most time is (probably) spent on the network stack, and you have to go through that for both DBs

So, in a nutshell, I don't see any reasons why redis would be a lot faster in this case, but again, performance is often guesswork, especially when the details are unknown.

like image 55
mnemosyn Avatar answered Sep 24 '22 02:09

mnemosyn