Top Banner
Databases Sargun Dhillon @Sargun
159
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Why Distributed Databases?

DatabasesSargun Dhillon

@Sargun

Page 2: Why Distributed Databases?

What is a database? A database is an organized collection of data

Page 3: Why Distributed Databases?

ApplicationsWhat are databases for?

Page 4: Why Distributed Databases?

Internet ApplicationsExperiencing exploding growth

Page 5: Why Distributed Databases?

Internet Traffic vs. Penetration

0

25

50

75

100

0

10000

20000

30000

40000

2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012

IP Traffic (PB/mo) Global Penetration (%)

Page 6: Why Distributed Databases?

Number of Internet Users in 2012

Page 7: Why Distributed Databases?

Average Distance to Every Human

Page 8: Why Distributed Databases?

ExtrapolatingWe have not yet Peak “Web” and we wont see it for

some time

Page 9: Why Distributed Databases?

ApplicationsHow are they built?

Page 10: Why Distributed Databases?

Basic Application

Page 11: Why Distributed Databases?

Useful ApplicationAdd Persistence

Page 12: Why Distributed Databases?

Scale Out

Page 13: Why Distributed Databases?

Scale Out with Correctness

Page 14: Why Distributed Databases?

What is a Transaction?A Unit of Work

Page 15: Why Distributed Databases?

Transaction SchedulingConcurrent Operations

Page 16: Why Distributed Databases?

Non-Conflicting ConcurrencyParallel Execution

Page 17: Why Distributed Databases?

ACID

Page 18: Why Distributed Databases?

ACID = AtomicityA transaction executes or it does not

Page 19: Why Distributed Databases?

ACID = ConsistencyCorrectness; Require the database to follow set of

invariants

Page 20: Why Distributed Databases?

ACID = IsolationPrevent inter-actor visibility during concurrent operations

Page 21: Why Distributed Databases?

ACID = DurabilityOnce you write, it will survive

Page 22: Why Distributed Databases?

Lifecycle of a Transaction

Page 23: Why Distributed Databases?

Vertically ScalabilityMoore’s Law can take us places

Page 24: Why Distributed Databases?

Biggest AWS Database• vCPUs: 32

• Memory: 244

• Storage: 3TB

• IOPs: 30,000 IOPs

• Networking: 10 Gigabit

• Resiliency: Multi-AZ

• SLA: 99.95%

• Backend: Postgresql

Page 25: Why Distributed Databases?

$141,052.66/yr

Page 26: Why Distributed Databases?

Scaling Beyond

Page 27: Why Distributed Databases?

Sharding?

Page 28: Why Distributed Databases?
Page 29: Why Distributed Databases?

Do we have a natural sharding key?

Page 30: Why Distributed Databases?

Add a Coordinator?

Page 31: Why Distributed Databases?

Two-phase commit?

Three-phase commit?

Paxos?

Enhanced Three-phase commit?

Wat?

Egalitarian Paxos?

Page 32: Why Distributed Databases?

Do we really want to run NxM databases?

Page 33: Why Distributed Databases?

Partial Availability

Page 34: Why Distributed Databases?

Failure detectors are hard

Page 35: Why Distributed Databases?

Database Failure

Page 36: Why Distributed Databases?

Cascading App Failure

Page 37: Why Distributed Databases?

Recovery

Page 38: Why Distributed Databases?

Hotspots? (The “Beiber” problem)

Page 39: Why Distributed Databases?

Scaling SSI databases is a hard problem

Page 40: Why Distributed Databases?

What if want multidatacenter?

Page 41: Why Distributed Databases?
Page 42: Why Distributed Databases?
Page 43: Why Distributed Databases?

No latency win for mutable data

Page 44: Why Distributed Databases?

Must sacrifice recency for latency win

Page 45: Why Distributed Databases?

Complex Routing Semantics

Page 46: Why Distributed Databases?

Multi-master requires at least 1 RTT

Page 47: Why Distributed Databases?

80ms+ writes!

Page 48: Why Distributed Databases?

“Average partition duration ranged from 6 minutes for software-related failures to more than 8.2 hours for

hardware-related failures (median 2.7 and 32 minutes; 95th percentile of 19.9 minutes and 3.7 days,

respectively).” -The Network is Reliable

WANs Fail

Page 49: Why Distributed Databases?

Is there another way?

Page 50: Why Distributed Databases?

Into Riak

Page 51: Why Distributed Databases?

Design Requirements

Page 52: Why Distributed Databases?

Incremental ScalabilityMust be able to add nodes for greater reliability, or

throughput

Page 53: Why Distributed Databases?

High AvailabilityMust be able to seamlessly handle failures, and always

respond to operations

Page 54: Why Distributed Databases?

EfficiencyMeet stringent latency requirements

Page 55: Why Distributed Databases?

Implementation

Page 56: Why Distributed Databases?

“Experience at Amazon has shown that data stores that provide

ACID guarantees tend to have poor availability.”

Dynamo: Amazon’s Highly Available Key-value Store

