0% found this document useful (0 votes)
20 views18 pages

No SQL Pr-8

The document discusses Redis, a key-value database that can be used as a cache, database, or message broker. It provides an overview of Redis architecture, data structures like strings, hashes, lists, sets and sorted sets. It also discusses use cases like caching, real-time analytics, messaging queues and advanced features like persistence, replication and scripting.

Uploaded by

VIPER VALORANT
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)
20 views18 pages

No SQL Pr-8

The document discusses Redis, a key-value database that can be used as a cache, database, or message broker. It provides an overview of Redis architecture, data structures like strings, hashes, lists, sets and sorted sets. It also discusses use cases like caching, real-time analytics, messaging queues and advanced features like persistence, replication and scripting.

Uploaded by

VIPER VALORANT
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/ 18

Practical-8

Aim: - Redis Basics: Introduction and Key-Value Operations


Overview of Redis-
Redis is a NoSQL database which follows the principle of key-value store. The key-
value store provides ability to store some data called a value, inside a key. You can
recieve this data later only if you know the exact key used to store it.

Redis, an open-source (BSD licensed) in-memory data structure store, serves as a


versatile solution functioning as a database, cache, and message broker. Classified as
a NoSQL database, Redis offers the flexibility to store substantial volumes of data
without the constraints typically associated with relational databases.

Distinguishing itself through support for an array of data structures, Redis


accommodates strings, hashes, lists, sets, sorted sets, bitmaps, hyperloglogs, and
geospatial indexes featuring radius queries. This diverse set of capabilities positions
Redis as a powerful tool for managing varied data types and enables its application
across a wide spectrum of use cases.

Redis Architecture-
Redis follows a client-server architecture and is known for its simplicity and
efficiency. The key components in Redis architecture are the Redis client and the Redis
server.

Redis Server : responsible for


storing data and serving to the
client

Redis Console Client Send & receive Data

Datastore
1. Redis Client:

- The Redis client is any application or program that interacts with the Redis server to
perform read and write operations.

- Clients can be written in various programming languages, and Redis provides official
client libraries for several languages, including Python, Java, Ruby, and more.

- Clients communicate with the Redis server using the Redis protocol, a lightweight
binary protocol optimized for performance.

2. Redis Server:

- The Redis server is the core component responsible for storing and managing data
in-memory.

- It operates as a daemon process, running independently on a server, and listens for


client connections on a specified port (default is 6379).

- The server manages various data structures, processes commands, and responds
to client requests.

- Redis can be configured to run in different modes, such as as a standalone instance,


as a master in a replication setup, or as a node in a clustered environment.

Key Characteristics of Redis Architecture:

- Redis stores all data in RAM, providing fast read and write operations. This makes it
ideal for use cases where low-latency access to data is crucial.

- Redis is single-threaded, meaning it processes one command at a time. While this


may seem like a limitation, it simplifies the design and allows for better predictability and
consistency.

- While Redis is an in-memory database, it provides options for persistence. Data can
be periodically saved to disk or written to an append-only file, ensuring data durability.

- Redis supports master-slave replication, allowing data to be replicated across


multiple nodes. This enhances data availability and provides fault tolerance.

- Redis can be horizontally scaled through partitioning, enabling large datasets to be


distributed across multiple nodes.

- Redis processes commands atomically, ensuring that operations either succeed


completely or fail, maintaining data consistency.
Redis data structures-
Redis supports a variety of data structures, each designed to serve specific use cases.
Here's an overview of the main data structures supported by Redis:

1. Strings:

- Strings are the simplest and most basic data type in Redis. They can contain any
kind of data, such as text, binary data, or serialized objects.

- Redis provides various operations on strings, including set, get, append, increment,
decrement, and more.

2. Hashes:

- Hashes are maps between string field names and string values, so they are useful
for representing objects with multiple attributes.

- Hashes in Redis are particularly suitable for storing and retrieving objects with a
small number of fields.

3. Lists:

- Lists are collections of ordered elements, where each element can be any data type.

- Redis provides powerful operations on lists, such as push, pop, index-based access,
range retrieval, and blocking pop operations.

4. Sets:

- Sets are collections of unique elements with no specific order.

- Set operations include adding, removing, and checking for the existence of
members. Redis also supports set operations like union, intersection, and difference
between sets.

5. Sorted Sets:

- Sorted sets are similar to sets, but each element in a sorted set is associated with a
score.

- Elements in a sorted set are kept sorted based on their scores, allowing for efficient
range queries and ranking of elements.

