68
Sharding MySQL with Vitess Harun Küçük

Sharding MySQL with Vitess - Percona...database traffic since 2011. Youtube had 256 shards and each shards had between 80 and 120 replicas across 20 datacenters all around the world

  • Upload
    others

  • View
    4

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Sharding MySQL with Vitess - Percona...database traffic since 2011. Youtube had 256 shards and each shards had between 80 and 120 replicas across 20 datacenters all around the world

Sharding MySQL with Vitess

Harun Küçük

Page 2: Sharding MySQL with Vitess - Percona...database traffic since 2011. Youtube had 256 shards and each shards had between 80 and 120 replicas across 20 datacenters all around the world

What is Sharding?

Sharding is a type of database partitioning that separates very large databases into smaller,faster and more easily managed parts called data shards.

Page 3: Sharding MySQL with Vitess - Percona...database traffic since 2011. Youtube had 256 shards and each shards had between 80 and 120 replicas across 20 datacenters all around the world

• Non-Scalable Master

Why we need Sharding?

Sample Traditional MySQL Replication

• Scalable App Layer

• Scalable Replicas

Page 4: Sharding MySQL with Vitess - Percona...database traffic since 2011. Youtube had 256 shards and each shards had between 80 and 120 replicas across 20 datacenters all around the world

Vitess

• Started 2010 , youtube https://vitess.io/• Open source since 2011 https://github.com/vitessio/vitess• Incubating project in CNCF https://www.cncf.io/projects/

Page 5: Sharding MySQL with Vitess - Percona...database traffic since 2011. Youtube had 256 shards and each shards had between 80 and 120 replicas across 20 datacenters all around the world

Vitess Architecture

• Lightweight proxy server• Routes traffic to correct vttablet• Returns consolidated results back to the clients

Page 6: Sharding MySQL with Vitess - Percona...database traffic since 2011. Youtube had 256 shards and each shards had between 80 and 120 replicas across 20 datacenters all around the world

Vitess Architecture

• Proxy server that sits in front of MySQL instance• Protect MySQL from harmful queries• Connection Pooling• Query rewriting• Hot row protection

Page 7: Sharding MySQL with Vitess - Percona...database traffic since 2011. Youtube had 256 shards and each shards had between 80 and 120 replicas across 20 datacenters all around the world

Vitess Architecture

• Stores metadata (running servers,sharding schema,Replication Graph)• Etcd, Apache Zookeeper or consul could be used for topology

Page 8: Sharding MySQL with Vitess - Percona...database traffic since 2011. Youtube had 256 shards and each shards had between 80 and 120 replicas across 20 datacenters all around the world

Vitess Architecture

• Vtctl is command line tool, Vtctld is an HTTP server that lets you browse the information stored in the topology.

Page 9: Sharding MySQL with Vitess - Percona...database traffic since 2011. Youtube had 256 shards and each shards had between 80 and 120 replicas across 20 datacenters all around the world

Vitess Architecture

• Replica tablets: candidates for master tablet , Readonly tables: for batch jobs, resharding,bigdata,backups etc.

Page 10: Sharding MySQL with Vitess - Percona...database traffic since 2011. Youtube had 256 shards and each shards had between 80 and 120 replicas across 20 datacenters all around the world

Vitess Key Adaptors

• Started 2010 at Youtube and It has been serving all Youtubedatabase traffic since 2011. Youtube had 256 shards and each shards had between 80 and 120 replicas across 20 datacenters all around the world. (Approx. 256K instance)

• JD.com is the 2nd largest retailer company in China. JD.com has more than 10.000 instance (master,replicas) in Vitess on kubernetes cluster

• Square Cash App fully runs on Vitess. Square has more than 64 shards.

• Slack migrated 40% database traffic to Vitess and their goal is 100%

• Pinterest’s all of advertising campaign management fully runs on Vitess

Page 11: Sharding MySQL with Vitess - Percona...database traffic since 2011. Youtube had 256 shards and each shards had between 80 and 120 replicas across 20 datacenters all around the world

Example: Sakila DVD Rental Company Database

Lets suppose we have a DVD rental company and our database diagram live below

Page 12: Sharding MySQL with Vitess - Percona...database traffic since 2011. Youtube had 256 shards and each shards had between 80 and 120 replicas across 20 datacenters all around the world

Day 1: Table Rows

Query:

Page 13: Sharding MySQL with Vitess - Percona...database traffic since 2011. Youtube had 256 shards and each shards had between 80 and 120 replicas across 20 datacenters all around the world

