Is Redis just a cache?
Asked Answered
T

12

286

I can't see any difference between Redis and caching technologies like Velocity or the Enterprise Library Caching Framework. You're effectively just adding objects to an in-memory data store using a unique key. There do not seem to be any relational semantics...

What am I missing?

Temperament answered 13/4, 2012 at 8:49 Comment(2)
From redis.io: Redis is an open source, advanced key-value store. It is often referred to as a data structure server since keys can contain strings, hashes, lists, sets and sorted sets. That said, I voted to close your question as it doesn't fit StackOverflow's format.Bijouterie
I agree its not SO format. Where do you think it would be more appropriate?Temperament
P
679

No, Redis is much more than a cache.

Like a cache, Redis stores key-value pairs. But unlike a cache, Redis lets you operate on the values. There are 5 data types in Redis - Strings, Sets, Hashs, Lists and Sorted Sets. Each data type exposes various operations.

The best way to understand Redis is to model an application without thinking about how you are going to store it in a database.

Lets say we want to build StackOverflow.com. To keep it simple, we need Questions, Answers, Tags and Users.

Modeling Questions, Users and Answers

Each object can be modeled as a Map. For example, a Question is a map with fields {id, title, date_asked, votes, asked_by, status}. Similarly, an Answer is a map with fields {id, question_id, answer_text, answered_by, votes, status}. Similarly, we can model a user object.

Each of these objects can be directly stored in Redis as a Hash. To generate unique ids, you can use the atomic increment command. Something like this:

$ HINCRBY unique_ids question 1
(integer) 1
$ HMSET question:1 title "Is Redis just a cache?" asked_by 12 votes 0
OK

$ HINCRBY unique_ids answer 1
(integer) 1
$ HMSET answer:1 question_id 1 answer_text "No, its a lot more" answered_by 15 votes 1
OK

Handling Up Votes

Now, every time someone upvotes a question or an answer, you just need to do this:

$ HINCRBY question:1 votes 1
(integer) 1
$ HINCRBY question:1 votes 1
(integer) 2

List of Questions for Homepage

Next, we want to store the most recent questions to display on the home page. If you were writing a .NET or a Java program, you would store the questions in a List. Turns out, that is the best way to store this in Redis as well.

Every time someone asks a question, we add its id to the list:

$ lpush questions question:1
(integer) 1
$ lpush questions question:2
(integer) 1

Now, when you want to render your homepage, you ask Redis for the most recent 25 questions:

$ lrange questions 0 24
1) "question:100"
2) "question:99"
3) "question:98"
4) "question:97"
5) "question:96"
...
25) "question:76"

Now that you have the ids, retrieve items from Redis using pipelining and show them to the user.

Questions by Tags, Sorted by Votes

Next, we want to retrieve questions for each tag. But SO allows you to see top voted questions, new questions or unanswered questions under each tag.

To model this, we use Redis' Sorted Set feature. A Sorted Set allows you to associate a score with each element. You can then retrieve elements based on their scores.

Lets go ahead and do this for the Redis tag:

$ zadd questions_by_votes_tagged:redis 2 question:1 
(integer) 1
$ zadd questions_by_votes_tagged:redis 10 question:2 
(integer) 1
$ zadd questions_by_votes_tagged:redis 5 question:613 
(integer) 1
$ zrange questions_by_votes_tagged:redis 0 5 
1) "question:1"
2) "question:613"
3) "question:2"
$ zrevrange questions_by_votes_tagged:redis 0 5 
1) "question:2"
2) "question:613"
3) "question:1"

What did we do over here? We added questions to a sorted set, and associated a score (number of votes) to each question. Each time a question gets upvoted, we will increment its score. And when a user clicks "Questions tagged Redis, sorted by votes", we just do a zrevrange and get back the top questions.

Realtime Questions without refreshing page

And finally, a bonus feature. If you keep the questions page opened, SO will notify you when a new question is added. How can Redis help over here?

Redis has a pub-sub model. You can create channels, for example "channel_questions_tagged_redis". You then subscribe users to a particular channel. When a new question is added, you would publish a message to that channel. All users would then get the message. You will have to use a web technology like web sockets or comet to actually deliver the message to the browser, but Redis helps you with all the plumbing on the server side.

Persistence, Reliability etc.

Unlike a cache, Redis persists data on the hard disk. You can have a master-slave setup to provide better reliability. To learn more, go through Persistence and Replication topics over here.

Peptize answered 13/4, 2012 at 11:45 Comment(5)
It's also an extremely simple service bus using the PUB/SUB related commands.Slemmer
How can i retrieve question by user? Should i create a list for each user with yours questions, like questions:user:1 or should i use tags?Boyla
This is gold the biggest difference is the mindset change.Phthisis
So is it safe (NEVER lose data)? I know Redis has HA documentations, but I have heard people saying it not suitable.Symbolist
Does HMSET question:1 title "Is Redis just a cache?" asked_by 12 votes 0 OK create a key named question:1 or the : notation is built-in in Redis to distinguish between multiple instances of the same type?Loreanloredana
B
5

Not just a cache:

  • In memory key-value storage;
  • Supports multiple data types (strings, hashes, lists, sets, sorted sets, bitmaps, and hyperloglogs);
  • Provides an ability to store cache data into physical storage (if needed);
  • Supports the pub-sub model;
  • Provides replication for high availability (master/slave).