Redis features efficient Bitmaps for binary flag representation, HyperLogLogs for
estimating unique element cardinality with minimal memory usage, and Geospatial
Indexes for location-based storage and retrieval. These data structures, coupled with
Redis's in-memory storage and atomic operations, make it a powerful tool for
applications like caching, real-time analytics, and messaging systems. Understanding
the distinct characteristics of each data structure is vital for maximizing Redis's
effectiveness in various scenarios.

Use cases for Redis-


1. Caching:

- Redis is widely used for caching frequently accessed data, reducing the load on
backend databases and improving overall application performance.

- Example: Large-scale e-commerce websites often use Redis to cache product


details, reducing the load on the primary database and providing users with faster
access to frequently viewed items.

2. Real-Time Analytics:

- Redis facilitates real-time analytics by providing low-latency access to data, making


it suitable for monitoring and analyzing dynamic information.

- Example: Online gaming platforms utilize Redis for real-time analytics to monitor
player activities, track in-game events, and provide instantaneous feedback on player
performance.

3. Message Queues:

- Redis's publish/subscribe mechanism makes it a popular choice for building scalable


message queues and real-time communication systems.

- Example: Popular messaging platforms leverage Redis as a message queue to


ensure seamless and real-time communication between users, allowing for instant
message delivery.

4. Session Storage:

- In web applications, Redis serves as an efficient session store, managing user


session data with quick read and write operations.

- Example: Social media platforms use Redis as a session store to manage user
sessions, ensuring a smooth and responsive experience as users navigate through
various features and pages.

5. Leader boards and Counting:

- Redis's sorted sets are utilized for implementing leaderboards and efficiently
managing scores associated with various entities.
- Example: Gaming applications implement Redis sorted sets to create dynamic
leader boards that showcase top scores in real-time, promoting competition and
engagement among players.

6. Geospatial Applications:

- Redis supports geospatial data types, making it valuable for location-based services,
such as storing and querying items based on geographic coordinates.

- Example: Ride-sharing apps use Redis for geospatial indexing to efficiently locate
and match drivers with passengers based on their real-time geographic coordinates,
optimizing service delivery.

These examples illustrate how Redis is applied in practical scenarios across different
industries, emphasizing its widespread adoption for improving performance, scalability,
and real-time capabilities.

Advanced features of Redis-


Redis offers several advanced features that contribute to its versatility and wide range
of use cases. Here are some notable advanced features of Redis:

1. Persistence:

- Redis supports different mechanisms for persistence, allowing data to be saved to


disk. This ensures that data is not lost when Redis is restarted. Options include RDB
snapshots and AOF (Append-Only File) logs.

2. Replication:

- Redis supports master-slave replication, allowing data to be asynchronously


replicated from one Redis server (master) to one or more Redis servers (slaves). This
provides data redundancy, high availability, and scalability.

3. Partitioning:

- Redis allows horizontal scaling through partitioning, where data is distributed across
multiple Redis instances. This helps handle large datasets and improves performance
by leveraging the capabilities of multiple servers.

4. Lua Scripting:

- Redis supports Lua scripting, allowing users to write custom scripts that can be
executed on the server. This feature enables complex operations and transactions to be
executed atomically on the server side.
5. Transactions:

- Redis supports transactions using the MULTI, EXEC, and DISCARD commands.
Multiple commands can be grouped together in a transaction, ensuring they are
executed atomically. This helps maintain data consistency.

6. Keyspace Notifications:

- Redis provides the ability to subscribe to notifications for specific keyspace events.
Clients can be notified when certain events, such as key expirations or modifications,
occur in the Redis dataset.

7. Bitmap Operations:

- Redis supports advanced bitmap operations, allowing efficient manipulation of sets


of bits. This is useful for scenarios such as tracking user behavior, handling flags, and
implementing efficient data structures.

8. HyperLogLogs:

- HyperLogLogs provide approximate cardinality estimation for sets of unique


elements with minimal memory usage. This feature is useful for counting distinct items
in large datasets with reduced memory requirements.

9. Geospatial Indexing:

- Redis supports geospatial data types and provides commands for storing, querying,
and manipulating data based on geographic location. This is valuable for location-based
services and applications.

10. Cluster Mode:

- Redis Cluster is a distributed implementation of Redis that provides high availability


and horizontal scaling. It divides the dataset into multiple partitions across nodes,
ensuring data is distributed and replicated for fault tolerance.

11. Security Features:

- Redis supports authentication through passwords and provides access control


