82
Synchronous Replica1on for MySQL Kenny Gryp <[email protected]> 15 05 2014 1

Synchronous Replication For MySQL with Percona XtraDB Cluster

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: Synchronous Replication For MySQL with Percona XtraDB Cluster

Synchronous  Replica1on  for  MySQL

Kenny  Gryp <[email protected]>  15  05  2014 1

Page 2: Synchronous Replication For MySQL with Percona XtraDB Cluster

Agenda

• Default  asynchronous  MySQL  replication  • Percona  XtraDB  Cluster:  • Introduction  /  Features  /  Load  Balancing  

• Use  Cases:  • High  Availability  /  WAN  Replication  /  Read  Scaling  

• Limitations

2

Page 3: Synchronous Replication For MySQL with Percona XtraDB Cluster

Percona

• Percona  is  the  oldest  and  largest  independent  MySQL  Support,  Consulting,  Remote  DBA,  Training,  and  Software  Development  company  with  a  global,  24x7  staff  of  over  100  serving  more  than  2,000  customers  in  50+  countries  since  2006  !  

• Our  contributions  to  the  MySQL  community  include:  • Percona  Server,  Percona  XtraDB  Cluster  • Percona  XtraBackup:  online  backup  • Percona  Toolkit,  Percona  Playback…  • books,  and  research  published  on  the  MySQL  Performance  Blog.

3

Page 4: Synchronous Replication For MySQL with Percona XtraDB Cluster

Agenda

• Default  asynchronous  MySQL  Replication  • Percona  XtraDB  Cluster:  • Introduction  /  Features  /  Load  Balancing  

• Use  Cases:  • High  Availability  /  WAN  Replication  /  Read  Scaling  

• Limitations

4

Page 5: Synchronous Replication For MySQL with Percona XtraDB Cluster

MySQL  Replication

If your HA is based on MySQL Replication -!You may be playing a dangerous game !

5

Page 6: Synchronous Replication For MySQL with Percona XtraDB Cluster

Traditional  Replication  Approach

Server  1 Server  2replication  stream

“master” “slave”

6

Page 7: Synchronous Replication For MySQL with Percona XtraDB Cluster

MySQL  Replication 7

1 2

3 4

5 6

• Common  Topologies:  • Master-­‐Master  (Only  1  active  master)  • 1  or  more  layers  of  replication

Page 8: Synchronous Replication For MySQL with Percona XtraDB Cluster

• Slaves  can  be  used  for  reads:  • asynchronous,  stale  data  is  the  rule  • data  loss  possible  (*semi-­‐sync)

MySQL  Replication 8

1 2

3 4

5 6

Page 9: Synchronous Replication For MySQL with Percona XtraDB Cluster

• non-­‐trivial:  • external  monitoring  • scripts  for  failover  • add  node  ==  restore  backup  • much  better  in  

MySQL  5.6:  GTIDs

MySQL  Replication

1 2

3 4

5 6

9

Page 10: Synchronous Replication For MySQL with Percona XtraDB Cluster

Agenda

• Default  asynchronous  MySQL  Replication  • Percona  XtraDB  Cluster:  • Introduction  /  Features  /  Load  Balancing  

• Use  Cases:  • High  Availability  /  WAN  Replication  /  Read  Scaling  

• Limitations

10

Page 11: Synchronous Replication For MySQL with Percona XtraDB Cluster

11Data  Centric

DATA

Server 1 Server 2 Server 3 Server N ...

Page 12: Synchronous Replication For MySQL with Percona XtraDB Cluster

12Percona  XtraDB  Cluster

Page 13: Synchronous Replication For MySQL with Percona XtraDB Cluster

13Percona  XtraDB  Cluster

• All  nodes  have  a  full  copy  of  the  data  • Every  node  is  equal  • No  central  management,  no  SPOF

Page 14: Synchronous Replication For MySQL with Percona XtraDB Cluster

14Understanding  Galera

38The cluster can be !seen as a meeting !

Page 15: Synchronous Replication For MySQL with Percona XtraDB Cluster

PXC  (Galera  cluster)  is  a  meeting

bfb912e5-f560-11e2-0800-1eefab05e57d

15

Page 16: Synchronous Replication For MySQL with Percona XtraDB Cluster

PXC  (Galera  cluster)  is  a  meeting

bfb912e5-f560-11e2-0800-1eefab05e57d

16

Page 17: Synchronous Replication For MySQL with Percona XtraDB Cluster

PXC  (Galera  cluster)  is  a  meeting