Bring answered 8/5, 2018 at 14:48 Comment(0)
U
4

Redis has unique abilities like ultra-fast lua-scripts. Its execution time equals to C commands execution. This also brings atomicity for sophisticated Redis data manipulation required for work many advanced objects like Locks and Semaphores.

There is a Redis based in memory data grid called Redisson which allows to easily build distributed application on Java. Thanks to distributed Lock, Semaphore, ReadWriteLock, CountDownLatch, ConcurrentMap objects and many others.

Perfectly works in cloud and supports AWS Elasticache, AWS Elasticache Cluster and Azure Redis Cache support

Uund answered 28/9, 2016 at 10:26 Comment(0)
I
1

Actually there is no dependency between relative data representation (or any type of data representation) and database role (cache, permanent persistence etc).

Redis is good for cache it's true, but it's much more then just a cache. It's high speed fully in-memory database. It does persist data on disk. It's not relational, it's key-value storage.

We use it in production. Redis helps us to build software that handles thousands of requests per second and keep customer business data during whole natural lifecycle.

Islamize answered 1/9, 2016 at 18:58 Comment(0)
D
1

Redis is a cache which best suited for distributed environment/Microservice architecture.

It is fast, reliable, provides atomicity and consistency and has range of datatypes such as sets, hashes, list etc.

I am using it from last one year and it really comes as a saviour when you to need provide a production ready solution very fast and for any performance related issues as you can always use it to cache data.

Davies answered 13/11, 2018 at 7:10 Comment(0)
M
1

Redis supports data structures such as strings, hashes, lists, sets, sorted sets with range queries, bitmaps, hyperloglogs, geospatial indexes with radius queries and streams. Redis has built-in replication, Lua scripting, LRU eviction, transactions and different levels of on-disk persistence, and provides high availability via Redis Sentinel and automatic partitioning with Redis Cluster.

implementaion with python

https://beyondexperiment.com/vijayravichandran06/redis-data-structure-with-python/

Maurreen answered 21/6, 2020 at 4:45 Comment(0)
C
1

Usages of Redis:

  1. Cache with multiple data structures, like: string, set, zset, list, hash and bitmap (which could be used in many aggregation use cases)
  2. KV DB. Data in Reids memory can be stored on disk: RDB and AOF can get the snapshots and edit logs.
  3. Message Queue. But one message can only be consumed by one consumer
  4. Pubsub
  5. Distributed lock. Rely on the setnx command, and only the first thread executing it successfully will hold the lock. https://redis.io/commands/setnx
Chesnut answered 5/5, 2021 at 21:1 Comment(0)
R
1
  • it is not just key-value cache, it is key-dataStructure cache.

  • Redis is not only cache, but also a data store. whatever is written to the cache is also written to the disk. that allows us to take backups. this allows us to restart our cache nodes. If we restart them, our cache nodes will be prepopulated with the backup. we can restart the entire cluster. But in Memcached, when a Memcached node fails or restarts, all keys stored on that node are lost

  • redis is also used as a message-queue

Rancell answered 2/10, 2022 at 14:55 Comment(0)
M
1

As an addition, Redis has capabilities beside caching purpose. Based on latest Redis Documentation (https://redis.io/docs/modules/), Redis has some external modules that support different kind of tasks such as:

Personally, I used Redis for message queue by utilize Celery for Django REST Framework application beside caching at production.

Mezzotint answered 30/10, 2022 at 6:9 Comment(0)
F
1
  1. Its key value datastore ,mainly deployed in private subnet main in conjunction with cloud databases to provide micro second latency. Its able to provide that with either lazy loading or write through strategy ,based on specific use-case.

  2. It way more complex than memcached & operates in cluster -enabled/disabled mode.

  3. It supports shards, which makes data highly avialable ,multi- az deployment.

  4. It supports encryption of data @ rest & in transit

& is extremely useful for use-cases such as streaming application ,messaging ,real time analytics ..& applications where data's value depreciates at a very fast pace w.r.t time...

Hence its not just cache ,it brings allot many more features with it ,which makes it all the more useful

Fatherly answered 22/2, 2023 at 12:58 Comment(0)
K
0

Besides being a cache server, Redis is specifically a data structure server. Being a cache in the form of a data structure server means a lot, because data structures are fundamentals of programs, or applications. Consider you are using SQL databases as storage technology and need to construct a list, a hash map, a ranking set or things like that, it's kind of pain in the neck. Redis can provide you these functionalities directly in a very simple way, thus highly simplify the development.

On the other hand, a data structure server does not have to be in the form of a cache. There are projects compatible with Redis but have persistent storage engines.

Kassel answered 28/3, 2019 at 2:19 Comment(0)
B
0

In addition to so far made answer's and then to summarize

  • Redis is a very fast non-relational database that stores a mapping of keys to five different types of values (strings, hashes, lists, sets, sorted sets, bitmaps, and hyperloglogs). This is explained by details @Sripathi Krishnan answers.

  • Redis supports in-memory persistent storage on disk

  • Replication to scale read performance

  • Client-side sharding to scale write performance

If you want to get more detail and depth information about Redis, you can look at Redis In Action and Redis Essentials's books.

Benilda answered 15/2, 2021 at 11:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.