mechanisms to restrict client access based on IP addresses. This helps enhance the
security of Redis deployments.

These advanced features contribute to Redis's appeal in various use cases, including
real-time analytics, caching, messaging systems, and more. Understanding and
leveraging these features allows developers and system administrators to optimize
Redis for specific requirements and achieve better performance and reliability.
Install Redis on Ubuntu
To install Redis on Ubuntu, go to the terminal and type the following commands −

$sudo apt-get update


$sudo apt-get install redis-server
This will install Redis on your machine.

Start Redis

$redis-server
Check If Redis is Working

$redis-cli
This will open a redis prompt.

redis 127.0.0.1:6379>
In the above prompt, 127.0.0.1 is your machine's IP address and 6379 is the port on
which Redis server is running. Now type the following PING command.

redis 127.0.0.1:6379> ping


PONG
This shows that Redis is successfully installed on your machine.

Redis - Configuration

In Redis, there is a configuration file (redis.conf) available at the root directory of Redis.
Although you can get and set all Redis configurations by Redis CONFIG command.

Syntax

Following is the basic syntax of Redis CONFIG command.

redis 127.0.0.1:6379> CONFIG GET CONFIG_SETTING_NAME


Example

redis 127.0.0.1:6379> CONFIG GET loglevel


1) "loglevel"
2) "notice"
To get all configuration settings, use * in place of CONFIG_SETTING_NAME

redis 127.0.0.1:6379> CONFIG GET *


Edit Configuration
To update configuration, you can edit redis.conf file directly or you can update
configurations via CONFIG set command.

redis 127.0.0.1:6379> CONFIG SET CONFIG_SETTING_NAME


NEW_CONFIG_VALUE
Example

redis 127.0.0.1:6379> CONFIG SET loglevel "notice"

Basic commands and operations

Redis commands are used to perform some operations on Redis server.

To run commands on Redis server, you need a Redis client. Redis client is available in
Redis package, which we have installed earlier.

Redis provides a rich set of commands and operations to interact with its various data
structures. Here are some basic Redis commands and operations:

1. Key-Value Operations:

Indeed, these key-value operations are fundamental to working with Redis. Let's delve
into these commands:

1. SET key value:

- This command sets the value associated with a given key. If the key already exists, it
updates the existing value; otherwise, it creates a new key-value pair.

SET mykey "Hello, Redis!"


2. GET key:

- Used to retrieve the value associated with a specific key. It is a fundamental read
operation in Redis.

GET mykey
This would return `"Hello, Redis!"` if the previous SET command was executed.

3. DEL key:

- Deletes a key and its associated value from the Redis database.

DEL mykey
After executing this command, the key "mykey" and its value will be removed from the
database.

These key-value operations form the basis for many Redis use cases, allowing efficient
storage and retrieval of data. They are commonly used for caching, session
management, and various other scenarios where quick and direct access to data is
essential.

2. String Operations:

Certainly, let's delve into these string operations in Redis:

1. APPEND key value:

- This command appends the specified value to the existing value of a key. If the key
does not exist, a new key is created with the provided value.

SET mystring "Hello"


APPEND mystring ", Redis!"
After executing these commands, the value of the key "mystring" would be "Hello,
Redis!".

2. STRLEN key:

- Retrieves the length of the string value stored at the specified key.

STRLEN mystring
This would return the length of the string in the key "mystring".

3. INCR key / DECR key:

- INCR increments the integer value stored at the specified key by 1, while DECR
decrements it by 1.If the key does not exist, a new key is created with the value set to 1.

SET mycounter 10
INCR mycounter
After these commands, the value of "mycounter" would be 11.

DECR mycounter
Subsequently, the value of "mycounter" would be 10 again.

These string operations are useful for manipulating and analyzing text-based data as
well as managing counters or numerical values in Redis.
3. Hash Operations:

Certainly, let's explore these hash operations in Redis:

1. HSET key field value:

- Sets the value of a field within a hash. If the hash does not exist, a new hash is
created with the specified key.

HSET user:1001 username "john_doe"


This command sets the username field to "john_doe" within the hash associated
with the key "user:1001".

2. HGET key field:

- Retrieves the value of a specified field within a hash.

HGET user:1001 username


This command would return the value "john_doe" associated with the username
field in the hash.

3. HGETALL key:

- Retrieves all fields and their corresponding values in a hash.

HGETALL user:1001
This command returns a list of field-value pairs for the hash associated with the key
"user:1001".

