Redis Introduction and Installation 1695738500
Redis Introduction and Installation 1695738500
Prerequisites:
1. You need to have Linux(Ubuntu) installed on your system.
Redis Brief:
Redis is an open source (BSD licensed), in-memory data structure store used as a
database, cache, message broker, and streaming engine. Redis provides data structures
such as strings, hashes, lists, sets, sorted sets with range queries, bitmaps, hyperloglogs,
geospatial indexes, 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.
Installation Steps:
redis-cli
Once in the Redis CLI, you can try basic commands such as `ping` to check if Redis is responsive.
For e.g. :-
redis-cli ping
should return
PONG
After making changes to the configuration file, you need to restart the Redis service for the new
configuration to take effect:
Redis provides a command-line interface (CLI) that allows you to interact with Redis directly through
the terminal. This section will guide you on how to work with Redis on the terminal using the Redis
CLI.
Prerequisites
Before getting started, ensure that you have Redis installed on your system. You can refer to the
Redis installation documentation for the specific instructions based on your operating system.
redis-cli
This command connects to the Redis server running on the default host (localhost) and port (6379). If
your Redis server is running on a different host or port, you can specify them as arguments to the
`redis-cli` command:
1. Strings:
- Set a key-value pair:
GET mykey
- To Set Expiration time i.e. Time-To-Live(TTL) of 60 seconds:
EXPIRE mykey 60
- To check the remaining time-to-live for a key or field, you can use the TTL command:
TTL mykey
Practical Use:
- Caching: Storing frequently accessed data to improve application performance.
- Session Management: Storing and retrieving session data for user authentication and session
2. Lists:
- Push elements to the left side of a list:
LRANGE mylist 0 -1
- To Set Expiration time i.e. Time-To-Live(TTL) of 60 seconds:
EXPIRE mylist 60
- To check the remaining time-to-live for a key or field, you can use the TTL command:
TTL mylist
Practical Use:
- Task Queues: Managing job queues and task prioritization.
3. Sets:
- Add elements to a set:
EXPIRE myset 60
- To check the remaining time-to-live for a key or field, you can use the TTL command:
TTL myset
Practical Use:
- Social Network Followers: Tracking and managing user followers or friends.
4. Sorted Sets:
- Add elements to a sorted set with scores:
ZRANGE myzset 0 -1
- To Set Expiration time i.e. Time-To-Live(TTL) of 60 seconds:
EXPIRE myzset 60
- To check the remaining time-to-live for a key or field, you can use the TTL command:
TTL myzset
Practical Use:
- Leaderboards: Tracking scores or rankings in games or competitions.
- Real-time Analytics: Storing and analyzing time-series data, such as website traffic or stock prices.
5. Hashes:
- Set multiple fields and values in a hash:
TTL myhash
Practical Use:
- User Profiles: Storing and retrieving user profile information, such as name, age, and preferences.
- Caching Complex Objects: Storing and retrieving structured data, such as serialized objects or
JSON.
6. HyperLogLog:
- Add elements to a HyperLogLog structure:
PFCOUNT myhyperloglog
- To Set Expiration time i.e. Time-To-Live(TTL) of 60 seconds:
EXPIRE myhyperloglog 60
- To check the remaining time-to-live for a key or field, you can use the TTL command:
TTL myhyperloglog
Practical Use:
- Unique User Counting: Estimating the number of unique visitors or distinct items in large datasets.
7. Geospatial:
- Add geospatial data (latitude and longitude) to a key:
EXPIRE mygeoset 60
- To check the remaining time-to-live for a key or field, you can use the TTL command:
TTL mygeoset
Practical Use:
- Location-Based Services: Storing and querying geospatial data for proximity searches, such as
finding nearby stores or restaurants.
- Fleet Management: Tracking and managing the real-time locations of vehicles or assets.
References:
1. Redis Official Documentaion has been utilized to make this document. You can refer to it for
in-depth coverage of Redis Data Structures.
If you want to explore Redis in Spring Boot, we have a documentation for the same. Refer here.