If you have a cluster of machines operating on a dataset, you can store that dataset in redis to get high performance reads and writes. In the simple case of a cache, it's a key value store. But other complex cases exist: A priority queue, an atomic transaction log, a lock server, and more.
It supports lua so if the data structure and operations you need doesn't exist you can generally build it yourself.
The one place I've run into it is in web development where it's used for caching? In some tutorials I've read.
I'm going to see if that resource is there by doing `GET external.foo.bar`. if there is nothing here, I perform the slow pull. After the slow pull is done, we store the result in redis under the same name `external.foo.bar` with a timeout for x seconds.
Next time that resource is requested from our code, it will be there, so `GET external.foo.bar` will get us that resource without having to perform a slow call to external service. "
Also, where does Redis sit (my guess is between request & database)?
That said, caching is just one of the possible uses for Redis. I think of it as an easy way to share arrays, dictionaries, queues, etc between different applications. Then it's easy to see how it can be used for almost anything.
Don't jump to use it unless you really have performance/scaling issues.
Redis gives you amazing speed and because it provides a kv interface, the work is very easy.
And where the usefulness of K:V store is beaten by SQL is the point you would want to run a "JOIN" or "GROUP" on the data, for example if you wanted to count the number of keys containing a certain data point, correct?
Yes, the usefulness of SQL always is the join or group but with something like session, the idea is to just use it to dump values into the store which you don't want to reach into the database every time. So different teams will want to put in their own keys and data into the session object which means your DB session store becomes extremely difficult to maintain over time.
On the other hand, the joins and groups based on this can be handled later in time than in the req-response cycle itself.
A simple non-caching example, but we use it for distributed locking, more specifically a distributed countdown latch. This is much cleaner and more performant (for us) than doing a similar operation in a traditional RDBMS.
Another very common example that is used in a lot of tutorials is maintaining a leaderboard using a sorted set.
In most of these scenarios, you are still persisting data to stable storage. So, you would take the performance hit and load the data from the database.
This only scratches the surface of what is possible, but it's some things redis is used for.
You can use it as a simple cache, and that's fine. But then why not just stick with memcached -- less is more?
There are probably some scenarios where single point of failure and data loss is fine and the additional data structures redis provides over memcached are handy (e.g. analytics), but I've never seen it used for that.
I'm not an applications programmer (and don't want to be one), so take what I say with a grain of salt, but I was first introduced to Redis several years ago during an analytical project working with a consultancy, and I asked what it was and why they wanted to bring it in.
"Its a memory-resident key-value store!"...
"So its...a hash table?"
"Its a memory resident key-value store!"...
"And don't modern languages already come with those and why aren't we just using those internal and mature solutions rather than bringing in an arbitrary new external dependency?"
blank look
"Its a memory-resident key-value store!"
The answer is, yes, exactly!. Only, it's a hash table that can be shared across all of the different processes on the server. So if you have e.g. two different web requests that want to update some value, then that's how you do that. The other main alternative is a regular database, but that's much heavier and isn't really built in terms of "data structures".
(Redis isn't just a hash table - it's a list, a set, a queue, etc, in other words, all the standard library of a programming language, only in a way that can be shared across all the processes.)
My coworker once called redis "NoSQLite", which I think is a very apt description.
[0] https://www.pearson.com/us/higher-education/program/Ousterho...
[1] https://tcl.apache.org/rivet/
[2] http://antirez.com/articoli/tclmisunderstood.html
[3] http://jim.tcl.tk/index.html/doc/www/www/index.html
[4] https://en.m.wikipedia.org/wiki/D._Richard_Hipp
[5] https://www.tcl.tk/community/tcl2017/assets/talk93/Paper.htm...
---
Also see: https://www.dbms2.com/2008/02/18/mike-stonebraker-calls-for-...
It's single-threaded, stores everything in RAM with optional persistence, and has Lua and modules so you can do more than the standard commands.
Great for caching, or anything distributed systems need quick access to.
So, no, they are not interchangeable! Only if you just store keys and strings and do not care about persistence.
k/v store is just 10% of what it can do. Your value doesn't need to be a value, it can be:
- an array or a set
- an associative map
- an ordered list by score
- A bit array
Also offers functionality like streams and pub/sub
need an atomic lock shared between multiple processes or servers (x)
need a set of unique values sorted by insertion time (x)
want to know the O(x) complexity of using any of the data-structures to design a system for scale (x)
need to notify multiple consumers of a modification ala pub-sub (x)
need to keep track of a stream of events for consumers (x)
need to do geo-spatial lookups (x)
oh and you want this thing to be durable to failure and easy to maintain (sentinel|cluster) (x)
redis is single process and easy to configure and understand that's my opinion of why it's so amazeballs.
[edit] unicode not supported so x instead
You can store basically any common data structure in Redis and operate on individual elements as if they were local variables in your program.