0
0
Redisquery~5 mins

Redis as cache vs data store vs message broker

Choose your learning style9 modes available
Introduction
Redis can be used in different ways to make apps faster, store data, or help parts of an app talk to each other.
When you want to quickly save and get data to make your app faster (cache).
When you need a simple place to keep data that your app uses often (data store).
When different parts of your app need to send messages or alerts to each other (message broker).
Syntax
Redis
SET key value
GET key
PUBLISH channel message
SUBSCRIBE channel
SET and GET are used to save and get data in Redis.
PUBLISH and SUBSCRIBE are used for sending and receiving messages.
Examples
Save the name John under user:1 and get it back.
Redis
SET user:1 "John"
GET user:1
Send a message to the news channel and listen for messages on it.
Redis
PUBLISH news "Hello subscribers!"
SUBSCRIBE news
Sample Program
This example shows Redis used as a cache by counting page views, as a data store by saving the count, and as a message broker by sending and receiving alerts.
Redis
SET page:view:123 1
GET page:view:123
INCR page:view:123
GET page:view:123

PUBLISH alerts "New user signed up"

SUBSCRIBE alerts
OutputSuccess
Important Notes
As a cache, Redis stores data temporarily to speed up access.
As a data store, Redis keeps data that apps need to use often.
As a message broker, Redis helps parts of an app communicate by sending messages.
Summary
Redis can be used in three main ways: cache, data store, and message broker.
Cache makes apps faster by saving data temporarily.
Data store keeps data for apps to use.
Message broker helps parts of apps talk by sending messages.