30 days later…

Query:

Page 14: Sharding MySQL with Vitess - Percona...database traffic since 2011. Youtube had 256 shards and each shards had between 80 and 120 replicas across 20 datacenters all around the world

6 months later…

Query:

Page 15: Sharding MySQL with Vitess - Percona...database traffic since 2011. Youtube had 256 shards and each shards had between 80 and 120 replicas across 20 datacenters all around the world

2 years later…

Query:

Page 16: Sharding MySQL with Vitess - Percona...database traffic since 2011. Youtube had 256 shards and each shards had between 80 and 120 replicas across 20 datacenters all around the world

Entity Group for Sharding

Payment,Rental and Customer tables have customer_id column. So these three tables should be sharded horizontally by customer_id

Page 17: Sharding MySQL with Vitess - Percona...database traffic since 2011. Youtube had 256 shards and each shards had between 80 and 120 replicas across 20 datacenters all around the world

Step 1: Vertical Sharding

Page 18: Sharding MySQL with Vitess - Percona...database traffic since 2011. Youtube had 256 shards and each shards had between 80 and 120 replicas across 20 datacenters all around the world

Step 2: Horizontal Sharding

Page 19: Sharding MySQL with Vitess - Percona...database traffic since 2011. Youtube had 256 shards and each shards had between 80 and 120 replicas across 20 datacenters all around the world

Step 3: Resharding

Page 20: Sharding MySQL with Vitess - Percona...database traffic since 2011. Youtube had 256 shards and each shards had between 80 and 120 replicas across 20 datacenters all around the world

Prerequisites for Demo

• Vitess (https://github.com/vitessio/vitess)• Kubernetes Cluster or Minicube• Etcd operator for topology cluster• Helm for vitess helm charts (vitess/helm/vitess)• NFS client provisioner for persistent NFS volumes

(https://github.com/helm/charts/tree/master/stable/nfs-client-provisioner)

Page 21: Sharding MySQL with Vitess - Percona...database traffic since 2011. Youtube had 256 shards and each shards had between 80 and 120 replicas across 20 datacenters all around the world

1.) initiate new cluster, Concepts

File: initial_cluster.yaml

• Cell : Zone, availability zone, datacenter• KeySpace : Logical Database• Vschema : Vitess Schema, contains

metadata about how tables are organized across keyspaces and shards

• Vindex : index to find shards

Page 22: Sharding MySQL with Vitess - Percona...database traffic since 2011. Youtube had 256 shards and each shards had between 80 and 120 replicas across 20 datacenters all around the world

1.) initiate new cluster

File: initial_cluster.yaml

Page 23: Sharding MySQL with Vitess - Percona...database traffic since 2011. Youtube had 256 shards and each shards had between 80 and 120 replicas across 20 datacenters all around the world

1.) initiate new cluster

Page 24: Sharding MySQL with Vitess - Percona...database traffic since 2011. Youtube had 256 shards and each shards had between 80 and 120 replicas across 20 datacenters all around the world

2.) Create New Keyspace

File:create_keyspace.yaml

Page 25: Sharding MySQL with Vitess - Percona...database traffic since 2011. Youtube had 256 shards and each shards had between 80 and 120 replicas across 20 datacenters all around the world

2.) Create New Keyspace

File:create_keyspace.yaml

Page 26: Sharding MySQL with Vitess - Percona...database traffic since 2011. Youtube had 256 shards and each shards had between 80 and 120 replicas across 20 datacenters all around the world

3.) Split Keyspace Schema

File:split_keyspace_schema.yaml

Page 27: Sharding MySQL with Vitess - Percona...database traffic since 2011. Youtube had 256 shards and each shards had between 80 and 120 replicas across 20 datacenters all around the world

3.) Split Keyspace Schema

File:split_keyspace_schema.yaml

Page 28: Sharding MySQL with Vitess - Percona...database traffic since 2011. Youtube had 256 shards and each shards had between 80 and 120 replicas across 20 datacenters all around the world

3.) Split KeySpace Schema

Page 29: Sharding MySQL with Vitess - Percona...database traffic since 2011. Youtube had 256 shards and each shards had between 80 and 120 replicas across 20 datacenters all around the world

4.) Vertical Split Clone

File:vertical_split_clone.yaml

Page 30: Sharding MySQL with Vitess - Percona...database traffic since 2011. Youtube had 256 shards and each shards had between 80 and 120 replicas across 20 datacenters all around the world

4.) Vertical Split Clone

File:vertical_split_clone.yaml