Page 57: Why Distributed Databases?

The RingA cluster composed of set of virtual nodes (vnodes)

Page 58: Why Distributed Databases?

The Ring

Page 59: Why Distributed Databases?

Virtual Node Placement

Page 60: Why Distributed Databases?

The Ring

Page 61: Why Distributed Databases?

Data Placement

Page 62: Why Distributed Databases?

Data Placement

Page 63: Why Distributed Databases?

Fault Tolerance

Page 64: Why Distributed Databases?

Hinted Handoff

Page 65: Why Distributed Databases?

Fallback Virtual Nodes

Page 66: Why Distributed Databases?

Hinted Handoff

Page 67: Why Distributed Databases?

Read Repair

Page 68: Why Distributed Databases?

Replicas

Page 69: Why Distributed Databases?

Partial Failure

Page 70: Why Distributed Databases?

Divergence

Page 71: Why Distributed Databases?

Read Repair

Page 72: Why Distributed Databases?

Read Repair

Page 73: Why Distributed Databases?

Read Repair

Page 74: Why Distributed Databases?

Active-Anti Entropy

Page 75: Why Distributed Databases?

Merkle Tree

Page 76: Why Distributed Databases?

Compare Trees

Page 77: Why Distributed Databases?

Compare Trees

Page 78: Why Distributed Databases?

Compare Trees

Page 79: Why Distributed Databases?

Compare Trees

Page 80: Why Distributed Databases?

Repair Trees

Page 81: Why Distributed Databases?

Fault Tolerance

• Read Repair

• Active Anti-Entropy

• Hinted Handoff

Page 82: Why Distributed Databases?

Eventual Consistency

Page 83: Why Distributed Databases?

CAP Theorem

Page 84: Why Distributed Databases?

“A shared-data system can have at most two of the three following properties:

Consistency, Availability, and tolerance to network Partitions.”

-Dr. Eric Brewer

Page 85: Why Distributed Databases?

On Consistency

• ACID Consistency: Any transaction, or operation will bring the database from one valid state to another

• CAP Consistency: All nodes see the same data at the same time (synchrony)

Page 86: Why Distributed Databases?

On Partition Tolerance

• The network will be allowed to lose arbitrarily many messages sent from one node to another.

• Databases systems, in order to be useful must have communication over the network

• Clients count

Page 87: Why Distributed Databases?

There is no such thing as a 100% reliable network:

Can’t choose CA

http://codahale.com/you-cant-sacrifice-partition-tolerance

Page 88: Why Distributed Databases?

Very “AP”

Page 89: Why Distributed Databases?

Weak Consistency

Page 90: Why Distributed Databases?

Weak Consistency

Page 91: Why Distributed Databases?

“This is a specific form of weak consistency; the storage system

guarantees that if no new updates are made to the object,

eventually all accesses will return the last updated value.”

Definition of “Eventual Consistency” from “Eventually Consistency Revisited” - Werner Vogels

Page 92: Why Distributed Databases?
Page 93: Why Distributed Databases?

Tunable CAP Controls• R (Read Acks) tunable: Default Quorum

• W (Write Acks) tunable: Default Quorum

• PR (Primary Read Acks) tunable: Default 0

• PW (Primary Write Acks) tunable: Default 0

• N (replicas) tunable: Default 3

Page 94: Why Distributed Databases?

Strong Eventual Consistency PW+PR>N

Page 95: Why Distributed Databases?

How do you even use this?

Page 96: Why Distributed Databases?

Vector Clocks

Page 97: Why Distributed Databases?

Vector Clocks

• Extension of Lamport Clocks

• Used to detect cause and effect in distributed systems

• Can determine concurrency of events, and causality violations

Page 98: Why Distributed Databases?

CRDTs

Page 99: Why Distributed Databases?

• CRDTs:

• Convergent Replicated Data Types

• Commutative Replication Data Types

• Enables data structures to be always writeable on both sides of a partition, and replay after healing a partition

• Enable distributed computation across monotonic functions

• Two Types:

• CvRDTs

• CmRDTs

CRDTs

Page 100: Why Distributed Databases?

CvRDTs

• State / value based CRDTs

• Minimal state

• Don’t require active garbage collection

Page 101: Why Distributed Databases?

Set CvRDT

Page 102: Why Distributed Databases?

CmRDTs

• Op / method based CRDTs

• Size grows monotonically

• Uses version vectors to determine order of operations

Page 103: Why Distributed Databases?

Counter CmRDT

Page 104: Why Distributed Databases?

CRDTs in the Wild• Sets

• Observe-remove set

• Grow-only sets

• Counters

• Grow-only counters

• PN-Counters

• Flags

• Maps

Page 105: Why Distributed Databases?

Data structures that are CRDTs

• Probabilistic, convergent data structures

• Hyper log log

• Bloom filter

• Co-recursive folding functions

• Maximum-counter

• Running Average

• Operational Transform

Page 106: Why Distributed Databases?

CRDTs

• Incredibly powerful primitive

• Not only useful for in-database manipulation but client-database interaction

