0% found this document useful (0 votes)
10 views6 pages

Redis Introduction and Installation 1695738500

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views6 pages

Redis Introduction and Installation 1695738500

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Redis Installation On Linux and It’s Basic Intro

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:

Step 1: Update System Packages


Before installing Redis, it's recommended to update the system packages to their latest versions.
Open a terminal and run the following command:

sudo apt update

Step 2: Install Redis


To install Redis, use the following command:

sudo apt install redis-server


During the installation, you may be prompted to confirm the installation and enter your password.

Step 3: Verify Redis Installation


Once the installation is complete, Redis will be automatically started as a background service. You
can verify the installation by checking the Redis service status:

sudo systemctl status redis-server


If Redis is running correctly, you should see an output indicating that the service is active and
running.

Step 4: Test Redis


To test if Redis is working properly, you can use the Redis command-line interface (CLI). Open a new
terminal window and run the following command to access the Redis CLI:

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

Step 5: Configuring Redis (Optional)


Redis can be configured to meet specific requirements. The main configuration file for Redis is
located at `/etc/redis/redis.conf`. You can open this file in a text editor to make any necessary
modifications. Details of it will be discussed later in documentation on redis-config file.

After making changes to the configuration file, you need to restart the Redis service for the new
configuration to take effect:

sudo systemctl restart redis-server

Working with Redis on Terminal:

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.

Accessing Redis CLI


To access the Redis CLI, open a terminal window and execute the following command:

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:

redis-cli -h <hostname> -p <port>


Upon successful connection, you will see the Redis CLI prompt, which indicates that you are now
interacting with Redis through the terminal.

Working with Different Data Structures

1. Strings:
- Set a key-value pair:

SET mykey "Hello, Redis!"


- Get the value of a key:

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:

LPUSH mylist "Apple"


LPUSH mylist "Banana"
LPUSH mylist "Orange"
- Retrieve all elements from 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.

- Activity Feeds: Storing recent activities or events in chronological order.

3. Sets:
- Add elements to a set:

SADD myset "Apple"


SADD myset "Banana"
SADD myset "Orange"
- Retrieve all elements from a set:
SMEMBERS myset
- To Set Expiration time i.e. Time-To-Live(TTL) of 60 seconds:

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.

- Unique User Tracking: Recording unique visitors to a website or application.

4. Sorted Sets:
- Add elements to a sorted set with scores:

ZADD myzset 1 "Apple"


ZADD myzset 2 "Banana"
ZADD myzset 3 "Orange"
- Retrieve elements from a sorted set within a specific range:

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:

HMSET myhash field1 "value1" field2 "value2" field3 "value3"


- Get the value of a specific field in a hash:

HGET myhash field2


- To Set Expiration time i.e. Time-To-Live(TTL) of 60 seconds:
EXPIRE myhash 60
- To check the remaining time-to-live for a key or field, you can use the TTL command:

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:

PFADD myhyperloglog "element1" "element2" "element3"


- Estimate the cardinality of 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.

- Probabilistic Counting: Counting unique elements with minimal memory usage.

7. Geospatial:
- Add geospatial data (latitude and longitude) to a key:

GEOADD mygeoset 13.361389 38.115556 "Palermo"


GEOADD mygeoset 15.087269 37.502669 "Catania"
- Retrieve members within a certain radius from a given point:

GEORADIUS mygeoset 15 37 200 km


- To Set Expiration time i.e. Time-To-Live(TTL) of 60 seconds:

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.

You might also like