No SQL Pr-8
No SQL Pr-8
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.
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.
- The server manages various data structures, processes commands, and responds
to client requests.
- 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.
- 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.
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:
- 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.
- Redis is widely used for caching frequently accessed data, reducing the load on
backend databases and improving overall application performance.
2. Real-Time Analytics:
- 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:
4. Session Storage:
- 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.
- 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.
1. Persistence:
2. Replication:
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:
8. HyperLogLogs:
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.
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 −
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 - 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
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:
- 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.
- 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:
- 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.
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".
- 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:
- Sets the value of a field within a hash. If the hash does not exist, a new hash is
created with the specified key.
3. HGETALL key:
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:
- 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:
- Adds one or more members to a set. If the set does not exist, a new set is created.
2. SMEMBERS key:
SMEMBERS myset
This command returns all members of the set associated with the key "myset."
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.
- Adds members with associated scores to a sorted set. If a member already exists, its
score is updated.
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.
- 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.
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):
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:
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:
EXEC
This command executes the queued commands within the transaction block. If
successful, the changes are committed.
3. DISCARD:
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.
- 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:
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.
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
}
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
}
redis.Do(`DEL otp:{phone_number}`)
return null
}