Module 2
Module 2
MODULE 2
Prepared By:
Madhuri J
Assistant Professor
Department of Computer Science and Engineering
Bangalore Institute of Technology
2
Distribution Models
• Depending on the distribution model the data store can
give us the ability:
• To handle large quantity of data,
• To process a greater read or write traffic
• To have more availability in the case of network slowdowns of
breakages
• There are two path for distribution: –
•Replication
•Sharding
3
Sharding
• Often, a data store is busy because different people are
accessing different part of the dataset.
• In this cases we can support horizontal scalability by
putting different part of the data onto different servers
(Sharding)
5
Sharding
• Sharding can be part of application logic.
• This complicates the programming model as the
application code needs ensure that queries are distributed
across various shards.
• In the ideal setting we have each user to talk one server
and the load is balanced.
6
7
Sharding Approaches
• In order to get the ideal case we have to guarantee that
data accessed together are stored in the same node.
• – This is very simple using aggregates.
• When considering data distribution across nodes.
• – If access is based on physical location, we can place data close
to where are accessed.
• Keep data balanced. We should arrange aggregates so
they are evenly distributed in order that each node
receive the same amount of the load.
• Its useful to put aggregate together if we think they may
be read in sequence (BigTable).
8
Master-Slave Replication
• In this setting one node is designated as the master, or
primary and the other as slaves.
• The master is the authoritative source for the data
• and designed to process updates and send them to
slaves.
• The slaves are used for read operations.
• This allows us to scale read intensive dataset.
• We can scale horizontally by adding more slaves to
handle more read requests.
• Limitation is the ability of the master in processing
incoming data.
12
Master-Slave Replication
• Read resilience.
• If the master fails the slaves can still handle read requests.
• Writes are not allowed until the master is not restored.
• To achieve resilience we need to ensure that read and
write paths are different.
• Masters can be appointed manually or automatically.
• Automatic- when cluster is configured, one node is chosen as the
master.
• Manual- Cluster of nodes created, master is chosen automatically.
• Slave can be appointed as master to replace failed
master.
13
Master-Slave Replication
• Replication in master-slave have the analyzed
advantages but it come with the problem of inconsistency.
• The readers reading from the slaves can read data not
updated.
• When master fails, any update not passed is lost.
14
Peer-to-Peer Replication
15
Peer-to-Peer Replication
• Master-Slave replication helps with read scalability but
has problems on scalability of writes. It provides resilience
on read but not on writes. The master is still a single point
of failure.
• All the replica are equal (accept writes and reads)
• With a Peer-to-Peer we can have node failures without
lose write capability and losing data.
• Can easily add nodes for performances.
• When we can write on different nodes, we increase the
probability to have inconsistency on writes.
16
Consistency
• Write-Write conflict occur when two clients try to write the
same data at the same time
• Server serializes them
• Can lead to lost update
• Read-write conflicts occur when one client reads
inconsistent data in the middle of another client write
• To get good consistency, more nodes have to be involved
in data operations but this increases latency. So there is a
trade off between consistency and latency.
22
Read Consistency
Read-Write Conflict.
Logical Consistency ensures that different data
items make sense together.
25
Replication Inconsistancy
26
Read Consistency
• Replication Consistency- Ensures same data has same
value when read from different replicas.
• Eventual consistency- Nodes may have replication
inconsistency, but if there are no further updates, all
nodes will be updated to the same value.
• Techniques to provide session consistency
• Sticky sessions (session affinity) – assign a session to a
given database node for all of its work to ensure read-your-
writes consistency.
• Version stamps
27
Relaxed consistency
• CAP Theorem – pick two of these three
• Consistency
• Availability – ability to read and write data to a node in the
cluster
• Partition tolerance – cluster can survive network
breakage that separates it into multiple isolated partitions
• If there is a network partition, need to trade off availability
of data vs. consistency
• Depending on the domain, it can be beneficial to balance
consistency with latency (performance)
• BASE – Basically Available, Soft state, Eventual
consistency
28
CAP Theorem
All client always have the
same view of the data
Availability
Consistency
Partition
tolerance
29
CAP Theorem
Each client always can
read and write.
Availability
Consistency
Partition
tolerance
30
CAP Theorem
A system can continue to
operate in the presence of
a network partitions
Availability
Consistency
Partition
tolerance
31
CAP theorem
32
Relaxed durability
Quorums
• The more nodes you involve in a request, the higher is the chance of
avoiding an inconsistency.
• How many nodes need to be involved to get strong consistency?
• All the nodes need not acknowledge a write to ensure strong consistency
• Write quorum are expressed as W > N/2, meaning the number of nodes
participating in the write (W) must be more than the half the number of nodes
involved in replication (N).
• How many nodes you need to contact to be sure you have the most
up-to-date change.
• Read quorum is dependent on how many nodes need to confirm a write.
• This relationship between the number of nodes you need to contact
for a read (R), those confirming a write (W), and the replication
factor (N) can be captured in an inequality: You can have a strongly
consistent read if R + W > N.
34
Version Stamps
• Provide a means of detecting concurrency conflicts
• Each data item has a version stamp which gets incremented each
time the item is updated
• Before updating a data item, a process can check its version
stamp to see if it has been updated since it was last read
• Implementation methods
• Counter – requires a single master to “own” the counter
• GUID (Guaranteed Unique ID) – can be computed by any node,
but are large and cannot be compared directly
• Hash the contents of a resource
• Timestamp of last update – node clocks must be synchronized
• Vector stamp – set of version stamps for all nodes in a
distributed system
• Allows detection of conflicting updates on different nodes
36
Vector stamp
• Vector stamp is a set of counters, one for each node. A vector
stamp for three nodes (blue, green, black) would look
something like [blue: 43, green: 54, black: 12].
• Each time a node has an internal update, it updates its own
counter, so an update in the green node would change the vector
to [blue: 43, green: 55, black: 12].
• Vector clocks and version vectors are the other terms used.
• By using this scheme you can tell if one version stamp is newer
than another
40
Vector stamp
• [blue: 1,green: 2, black: 5] is newer than [blue:1, green: 1, black
5] since one of its counters is greater.
• If both stamps have a counter greater than the other, e.g. [blue: 1,
green: 2, black: 5] and [blue: 2, green: 1, black: 5], then you have
a write-write conflict.