NoSQL Databases UNIT-3
NoSQL Databases UNIT-3
VISWANADH. MNV
1
UNIT-3 Key-Value Databases
Introduction
● A key-value store is like a simple hash table.
● It is used when all access to the database is done through a primary key.
● Imagine a table with two columns: ID and NAME.
● The ID column is the key.
● The NAME column stores the value.
● In a relational database (RDBMS), the NAME column stores data as a String.
● You can provide an ID and a VALUE to store in the database.
● If the ID already exists, the value is updated.
● If the ID does not exist, a new entry is created.
● Here's how the terminology compares in Oracle and Riak.
Figure
3.1 What Is a Key-Value Store
2
○ Amazon DynamoDB
○ Berkeley DB
○ HamsterDB
○ Memcached and its variants
● Differences between Amazon DynamoDB, Redis and Riak
In Memory No Yes No
Capabilities
Installation AWS Cloud only Local and Cloud Local and Cloud
Redis Features
3
○ Supports operations like range queries, set differences, unions, and
intersections.
○ Example: You can perform set operations like finding common elements
between two sets.
● Atomic Operations
○ Ensures data integrity by performing atomic operations, which means
operations are completed without interference from other operations.
○ Example: Incrementing a counter is done atomically to prevent race
conditions.
INCR counter # Increments counter by 1
● Pub/Sub Messaging
○ Redis supports publish/subscribe messaging, allowing applications to
communicate in real-time.
○ Example: A chat application where users can subscribe to channels and
receive messages instantly.
SUBSCRIBE channel1 # Subscribes to a channel
PUBLISH channel1 "Hello World" # Publishes a message to
the channel
● Persistence Options
○ Offers different levels of persistence to balance between performance
and data durability.
○ Options include snapshotting (RDB) and append-only file (AOF)
persistence.
SAVE # Manually trigger a snapshot
CONFIG SET appendonly yes # Enable AOF persistence
4
○ Suitable for caching, real-time analytics, and other high-performance
applications.
● Expiration and Eviction
○ Allows setting expiration times for keys to manage memory efficiently.
○ Example: Setting a time-to-live (TTL) for session data.
SET session:12345 data EX 3600 # Expires in 1 hour
● Compared with RDBMS Key-Value Stores do not have all features of RDBMS and
to use them the application architecture should be adjusted to use key-value
stores effectively.
● Key Features to Consider
○ Consistency
○ Transactions
○ Query features
○ Data structure
○ Scaling
3.3 Consistency
5
● Example: Performing read and write operations on a single key guarantees
consistent results within a Redis instance.
2. Optimistic Writes
● Redis supports optimistic writes by allowing clients to perform conditional
operations.
SET key1 "value1"
WATCH key1
value = GET key1
MULTI
SET key1 "value2"
EXEC
● Example: Using WATCH, MULTI, and EXEC commands to perform optimistic
locking to ensure consistency in concurrent operations.
3. Eventual Consistency
● Redis employs an eventually consistent model across distributed nodes.
SET key1 "value1"
● Example: After a SET operation, eventual consistency ensures that the value of
`key1` will eventually propagate and be consistent across all nodes in a Redis
Cluster.
6
● Example: Setting `min-replicas-to-write` ensures that a write operation is
considered successful only after being replicated to a minimum number of
nodes, ensuring data consistency in distributed setups.
6. Transaction Consistency
● Redis transactions ensure atomicity of grouped operations.
MULTI
SET key1 "value1"
SET key2 "value2"
EXEC
● Example: Using MULTI and EXEC commands to execute a series of operations
atomically, maintaining consistency across multiple keys.
3.4 Transactions
Transactions in Redis allow multiple commands to be executed atomically, ensuring
consistency and integrity of data operations. Here are some key aspects of transactions
in Redis along with examples:
2. Transactional Integrity
● Redis ensures that all commands in a transaction either succeed or fail together,
maintaining data integrity.
MULTI
INCR counter
LPUSH mylist "value1"
EXEC
7
● Example: Incrementing a counter and pushing a value into a list are executed as
a single atomic operation. If any command fails (e.g., due to a key not existing),
the entire transaction fails and no changes are applied.
3. Watch and Optimistic Locking
● Redis supports optimistic locking with the `WATCH` command, allowing
transactions to be aborted if monitored keys are modified by other clients.
WATCH key1
GET key1
MULTI
SET key1 "new_value"
EXEC
● Example: Before starting a transaction, `key1` is watched. If its value changes
between `WATCH` and `EXEC`, the transaction is aborted to maintain data
consistency.
5. Transactional Guarantees
● Redis transactions guarantee that all commands are processed sequentially and
atomically, providing isolation for concurrent clients.
MULTI
SET key1 "value1"
GET key1
EXEC
● Example: `GET key1` within the transaction retrieves `"value1"` consistently,
ensuring that the state of `key1` is unchanged during the transaction.
8
3.5 Query Features
Redis, being a key-value store, primarily supports querying data by keys. However, it
offers additional features and capabilities that enhance querying and data retrieval
beyond basic key operations. Here’s an exploration of Redis query features along with
examples:
9
5. Sets and Set Operations
● Redis sets (`SADD`, `SMEMBERS`, etc.) allow set operations like union,
intersection, and difference.
SADD set1 "member1"
SADD set2 "member2"
SINTER set1 set2
● Example: Computes the intersection of `set1` and `set2`, returning common
members.
Redis allows flexibility in how data is structured and stored, supporting various data
types and operations tailored to different use cases.
1. String Values
● Basic Key-Value Storage: Stores a simple string value associated with a key.
10
GET key1
2. Lists
LRANGE mylist 0 -1
3. Sets
SMEMBERS myset
4. Sorted Sets
5. Hashes
● Key-Value Maps: Stores fields and their values within a single key.
11
HGETALL user:1
6. Bitmaps
SETBIT mybitmap 0 1
GETBIT mybitmap 0
7. HyperLogLogs
PFCOUNT visitors
8. Streams
XRANGE mystream - +
3.7 Scaling
● Scaling is a critical feature of key-value stores like Redis, enabling them to handle
large amounts of data and high traffic loads efficiently.
● Scaling is a fundamental feature of key-value stores like Redis, providing the
ability to handle increasing amounts of data and user traffic efficiently. By
12
leveraging horizontal and vertical scaling, sharding, replication, and load
balancing, Redis can ensure high performance, availability, and reliability for
various applications. Here's an expanded explanation with examples:
1. Horizontal Scaling
● Definition: Horizontal scaling, also known as scaling out, involves adding more
servers or nodes to distribute the load. This method enhances the system's
capacity to handle more operations by dividing the workload among multiple
machines.
● Example: In Redis, horizontal scaling can be achieved through clustering. A Redis
cluster spreads the dataset across multiple nodes, allowing for parallel
processing of read and write requests.
● Setting up a Redis Cluster:
redis-server --cluster-enabled yes --cluster-config-file nodes.conf
--cluster-node-timeout 5000 --appendonly yes
2 Vertical Scaling
● Definition: Vertical scaling, or scaling up, involves increasing the resources (CPU,
RAM, storage) of an existing server. While this can improve performance, it has
physical and economic limits.
● Example: Upgrading a Redis server to a machine with more RAM allows it to
store more data in memory, thereby increasing its capacity to handle larger
datasets without increasing latency.
3 Sharding
● Definition: Sharding is a technique used to distribute data across multiple
databases or nodes. Each shard holds a portion of the dataset, determined by a
shard key.
● Example: In Redis, sharding can be implemented by using consistent hashing or
custom sharding logic. The data is distributed across different shards based on
the hash of the key.
13
4 Replication
● Definition: Replication involves copying data from one Redis server (the master)
to one or more Redis servers (the replicas). This enhances data availability and
fault tolerance.
● Example: In Redis, replication is straightforward to set up. The replicas can be
used to offload read traffic from the master, improving read scalability.
● Setting Up Replication:
# Master server
redis-server --port 6379
# Replica server
redis-server --port 6380
redis-cli -p 6380 SLAVEOF 127.0.0.1 6379
Key-value stores are well-suited for specific use cases where their simplicity,
performance, and scalability provide significant advantages. Here are some scenarios
where key-value stores like Redis excel, along with examples:
● Use Case: Web applications often need to manage session data for each user,
storing information such as login status, preferences, and temporary user data.
14
● Example: Storing session information in Redis using a key-value pair where the
key is the session ID (`session:id`) and the value is a serialized JSON object
containing user preferences and session details.
Example: Storing session information for a user during their web session.
SET session:12345
'{"userId":"12345","loginTime":"2024-07-06T10:00:00Z","preferences":{"them
e":"dark"}}' EX 3600
GET session:12345
Figure 2.2. Change the key design to segment the data in a single bucket.
● Use Case: Applications frequently store user profiles and preferences, which can
be accessed and updated quickly.
● Example: Storing user profile details and preferences in Redis using hashes for
efficient retrieval and updates.
HSET user:12345 name "John Doe"
HSET user:12345 email "john@example.com"
15
HSET user:12345 preferences '{"theme": "dark", "language": "en"}'
HGETALL user:12345:preferences
HGETALL cart:12345
16
Figure 2.1. Storing all the data in a single bucket
4. Caching
● Use Case: Key-value stores are widely used for caching frequently accessed data
to improve application performance and reduce latency.
● Example: Caching database query results or computed values in Redis to avoid
repeated expensive operations.
SET cache:user_profile:12345 '{"name": "John Doe", "age": 30, "address":
"123 Main St"}'
5. Configuration Management
6. Real-Time Analytics
● Use Case: Logging and analyzing real-time events or metrics where quick data
retrieval is critical.
● Example: Storing event data in Redis sorted sets or lists for efficient aggregation
and analysis.
17
ZADD analytics:page_views 1 "page1"
ZINCRBY analytics:page_views 1 "page1"
● Performance: Key-value stores offer fast read and write operations due to their
simple data structure and in-memory storage.
● Scalability: They can scale horizontally by adding more nodes, distributing data
across multiple servers.
● Flexibility: Support for various data types (strings, hashes, sets, sorted sets)
allows diverse use cases beyond simple key-value storage.
While Redis offers powerful features for specific use cases, there are scenarios where a
key-value store may not be the ideal solution due to its inherent limitations. Here are
some situations where you might reconsider using Redis:
2. Multi-Operation Transactions
18
3. Querying by Data
● Issue: Key-value stores like Redis primarily allow querying by keys rather than by
the data stored within the values.
● Example: If you frequently need to query data based on attributes stored within
the values (e.g., finding all users with a specific age range or searching
documents by content), Redis's lack of secondary indexing or querying
capabilities could lead to inefficient solutions. Although the Redisearch module
provides full-text search capabilities, it's an add-on and not part of the core Redis
capabilities.
4. Operations by Sets
● Issue: Redis operations are typically limited to single keys or small sets of keys
managed by the same client-side logic.
● Example: If your application requires complex set operations across multiple
keys, such as set intersections, unions, or differences involving large datasets,
Redis's lack of native support for operations spanning multiple keys could result
in performance bottlenecks. While Redis supports sets and set operations,
managing large distributed sets or complex operations may require additional
client-side logic.
● Key-value stores do not inherently validate the data being stored. The application
must handle all data validation, which can lead to inconsistencies if not managed
properly.
● The simple key-value model is not suitable for complex data models.
Representing intricate data relationships or hierarchical data structures can be
cumbersome and inefficient.
19
3. Lack of Standardization:
20