bfb912e5-f560-11e2-0800-1eefab05e57d

17

Page 18: Synchronous Replication For MySQL with Percona XtraDB Cluster

PXC  (Galera  cluster)  is  a  meeting

bfb912e5-f560-11e2-0800-1eefab05e57d

18

Page 19: Synchronous Replication For MySQL with Percona XtraDB Cluster

PXC  (Galera  cluster)  is  a  meeting

bfb912e5-f560-11e2-0800-1eefab05e57d

19

Page 20: Synchronous Replication For MySQL with Percona XtraDB Cluster

PXC  (Galera  cluster)  is  a  meeting

bfb912e5-f560-11e2-0800-1eefab05e57d

20

Page 21: Synchronous Replication For MySQL with Percona XtraDB Cluster

PXC  (Galera  cluster)  is  a  meeting

bfb912e5-f560-11e2-0800-1eefab05e57d

21

Page 22: Synchronous Replication For MySQL with Percona XtraDB Cluster

PXC  (Galera  cluster)  is  a  meeting 22

Page 23: Synchronous Replication For MySQL with Percona XtraDB Cluster

PXC  (Galera  cluster)  is  a  meeting

???

23

Page 24: Synchronous Replication For MySQL with Percona XtraDB Cluster

PXC  (Galera  cluster)  is  a  meeting

4fd8824d-ad5b-11e2-0800-73d6929be5cf

New meeting !

24

Page 25: Synchronous Replication For MySQL with Percona XtraDB Cluster

25CAP  Theorem  

• MySQL  (Asynchronous)  Replication:  • Availability  • Partition  Tolerance  

• Percona  XtraDB  Cluster  • Consistency  • Availability

Consistency

AvailabilityPartition  

Tolerance

Page 26: Synchronous Replication For MySQL with Percona XtraDB Cluster

What  is  Percona  XtraDB  Cluster  ?

• Percona  Server  • +  WSREP  patches  • +  Galera  library  • +  Utilities  (init,  SST  and  cluster  check  scripts)  

26

Page 27: Synchronous Replication For MySQL with Percona XtraDB Cluster

27

• This  is  a  free  open  source  solution,  Percona  Server  is  a  MySQL  alternative  which  offers  breakthrough  performance,  scalability,  features,  and  instrumentation.  Self-­‐tuning  algorithms  and  support  for  extremely  high-­‐performance  hardware  make  it  the  clear  choice  for  organisations  that  demand  excellent  performance  and  reliability  from  their  MySQL  database  server.

Percona  Server

Page 28: Synchronous Replication For MySQL with Percona XtraDB Cluster

WSREP  and  Galera

• WSREP  API  is  a  project  to  develop  generic  replication  plugin  interface  for  databases  (WriteSet  Replication)  

• Galera  is  a  wsrep  provider  that  implements  multi-­‐master,  synchronous  replication  

• Other  Implementation:  MariaDB  Galera  Cluster

28

Page 29: Synchronous Replication For MySQL with Percona XtraDB Cluster

What  is  Percona  XtraDB  Cluster  ?

Full  compatibility    with  existing  systems

29

Page 30: Synchronous Replication For MySQL with Percona XtraDB Cluster

What  is  Percona  XtraDB  Cluster  ?

Minimal  efforts  to  migrate

30

Page 31: Synchronous Replication For MySQL with Percona XtraDB Cluster

What  is  Percona  XtraDB  Cluster  ?

Minimal  efforts  to  return  back  to  MySQL

31

Page 32: Synchronous Replication For MySQL with Percona XtraDB Cluster

32Features

• Synchronous  Replication  • Multi  Master  • Parallel  Applying  • Quorum  Based  • Certification/Optimistic  Locking  • Automatic  Node  Provisioning

Page 33: Synchronous Replication For MySQL with Percona XtraDB Cluster

33Features

• Synchronous  Replication  • Multi  Master  • Parallel  Applying  • Quorum  Based  • Certification/Optimistic  Locking  • Automatic  Node  Provisioning

Page 34: Synchronous Replication For MySQL with Percona XtraDB Cluster

34(Virtual)  Synchronous  Replication

• Writesets  (transactions)  are  replicated  to  all  available  nodes  on  commit  (and  queued  on  each)  

• Writesets  are  individually  “certified”  on  every  node,  deterministically.Either  it  is  committed  on  all  nodes  or  no  node  at  all  (NO  2PC)  

• Queued  writesets  are  applied  on  those  nodes  independently  and  asynchronously  

