0% found this document useful (0 votes)
10 views

Module 2

Uploaded by

Zyedahmedkhazi
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 views

Module 2

Uploaded by

Zyedahmedkhazi
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/ 22

Key-Value Databases

-
Prof. Kaushika S.,
Department of CSE,
SOET, CMR University.
Key-Value DB

Data organized as pairs of unique keys and corresponding values.

Simple structure enables fast data retrieval, making it efficient for high-speed data access.

Key-value stores are the simplest NoSQL data stores to use from an API perspective.
The client can either get the value for the key, put a value for a key, or delete a key from
the data store.
Key-value stores always use primary-key access, they generally have great performance
and can be easily scaled.
Popular Key-Value DB

Riak [Riak]
Redis (often referred to as Data Structure server) [Redis]
HamsterDB (especially suited for embedded use) [HamsterDB]
Amazon DynamoDB [Amazon’s Dynamo] (not open-source), and Project Voldemort
[Project Voldemort] (an open-source implementation of Amazon DynamoDB).
Key-Value DB

Real Time Example: Online Game

Riak helps an online game remember who's playing.

Each player has a unique "key" like their username, and Riak stores all their game progress,
like levels completed and scores earned, as the "value" associated with that key. So, when a
player logs in, Riak quickly finds their key and loads their game data.
Key-Value Concepts
Key-value databases store data as pairs of keys and values.
Each pair is like a question and its answer, making data retrieval simple.
Example: Storing book information where each book has a unique ID (key) and
details like title, author, and publication date (value).
The ID will be unique and acts as the primary key for quick data retrieval
Each book can have its own set of details, or schema, making the database flexible
and easy to manage.
Suitable Use Cases
Session Management in Web Applications
In a social media platform, users frequently log in and out, and their session data needs to be
managed seamlessly. Key-Value stores efficiently handle session data, ensuring quick access
and updates for a smooth user experience.

Real-time Analytics and Monitoring

A ride-sharing app needs to track driver locations and passenger requests in real-time to
optimize matching and provide timely updates to users. Key-Value stores capture and analyze
event data as it happens, enabling instant insights into user behavior.

Caching

An e-commerce website experiences heavy traffic during sales events. Key-Value stores cache
product information, reducing the load on backend systems and ensuring fast response times
for customers browsing the site.
Key-Value Store Features

Consistency:

Consistency ensures that data remains accurate and coherent across distributed systems.

NoSQL data stores offer various consistency models, allowing users to choose between
strong consistency and eventual consistency based on application requirements.

Strong Consistency: Guarantees that all updates are immediately visible to all users,
ensuring data integrity but potentially impacting availability.

Eventual Consistency: Allows for faster read and write operations by prioritizing
availability over immediate consistency. Updates are propagated asynchronously, and
eventual consistency is achieved over time.
Key-Value Store Features

Transactions:

Transactions enable atomic operations on data, ensuring that multiple operations either
succeed or fail together, maintaining data integrity.

NoSQL data stores provide support for transactions, though the implementation may vary
across different databases.

Transactions in NoSQL databases allow for complex operations involving multiple data
items to be executed atomically.

While traditional ACID transactions may not always be supported, many NoSQL databases
offer lightweight transaction mechanisms to ensure data consistency.
Key-Value Store Features

Query Features:

Query features allow users to retrieve and manipulate data efficiently, enabling flexible
data access patterns.

NoSQL databases offer various query capabilities tailored to specific data models and use
cases.

NoSQL databases support a range of query features, including keybased lookups,


secondary indexes, and advanced query languages.

Query capabilities vary depending on the database type, with documentoriented databases
offering rich querying capabilities for semistructured data, while graph databases excel in
complex graph traversals.
Key-Value Store Features

Structure of the Data:

NoSQL data stores accommodate diverse data structures, including structured,


semistructured, and unstructured data.

They offer flexibility in data modeling, allowing users to adapt to changing data
requirements.

NoSQL databases support flexible data modeling, enabling storage of structured,


semistructured, and unstructured data types.

Documentoriented databases store data in flexible JSON or XML documents, while


columnar databases organize data into columns for efficient querying and storage.

Graph databases represent data as nodes and edges, facilitating complex relationships and
network analysis.
Key-Value Store Features

Scaling:

Scaling refers to the ability of the database to handle increasing data volumes and user
loads efficiently.

NoSQL data stores are designed for horizontal scalability, allowing seamless expansion by
adding more nodes to the cluster.

NoSQL databases excel in horizontal scalability, distributing data across multiple nodes to
accommodate growing workloads.

They employ distributed architectures, such as sharding and replication, to ensure data
availability, fault tolerance, and load balancing.

NoSQL databases scale linearly with the addition of new nodes, making them suitable for
largescale applications with high throughput and performance requirements.
Pros of Key-Value Store
Simplified Data Model:

A content management system uses a key-value database to store user preferences. Each user's
preferences, such as theme settings and language preferences, are stored as key-value pairs,
simplifying data management and enhancing user experience.

High Performance:

A real-time chat application relies on a key-value database to store message data. The fast read and
write operations of the key-value store ensure that messages are delivered and displayed to users
with minimal latency, enhancing the user experience.