• You can compose them, and build your own

• Garbage collection is tricky

Page 107: Why Distributed Databases?

RAMP: Read Atomic Multi-Partition Transactions

Page 108: Why Distributed Databases?

Multikey Transaction

Page 109: Why Distributed Databases?

Potential Consistency Violation

Page 110: Why Distributed Databases?

Add Metadata

Page 111: Why Distributed Databases?

Uncommitted State

Page 112: Why Distributed Databases?

Uncommitted State

Page 113: Why Distributed Databases?

Committed State

Page 114: Why Distributed Databases?

Have your availability and consistency too

RAMP

Page 115: Why Distributed Databases?

Eventual Consistency in the WAN

Page 116: Why Distributed Databases?

Low-latency everywhere

Page 117: Why Distributed Databases?

Write AnywhereBeat the speed of the light

Page 118: Why Distributed Databases?

MDC Replication

Page 119: Why Distributed Databases?

Hybrid Topologies

Page 120: Why Distributed Databases?

Bidirectional Replication

Page 121: Why Distributed Databases?

Unidirectional Replication

Page 122: Why Distributed Databases?

Replication Hooks

Page 123: Why Distributed Databases?

Tied Writes

Page 124: Why Distributed Databases?

Hook on Replication

Page 125: Why Distributed Databases?

Hook on Replication

Page 126: Why Distributed Databases?

Replicate Hook Return Data

Page 127: Why Distributed Databases?

Build for WAN locality

Page 128: Why Distributed Databases?

Eventual Consistency In Summary

Page 129: Why Distributed Databases?

Invariant Operation AP / CPSpecify unique ID Any CP

Generate unique ID Any AP

> INCREMENT AP

> DECREMENT CP

< INCREMENT CP

< DECREMENT AP

Secondary Index Any AP

Materialized View Any APAUTO_INCREMENT INSERT CP

Linearizability CAS CP

Operations Requiring

Weak Consistency

vs.

Strong Consistency

Page 130: Why Distributed Databases?

BASE not ACID• Basically Available: There will be a response

per request (failure, or success)

• Soft State: Any two reads against the system may yield different data (when measured against time)

• Eventually Consistent: The system will eventually become consistent when all failures have healed, and time goes to infinity

Page 131: Why Distributed Databases?

Deploying Riak

Page 132: Why Distributed Databases?

AWS Deployment• 6 x i2.4xlarge

• 732GB of RAM

• 19TB of storage

• 960,000 IOPs

• 96 vCPUs

• 3 x Replication

• 10 Gigabit networking

• 99.9999999997% availability

Page 133: Why Distributed Databases?

$74,790/yr

Page 134: Why Distributed Databases?

Real World Use Case

Page 135: Why Distributed Databases?

Ad Network

• Sell targeted ads with minimum latency

• Two datasets:

• Ads

• Users

Page 136: Why Distributed Databases?

Deployment

Page 137: Why Distributed Databases?

Deployment

Page 138: Why Distributed Databases?

Overselling Ads is Okay

Page 139: Why Distributed Databases?

Choose Random Ad Based on Weight of

Outstanding Impressions

Page 140: Why Distributed Databases?

Batch System

Page 141: Why Distributed Databases?

Batch SystemGenerated targeted ads in offline process

Page 142: Why Distributed Databases?

Ad Graph

Page 143: Why Distributed Databases?

Ad Store

Page 144: Why Distributed Databases?

Initial Visit

Page 145: Why Distributed Databases?

Fetch All Ads

Page 146: Why Distributed Databases?

Choose AdBased upon weighted random

Page 147: Why Distributed Databases?

Decrement Value

Page 148: Why Distributed Databases?

Test Model

• 50 actors

• 5 Ads with inventory between 1000, and 1200

• Actors randomly get [1,3] times to choose per round

• Rounds continue until entire inventory is exhausted

Page 149: Why Distributed Databases?

Test Model

Out

stan

ding

Impr

essi

ons

-300

0

300

600

900

1200

Round Number

1 4 7 10 13 16 19 22 25 28 31 34 37 40 43 46 49 52 55 58 61 64 67 70 73 76

Ad 1 Ad 2 Ad 3 Ad 4 Ad 5

Page 150: Why Distributed Databases?

Garbage Collection

Page 151: Why Distributed Databases?

Garbage CollectionUtilizes secondary indexes in batch process to delete

exhausted ads from user records

Page 152: Why Distributed Databases?

Ad Serving

• Requires batch generation of targets

• Requires external GC

• Allows for multidatacenter operation

Page 153: Why Distributed Databases?

In Summary

Page 154: Why Distributed Databases?

Riak

Page 155: Why Distributed Databases?

Distributed

Page 156: Why Distributed Databases?

Fault-Tolerant

Page 157: Why Distributed Databases?

ScalableSc

alab

ility

Processors

Page 158: Why Distributed Databases?

Toolchest

Page 159: Why Distributed Databases?

Why Distributed Databases?

Sargun Dhillon

@Sargun