• Flow  Control  avoids  too  much  ‘lag’

Page 35: Synchronous Replication For MySQL with Percona XtraDB Cluster

35(Virtual)  Synchronous  Replication

Page 36: Synchronous Replication For MySQL with Percona XtraDB Cluster

36(Virtual)  Synchronous  Replication

• Reads  can  read  old  data  • Flow  Control  (by  default  16  trx)  avoids  lag  • wsrep_causal_reads  can  be  enabled  to  

ensure  full  synchronous  reads  • Latency:  writes  are  fast,  only  at  COMMIT,  

communication  with  other  nodes  happen

Page 37: Synchronous Replication For MySQL with Percona XtraDB Cluster

37Stale  Reads

Page 38: Synchronous Replication For MySQL with Percona XtraDB Cluster

38Latency

Page 39: Synchronous Replication For MySQL with Percona XtraDB Cluster

39Features

• Synchronous  Replication  • Multi  Master  • Parallel  Applying  • Quorum  Based  • Certification/Optimistic  Locking  • Automatic  Node  Provisioning

Page 40: Synchronous Replication For MySQL with Percona XtraDB Cluster

Multi-­‐Master  Replication

• You  can  write  to  any  node  in  your  cluster*  • Writes  are  ordered  inside  the  cluster  

writes

writeswrites

40

Page 41: Synchronous Replication For MySQL with Percona XtraDB Cluster

41Features

• Synchronous  Replication  • Multi  Master  • Parallel  Applying  • Quorum  Based  • Certification/Optimistic  Locking  • Automatic  Node  Provisioning

Page 42: Synchronous Replication For MySQL with Percona XtraDB Cluster

Parallel  Replication

• Standard  MySQL

Application writes N threads Apply  1  thread  

(MySQL  5.6:  max  1  thread    per  schema)

42

Page 43: Synchronous Replication For MySQL with Percona XtraDB Cluster

Parallel  Replication

• PXC  /  Galera

Application writes N threads Apply M threads

(wsrep_slave_threads)

43

Page 44: Synchronous Replication For MySQL with Percona XtraDB Cluster

44Features

• Synchronous  Replication  • Multi  Master  • Parallel  Applying  • Quorum  Based  • Certification/Optimistic  Locking  • Automatic  Node  Provisioning

Page 45: Synchronous Replication For MySQL with Percona XtraDB Cluster

Quorum  Based

• If  a  node  does  not  see  more  than  50%  of  the  total  amount  of  nodes:  reads/writes  are  not  accepted.  

• Split  brain  is  prevented  • This  requires  at  least  3  nodes  to  be  effective  • a  node  can  be  an  arbitrator  (garbd),  joining  the  communication,  but  not  having  any  MySQL  running  

• Can  be  disabled  (but  be  warned!)

45

Page 46: Synchronous Replication For MySQL with Percona XtraDB Cluster

• Loss  of  connectivity

Quorum  Based 46

Network  Problem

Does  not  accept  Reads  &  Writes

Page 47: Synchronous Replication For MySQL with Percona XtraDB Cluster

• 4  Nodes

Quorum  Based 47

Page 48: Synchronous Replication For MySQL with Percona XtraDB Cluster

• Default  quorum  configuration:4  Nodes,  0  Nodes  have  quorum

Quorum  Based 48

Network  Problem

Page 49: Synchronous Replication For MySQL with Percona XtraDB Cluster

49Features

• Synchronous  Replication  • Multi  Master  • Parallel  Applying  • Quorum  Based  • Certification/Optimistic  Locking  • Automatic  Node  Provisioning

Page 50: Synchronous Replication For MySQL with Percona XtraDB Cluster

50Certification

Page 51: Synchronous Replication For MySQL with Percona XtraDB Cluster

51Optimistic  Locking

• Communication  to  the  other  nodes  of  the  cluster  only  happens  during  COMMIT,  this  affects  locking  behavior.  

• Optimistic  Locking  is  done:  • InnoDB  Locking  happens  local  to  the  node  • During  COMMIT/Certification,  the  other  

nodes  bring  deadlocks

Page 52: Synchronous Replication For MySQL with Percona XtraDB Cluster

52Optimistic  Locking

• Some  Characteristics:  • also  COMMIT  and  SELECT’s  can  fail  on  

deadlock  • Might  require  application  changes:

Not  all  applications  handle  this  properly

Page 53: Synchronous Replication For MySQL with Percona XtraDB Cluster

53Traditional  InnoDB  Locking

