0% found this document useful (0 votes)
14 views3 pages

INS Assignments

The document outlines various real-world scenarios where Redis is advantageous, including caching, session management, real-time analytics, and message queuing. It also provides examples of basic Redis operations using commands like SET, GET, and HSET, as well as demonstrating the use of transactions for atomic operations. Additionally, it explains how transactions ensure consistency and atomicity in Redis commands.

Uploaded by

kariyasneha
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)
14 views3 pages

INS Assignments

The document outlines various real-world scenarios where Redis is advantageous, including caching, session management, real-time analytics, and message queuing. It also provides examples of basic Redis operations using commands like SET, GET, and HSET, as well as demonstrating the use of transactions for atomic operations. Additionally, it explains how transactions ensure consistency and atomicity in Redis commands.

Uploaded by

kariyasneha
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/ 3

Assignment - 5

1. Identify real-world scenarios where Redis is beneficial, such as caching,


session management, real-time analytics, and message queuing.
Ans =

Here are real-world use cases where Redis is beneficial:

1.​ Caching – Speeds up web applications by storing frequently accessed data.


2.​ Session Management – Stores user sessions for fast authentication.
3.​ Real-Time Analytics – Processes live data, e.g., stock prices, user activity.
4.​ Message Queuing – Enables instant messaging and notifications with Pub/Sub.
5.​ Leaderboards – Ranks scores efficiently in gaming apps using Sorted Sets.
6.​ Rate Limiting – Controls API request limits per user.
7.​ Geospatial Queries – Finds nearby locations in ride-sharing and logistics apps.
8.​ Machine Learning Feature Store – Stores precomputed data for quick AI model
inference.
9.​ Distributed Locks – Prevents race conditions in multi-server environments.
10.​Streaming Data Processing – Supports event-driven architectures with Redis
Streams.
11.​E-commerce Inventory Management – Ensures real-time stock updates.
12.​Auto-Completing & Search Suggestions – Provides instant search results.
13.​Shopping Cart Management – Stores active carts in online stores.
14.​IoT Data Storage – Collects and processes sensor data in real time.
15.​Social Media Feeds – Manages real-time newsfeeds and activity logs.

2.Perform basic operations in Redis using commands like SET, GET,

LPUSH, RPUSH, SADD, SMEMBERS, ZADD, and HSET.

Ans =

# String Operations

SET name "Alice"

GET name

# List Operations
LPUSH fruits "Apple" "Banana" "Mango"

RPUSH fruits "Orange"

LRANGE fruits 0 -1

# Set Operations

SADD colors "Red" "Green" "Blue"

SMEMBERS colors

# Sorted Set Operations

ZADD leaderboard 100 "Player1" 200 "Player2"

ZRANGE leaderboard 0 -1 WITHSCORES

# Hash Operations

HSET user:1 name "John" age 25 country "USA"

HGETALL user:1

3.Implement transactions in Redis using multi/exec commands to ensure

atomicity and consistency for a series of operations.

Ans =

Using Redis Transactions

Redis transactions allow executing multiple commands atomically, meaning all


commands will either be executed together or none at all if an error occurs.
Example: Banking Transaction

Suppose you want to decrease a user's balance and increase another user's balance in
a single atomic operation.

CODE :

MULTI # Start a transaction

SET balance 1000 # Set initial balance

DECRBY balance 200 # Deduct 200 from balance

INCRBY balance 150 # Add 150 to balance

EXEC # Execute the transaction

Explanation:

1.​ MULTI – Starts the transaction.


2.​ SET balance 1000 – Sets the initial balance to 1000.
3.​ DECRBY balance 200 – Deducts 200 from the balance.
4.​ INCRBY balance 150 – Adds 150 to the balance.
5.​ EXEC – Executes all queued commands atomically.

If any command fails before EXEC, the transaction can be discarded using DISCARD.
This ensures atomicity and consistency in Redis transactions.

You might also like