Unit V
Unit V
Goal:
• Place data closer to where it’s used
• Improve query performance
• Enhance availability and parallelism
Data Replication
• A relation or fragment of a relation is replicated if it is stored
redundantly in two or more sites.
• Full replication of a relation is the case where the relation is stored at
all sites.
• Fully redundant databases are those in which every site contains a
copy of the entire database.
Data Replication (Cont.)
• Advantages of Replication
• Availability: failure of a site containing relation r does not result in the unavailability of
r if replicas exist.
• Parallelism: Queries on r may be processed by several nodes in parallel.
• Reduced data transfer: relation r is available locally at each site containing a replica of
r.
• Disadvantages of Replication
• Increased cost of updates: each replica of relation r must be updated.
• Increased complexity of concurrency control: Concurrent updates to distinct replicas
may lead to inconsistent data unless special concurrency control mechanisms are
implemented.
• One solution: choose one copy as the primary copy and apply concurrency control operations on
the primary copy
Data Fragmentation
• Division of relation r into fragments r1, r2, …, rn which contain sufficient
information to reconstruct relation r.
• Horizontal fragmentation: each tuple of r is assigned to one or more fragments
• Vertical fragmentation: the schema for relation r is split into several smaller
schemas
• All schemas must contain a common candidate key (or superkey) to ensure lossless join
property.
• A special attribute, the tuple-id attribute may be added to each schema to serve as a
candidate key.
• Example : relation account with following schema
• Account-schema = (branch-name, account-number, balance)
Horizontal Fragmentation of account
Relation
branch-name account-number balance
account1=branch-name=“Hillside”(account)
account2=branch-name=“Valleyview”(account)
Vertical Fragmentation of employee-info
Relation
branch-name customer-name tuple-id
Hillside Lowman 1
Hillside Camp 2
Valleyview Camp 3
Valleyview Kahn 4
Hillside Kahn 5
Valleyview Kahn 6
Valleyview Green 7
deposit1=branch-name, customer-name, tuple-id(employee-info)
account number balance tuple-id
A-305 500 1
A-226 336 2
A-177 205 3
A-402 10000 4
A-155 62 5
A-408 1123 6
A-639 750 7
deposit2=account-number, balance, tuple-id(employee-info)
1. Horizontal Fragmentation
Definition: Divides a relation by rows (tuples)
Uses a condition (e.g., Dno=5) to select relevant tuples.
Each fragment stores specific rows of a relation.
EMPLOYEE1 = σDno=5(EMPLOYEE)
EMPLOYEE2 = σDno=4(EMPLOYEE)
Key Concepts:
Complete: All tuples are covered by the fragments.
Disjoint: No tuple appears in more than one fragment.
Derived Fragmentation: Use the partitioning of a primary relation to fragment related secondary relations.
2. Vertical Fragmentation
Definition: Divides a relation by columns (attributes).
Each fragment has some attributes and includes the primary key to allow rejoining.
Example:
Fragment 1:{Ssn, Name, Bdate, Address, Sex}
Fragment 2: {Ssn, Salary, Super_ssn, Dno}
Key Concepts:
Complete: All attributes are included across fragments.
Disjoint: Only primary keys can repeat between fragments.
3. Mixed (Hybrid) Fragmentation
Definition: Combination of horizontal and vertical fragmentation.
First, horizontally fragment based on some condition, then apply vertical fragmentation to
each horizontal fragment (or vice versa).
πL1(σDno=5(EMPLOYEE)), where L1 = {Ssn, Name}
Advantages of Fragmentation
• Horizontal:
• allows parallel processing on fragments of a relation
• allows a relation to be split so that tuples are located where they are most
frequently accessed
• Vertical:
• allows tuples to be split so that each part of the tuple is stored where it is
most frequently accessed
• tuple-id attribute allows efficient joining of vertical fragments
• allows parallel processing on a relation
• Vertical and horizontal fragmentation can be mixed.
• Fragments may be successively fragmented to an arbitrary depth.
Data Transparency
• Data transparency: Degree to which system user may remain
unaware of the details of how and where the data items are stored in
a distributed system
• Consider transparency issues in relation to:
• Fragmentation transparency
• Replication transparency
• Location transparency
Distributed Transactions
• Transaction may access data at several sites.
• Each site has a local transaction manager responsible for:
• Maintaining a log for recovery purposes
• Participating in coordinating the concurrent execution of the transactions
executing at that site.
• Each site has a transaction coordinator, which is responsible for:
• Starting the execution of transactions that originate at the site.
• Distributing subtransactions at appropriate sites for execution.
• Coordinating the termination of each transaction that originates at the site,
which may result in the transaction being committed at all sites or aborted at
all sites.
Transaction System Architecture
Commit Protocols
• Commit protocols are used to ensure atomicity across sites
• a transaction which executes at multiple sites must either be committed at all
the sites, or aborted at all the sites.
• not acceptable to have a transaction committed at one site and aborted at
another
• The two-phase commit (2 PC) protocol is widely used
• The three-phase commit (3 PC) protocol is more complicated and
more expensive, but avoids some drawbacks of two-phase commit
protocol.
Two Phase Commit Protocol (2PC)
• Assumes fail-stop model – failed sites simply stop working, and do
not cause any other harm, such as sending incorrect messages to
other sites.
• Execution of the protocol is initiated by the coordinator after the last
step of the transaction has been reached.
• The protocol involves all the local sites at which the transaction
executed
• Let T be a transaction initiated at site Si, and let the transaction
coordinator at Si be Ci
Phase 1: Obtaining a Decision
• Coordinator asks all participants to prepare to commit transaction Ti.
• Ci adds the records <prepare T> to the log and forces log to stable storage
• sends prepare T messages to all sites at which T executed
• Upon receiving message, transaction manager at site determines if it
can commit the transaction
• if not, add a record <no T> to the log and send abort T message to Ci
• if the transaction can be committed, then:
• add the record <ready T> to the log
• force all records for T to stable storage
• send ready T message to Ci
Phase 2: Recording the Decision
• T can be committed of Ci received a ready T message from all the
participating sites: otherwise T must be aborted.
• Coordinator adds a decision record, <commit T> or <abort T>, to the
log and forces record onto stable storage. Once the record stable
storage it is irrevocable (even if failures occur)
• Coordinator sends a message to each participant informing it of the
decision (commit or abort)
• Participants take appropriate action locally.
Handling of Failures - Site Failure
When site Si recovers, it examines its log to determine the fate of
transactions active at the time of the failure.
• Log contain <commit T> record: site executes redo (T)
• Log contains <abort T> record: site executes undo (T)
• Log contains <ready T> record: site must consult Ci to determine the fate of T.
• If T committed, redo (T)
• If T aborted, undo (T)
• The log contains no control records concerning T replies that Sk failed before responding
to the prepare T message from Ci
• since the failure of Sk precludes the sending of such a
response C1 must abort T
• Sk must execute undo (T)
Handling of Failures- Coordinator
Failure
• If coordinator fails while the commit protocol for T is executing then
participating sites must decide on T’s fate:
1. If an active site contains a <commit T> record in its log, then T must be committed.
2. If an active site contains an <abort T> record in its log, then T must be aborted.
3. If some active participating site does not contain a <ready T> record in its log, then
the failed coordinator Ci cannot have decided to commit T. Can therefore abort T.
4. If none of the above cases holds, then all active sites must have a <ready T> record
in their logs, but no additional control records (such as <abort T> of <commit T>).
In this case active sites must wait for Ci to recover, to find decision.
• Blocking problem : active sites may have to wait for failed coordinator to
recover.
Handling of Failures - Network
Partition
• If the coordinator and all its participants remain in one partition, the
failure has no effect on the commit protocol.
• If the coordinator and its participants belong to several partitions:
• Sites that are not in the partition containing the coordinator think the
coordinator has failed, and execute the protocol to deal with failure of the
coordinator.
• No harm results, but sites may still have to wait for decision from coordinator.
• The coordinator and the sites are in the same partition as the
coordinator think that the sites in the other partition have failed, and
follow the usual commit protocol.
• Again, no harm results
Three Phase Commit (3PC)
• Assumptions:
• No network partitioning
• At any point, at least one site must be up.
• At most K sites (participants as well as coordinator) can fail
• Phase 1: Obtaining Preliminary Decision: Identical to 2PC Phase 1.
• Every site is ready to commit if instructed to do so
• Phase 2 of 2PC is split into 2 phases, Phase 2 and Phase 3 of 3PC
• In phase 2 coordinator makes a decision as in 2PC (called the pre-commit decision) and records it in multiple (at least K)
sites
• In phase 3, coordinator sends commit/abort message to all participating sites,
• Under 3PC, knowledge of pre-commit decision can be used to commit despite coordinator failure
• Avoids blocking problem as long as < K sites fail
• Drawbacks:
• higher overheads
• assumptions may not be satisfied in practice
Operating System Support for Transaction Management
• The following are the main benefits of operating system (OS)-supported
transaction management:
• Typically, DBMSs use their own semaphores to guarantee mutually exclusive
access to shared resources. Since these semaphores are implemented in
userspace at the level of the DBMS application software, the OS has no
knowledge about them. Hence if the OS deactivates a DBMS process holding
a lock, other DBMS processes wanting this lock resource get queued. Such a
situation can cause serious performance degradation. OS-level knowledge of
semaphores can help eliminate such situations.
• Specialized hardware support for locking can be exploited to reduce
associated costs. This can be of great importance, since locking is one of the
most common DBMS operations.
• Providing a set of common transaction support operations though the kernel
allows application developers to focus on adding new features to their
products as opposed to reimplementing the common functionality for each
application. For example, if different DDBMSs are to coexist on the same
machine and they chose the two-phase commit protocol, then it is more
beneficial to have this protocol implemented as part of the kernel so that the
DDBMS developers can focus more on adding new features to their products.
Query Processing and Optimization in Distributed
Databases
In Distributed Database Systems (DDBS), query processing and optimization are critical for ensuring that user
queries execute efficiently across multiple distributed sites. Below is a detailed explanation:
1. What is Query Processing?
Query processing in DDB involves:
Translating a high-level query (like SQL) into a low-level execution plan.
Accessing and retrieving data from multiple distributed fragments located at different sites.
Ensuring correctness, completeness, and efficiency of results.
2. Challenges in Distributed Query Processing
Data Distribution: Data may be fragmented and stored across different locations.
Communication Cost: Data transmission between sites is expensive.
Heterogeneity: Different sites may have different DBMSs.
Latency: Query response time increases with remote access.
Concurrency and Synchronization: Coordinating multiple data sources is complex.
3. Steps in Distributed Query Processing
Step Description
Break down the SQL query into an algebraic query tree
1. Query Decomposition
or logical plan.
2. Data Localization Identify the fragments and sites involved in the query.
Generate alternative query plans using relational
3. Global Optimization
algebra.
4. Local Optimization Optimize query execution at individual sites.
Schedule and execute subqueries in parallel across the
5. Distributed Execution
network.
-- Creating a table
CREATE TABLE college.students (id int PRIMARY KEY, name text, age int);
-- Inserting data
INSERT INTO college.students (id, name, age) VALUES (1, 'John', 20);
-- Querying data
SELECT * FROM college.students;
Neo4j Queries
• // Creating nodes
CREATE (s:Student {name:'John', age:20})
CREATE (c:Course {name:'DBMS'})
• // Creating relationship
MATCH (s:Student), (c:Course)
WHERE s.name='John' AND c.name='DBMS’
CREATE (s)-[:ENROLLED_IN]->(c)
// Querying relationships
MATCH (s:Student)-[:ENROLLED_IN]->(c:Course)
RETURN s.name, c.name
Key terminologies
Term Meaning
Sharding Distributing data across multiple servers.
Replication Copying data across servers for availability.
A distributed database can only guarantee 2 of
CAP Theorem Consistency, Availability, and Partition Tolerance at a
time.
Basically Available, Soft state, Eventual consistency –
BASE Model
NoSQL model vs ACID (SQL).
NoSQL
• NoSQL supports big data, real-time, and flexible applications.
• Understanding types, features, and queries is essential.
• Vital for modern apps in AI/ML, IoT, and cloud environments.
CAP Theorem: Concept and Proof
Introduction
The CAP Theorem, also known as Brewer's Theorem, is a fundamental concept in distributed database
systems. It states that a distributed system cannot simultaneously guarantee all three of the following properties:
1. Consistency (C)
2. Availability (A)
3. Partition Tolerance (P)
This theorem is extremely important for understanding the trade-offs in NoSQL and distributed databases.
Key Terms Explained
1. Consistency
Every read receives the most recent write.
All nodes return the same data even if read at the same time.
2. Availability
Every request receives a (non-error) response, without guarantee that it contains the most recent data.
3. Partition Tolerance
The system continues to operate despite network failures (message loss or delays) between nodes in a
distributed system.
CAP Theorem Statement
In the presence of a network partition, a distributed system can provide only two of the three guarantees: Consistency, Availability, or Partition
Tolerance.
So, a system must sacrifice one of the properties when there is a partition.
Proof:
Let's assume a distributed system with two nodes (Node A and Node B), and a network partition occurs (A cannot communicate with B).
Suppose:
A client writes data to Node A.
At the same time, another client tries to read from Node B.
Now the system must choose:
Case 1: Prioritize Availability and Partition Tolerance
Node B responds to the read request (Available) despite not getting the latest write from A (Inconsistent).
So, it satisfies Partition Tolerance, Availability, but not Consistency.
Case 2: Prioritize Consistency and Partition Tolerance
Node B refuses the read or delays it until the partition resolves (to ensure consistency).
So, it satisfies Partition Tolerance, Consistency, but not Availability.
Case 3: Prioritize Consistency and Availability
This would require no network partition, because consistency requires communication between A and B.
But partition tolerance is a must in real-world systems ⇒ Not feasible in distributed systems.
You must choose 2 out of 3, not all 3, especially in the presence of a partition.
CAP Combinations and Examples
Chosen Properties Sacrificed Database Examples
Rare in real systems (single node
Consistency + Availability Partition Tolerance
systems)
Consistency + Partition Tolerance Availability HBase, MongoDB (in CP mode)
Availability + Partition Tolerance Consistency Cassandra, CouchDB (in AP mode)