Getting started with Redis
What is redis
Redis (which stands for REmote DIctionary Server) is a popular, open-source, in-memory data store.
It is super-fast database because it keeps all its data in your computer's RAM (memory) instead of on a slower disk. This makes reading and writing data almost instantaneous.
Even if it is faster than postgres, it has some latest as it still needs to be writter and read from localhost.
It's a "key-value" store, meaning data is saved and retrieved using a simple key in a dictionary to find its definition (the value). It supports various data structures, including strings, lists, sets, and hashes, making it very flexible.
Common Use Cases
Because of its high speed, Redis is perfect for tasks that require real-time data access.
- Caching: This is most populaer use case. Redis stores frequently accessed data (like database query results, web pages, or user profiles) in memory. This prevents the application from having to fetch that data from a slower database every time, dramatically speeding up response times.
- Session Store: Redis is excellent for managing user session data (like login status, shopping cart contents, etc.) for web applications. Its speed ensures that user information is loaded instantly on every page.
- Real-time Analytics: It's used to process and display real-time data, such as for live leaderboards in games, website visitor counters, or tracking real-time metrics.
- Message Broker (Pub/Sub): Redis can create a "Publish/Subscribe" system. This allows different parts of an application to communicate with each other instantly, making it ideal for building chat rooms, live notification systems, or processing background jobs. This can be used instead of kafka.
- Rate Limiting: It can be used to track how many requests a user makes to an API in a given timeframe, helping to prevent abuse or overload.
How to install
Ubuntu
First update the package index and then install redis
sudo apt update sudo apt install redis-server
verify the installation
redis-server --version
Server related commands
# start the server sudo systemctl start redis-server # enable redis on boot sudo systemctl enable redis-server # check status sudo systemctl status redis-server # stop sudo systemctl stop redis-server restart sudo systemctl restart redis-server
Start the CLI to test
redis-cli # to exit the CLI exit
Mac
Install Redis
brew install redis
verify the installation without starting the server
redis-server --version
Run service as background
brew services start redis
Run manually
redis-server
Check if it is running
redis-cli ping