system 1Transaction 1

Transaction 2BEGIN

Transaction1

BEGINUPDATE t WHERE id=14

UPDATE t WHERE id=14...

COMMIT

Waits on COMMIT in trx 1

Page 54: Synchronous Replication For MySQL with Percona XtraDB Cluster

54InnoDB  Locking  With  PXC

system 1Transaction 1

Transaction 2BEGIN

Transaction1

BEGINUPDATE t WHERE id=14

UPDATE t WHERE id=14...

COMMITDML,COMMIT or SELECT

ERROR due row conflict

system 2

...

Page 55: Synchronous Replication For MySQL with Percona XtraDB Cluster

55InnoDB  Locking  With  PXC

system 1Transaction 1

Transaction 2BEGIN

Transaction1

BEGINUPDATE t WHERE id=14

UPDATE t WHERE id=14...

COMMITCOMMIT

ERROR due row conflict

system 2

...ERROR 1213 (40001): Deadlock found when trying to get lock; try restarting transaction

Page 56: Synchronous Replication For MySQL with Percona XtraDB Cluster

56Optimistic  Locking

Page 57: Synchronous Replication For MySQL with Percona XtraDB Cluster

57Features

• Synchronous  Replication  • Multi  Master  • Parallel  Applying  • Quorum  Based  • Certification/Optimistic  Locking  • Automatic  Node  Provisioning

Page 58: Synchronous Replication For MySQL with Percona XtraDB Cluster

Automatic  Node  Provisioning

• When  a  node  joins  the  cluster:  • the  data  is  automatically  copied    • when  finished:  the  new  node  is  automatically  ready  and  accepting  connections  

• 2  different  types  of  joining:  – SST  (state  snapshot  transfer):  full  copy  of  the  data  – IST  (incremental  state  transfer):  send  only  the  missing  writesets  (if  available)  

58

Page 59: Synchronous Replication For MySQL with Percona XtraDB Cluster

StateTransfer  Summary

Full data!SST

Incremental!IST

New node

Node long!time!

disconnected

Node!disconnected!

short time

59

Page 60: Synchronous Replication For MySQL with Percona XtraDB Cluster

Snapshot  State  Transfer

mysqldump

Small!databases

rsync

Donor!disconnected!for copy time

Faster

XtraBackup

Donor!available

Slower

60

Page 61: Synchronous Replication For MySQL with Percona XtraDB Cluster

Incremental  State  Transfer

Node was!in the cluster

Disconnected!for maintenance

Node!crashed

61

Page 62: Synchronous Replication For MySQL with Percona XtraDB Cluster

Automatic  Node  Provisioning

writes

writeswrites

new node joining

data is copied via SST or IST

62

Page 63: Synchronous Replication For MySQL with Percona XtraDB Cluster

Automatic  Node  Provisioning

writes

writeswrites

new node joiningwhen ready

writes

63

Page 64: Synchronous Replication For MySQL with Percona XtraDB Cluster

PXC  with  a  Load  balancer

• PXC  is  often  integrated  with  a  load  balancer  • service  can  be  checked  using  clustercheck  or  pyclustercheck  

• The  load  balancer  can  • be  a  dedicated  layer  • integrated  at  application  layer  • integrated  at  database  layer

64

Page 65: Synchronous Replication For MySQL with Percona XtraDB Cluster

Dedicated  shared  HAProxy  

application server 1 application server 2 application server 3

PXC node 1 PXC node 2 PXC node 3

HA PROXY

65

Page 66: Synchronous Replication For MySQL with Percona XtraDB Cluster

Dedicated  shared  HAProxy  

application server 1 application server 2 application server 3

PXC node 1 PXC node 2 PXC node 3

HA PROXY

66

Page 67: Synchronous Replication For MySQL with Percona XtraDB Cluster

Dedicated  shared  HAProxy  

application server 1 application server 2 application server 3

PXC node 1 PXC node 2 PXC node 3

HA PROXY

SST

available_when_donor=0

67

Page 68: Synchronous Replication For MySQL with Percona XtraDB Cluster

HAProxy  on  application  side

application server 1 application server 2 application server 3

PXC node 1 PXC node 2 PXC node 3

68

HA PROXY HA PROXY HA PROXY

Page 69: Synchronous Replication For MySQL with Percona XtraDB Cluster

Agenda

• Default  asynchronous  MySQL  Replication  • Percona  XtraDB  Cluster:  • Introduction  /  Features  /  Load  Balancing  