Page 31: Sharding MySQL with Vitess - Percona...database traffic since 2011. Youtube had 256 shards and each shards had between 80 and 120 replicas across 20 datacenters all around the world

4.) Vertical Split Clone

Page 32: Sharding MySQL with Vitess - Percona...database traffic since 2011. Youtube had 256 shards and each shards had between 80 and 120 replicas across 20 datacenters all around the world

4.) Migrate

File: migrate_readonly_replica.yamlmigrate_master.yaml

Page 33: Sharding MySQL with Vitess - Percona...database traffic since 2011. Youtube had 256 shards and each shards had between 80 and 120 replicas across 20 datacenters all around the world

4.) Migrate ReadOnly and Replica

Page 34: Sharding MySQL with Vitess - Percona...database traffic since 2011. Youtube had 256 shards and each shards had between 80 and 120 replicas across 20 datacenters all around the world

4.) Migrate Master

Page 35: Sharding MySQL with Vitess - Percona...database traffic since 2011. Youtube had 256 shards and each shards had between 80 and 120 replicas across 20 datacenters all around the world

5.) Drop Blacklisted Tables

Page 36: Sharding MySQL with Vitess - Percona...database traffic since 2011. Youtube had 256 shards and each shards had between 80 and 120 replicas across 20 datacenters all around the world

VtGate Quey Routing

Page 37: Sharding MySQL with Vitess - Percona...database traffic since 2011. Youtube had 256 shards and each shards had between 80 and 120 replicas across 20 datacenters all around the world

VtGate Quey Routing

Page 38: Sharding MySQL with Vitess - Percona...database traffic since 2011. Youtube had 256 shards and each shards had between 80 and 120 replicas across 20 datacenters all around the world

Horizontal Sharding

Page 39: Sharding MySQL with Vitess - Percona...database traffic since 2011. Youtube had 256 shards and each shards had between 80 and 120 replicas across 20 datacenters all around the world

6.) Primary Vindex for Horizontal Sharding

File: create_vindex.yaml

Page 40: Sharding MySQL with Vitess - Percona...database traffic since 2011. Youtube had 256 shards and each shards had between 80 and 120 replicas across 20 datacenters all around the world

Sharding by Hash Function

Page 41: Sharding MySQL with Vitess - Percona...database traffic since 2011. Youtube had 256 shards and each shards had between 80 and 120 replicas across 20 datacenters all around the world

Sharding by Hash Function

1001

1000

1002

1003

1004

1005

1006

1007

Page 42: Sharding MySQL with Vitess - Percona...database traffic since 2011. Youtube had 256 shards and each shards had between 80 and 120 replicas across 20 datacenters all around the world

Vitess Hash Algorithm (3des hash)

4000 = 40000000000000008000 = 8000000000000000

Page 43: Sharding MySQL with Vitess - Percona...database traffic since 2011. Youtube had 256 shards and each shards had between 80 and 120 replicas across 20 datacenters all around the world

Vitess Hash Based Sharding (3des hash)

264 combination. theoretically allows us to have an infinite number of shards

Page 44: Sharding MySQL with Vitess - Percona...database traffic since 2011. Youtube had 256 shards and each shards had between 80 and 120 replicas across 20 datacenters all around the world

Vitess Hash Based Sharding (3des hash)

Page 45: Sharding MySQL with Vitess - Percona...database traffic since 2011. Youtube had 256 shards and each shards had between 80 and 120 replicas across 20 datacenters all around the world

7.) Create Horizontal Shards

File: create_horizontal_shards.yaml

Page 46: Sharding MySQL with Vitess - Percona...database traffic since 2011. Youtube had 256 shards and each shards had between 80 and 120 replicas across 20 datacenters all around the world

7.) Create Horizontal Shards

Page 47: Sharding MySQL with Vitess - Percona...database traffic since 2011. Youtube had 256 shards and each shards had between 80 and 120 replicas across 20 datacenters all around the world

7.) Horizontal SplitClone

File: horizontal_split_clone.yaml

Page 48: Sharding MySQL with Vitess - Percona...database traffic since 2011. Youtube had 256 shards and each shards had between 80 and 120 replicas across 20 datacenters all around the world

7.) Horizontal SplitClone

File: horizontal_split_clone.yaml

Page 49: Sharding MySQL with Vitess - Percona...database traffic since 2011. Youtube had 256 shards and each shards had between 80 and 120 replicas across 20 datacenters all around the world

7.) Horizontal SplitClone

