Slide 1

Slide 1 text

NoSQL and Bin Liu 2017.7.7

Slide 2

Slide 2 text

‣ Comment on mysqlperformanceblog.com Select queries were slow until I added an index onto the timestamp eld... Adding the index really helped our reporting, BUT now the inserts are taking forever. “ “

Slide 3

Slide 3 text

‣ MySQL bug #9544 I'm trying to create indexes on a table with 308 million rows. It took ~20 minutes to load the table but 10 days to build indexes on it. “ “

Slide 4

Slide 4 text

Schema Scalability Performance 4

Slide 5

Slide 5 text

NoSQL KV Column-oriented Document Database Graph Database 5

Slide 6

Slide 6 text

NoSQL Redis BigTable Dynamo CouchDB Cassandra MongoDB Neo4j 6

Slide 7

Slide 7 text

CAP Consistency Availability Partition-tolerance 7

Slide 8

Slide 8 text

CP Bigtable/HBase Redis AP Dynamo/Cassandra CouchDB CA MySQL Postgre 8

Slide 9

Slide 9 text

Hbase vs Cassandra HBase Cassandra CAP cp ap sharding(Region) SPoF Y N HOP 1-3 1 Consistency Level OP W/R 9

Slide 10

Slide 10 text

Cassandra history 2007 Amazon Dynamo key/value store 2008 Facebook Dynamo ASF 10

Slide 11

Slide 11 text

Cassandra 11

Slide 12

Slide 12 text

Amazon Dynamo Consistent hashing Partitioning Replication One-hop routing Google BigTable Column Families Memtables SSTables 12

Slide 13

Slide 13 text

13

Slide 14

Slide 14 text

HBase 14

Slide 15

Slide 15 text

Cassandra 15

Slide 16

Slide 16 text

Virtual nodes: Partitioner: Replication strategy: Snitch: 16

Slide 17

Slide 17 text

Consistent hashing 17

Slide 18

Slide 18 text

Tokens row key -> partition key -> token 0 - 2127-1 18

Slide 19

Slide 19 text

HBase 19

Slide 20

Slide 20 text

Virtual nodes Vnodes 20

Slide 21

Slide 21 text

21

Slide 22

Slide 22 text

Partitioners distribute func: hash(row_key) -> token 22

Slide 23

Slide 23 text

Cassandra partitioners Murmur3Partitioner RandomPartitioner MD5 hash ByteOrderedPartitioner = 23

Slide 24

Slide 24 text

Murmur3Partitioner MurmurHash function 64-bit hash value of the partition key -263 to +263-1. RandomPartitioner 3-5 Auto loadblanace 24

Slide 25

Slide 25 text

RandomPartitioner MD5 hash value Cryptographic hash 0 to 2127 -1 Auto loadblanace 25

Slide 26

Slide 26 text

ByteOrderedPartitioner key Range scan 26

Slide 27

Slide 27 text

Snitches rack 27

Slide 28

Slide 28 text

Cassandra Replica 28

Slide 29

Slide 29 text

29

Slide 30

Slide 30 text

Cassandra Data model 30

Slide 31

Slide 31 text

Cassandra Data model keyspace vs database column family vs table super column column key 31

Slide 32

Slide 32 text

Keyspace key String value ColumnFamily[] 32

Slide 33

Slide 33 text

ColumnFamily key byte[] value Column[] 33

Slide 34

Slide 34 text

Column name(byte[]) value(byte[]) timestamp(long) 34

Slide 35

Slide 35 text

gossip P2P 35

Slide 36

Slide 36 text

SEED NODES 3 seed node 36

Slide 37

Slide 37 text

Storage engine Log-Structured Merge Tree I/O Read/Write 37

Slide 38

Slide 38 text

Data structure Memtable SSTable Bloom Filter Index Data Row Cache(LRU, CF data) Partition Key Cache(SSTable postion) Commit log(shared among tables) 38

Slide 39

Slide 39 text

RDMBS B+Tree for read O(log N) Sharding Scale out 39

Slide 40

Slide 40 text

SSTable row key bloom lter 40

Slide 41

Slide 41 text

41

Slide 42

Slide 42 text

Bloom Filter : O(k) 42

Slide 43

Slide 43 text

Bloom var: m,k: int hash_func: func(Object) -> n // 0 <= n < m f[k]: hash_func[] array_search[m], array_dict[m]:bit[] input: Object # write for i := range(k): array_dict[f[i](input)] = 1 # read for i := range(k): array_search[f[i](input)] = 1 if array_dict & array_search = array_search: // may be exists else: // not exists 43

Slide 44

Slide 44 text

Bloom m=10, k=3; f1= %10; f2= %10; f3= %10 A: (82,171,25) -> 2,1,5 B: (85,173,29) -> 5,3,9 C: (87,171,25) -> 7,1,5 D: (82,175,31) -> 2,5,1 Bit 0 1 2 3 4 5 6 7 8 9 A 0 1 1 0 0 1 0 0 0 0 B 0 0 0 1 0 1 0 0 0 1 Dict 0 1 1 1 0 1 0 0 0 1 C 0 1 0 0 0 1 0 1 0 0 D 0 1 1 0 0 1 0 0 0 0 44

Slide 45

Slide 45 text

45

Slide 46

Slide 46 text

Dictionary Encoding From: http://engineer.retty.me/entry/columnar-storage-format 46

Slide 47

Slide 47 text

Run-Length Encoding RLE From: http://engineer.retty.me/entry/columnar-storage-format 47

Slide 48

Slide 48 text

Delta Encoding From: http://engineer.retty.me/entry/columnar-storage-format 48

Slide 49

Slide 49 text

Double-delta-encoding 0: 0 [-63, 64]: 10 + D (7bits) [-255, 256]: 110 + D (9bits) [-2047, 2048]: 1110 + D (12bits) Other: 1111 + D(32bits) 49

Slide 50

Slide 50 text

XOR encoding x = value1 `XOR` value2 if x == 0: write 0 else: write 1 if this_meaningful_bits == prev_meaningful_bits: write 0 write this_meaningful_bits else: write 1 write length(prefix_zero) # 5 bits write length(this_meaningful_bits) # 6 bits write this_meaningful_bits 50

Slide 51

Slide 51 text

Gorilla timestamp and value 51

Slide 52

Slide 52 text

String CPU gzip lzo snappy 52

Slide 53

Slide 53 text

Write data 53

Slide 54

Slide 54 text

Compaction SSTable SSTable I/O 54

Slide 55

Slide 55 text

Reading Data 55

Slide 56

Slide 56 text

Consistency Eventual consistency Tunable(operation-level) consistency CP or AP 56

Slide 57

Slide 57 text

Strong consistency R + W > N R: W: N: ONE/ANY/QUORUM/ALL/... ... 57

Slide 58

Slide 58 text

Consistency (R + W > N) (R + W <= N) W=ONE,R=ALL;W=ALL,R=ONE 58

Slide 59

Slide 59 text

? read repair hinted handoffs 59

Slide 60

Slide 60 text

SEDA(Staged Event-Driven Architecture) Thread + Event (Event Queue+Thread Pool) Thread pool by stage Event Driven between stage by message passing 60

Slide 61

Slide 61 text

Questions ? 61