View stunning SlideShares in full-screen with the new iOS app!Introducing SlideShare for AndroidExplore all your favorite topics in the SlideShare appGet the SlideShare app to Save for Later — even offline
View stunning SlideShares in full-screen with the new Android app!View stunning SlideShares in full-screen with the new iOS app!
PARALLEL AND DISTRIBUTED DATABASES
Database System Architectures: Centralized and ClientServer Architectures–
Server System Architectures -Parallel Systems-Distributed Systems Parallel
Databases: I/O Parallelism - Inter and Intra Query Parallelism –Inter and Intra
operation Parallelism –Design of Parallel Systems-Distributed Database
Concepts -Distributed Data Storage –Distributed Transactions –Commit
Protocols –Concurrency Control –Distributed Query Processing –Case Studies
Client-Server Architecture for DDBMS
General idea: Divide the functionality into two
∗mainly data management, including
query processing, optimization, transaction management, etc.
∗might also include some data management functions (consistency checking,
transaction management, etc.) not just
•Provides a two-level architecture
•More efficient division of work
•Different types of client/server architecture
–Multiple client/single server
–Multiple client/multiple server
One server, many clients
Components of Client-Server
Many servers, many clients
Problems of Parallel DBMS
Architecture of Parallel DBMS
IBM DB2 UDB Enterprise
Oracle Real Application Cluster
Summary: DBMS Architecture Comparisons
Searching for performance through the exploitation in parallel of
An ideal parallel system must have two properties (DeWitt/Gray
N times more resources make it possible to treat a
given task in N times less than the reference system
(typical case of DSS)
N times more resources make it possible to deal with
an N times larger problem in the same time as the
reference system (typical case of OLTP: updating a N
times larger database by a N times larger number of
Design of Parallel Systems (Cont.)
• On-line reorganization of data and schema changes
must be supported.
– For example, index construction on terabyte databases
can take hours or days even on a parallel system.
• Need to allow other processing (insertions/deletions/updates) to
be performed on relation even as index is being constructed.
– Basic idea: index construction tracks changes and ``catches
up'‘ on changes at the end.
• Also need support for on-line repartitioning and
schema changes (executed concurrently with other
Parallelism in Databases
• Data can be partitioned across multiple disks for parallel
• Individual relational operations (e.g., sort, join,
aggregation) can be executed in parallel
– data can be partitioned and each processor can work
independently on its own partition.
• Queries are expressed in high level language (SQL,
translated to relational algebra)
– makes parallelization easier.
• Different queries can be run in parallel with each other.
Concurrency control takes care of conflicts.
• Thus, databases naturally lend themselves to parallelism.
• Reduce the time required to retrieve relations from disk by
• the relations on multiple disks.
• Horizontal partitioning – tuples of a relation are divided
among many disks such that each tuple resides on one disk.
• Partitioning techniques (number of disks = n):
Send the ith tuple inserted in the relation to disk i mod n.
– Choose one or more attributes as the partitioning attributes.
– Choose hash function h with range 0…n - 1
– Let i denote result of hash function h applied to the
partitioning attribute value of a tuple. Send tuple to disk i.
I/O Parallelism (Cont.)
• Partitioning techniques (cont.):
• Range partitioning:
– Choose an attribute as the partitioning attribute.
– A partitioning vector [vo, v1, ..., vn-2] is chosen.
– Let v be the partitioning attribute value of a tuple.
Tuples such that vi ≤ vi+1 go to disk I + 1. Tuples with v <
v0 go to disk 0 and tuples with v ≥ vn-2 go to disk n-1.
E.g., with a partitioning vector [5,11], a tuple with
partitioning attribute value of 2 will go to disk 0, a
tuple with value 8 will go to disk 1, while a tuple with
value 20 will go to disk2.
Comparison of Partitioning Techniques
• Evaluate how well partitioning techniques
support the following types of data access:
1.Scanning the entire relation.
2.Locating a tuple associatively – point queries.
– E.g., r.A = 25.
3.Locating all tuples such that the value of a
given attribute lies within a specified range –
– E.g., 10 ≤ r.A < 25.
Comparison of Partitioning Techniques
– Best suited for sequential scan of entire relation on
– All disks have almost an equal number of tuples;
retrieval work is thus well balanced between disks.
• Range queries are difficult to process
– No clustering -- tuples are scattered across all disks
Comparison of Partitioning
• Good for sequential access
– Assuming hash function is good, and partitioning
attributes form a key, tuples will be equally distributed
– Retrieval work is then well balanced between disks.
• Good for point queries on partitioning attribute
– Can lookup single disk, leaving others available for
answering other queries.
– Index on partitioning attribute can be local to disk, making
lookup and update more efficient
• No clustering, so difficult to answer range queries
Comparison of Partitioning Techniques
Provides data clustering by partitioning attribute value.
Good for sequential access
Good for point queries on partitioning attribute: only one
disk needs to be accessed.
• For range queries on partitioning attribute, one to a few
disks may need to be accessed
– Remaining disks are available for other queries.
– Good if result tuples are from one to a few blocks.
– If many blocks are to be fetched, they are still fetched from one
to a few disks, and potential parallelism in disk access is wasted
• Example of execution skew.
Partitioning a Relation across Disks
• If a relation contains only a few tuples which
will fit into a single disk block, then assign the
relation to a single disk.
• Large relations are preferably partitioned
across all the available disks.
• If a relation consists of m disk blocks and there
are n disks available in the system, then the
relation should be allocated min(m,n) disks.
Handling of Skew
• The distribution of tuples to disks may be skewed —
that is, some disks have many tuples, while others may
have fewer tuples.
• Types of skew:
– Attribute-value skew.
• Some values appear in the partitioning attributes of many tuples;
all the tuples with the same value for the partitioning attribute
end up in the same partition.
• Can occur with range-partitioning and hash-partitioning.
– Partition skew.
• With range-partitioning, badly chosen partition vector may assign
too many tuples to some partitions and too few to others.
• Less likely with hash-partitioning if a good hash-function is chosen.
Handling Skew in Range-Partitioning
• To create a balanced partitioning vector (assuming
partitioning attribute forms a key of the relation):
– Sort the relation on the partitioning attribute.
– Construct the partition vector by scanning the relation in
sorted order as follows.
• After every 1/nth of the relation has been read, the value of the
partitioning attribute of the next tuple is added to the partition
– n denotes the number of partitions to be constructed.
– Duplicate entries or imbalances can result if duplicates are
present in partitioning attributes.
• Alternative technique based on histograms used in
Handling Skew using Histograms
Balanced partitioning vector can be constructed from histogram in a relatively
Assume uniform distribution within each range of the histogram
Histogram can be constructed by scanning relation, or sampling (blocks containing)
tuples of the relation
Handling Skew Using Virtual Processor
• Skew in range partitioning can be handled elegantly
using virtual processor partitioning:
– create a large number of partitions (say 10 to 20 times the
number of processors)
– Assign virtual processors to partitions either in roundrobin fashion or based on estimated cost of processing
each virtual partition
• Basic idea:
– If any normal partition would have been skewed, it is very
likely the skew is spread over a number of virtual
– Skewed virtual partitions get spread across a number of
processors, so work gets distributed evenly!
• Queries/transactions execute in parallel with one another.
• Increases transaction throughput; used primarily to scale up a
transaction processing system to support a larger number of
transactions per second.
• Easiest form of parallelism to support, particularly in a sharedmemory parallel database, because even sequential database
systems support concurrent processing.
• More complicated to implement on shared-disk or shared-nothing
– Locking and logging must be coordinated by passing messages
– Data in a local buffer may have been updated at another processor.
– Cache-coherency has to be maintained — reads and writes of data in
buffer must find latest version of data.
Cache Coherency Protocol
• Example of a cache coherency protocol for shared disk
– Before reading/writing to a page, the page must be locked in
– On locking a page, the page must be read from disk
– Before unlocking a page, the page must be written to disk if it
• More complex protocols with fewer disk reads/writes exist.
• Cache coherency protocols for shared-nothing systems are
similar. Each database page is assigned a home processor.
Requests to fetch the page or write it to disk are sent to the
• Execution of a single query in parallel on multiple
processors/disks; important for speeding up long-running
• Two complementary forms of intraquery parallelism :
– Intraoperation Parallelism – parallelize the execution of each
individual operation in the query.
– Interoperation Parallelism – execute the different operations in
a query expression in parallel.
the first form scales better with increasing parallelism
the number of tuples processed by each operation is
typically more than the number of operations in a query
Parallel Processing of Relational
• Our discussion of parallel algorithms assumes:
– read-only queries
– shared-nothing architecture
– n processors, P0, ..., Pn-1, and n disks D0, ..., Dn-1, where disk Di
is associated with processor Pi.
• If a processor has multiple disks they can simply
simulate a single disk Di.
• Shared-nothing architectures can be efficiently
simulated on shared-memory and shared-disk systems.
– Algorithms for shared-nothing systems can thus be run on
shared-memory and shared-disk systems.
– However, some optimizations may be possible.
• Choose processors P0, ..., Pm, where m ≤ n -1 to do sorting.
• Create range-partition vector with m entries, on the sorting attributes
• Redistribute the relation using range partitioning
– all tuples that lie in the ith range are sent to processor Pi
– Pi stores the tuples it received temporarily on disk Di.
– This step requires I/O and communication overhead.
Each processor Pi sorts its partition of the relation locally.
Each processors executes same operation (sort) in parallel with other
processors, without any interaction with the others (data parallelism).
Final merge operation is trivial: range-partitioning ensures that, for 1 j m,
the key values in processor Pi are all less than the key values in Pj.
Parallel Sort (Cont.)
Parallel External Sort-Merge
• Assume the relation has already been partitioned among
disks D0, ..., Dn-1 (in whatever manner).
• Each processor Pi locally sorts the data on disk Di.
• The sorted runs on each processor are then merged to get
the final sorted output.
• Parallelize the merging of sorted runs as follows:
– The sorted partitions at each processor Pi are range-partitioned
across the processors P0, ..., Pm-1.
– Each processor Pi performs a merge on the streams as they are
received, to get a single sorted run.
– The sorted runs on processors P0,..., Pm-1 are concatenated to get
the final result.
• The join operation requires pairs of tuples to be
tested to see if they satisfy the join condition,
and if they do, the pair is added to the join
• Parallel join algorithms attempt to split the pairs
to be tested over several processors. Each
processor then computes part of the join locally.
• In a final step, the results from each processor
can be collected together to produce the final
• For equi-joins and natural joins, it is possible to partition the two
input relations across the processors, and compute the join locally
at each processor.
• Let r and s be the input relations, and we want to compute r r.A=s.B s.
• r and s each are partitioned into n partitions, denoted r0, r1, ..., rn-1
and s0, s1, ..., sn-1.
• Can use either range partitioning or hash partitioning.
• r and s must be partitioned on their join attributes r.A and s.B),
using the same range-partitioning vector or hash function.
• Partitions ri and si are sent to processor Pi,
• Each processor Pi locally computes ri ri.A=si.B si. Any of the standard
join methods can be used.
• Partitioning not possible for some join conditions
– e.g., non-equijoin conditions, such as r.A > s.B.
• For joins were partitioning is not applicable,
parallelization can be accomplished by fragment and
– Depicted on next slide
• Special case – asymmetric fragment-and-replicate:
– One of the relations, say r, is partitioned; any partitioning
technique can be used.
– The other relation, s, is replicated across all the
– Processor Pi then locally computes the join of ri with all of s
using any join technique.
Fragment-and-Replicate Join (Cont.)
• General case: reduces the sizes of the relations at each
– r is partitioned into n partitions,r0, r1, ..., r n-1;s is partitioned
into m partitions, s0, s1, ..., sm-1.
– Any partitioning technique may be used.
– There must be at least m * n processors.
– Label the processors as
– P0,0, P0,1, ..., P0,m-1, P1,0, ..., Pn-1m-1.
– Pi,j computes the join of ri with sj. In order to do so, ri is
replicated to Pi,0, Pi,1, ..., Pi,m-1, while si is replicated to P0,i,
P1,i, ..., Pn-1,i
– Any join technique can be used at each processor Pi,j.
Fragment-and-Replicate Join (Cont.)
• Both versions of fragment-and-replicate work with any
join condition, since every tuple in r can be tested with
every tuple in s.
• Usually has a higher cost than partitioning, since one of
the relations (for asymmetric fragment-and-replicate)
or both relations (for general fragment-and-replicate)
have to be replicated.
• Sometimes asymmetric fragment-and-replicate is
preferable even though partitioning could be used.
– E.g., say s is small and r is large, and already partitioned. It
may be cheaper to replicate s across all processors, rather
than repartition r and s on the join attributes.
Partitioned Parallel Hash-Join
Parallelizing partitioned hash join:
• Assume s is smaller than r and therefore s is chosen as the
• A hash function h1 takes the join attribute value of each
tuple in s and maps this tuple to one of the n processors.
• Each processor Pi reads the tuples of s that are on its disk
Di, and sends each tuple to the appropriate processor
based on hash function h1. Let si denote the tuples of
relation s that are sent to processor Pi.
• As tuples of relation s are received at the destination
processors, they are partitioned further using another hash
function, h2, which is used to compute the hash-join locally.
Partitioned Parallel Hash-Join (Cont.)
• Once the tuples of s have been distributed, the larger relation r is
redistributed across the m processors using the hash function h1
Let ri denote the tuples of relation r that are sent to processor Pi.
• As the r tuples are received at the destination processors, they are
repartitioned using the function h2
– (just as the probe relation is partitioned in the sequential hash-join
• Each processor Pi executes the build and probe phases of the hashjoin algorithm on the local partitions ri and s of r and s to produce a
partition of the final result of the hash-join.
• Note: Hash-join optimizations can be applied to the parallel case
– e.g., the hybrid hash-join algorithm can be used to cache some of the
incoming tuples in memory and avoid the cost of writing them and
reading them back in.
Parallel Nested-Loop Join
• Assume that
– relation s is much smaller than relation r and that r is stored by
– there is an index on a join attribute of relation r at each of the
partitions of relation r.
• Use asymmetric fragment-and-replicate, with relation s being
replicated, and using the existing partitioning of relation r.
• Each processor Pj where a partition of relation s is stored reads the
tuples of relation s stored in Dj, and replicates the tuples to every
other processor Pi.
– At the end of this phase, relation s is replicated at all sites that store
tuples of relation r.
• Each processor Pi performs an indexed nested-loop join of relation s
with the ith partition of relation r.
• Partition the relation on the grouping attributes and then
compute the aggregate values locally at each processor.
• Can reduce cost of transferring tuples during partitioning
by partly computing aggregate values before partitioning.
• Consider the sum aggregation operation:
– Perform aggregation operation at each processor Pi on those
tuples stored on disk Di
• results in tuples with partial sums at each processor.
– Result of the local aggregation is partitioned on the grouping
attributes, and the aggregation performed again at each
processor Pi to get the final result.
• Fewer tuples need to be sent to other processors during
Cost of Parallel Evaluation of
• If there is no skew in the partitioning, and there is no
overhead due to the parallel evaluation, expected
speed-up will be 1/n
• If skew and overheads are also to be taken into
account, the time taken by a parallel operation can be
Tpart + Tasm + max (T0, T1, …, Tn-1)
– Tpart is the time for partitioning the relations
– Tasm is the time for assembling the results
– Ti is the time taken for the operation at processor Pi
• this needs to be estimated taking into account the skew, and the
time wasted in contentions.
• Pipelined parallelism
– Consider a join of four relations
– Set up a pipeline that computes the three joins in parallel
• Let P1 be assigned the computation of
temp1 = r1 r2
• And P2 be assigned the computation of temp2 = temp1
• And P3 be assigned the computation of temp2 r 4
– Each of these operations can execute in parallel, sending
result tuples it computes to the next operation even as it is
computing further results
• Provided a pipelineable join evaluation algorithm (e.g. indexed
nested loops join) is used
Factors Limiting Utility of Pipeline
• Pipeline parallelism is useful since it avoids writing
intermediate results to disk
• Useful with small number of processors, but does not
scale up well with more processors. One reason is that
pipeline chains do not attain sufficient length.
• Cannot pipeline operators which do not produce
output until all inputs have been accessed (e.g.
aggregate and sort)
• Little speedup is obtained for the frequent cases of
skew in which
one operator's execution cost is
much higher than the others.
• Independent parallelism
– Consider a join of four relations
r1 r2 r3 r4
Let P1 be assigned the computation of
temp1 = r1 r2
And P2 be assigned the computation of temp2 = r3 r4
And P3 be assigned the computation of temp1 temp2
P1 and P2 can work independently in parallel
P3 has to wait for input from P1 and P2
– Can pipeline output of P1 and P2 to P3, combining independent
parallelism and pipelined parallelism
– Does not provide a high degree of parallelism
• useful with a lower degree of parallelism.
• less useful in a highly parallel system,
• A distributed database system consists of
loosely coupled sites that share no physical
• Database systems that run on each site are
independent of each other
• Transactions may access data at one or more
Homogeneous Distributed Databases
• In a homogeneous distributed database
– All sites have identical software
– Are aware of each other and agree to cooperate in
processing user requests.
– Each site surrenders part of its autonomy in terms of right
to change schemas or software
– Appears to user as a single system
• In a heterogeneous distributed database
– Different sites may use different schemas and software
• Difference in schema is a major problem for query processing
• Difference in softwrae is a major problem for transaction
– Sites may not be aware of each other and may provide
limited facilities for cooperation in transaction processing
Distributed Data Storage
• Assume relational data model
– System maintains multiple copies of data, stored in
different sites, for faster retrieval and fault tolerance.
– Relation is partitioned into several fragments stored
in distinct sites
• Replication and fragmentation can be combined
– Relation is partitioned into several fragments: system
maintains several identical replicas of each such
• A relation or fragment of a relation is
replicated if it is stored redundantly in two or
• 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
Data Replication (Cont.)
• Advantages of Replication
– Availability: failure of site containing relation r does not result
in unavailability of r is replicas exist.
– Parallelism: queries on r may be processed by several nodes in
– 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
– 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 primary copy and apply
concurrency control operations on primary copy
• 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,
Advantages of Fragmentation
– 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
– 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
• 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
– 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.
System Failure Modes
• Failures unique to distributed systems:
– Failure of a site.
– Loss of massages
• Handled by network transmission control protocols such as TCP-IP
– Failure of a communication link
• Handled by network protocols, by routing messages via alternative
– Network partition
• A network is said to be partitioned when it has been split into two
or more subsystems that lack any connection between them
– Note: a subsystem may consist of a single node
• Network partitioning and site failures are generally
• Commit protocols are used to ensure atomicity across
– 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
– Ci adds the records <prepare T> to the log and forces log to
– 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
– 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
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
2. If an active site contains an <abort T> record in its log, then T must be
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
• 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
– 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
• 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):
– 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
– Avoids blocking problem as long as < K sites fail
– higher overheads
– assumptions may not be satisfied in practice
Won’t study it further
Alternative Models of Transaction
• Notion of a single transaction spanning multiple sites is
inappropriate for many applications
– E.g. transaction crossing an organizational boundary
– No organization would like to permit an externally initiated
transaction to block local transactions for an indeterminate
• Alternative models carry out transactions by sending
– Code to handle messages must be carefully designed to ensure
atomicity and durability properties for updates
• Isolation cannot be guaranteed, in that intermediate stages are
visible, but code must ensure no inconsistent states result due to
– Persistent messaging systems are systems that provide
transactional properties to messages
• Messages are guaranteed to be delivered exactly once
• Will discuss implementation techniques later
• Modify concurrency control schemes for use
in distributed environment.
• We assume that each site participates in the
execution of a commit protocol to ensure
global transaction automicity.
• We assume all replicas of any item are
– Will see how to relax this in case of site failures
• System maintains a single lock manager that
resides in a single chosen site, say Si
• When a transaction needs to lock a data item, it
sends a lock request to Si and lock manager
determines whether the lock can be granted
– If yes, lock manager sends a message to the site which
initiated the request
– If no, request is delayed until it can be granted, at
which time a message is sent to the initiating site
Single-Lock-Manager Approach (Cont.)
• The transaction can read the data item from any one
of the sites at which a replica of the data item resides.
• Writes must be performed on all replicas of a data
• Advantages of scheme:
– Simple implementation
– Simple deadlock handling
• Disadvantages of scheme are:
– Bottleneck: lock manager site becomes a bottleneck
– Vulnerability: system is vulnerable to lock manager site
Distributed Lock Manager
• In this approach, functionality of locking is implemented by
lock managers at each site
– Lock managers control access to local data items
• But special protocols may be used for replicas
• Advantage: work is distributed and can be made robust to
• Disadvantage: deadlock detection is more complicated
– Lock managers cooperate for deadlock detection
• More on this later
• Several variants of this approach
• Choose one replica of data item to be the primary copy.
– Site containing the replica is called the primary site for that
– Different data items can have different primary sites
• When a transaction needs to lock a data item Q, it requests
a lock at the primary site of Q.
– Implicitly gets lock on all replicas of the data item
– Concurrency control for replicated data handled similarly to
unreplicated data - simple implementation.
– If the primary site of Q fails, Q is inaccessible even though other
sites containing a replica may be accessible.
• Local lock manager at each site administers lock
and unlock requests for data items stored at that
• When a transaction wishes to lock an
unreplicated data item Q residing at site Si, a
message is sent to Si ‘s lock manager.
– If Q is locked in an incompatible mode, then the
request is delayed until it can be granted.
– When the lock request can be granted, the lock
manager sends a message back to the initiator
indicating that the lock request has been granted.
Majority Protocol (Cont.)
• In case of replicated data
– If Q is replicated at n sites, then a lock request message must be
sent to more than half of the n sites in which Q is stored.
– The transaction does not operate on Q until it has obtained a
lock on a majority of the replicas of Q.
– When writing the data item, transaction performs writes on all
– Can be used even when some sites are unavailable
• details on how handle writes in the presence of site failure later
– Requires 2(n/2 + 1) messages for handling lock requests, and
(n/2 + 1) messages for handling unlock requests.
– Potential for deadlock even with single item - e.g., each of 3
transactions may have locks on 1/3rd of the replicas of a data.
• Local lock manager at each site as in majority protocol,
however, requests for shared locks are handled
differently than requests for exclusive locks.
• Shared locks. When a transaction needs to lock data
item Q, it simply requests a lock on Q from the lock
manager at one site containing a replica of Q.
• Exclusive locks. When transaction needs to lock data
item Q, it requests a lock on Q from the lock manager
at all sites containing a replica of Q.
• Advantage - imposes less overhead on read
• Disadvantage - additional overhead on writes
Quorum Consensus Protocol
• A generalization of both majority and biased protocols
• Each site is assigned a weight.
– Let S be the total of all site weights
• Choose two values read quorum Qr and write quorum Qw
– Such that Qr + Qw > S and 2 * Qw > S
– Quorums can be chosen (and S computed) separately for each
• Each read must lock enough replicas that the sum of the
site weights is >= Qr
• Each write must lock enough replicas that the sum of the
site weights is >= Qw
• For now we assume all replicas are written
– Extensions to allow some sites to be unavailable described later
Consider the following two transactions and history, with item X
and transaction T1 at site 1, and item Y and transaction T2 at
X-lock on X
X-lock on Y
wait for X-lock on X
Wait for X-lock on Y
Result: deadlock which cannot be detected locally at either site
• A global wait-for graph is constructed and maintained in a
single site; the deadlock-detection coordinator
– Real graph: Real, but unknown, state of the system.
– Constructed graph:Approximation generated by the controller
during the execution of its algorithm .
• the global wait-for graph can be constructed when:
– a new edge is inserted in or removed from one of the local
– a number of changes have occurred in a local wait-for graph.
– the coordinator needs to invoke cycle-detection.
• If the coordinator finds a cycle, it selects a victim and
notifies all sites. The sites roll back the victim transaction.
False Cycles (Cont.)
• Suppose that starting from the state shown in figure,
1. T2 releases resources at S1
• resulting in a message remove T1 → T2 message from the
Transaction Manager at site S1 to the coordinator)
2. And then T2 requests a resource held by T3 at site S2
• resulting in a message insert T2 → T3 from S2 to the coordinator
• Suppose further that the insert message reaches
before the delete message
– this can happen due to network delays
• The coordinator would then find a false cycle
T1 → T2 → T3 → T1
• The false cycle above never existed in reality.
• False cycles cannot occur if two-phase locking is used.
• Unnecessary rollbacks may result when
deadlock has indeed occurred and a victim has
been picked, and meanwhile one of the
transactions was aborted for reasons
unrelated to the deadlock.
• Unnecessary rollbacks can result from false
cycles in the global wait-for graph; however,
likelihood of false cycles is low.
• Timestamp based concurrency-control protocols
can be used in distributed systems
• Each transaction must be given a unique
• Main problem: how to generate a timestamp in
a distributed fashion
– Each site generates a unique local timestamp using
either a logical counter or the local clock.
– Global unique timestamp is obtained by
concatenating the unique local timestamp with the
• A site with a slow clock will assign smaller timestamps
– Still logically correct: serializability not affected
– But: “disadvantages” transactions
• To fix this problem
– Define within each site Si a logical clock (LCi), which
generates the unique local timestamp
– Require that Si advance its logical clock whenever a request
is received from a transaction Ti with timestamp < x,y>
and x is greater that the current value of LCi.
– In this case, site Si advances its logical clock to the value x +
Replication with Weak Consistency
• Many commercial databases support replication of data
with weak degrees of consistency (I.e., without a guarantee
• E.g.: master-slave replication: updates are performed at a
single “master” site, and propagated to “slave” sites.
– Propagation is not part of the update transaction: its is
• May be immediately after transaction commits
• May be periodic
– Data may only be read at slave sites, not updated
• No need to obtain locks at any remote site
– Particularly useful for distributing information
• E.g. from central office to branch-office
– Also useful for running read-only queries offline from the main
Replication with Weak Consistency (Cont.)
• Replicas should see a transaction-consistent
snapshot of the database
– That is, a state of the database reflecting all effects of
all transactions up to some point in the serialization
order, and no effects of any later transactions.
• E.g. Oracle provides a create snapshot
statement to create a snapshot of a relation or a
set of relations at a remote site
– snapshot refresh either by recomputation or by
– Automatic refresh (continuous or periodic) or manual
• With multimaster replication (also called updateanywhere replication) updates are permitted at any
replica, and are automatically propagated to all
– Basic model in distributed databases, where transactions
are unaware of the details of replication, and database
system propagates updates as part of the same
• Coupled with 2 phase commit
– Many systems support lazy propagation where updates
are transmitted after transaction commits
• Allow updates to occur even if some sites are disconnected from
the network, but at the cost of consistency
Lazy Propagation (Cont.)
Two approaches to lazy propagation
– Updates at any replica translated into update at primary site, and then propagated back
to all replicas
• Updates to an item are ordered serially
• But transactions may read an old value of an item and use it to perform an update,
result in non-serializability
– Updates are performed at any replica and propagated to all other replicas
• Causes even more serialization problems:
– Same data item may be updated concurrently at multiple sites!
Conflict detection is a problem
– Some conflicts due to lack of distributed concurrency control can be detected when
updates are propagated to other sites (will see later, in Section 23.5.4)
Conflict resolution is very messy
– Resolution may require committed transactions to be rolled back
• Durability violated
– Automatic resolution may not be possible, and human intervention
may be required
Distributed Query Processing
• For centralized systems, the primary criterion
for measuring the cost of a particular strategy
is the number of disk accesses.
• In a distributed system, other issues must be
taken into account:
– The cost of a data transmission over the network.
– The potential gain in performance from having
several sites process parts of the query in parallel
• Translating algebraic queries on fragments.
– It must be possible to construct relation r from its fragments
– Replace relation r by the expression to construct relation r from
• Consider the horizontal fragmentation of the account
account1 = σ branch-name = “Hillside” (account)
account2 = σ branch-name = “Valleyview” (account)
• The query σ branch-name = “Hillside” (account) becomes
σ branch-name = “Hillside” (account1 ∪ account2)
which is optimized into
σ branch-name = “Hillside” (account1) ∪ σ branch-name = “Hillside” (account2)
Example Query (Cont.)
• Since account1 has only tuples pertaining to
the Hillside branch, we can eliminate the
• Apply the definition of account2 to obtain
σ branch-name = “Hillside” (σ branch-name = “Valleyview” (account)
• This expression is the empty set regardless of
the contents of the account relation.
• Final strategy is for the Hillside site to return
account1 as the result of the query.
Simple Join Processing
• Consider the following relational algebra
expression in which the three relations are
neither replicated nor fragmented
account depositor branch
• account is stored at site S1
• depositor at S2
• branch at S3
• For a query issued at site SI, the system needs to
produce the result at site SI
Possible Query Processing Strategies
• Ship copies of all three relations to site SI and choose a
strategy for processing the entire locally at site SI.
• Ship a copy of the account relation to site S2 and
compute temp1 = account
depositor at S2. Ship
temp1 from S2 to S3, and compute temp2 = temp1 branch
at S3. Ship the result temp2 to SI.
• Devise similar strategies, exchanging the roles S1, S2, S3
• Must consider following factors:
– amount of data being shipped
– cost of transmitting a data block between sites
– relative processing speed at each site
• Let r1 be a relation with schema R1 stores at site S1
Let r2 be a relation with schema R2 stores at site S2
• Evaluate the expression r1 r2 and obtain the result at S1.
1. Compute temp1 ← ∏R1 ∩ R2 (r1) at S1.
• 2. Ship temp1 from S1 to S2.
• 3. Compute temp2 ← r2 temp1 at S2
• 4. Ship temp2 from S2 to S1.
• 5. Compute r1 temp2 at S1. This is the same as r1 r2.
• The semijoin of r1 with r2, is denoted by:
• it is defined by:
• ∏R1 (r1 r2)
• Thus, r1 r2 selects those tuples of r1 that
contributed to r1 r2.
• In step 3 above, temp2=r2 r1.
• For joins of several relations, the above strategy
can be extended to a series of semijoin steps.