• Use  Cases:  • High  Availability  /  WAN  Replication  /  Read  Scaling  

• Limitations  

69

Page 70: Synchronous Replication For MySQL with Percona XtraDB Cluster

70Use  Cases

• High  Availability  • WAN  Replication  • Read  Scaling

Page 71: Synchronous Replication For MySQL with Percona XtraDB Cluster

High  Availability   71

• Each  node  is  the  same  (no  master-­‐slave)  • Consistency  ensured,  no  data  loss  • Quorum  avoids  split-­‐brain  • Cluster  issues  are  immediately  handled  on  • no  ‘failover’  necessary  • no  external  scripts,  no  SPOF

Page 72: Synchronous Replication For MySQL with Percona XtraDB Cluster

72WAN  replication

MySQL

MySQL

MySQL

• No  impact  on  reads  • No  impact  within  a  trx  • Communication  only  happens  during

COMMIT  (or  if  autocommit=1)  • Use  higher  timeouts  and

send  windows

Page 73: Synchronous Replication For MySQL with Percona XtraDB Cluster

• Beware  of  increased  latency  • Within  EUROPE  EC2  • COMMIT:  0.005100  sec  

• EUROPE  <-­‐>  JAPAN  EC2  • COMMIT:  0.275642  sec

73WAN  replication  -­‐  latency

MySQL

MySQL

MySQL

Page 74: Synchronous Replication For MySQL with Percona XtraDB Cluster

74WAN  replication  with  MySQL  asynchronous  replication

MySQL

MySQL

MySQL

•You  can  mix  both  types  of  replication  •Good  option  on  slow  WAN  link  •Requires  more  nodes

MySQL

MySQL

MySQL

MySQL

MySQL MySQL

Page 75: Synchronous Replication For MySQL with Percona XtraDB Cluster

Cluster  Segmentation  -­‐  WAN 75

Page 76: Synchronous Replication For MySQL with Percona XtraDB Cluster

76Read  Scaling

• No  Read/Write  Splitting  necessary  in  App.  • Write  to  all  nodes(*)

Page 77: Synchronous Replication For MySQL with Percona XtraDB Cluster

Agenda

• Default  asynchronous  MySQL  Replication  • Percona  XtraDB  Cluster:  • Introduction  /  Features  /  Load  Balancing  

• Use  Cases:  • High  Availability  /  WAN  Replication  /  Read  Scaling  

• Limitations

77

Page 78: Synchronous Replication For MySQL with Percona XtraDB Cluster

78Limitations

• Supports  only  InnoDB  tables  • MyISAM  support  will  most  likely  stay  in  alpha.  

• The  weakest  node  limits  write  performance  • All  tables  must  have  a  Primary  Key!

Page 79: Synchronous Replication For MySQL with Percona XtraDB Cluster

79Limitations

• Large  Transactions  are  not  recommended  if  you  write  on  all  nodes  simultaneously  

• Long  Running  Transactions  • If  the  workload  has  a  hotspot  then  

(frequently  writing  to  the  same  rows  across  multiple  nodes)  

• Solution:  Write  to  only  1  node

Page 80: Synchronous Replication For MySQL with Percona XtraDB Cluster

80Credits

• WSREP  patches  and  Galera  library  is  developed  by  Codership  Oyhttp://www.codership.com  

• Percona  &  Codership  will  present  on  Percona  Live  UK  2014,  Nov  11-­‐12   http://www.percona.com/live/london-­‐2013/

Page 81: Synchronous Replication For MySQL with Percona XtraDB Cluster

Summary

• Default  asynchronous  MySQL  Replication  • Percona  XtraDB  Cluster:  • Introduction  /  Features  /  Load  Balancing  

• Use  Cases:  • High  Availability  /  WAN  Replication  /  Read  Scaling  

• Limitations

81

Page 82: Synchronous Replication For MySQL with Percona XtraDB Cluster

82Resources

• Percona  XtraDB  Cluster  website:  http://www.percona.com/software/percona-­‐xtradb-­‐cluster/  

• Codership  website:   http://galeracluster.com  

• PXC  articles  on  mysqlperformanceblog:  http://www.mysqlperformanceblog.com/category/percona-­‐xtradb-­‐cluster/  

• Test  it  now  using  Vagrant  !  https://github.com/jayjanssen/vagrant-­‐perconahttps://github.com/lefred/percona-­‐clusterhttps://github.com/percona/xtradb-­‐cluster-­‐tutorial/tree/v2

Questions?Kenny  Gryp

<[email protected]>