When to use a key/value store such as Redis instead/along side of a SQL database?
Asked Answered
A

4

219

I have read great things about key/value stores such as Redis but I can't seem to figure out when it's time to use it in an application.

Say I am architecting a web-based application; I know what stack I am going to use for the front-end, back-end, database(s), etc..what are some scenarios where I would go "oh we also need Redis for X,Y, or Z."

I would appreciate node.js examples as well as non-node.js examples.

Anonym answered 23/9, 2011 at 21:43 Comment(1)
See https://mcmap.net/q/128203/-redis-as-a-database/…Ilyssa
E
128

I can't seem to figure out when it's time to use it in an application.

I would recommend you to read this tutorial which contains also use cases. Since redis is rather memory oriented it's really good for frequently updated real-time data, such as session store, state database, statistics, caching and its advanced data structures offers versatility to many other scenarios.

Redis, however, isn't NoSQL replacement for classic relational databases since it doesn't support many standard features of RDBMS world such as querying of your data which might slow it down. Replacement are rather document databases like MongoDB or CouchDB and redis is great at supplementing specific functionality where speed and support for advanced data structures comes handy.

Escapade answered 24/9, 2011 at 8:39 Comment(4)
I did a quick Google search with that tutorial site URL and came across this as a top hit - slideshare.net/dvirsky/introduction-to-redis-version-2Parmenter
It's ironic because state storage/reading/updating is the sort of thing where you need transactional security when multiple servers are going to be accessing it, and Redis doesn't give you that (it gives you the ability to auto-abort a transaction if data changes but that sucks because the transaction fails where it might have succeeded in a SQL DB context).Pippo
@Pippo I used to freak out about that too. Very much. Then I realised that we don't live in that world anymore. A distributed system needs to give up some consistency, in favour of availability. A serious product in this day and age has 0 chance of surviving w/o the completely unhampered ability to horizontally scale. en.wikipedia.org/wiki/CAP_theorem Be very careful in staying stuck in a strict ACID way of thinking. I resisted it for too long until it bit me in the ass. Yes you'll deal with some inconsistency. That's fine. en.wikipedia.org/wiki/Eventual_consistencyEssex
That being said, if you can absolutely positively guarantee that whatever it is you're building won't be a publicly accessed product with any potential whatsoever to acquire a sizeable amount of users, go for strict ACID. By all means. The right tool for the right job. Just always keep in mind that times have changed drastically.Essex
I
80

I think nothing explains better the use cases for Redis than this article: http://oldblog.antirez.com/post/take-advantage-of-redis-adding-it-to-your-stack.html

I bet you'll have an aha! moment. ;)

A quote from a previous reader:

I've read about Redis before and heard how companies are using it, but never completely understood it's purpose. After reading this I can actually say I understand Redis now and how it's useful. Amazing that after hearing so much about it all it took was a relatively simple article.

A quote from the article:

Redis is different than other database solutions in many ways: it uses memory as main storage support and disk only for persistence, the data model is pretty unique, it is single threaded and so forth. I think that another big difference is that in order to take advantage of Redis in your production environment you don't need to switch to Redis. You can just use it in order to do new things that were not possible before, or in order to fix old problems.

Use cases the article touches on:

  • Slow latest items listings in your home page
  • Leaderboards and related problems
  • Order by user votes and time
  • Implement expires on items
  • Counting stuff
  • Unique N items in a given amount of time
  • Real time analysis of what is happening, for stats, anti spam, or whatever
  • Pub/Sub
  • Queues
  • Caching
Incommunicado answered 18/11, 2011 at 23:1 Comment(7)
This article is very useful, I has what I wanted to knowMaryalice
@Zenw0lf Is redis still the best for simple caching? Your post is from 2011, so not sure if there is something else I should be using.Mayotte
@Mayotte Yes, the project is still very much alive and it's creator makes frequent releases. It is still a superb project for different usage scenarios!Incommunicado
@Incommunicado Good article but I still get a bit confused in some use case. For example the "latest item" use case, why is it better to add Redis in this case? what is in database we add in a specific table just to have user id, comment(id), and/or timestamp, and use this directly. Won't this be the same?Roustabout
@TonyLin Yes, but with Redis you skip the query to the DB because it's an in-memory store. If you are doing thousands or millions of queries of that kind is better to have those ready at hand than querying the database for them every time.Incommunicado
@Incommunicado Thanks for the reply! May I say it's the nature that it's in-memory storage makes it super fast and can be used in many scenarios to solve problems? not actually because of its data model. It's equivalent to having a dictionary in the code to store these information directly? sorry this question may sound really dumb coz I'm quite new to this.Roustabout
@TonyLin It's both, it's fast because it's in-memory and because Redis also has a pretty good implementation of cutting edge data models to help it do its work as best as it can.Incommunicado
D
12
  • I would love to use redis on the real time projects. I did recently for one gps tracking system which was previously built on mysql as a database.

    ADVANTAGE

    1. Every time the tracker broadcast data I do not need to open mysql connection and store on it. We can save it on redis and later migrate to mysql using some other process. This will avoid concurrent connection from mutiple tracker to mysql.
    2. I can publish all those gps data and other clients(javascript/android) can subscribe in a real time using message queue based on redis
    3. I can trigger real time alerts
Decal answered 21/12, 2016 at 19:52 Comment(0)
S
7

One thing off hand is that Redis isn't a relational database. If you're going to be needing an SQL "JOIN" then you won't want to use Redis, nor any other non-relational database. Redis is faster though than most relational databases. If you're only going to be doing key:value pair queries, then you'll want to use Redis.

Senghor answered 24/9, 2011 at 1:57 Comment(2)
So for example, would it be good to use redis for information regarding a user session so it is quicker to access name, email, ID, etc?Anonym
I would think so. Kyoto Cabinet would be even quicker for that, I think.Senghor

© 2022 - 2024 — McMap. All rights reserved.