These hash operations are particularly useful for representing and managing structured
data within Redis. Hashes can be employed to store and retrieve information related to
entities, making them a valuable choice for scenarios where data needs to be organized
into fields and subfields.

4. List Operations:

Certainly, let's explore these list operations in Redis:

1. LPUSH key value1 value2 ...:

- Inserts one or more values at the beginning of a list.

LPUSH mylist "apple" "banana" "cherry"


This command inserts the values "apple," "banana," and "cherry" at the beginning of
the list associated with the key "mylist."
2. RPUSH key value1 value2 ...:

- Inserts one or more values at the end of a list.

RPUSH mylist "date" "fig"


This command appends the values "date" and "fig" to the end of the list associated
with the key "mylist."

3. LRANGE key start stop:

- Retrieves a range of elements from a list. The range is specified by the start and
stop indices.

LRANGE mylist 0 -1
This command retrieves all elements from the list associated with the key "mylist."

These list operations in Redis are beneficial for scenarios where data needs to be
stored in an ordered sequence. Lists are commonly used for implementing queues,
managing job queues, and storing logs where the order of events is crucial. The ability
to insert and retrieve elements from both ends of the list makes Redis lists versatile for
various use cases.

5. Set Operations:

1. SADD key member1 member2 ...:

- Adds one or more members to a set. If the set does not exist, a new set is created.

SADD myset "apple" "banana" "orange"


This command adds the members "apple," "banana," and "orange" to the set
associated with the key "myset."

2. SMEMBERS key:

- Retrieves all members of a set.

SMEMBERS myset
This command returns all members of the set associated with the key "myset."

3. SINTER key1 key2 ...:

- Retrieves the intersection of multiple sets.

SADD set1 "apple" "banana" "orange"


SADD set2 "banana" "cherry" "orange"
SINTER set1 set2
In this example, the last command returns the members "banana" and "orange,"
which are common to both sets.

Redis sets are useful for scenarios where you need to represent a collection of unique
elements. Set operations like union, intersection, and difference provide powerful tools
for analyzing and manipulating sets in various applications.

6. Sorted Set Operations:

Certainly, let's explore these sorted set operations in Redis:

1. ZADD key score1 member1 score2 member2 ...:

- Adds members with associated scores to a sorted set. If a member already exists, its
score is updated.

ZADD highscores 100 "PlayerA" 150 "PlayerB" 80 "PlayerC"


This command adds players to the "highscores" sorted set with corresponding
scores.

2. ZRANGE key start stop:

- Retrieves a range of elements from a sorted set based on their scores.

ZRANGE highscores 0 -1 WITHSCORES


This command retrieves all members and their scores from the "highscores" sorted
set.

Sorted sets in Redis are particularly useful when you need to maintain a ranking or
leaderboard based on scores. These operations allow you to efficiently retrieve and
update elements based on their scores, making sorted sets suitable for scenarios such
as gaming leaderboards or any application involving ordered data.

7. Bitwise Operations (Bitmaps):

Certainly, let's explore these bitwise operations with bitmaps in Redis:

1. SETBIT key offset value:

- Sets or clears the bit at a specified offset in the string value stored at a key. The
value can be 0 or 1.

SETBIT mybitmap 5 1
This command sets the bit at offset 5 in the bitmap associated with the key
"mybitmap" to 1.

2. GETBIT key offset:


- Retrieves the bit value at a specified offset in the string value stored at a key.

GETBIT mybitmap 5
This command returns the bit value at offset 5 in the bitmap associated with the key
"mybitmap."

Bitwise operations in Redis are often used for scenarios where you need to efficiently
represent and manipulate sets of binary flags or indicators. Bitmaps can be employed
for tasks such as tracking user preferences, monitoring system states, or implementing
compact data structures that rely on binary representation.

8. Pub/Sub (Publish/Subscribe):

Certainly, let's explore these Pub/Sub operations in Redis:

1. PUBLISH channel message:

- Publishes a message to a specified channel. Any clients subscribed to that channel


will receive the message.

PUBLISH news_channel "Breaking News: Redis 7.0 Released!"


This command publishes the message "Breaking News: Redis 7.0 Released!" to the
"news_channel."

2. SUBSCRIBE channel:

- Subscribes the client to the specified channel. The client will receive messages
published to that channel.

SUBSCRIBE news_channel
This command subscribes the client to the "news_channel," allowing it to receive
messages published to that channel.

Redis Pub/Sub is commonly used for building real-time messaging systems, chat
applications, and event notification systems. Publishers broadcast messages to specific
channels, and subscribers receive messages from channels they are interested in,
facilitating efficient communication between different parts of an application.

