NoSQL Databases UNIT-2
NoSQL Databases UNIT-2
VISWANADH. MNV
UNIT-2 Distribution Models, Consistency & Version
Stamps
1.2 Distribution Models
2.1.1 Introduction
● The main reason for interest in NoSQL databases is their ability to run on large
clusters of servers. As data grows, it's harder and more costly to upgrade to a
bigger server. Instead, it's better to spread the database across multiple servers.
This approach works well because data can be easily divided and distributed.
● To handle larger amounts of data and increase processing power, there are two
main methods: replication and sharding.
○ Replication: involves copying the same data to multiple servers, which
can improve availability and fault tolerance.
○ Sharding involves splitting different pieces of data across different
servers, which can handle more data and traffic.
● These methods can be used separately or together. Replication comes in two
types:
○ Master-slave replication, where one server is the master and others are
backups.
○ Peer-to-peer replication, where all servers are equal and share data.
● The process starts from the simplest setup (a single server) and progresses to
more complex arrangements:
1. Single server
2. Master-slave replication
3. Sharding
4. Peer-to-peer replication
1
Fig: Single Server Database or Centralized Database
● Advantages:
○ Eliminates complexities of distributed systems.
○ Simplifies management and reasoning for developers.
2.1.3 Sharding
Database sharding is splitting a large database into smaller, manageable pieces called
shards, which are stored on different servers. This approach helps overcome the
limitations of a single server's storage and processing capacity by distributing data
across multiple servers that work together. ( Figure 1.1).
2
Figure 1.1. Sharding puts different data on separate nodes, each of which does its own reads
and writes.
How Database Sharding Works
Sharding splits a database table into smaller pieces, with each piece (or shard) stored on
a different server (or node). All shards follow the original database’s schema but contain
unique rows of data.
Example:
● Original Database:
- Customer ID 1: John (California)
- Customer ID 2: Jane (Washington)
- Customer ID 3: Paulo (Arizona)
- Customer ID 4: Wang (Georgia)
● Sharded Database:
Server A: John, Jane
Server B: Paulo, Wang
Key Terms
● Shards: Partitions of data stored on different servers.
● Shard Key: A column used to determine data distribution across shards.
● Shared-nothing Architecture: Each shard operates independently, unaware of
others.
3
Sharding Methods
1. Range-based Sharding: Divides data based on a range of values.
Example: Names starting with A-I on Shard A, J-S on Shard B, T-Z on Shard C.
Pros: Simple to implement.
Cons: Risk of uneven data distribution.
2. Hashed Sharding: Uses a hash function to distribute data evenly.
Pros: Ensures even data distribution.
Cons: Complex to reassign data when adding new shards.
3. Directory Sharding: Uses a lookup table to assign data to shards.
Pros: Flexible and meaningful data distribution.
Cons: Lookup table errors can cause issues.
4. Geo Sharding: Stores data based on geographical location.
Pros: Faster data retrieval due to proximity.
Cons: Potential for uneven data distribution.
2.1.4 Master-Slave
Replication with Master-Slave Setup:
● Master Node: The primary, authoritative source for data, responsible for
processing updates.
● Slave Nodes: Secondary nodes synchronized with the master, primarily handling
read requests.
4
Figure 1.2. Data is replicated from master to slaves. The master services all writes; reads may
come from either master or slaves.
Benefits:
● Scalability for Read-Intensive Data:
○ Adding more slave nodes allows horizontal scaling to handle more read
requests.
○ All read requests are directed to the slaves.
○ Not ideal for heavy write traffic due to the master’s update processing
limits.
● Read Resilience:
○ If the master fails, slaves can still handle read requests.
○ - Writes are paused until the master is restored or a new master is
appointed.
○ - Quick recovery as a slave can become the new master.
● Hot Backup:
○ Slaves act as a hot backup for the master.
○ Provides resilience, allowing graceful handling of server failures.
○ Think of the system as a single-server store with an immediate backup.
Master Appointment:
● Manual Appointment: Configure one node as the master during setup.
● Automatic Appointment: Nodes in the cluster elect a master automatically.
○ Simplifies configuration.
5
○ Reduces downtime by quickly appointing a new master if the current one
fails.
Figure 1.3. Peer-to-peer replication has all nodes applying reads and writes to all the data.
Benefits:
● Enhanced Scalability: Easily add nodes to improve performance.
6
● Improved Resilience: Continue accessing data despite node failures.
Challenges:
● Consistency Issues due to W-W issue:
○ Risk of write-write conflicts when two nodes update the same record
simultaneously.
○ Inconsistent reads are temporary, but inconsistent writes can cause
long-term issues.
● Trade-off: Balance between consistency and availability.
7
Figure 1.4. Using master-slave replication together with sharding
Benefits:
● Scalability:
○ Both read and write operations can be distributed across multiple nodes.
○ Enhances performance by balancing the load.
● Resilience:
○ Ensures data availability even if some nodes fail.
8
○ Increases fault tolerance and speeds up recovery.
Key Points
● There are two styles of distributing data:
○ Sharding distributes different data across multiple servers, so each server
acts as the single source for a subset of data.
○ Replication copies data across multiple servers, so each bit of data can be
found in multiple places. A system may use either or both techniques.
● Replication comes in two forms:
○ Master-slave replication makes one node the authoritative copy that
handles writes while slaves synchronize with the master and may handle
reads.
○ Peer-to-peer replication allows writes to any node; the nodes coordinate
to synchronize their copies of the data.
● Master-slave replication reduces the chance of update conflicts but peer-to-peer
replication avoids loading all writes onto a single point of failure.
9
2.2 Consistency
2.2.1 Introduction
● Switching from a centralized relational database to a cluster-oriented NoSQL
database involves a big shift in how you think about consistency.
● Relational databases aim for strong consistency by avoiding many types of
inconsistencies.
● In the NoSQL world, terms like "CAP theorem" and "eventual consistency"
become important.
● When building with NoSQL, you need to decide what level of consistency your
system requires.
● Consistency has different forms, and each form can introduce different types of
errors.
10
Scenario:
The diagram depicts a scenario with two nodes: Node 1 and Node 2. Both nodes are
interacting with a shared data item, "A." The timeline shows various operations
performed on "A" at different time points (t1, t2, ..., t8).
The diagram illustrates this problem through the following steps:
● t1: Node 1 reads the value of A.
● t2: Node 1 updates the value of A by adding 100 to it.
● t3: Node 2 reads the value of A. This read operation might return the old value of
A, as the update might not have been propagated to Node 2 yet.
● t4: Node 2 updates the value of A by adding 200 to it.
● t5: Node 1 writes the updated value of A back.
● t7: Node 2 writes the updated value of A back.
11
○ Optimistic: Allows conflicts but detects and resolves them. For instance,
if Martin updates first, Pramod’s attempt would recognize the change and
adjust accordingly.
● Handling Distributed Updates: In distributed systems with multiple servers,
maintaining consistent updates becomes more complex. Methods like sequential
consistency ensure all servers apply updates in the same order.
● Conflict Resolution: Sometimes, conflicting updates are saved separately and
marked for resolution. This approach, akin to version control systems, requires
merging conflicting changes intelligently, possibly involving user intervention.
12
Scenario:
The diagram illustrates this problem through the following steps:
● t1: Master Node reads the value of A.
● t2: Master Node updates the value of A by subtracting 500.
● t3: Master Node writes the updated value of A back.
● t4: Client Node reads the value of A. This read operation might return the old
value of A, as the update might not have been propagated to the Client Node yet.
● t5: Master Node updates the value of A again by subtracting 500.
● t6: Master Node writes the updated value of A back.
● t7: Client Node reads the value of A again. It might still read the old value of A,
depending on the replication and consistency settings of the NoSQL database.
While NoSQL databases often prioritize availability and performance over strong
consistency, there are strategies to mitigate read consistency problems:
1. Inconsistency Window:
● The time during which clients might read inconsistent data.
● Can be very short; for example, Amazon's SimpleDB service often has an
inconsistency window of less than a second.
2. Replication Consistency:
● Ensures the same data item has the same value when read from different
replicas.
13
● Example: A hotel room booking may show different availability on
different nodes due to replication delays.
3. Eventual Consistency:
● Over time, all nodes will update to the same value if there are no further
updates.
● Data that hasn't been updated yet is considered stale.
4. Sequential Consistency:
● Ensures operations are applied in the same order across all nodes.
5. Consistency Levels:
● Applications can often specify the consistency level for individual
requests.
● Use weak consistency when acceptable, and strong consistency when
necessary.
6. Read-Your-Writes Consistency:
● Ensures once a user makes an update, they see it in subsequent reads.
● Important for scenarios like posting comments on a blog where users
expect to see their updates immediately.
7. Session Consistency:
● Provides read-your-writes consistency within a user’s session.
● Can be implemented using sticky sessions (tying a session to one node) or
version stamps (including the latest version stamp in each interaction).
8. Handling Read Consistency with Replication:
● Using sticky sessions might reduce load balancing effectiveness.
● Another approach is to switch the session to the master for writes and
keep reads consistent until slaves catch up.
9. Application Design Considerations:
● Maintaining replication consistency is crucial for overall application
design.
● Avoid keeping transactions open during user interactions to prevent
conflicts. Use approaches like offline locks instead.
14
is a trade-off where strict data accuracy is sometimes sacrificed to make the system
faster and more responsive.
Key Points
1. Trade-offs in System Design:
● Always maintaining perfect consistency can slow down the system.
● Designers often need to balance consistency with other factors like speed
and availability.
● Different applications can tolerate different levels of inconsistency,
depending on their needs.
Example from Relational Databases:
● Traditional databases use transactions to ensure strong consistency.
● However, to boost performance, databases can relax these consistency
rules (e.g., allowing some uncommitted data to be read).
● This is commonly done using a "read-committed" level, which prevents
some conflicts but not all.
2. Impact on Performance:
● Transactions can slow down the system, so some systems avoid using
them entirely.
● For example, early versions of MySQL were popular because they were
fast, even though they didn’t support transactions.
● Large websites like eBay sometimes skip transactions to handle large
volumes of data efficiently, especially when data is split across multiple
servers (sharding).
3. Handling Remote Systems:
● Many applications interact with external systems that can't be included in
a transaction.
● This means updates often happen outside of strict transaction
boundaries, which is common in enterprise applications.
Overview
The CAP Theorem is about NoSQL databases to explain why maintaining consistency
can be challenging. Proposed by Eric Brewer in 2000, and later formally proven by Seth
Gilbert and Nancy Lynch, it helps to understand the trade-offs in distributed systems.
15
Understanding the CAP Theorem helps developers choose the right database for their
specific use case, balancing the trade-offs based on the requirements of their
application.
Theorem statement
1. Consistency (C): All nodes see the same data at the same time.
You can have at most two of these three guarantees at any given time.
According to the CAP Theorem, a distributed system can achieve at most two of these
three properties at the same time. This leads to different types of NoSQL databases
being classified based on their design choices:
16
● AP (Availability and Partition Tolerance): These systems focus on availability
and partition tolerance, potentially allowing for temporary inconsistencies. An
example is Cassandra.
● CA (Consistency and Availability): This is typically not achievable in a
distributed system because network partitions are a reality in any distributed
setup. Most systems cannot guarantee both consistency and availability in the
presence of network failures.
Example scenarios
Here are example scenarios for each combination of the CAP Theorem (Consistency,
Availability, and Partition Tolerance):
17
● Description: An e-commerce platform that needs to maintain accurate inventory
counts across multiple geographic locations.
● Behavior: When a user tries to purchase a product, the system ensures that the
inventory count is updated consistently across all nodes before completing the
transaction (consistency). If there’s a network partition, the system may
temporarily reject orders or limit access to ensure that inventory counts remain
consistent.
● Limitation: During a partition, users may experience delays or rejections when
trying to place orders, sacrificing availability to maintain consistency in inventory
management.
Key Points
1. Durability:Ensures that once data is written, it won't be lost, even if the system
crashes.
18
4. Issues
Replication Durability:
● Replication Failure: When an update is processed but not replicated before a
node fails.
5. Benefits:
● Durability Trade-offs:
○ Choose based on how critical data retention is.
○ Allow individual updates to specify their durability needs.
● Balancing Performance and Durability:
○ Assess the impact of potential data loss against the performance benefits.
○ Use non durable writes for less critical data to improve system
responsiveness.
2.2.7 Quorums
Definition: In NoSQL databases, quorums are the rules used to decide node count and
replication factor to balance consistency and availability when making data operations.
Quorums example:
19
1. Write Quorum:
● Definition: The minimum number of nodes that must confirm a write operation
to ensure strong consistency. The rule for the write quorum is as follows
W > N/2
W: number of nodes participating in the write
N: number of nodes involved in replication
Meaning: number of nodes participating in the write must be
more than the half the number of nodes involved in replication
Replication Factor: The number of replicas is often called the replication factor.
● Example: With data replicated across three nodes, a write quorum might require
at least two nodes to acknowledge the write.
2. Read Quorum:
20
● Definition: The minimum number of nodes that need to be contacted to ensure
reading the most up-to-date data.
● Example: Depending on the write quorum (e.g., if two nodes confirm writes), you
may need to contact at least two nodes for a read.
5. Benefits
Resilience and Automatic Rebalancing:
● Definition: Ensuring data availability and redundancy by maintaining replicas
and automatically redistributing data if a node fails.
● Example: A replication factor of 3 allows for continued operation even if one
node fails.
21
Flexibility in Operations:
● Definition: The ability to adjust the number of nodes involved in operations
based on the desired balance between consistency and availability.
● Example: Some operations might require full quorum for strong consistency,
while others may prioritize speed over freshness of data.
8. Tradeoffs in NoSQL:
● Definition: Balancing consistency (data accuracy) and availability (system uptime)
based on specific operational needs and constraints.
● Example: Choosing whether to prioritize fast reads or strong consistency
depending on the application requirements.
Key Points
● Write-write conflicts occur when two clients try to write the same data at the
same time.
● Read- write conflicts occur when one client reads inconsistent data in the middle
of another client's write.
● Pessimistic approaches lock data records to prevent conflicts. Optimistic
approaches detect conflicts and fix them.
● Distributed systems see read-write conflicts due to some nodes having received
updates while other nodes have not. Eventual consistency means that at some
22
point the system will become consistent once all the writes have propagated to
all the nodes.
● Clients usually want read-your-writes consistency, which means a client can write
and then immediately read the new value. This can be difficult if the read and the
write happen on different nodes.
● To get good consistency, you need to involve many nodes in data operations, but
this increases latency. So you often have to trade off consistency versus latency.
● The CAP theorem states that if you get a network partition, you have to trade off
availability of data versus consistency.
● Durability can also be traded off against latency, particularly if you want to
survive failures with replicated data.
● You do not need to contact all replicants to preserve strong consistency with
replication; you just need a large enough quorum.
23
● Transactions have limitations. Sometimes updates need human intervention and
can't be handled within a transaction because it would take too long. Version
stamps can help in these cases and are useful in distributed systems as well.
Example:
{ "_id": ObjectId("60e7c9c2eab8f44813c0494f"),
"name": "Laptop",
"price": 1200,
"version": 1
db.products.updateOne(
{ _id: ObjectId("60e7c9c2eab8f44813c0494f") },
24
{ $inc: { version: 1 } }
);
Example:
db.collection.insertOne({
});
Example
"_id": ObjectId("60e7c9c2eab8f44813c0494f"),
"age": 30,
db.documents.aggregate([{
$addFields: {
contentHash: {
$hash: {
25
value: {
}])
Example
db.collection.insertOne({
_id: ObjectId(),
});
● You can combine these methods for better results. For example, CouchDB uses
both counters and content hashes to manage version stamps.
● Simple version stamps work well with a single data source or master-slave
replication, where the master controls the stamps. However, in a peer-to-peer
system, things get more complex because there isn't a single authority.
● A counter-based version stamp is simple: each node increments its counter with
each update. If two nodes have different counters, the higher one is more
recent.
● In multiple-master systems, you need more sophisticated methods, like keeping
a history of version stamps to track changes and detect inconsistencies.
Timestamps can be problematic due to clock synchronization issues and can't
detect write-write conflicts.
● A common approach in peer-to-peer NoSQL systems is the vector stamp. This
uses a set of counters, one for each node. Each node updates its counter with
every change, and nodes synchronize their vector stamps when they
communicate.
26
● By comparing vector stamps, you can detect which version is newer or if there
are conflicts. Missing values in vectors are treated as zeros, allowing for flexible
node addition without invalidating stamps.
● Vector stamps are good for detecting inconsistencies but don't resolve them.
Resolving conflicts depends on the specific use case and the trade-off between
consistency and latency.
27
Key Points
● Version stamps help you detect concurrency conflicts. When you read data, then
update it, you can check the version stamp to ensure nobody updated the data
between your read and write.
● Version stamps can be implemented using counters, GUIDs, content hashes,
timestamps, or a combination of these.
● With distributed systems, a vector of version stamps allows you to detect when
different nodes have conflicting updates.
28