Scalability:

An e-commerce platform uses a key-value database to store product catalog data. As the platform
grows and the number of products increases, the key-value database scales horizontally by adding
more nodes, ensuring the platform can handle the growing data volume and user traffic effectively.
Pros of Key-Value Store
Flexibility:

A customer relationship management (CRM) system utilizes a key-value database to store


customer information. The flexibility of the key-value store allows the CRM system to
accommodate diverse data types, such as customer profiles, interaction history, and
preferences, providing a comprehensive view of each customer.

Fault Tolerance:

A financial trading platform relies on a key-value database to store transaction data. The
built-in fault tolerance mechanisms of the key-value store ensure data reliability and integrity,
even in the event of server failures or network disruptions, safeguarding critical financial
transactions.
Cons of Key-Value Store
Limited Querying Capabilities:

An analytics platform requires complex data analysis and reporting. The limited querying
capabilities of the key-value database make it challenging to perform advanced analytics tasks, such
as aggregations, joins, and complex filtering, leading to inefficiencies in data analysis workflows.

Lack of Transactions:

An inventory management system relies on a key-value database to track product inventory. The
lack of support for multi-key transactions in the key-value store makes it difficult to maintain data
consistency and integrity, leading to potential issues such as inventory discrepancies and order
fulfilment errors.

Data Redundancy:

A social media platform uses a distributed key-value database to store user-generated content. The
replication strategies employed by the key-value store may result in data redundancy, leading to
increased storage costs and complexity in data management.
Cons of Key-Value Store
Complex Data Modeling:

A healthcare application stores patient records and medical data in a key-value database.
Storing complex, nested data structures such as medical histories and treatment plans in the
key-value store requires additional application logic and data modeling efforts, leading to
increased development complexity and maintenance overhead.

Consistency Trade-offs:

A real-time bidding platform relies on a distributed key-value database to store ad campaign


data. The eventual consistency model of the key-value store may result in inconsistencies in
bidding data, leading to discrepancies in ad placement and revenue optimization.
Key-Value Database using Riak

Riak is an open-source, distributed NoSQL database developed by Basho Technologies.


Riak is a distributed, NoSQL key-value database designed for high availability and fault
tolerance.

Riak simplifies data storage and retrieval by storing information as key-value pairs.

Riak's distributed architecture allows for seamless scalability by adding or removing nodes
as needed.
CAP Theorem
The CAP Theorem, formulated by computer scientist Eric Brewer, states that in a distributed system, it's
impossible to simultaneously achieve all three of the following properties: Consistency, Availability, and
Partition tolerance.
Consistency:

Consistency refers to the guarantee that all nodes in a distributed system have the same data at the
same time. In other words, whenever data is updated, all subsequent reads will return the most recent
write.

Availability:

Availability ensures that every request to the system receives a response, regardless of the state of
the system or the number of nodes that may have failed.

Partition Tolerance:

Partition tolerance means that the system continues to operate even if communication between nodes
is disrupted or partitions occur, leading to network failures or message delays.
Riak & CAP Theorem
Riak, as a distributed key-value database, adheres to the principles of the CAP Theorem.

Riak prioritizes Availability and Partition tolerance over strict Consistency, making it an
AP (Available and Partition tolerant) system.

This means that in the event of network partitions or failures, Riak will prioritize
responding to read and write requests to maintain system availability, even if it means
sacrificing immediate consistency.
Features of Riak
Distributed Architecture: Riak's decentralized architecture distributes data across
multiple nodes for fault tolerance and scalability.

AP Model: Riak ensures high Availability and Partition tolerance, making it resilient to
network failures.

KeyValue Model: Data in Riak is stored as key-value pairs, enabling fast and efficient data
access.

Multi-Datacenter Replication: Riak supports replication across multiple data centers for
disaster recovery and improved data locality.
Use Cases of Riak

Storing Session Information:

Riak is commonly used for session management in web applications.

Session information, such as user authentication tokens and session IDs, is stored as
key-value pairs in Riak.

Riak's high availability and fault tolerance ensure that session data remains accessible, even
during server failures or network issues.

Fast and efficient access to session data enhances user experience by providing seamless
navigation across web pages.
Use Cases of Riak

Managing User Profiles and Preferences:

Riak serves as a reliable storage solution for managing user profiles and preferences in
various applications.

User profiles, including personal information, preferences, and settings, are stored as
key-value pairs in Riak.

Riak's scalability and distributed architecture ensure that user data remains available and
consistent across multiple nodes.

Personalized experiences, such as customized content recommendations and tailored


settings, are enabled by efficient retrieval and updating of user data in Riak.
Use Cases of Riak

Handling Shopping Cart Data:

Riak is well-suited for managing shopping cart data in ecommerce applications.

Shopping cart information, such as product IDs, quantities, and prices, is stored as
key-value pairs in Riak.

Riak's key-value model allows for fast and efficient updates to shopping cart contents,
ensuring a smooth checkout process for customers.

High availability and fault tolerance of Riak ensure that shopping cart data is preserved,
even during peak shopping periods or server failures.

You might also like