9. Transactions:

Certainly, let's explore these transaction-related commands in Redis:

1. MULTI:

- Marks the start of a transaction block. Subsequent commands are queued up for
execution as part of the transaction.
MULTI
SET key1 "value1"
SET key2 "value2"
This command initiates a transaction block, and the subsequent SET commands will
be executed atomically.

2. EXEC:

- Executes all previously queued commands in a transaction. If the transaction is


successful, the changes are committed; otherwise, they are rolled back.

EXEC
This command executes the queued commands within the transaction block. If
successful, the changes are committed.

3. DISCARD:

- Discards all commands in the current transaction, effectively canceling the


transaction.

DISCARD
This command discards all commands queued in the current transaction block,
undoing any changes made so far.

These transaction commands in Redis provide a way to group multiple commands into
a single atomic operation. This ensures that either all commands in the transaction are
executed, or none of them are, maintaining data consistency. Transactions are
particularly useful in scenarios where it's crucial to perform a series of operations
atomically.

10. Key Expiry:

Certainly, let's explore these commands related to key expiration in Redis:

1. EXPIRE key seconds:

- Sets the time to live (TTL) of a key, indicating how long the key should be retained
before it is automatically deleted.

EXPIRE mykey 60
This command sets the key "mykey" to expire in 60 seconds. After this time elapses,
the key will be automatically removed from the database.
2. TTL key:

- Retrieves the remaining time to live of a key in seconds.

TTL mykey
This command returns the remaining time to live of the key "mykey." If the key is
persistent (does not have a set expiration), TTL returns -1. If the key does not exist or
has expired, TTL returns -2.

These commands are valuable for managing the lifecycle of keys in Redis. Setting an
expiration time is useful for scenarios such as caching, where you want to automatically
refresh data after a certain period. The TTL command provides a convenient way to
check the remaining time before a key expires.

These are just a few examples, and Redis provides a comprehensive set of commands
to perform a wide range of operations on different data structures. Understanding these
basic commands is essential for effectively working with Redis in various applications.

Using Redis in real-world scenarios


Example-1: Redis for Session Storage (Login & Logout)

Related Commands: GET, SET, EXPIRE, DEL

Session-based authentication is a widely used approach for user authentication in web


applications. It involves generating a session token after login, which is then used to
keep track of the authenticated user.

Similar to the issue with user profiles, the session token needs to be checked every time
a user performs an action that requires authentication. Consequently, querying the
database can easily become a bottleneck in high-traffic use cases. Therefore, using
Redis for session storage is considered one of the best solutions.
function Login(email, password){
var session_token = loginLogic()
redis.Do(`SET user:session:{session_token} {email} EX 3600`)
return session_token
}

function Logout(session_token){
redis.Do(`DEL user:session:{session_token}`)
}

function VerifyToken(session_token) {
var email = redis.Do(`GET user:session:{session_token}`)
if(!email) {
return null, errors(`token is expired or not exists`)
}
var user_detail = getUserDetail(email)
return user_detail, null
}

Example-2: OTP (One Time Password) using Redis

Related Command: SET NX EX, GET, DEL

"OTP" stands for "One-Time Password." It is a security measure used to authenticate


users and verify their identity during online transactions or account logins. A one-time
password is a unique and temporary code that is typically valid for a short period of
time, usually for a single login session or transaction. Since Redis has time-based
expiration using the EXPIRE command, it is well-suited for building an OTP (One-Time
Password) system.

The OTP system consists of two functions:

 Generate OTP
 Verify OTP
Redis can be leveraged to build an OTP system. Below is pseudocode that
demonstrates how Redis can be used to develop an OTP system:

function GenerateOTP(phone_number) {
var otp_code = generate_otp_code(6)
var is_success = redis.Do(`SET otp:{phone_number} otp_code NX EX 600`)
if(!is_success){
var ttl = redis.Do(`TTL otp:{phone_number}`)
return errors("otp has been sent, try again in {ttl} minutes")
}
sendOtpViaSMS(phone_number, otp_code)
return null
}

function VerifyOTP(phone_number, otp_code) {


var stored_otp = redis.Do(`GET otp:{phone_number}`)
if(!stored_otp){
return errors(`Code has been expired`)
} else if(stored_otp != otp_code){
return errors(`Invalid OTP Code`)
}

redis.Do(`DEL otp:{phone_number}`)
return null
}

You might also like