File: horizontal_split_clone.yaml

Page 50: Sharding MySQL with Vitess - Percona...database traffic since 2011. Youtube had 256 shards and each shards had between 80 and 120 replicas across 20 datacenters all around the world

8.) Migrate RdOnly,Replica and Master

File: 13_migrate_readonly_replica.yaml14_migrate_master.yaml

Page 51: Sharding MySQL with Vitess - Percona...database traffic since 2011. Youtube had 256 shards and each shards had between 80 and 120 replicas across 20 datacenters all around the world

8.) Migrate RdOnly,Replica and Master

File: 13_migrate_readonly_replica.yaml14_migrate_master.yaml

Page 52: Sharding MySQL with Vitess - Percona...database traffic since 2011. Youtube had 256 shards and each shards had between 80 and 120 replicas across 20 datacenters all around the world

Querying Vitess Cluster - Counts

Page 53: Sharding MySQL with Vitess - Percona...database traffic since 2011. Youtube had 256 shards and each shards had between 80 and 120 replicas across 20 datacenters all around the world

Querying Vitess Cluster - Routing

Page 54: Sharding MySQL with Vitess - Percona...database traffic since 2011. Youtube had 256 shards and each shards had between 80 and 120 replicas across 20 datacenters all around the world

Querying Vitess Cluster - Routing

Page 55: Sharding MySQL with Vitess - Percona...database traffic since 2011. Youtube had 256 shards and each shards had between 80 and 120 replicas across 20 datacenters all around the world

Querying Vitess Cluster – @replica,@rdonly

Page 56: Sharding MySQL with Vitess - Percona...database traffic since 2011. Youtube had 256 shards and each shards had between 80 and 120 replicas across 20 datacenters all around the world

Querying Vitess Cluster – Cross Shard Join

Page 57: Sharding MySQL with Vitess - Percona...database traffic since 2011. Youtube had 256 shards and each shards had between 80 and 120 replicas across 20 datacenters all around the world

Querying Vitess Cluster – Cross Shard Join

Page 58: Sharding MySQL with Vitess - Percona...database traffic since 2011. Youtube had 256 shards and each shards had between 80 and 120 replicas across 20 datacenters all around the world

Querying Vitess Cluster – Cross Shard Join

= +N *

N = row count of 2. query’s result

1

2

3

Page 59: Sharding MySQL with Vitess - Percona...database traffic since 2011. Youtube had 256 shards and each shards had between 80 and 120 replicas across 20 datacenters all around the world

VReplication

Files:copy_schema_shard.sh,vreplication.sh,apply_routing_rules.sh,add_reference_table.sh

Page 60: Sharding MySQL with Vitess - Percona...database traffic since 2011. Youtube had 256 shards and each shards had between 80 and 120 replicas across 20 datacenters all around the world

Querying Vitess Cluster –Cross Shard

Join,VReplication

Page 61: Sharding MySQL with Vitess - Percona...database traffic since 2011. Youtube had 256 shards and each shards had between 80 and 120 replicas across 20 datacenters all around the world

Querying Vitess Cluster –Scatter Query

Page 62: Sharding MySQL with Vitess - Percona...database traffic since 2011. Youtube had 256 shards and each shards had between 80 and 120 replicas across 20 datacenters all around the world

Querying Vitess Cluster –Scatter Query

LookupVindex

Page 63: Sharding MySQL with Vitess - Percona...database traffic since 2011. Youtube had 256 shards and each shards had between 80 and 120 replicas across 20 datacenters all around the world

Drawbacks: Application Transactions

• Best Effort Commit -> Consistency Problems

• 2 Phase Commit -> Performance Cost

Page 64: Sharding MySQL with Vitess - Percona...database traffic since 2011. Youtube had 256 shards and each shards had between 80 and 120 replicas across 20 datacenters all around the world

Drawbacks: Distributed Deadlocks

Shard Level Deadlock

Page 65: Sharding MySQL with Vitess - Percona...database traffic since 2011. Youtube had 256 shards and each shards had between 80 and 120 replicas across 20 datacenters all around the world

Drawbacks: Distributed Deadlocks

Database Level Deadlock

Page 66: Sharding MySQL with Vitess - Percona...database traffic since 2011. Youtube had 256 shards and each shards had between 80 and 120 replicas across 20 datacenters all around the world

Application Transactions

Page 68: Sharding MySQL with Vitess - Percona...database traffic since 2011. Youtube had 256 shards and each shards had between 80 and 120 replicas across 20 datacenters all around the world

Thank You