mysql user camp: multi-threaded slaves
DESCRIPTION
The agenda of this talk was to introduce MySQL Replication and then follow it up with Multi-threaded slaves(MTS) support. The presentation introduces Multi threading slaves by database which is a part of MySQL-5.6 as well as multi-threading policy introduced in MySQL-5.7.2. Finally there is a brief coverage of the new replication monitoring tables to monitor MySQL Replication. These tables are part of MySQL Performance Schema.TRANSCRIPT
MySQL Replication and
Multi-threaded Slaves
Shivji Kumar Jha, Software Developer,MySQL Replication Team
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. | MySQL User Camp | Bangalore, India | 8 November 2013 | `2
Safe Harbour Statement
The following is intended to outline our general product direction. It is
intended for information purposes only, and may not be incorporated into any
contract.
It is not a commitment to deliver any material, code, or functionality, and
should not be relied upon in making purchasing decisions. The development,
release, and timing of any features or functionality described for Oracle’s
products remains at the sole discretion of Oracle.
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. | MySQL User Camp | Bangalore, India | 8 November 2013 |`3
Agenda Why Replication?
Replication Internals- A Simple Introduction
Why Multi-threaded Slaves (MTS)?
Different policies for Multi-threading Slaves
Keeping track of replication threads
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. | MySQL User Camp | Bangalore, India | 8 November 2013 |`4
Replication: Copy Changes Master → Slave
MySQL Master Server
– Changes data
– Sends changes to slave
MySQL Slave Server– Receives changes from master
– Applies received changes to database
M S
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. | MySQL User Camp | Bangalore, India | 8 November 2013 |`5
Why Replication? – Scalability
Read scale-out
M S
write clients read clients
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. | MySQL User Camp | Bangalore, India | 8 November 2013 |`6
Why Replication? – Scalability
Read scale-out
M S
write clients read clients
Morereads?
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. | MySQL User Camp | Bangalore, India | 8 November 2013 |`7
Why Replication? – Scalability
Read scale-out
M S
write clients read clients
Morereads?More
slaves!
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. | MySQL User Camp | Bangalore, India | 8 November 2013 |`8
Why Replication? – Scalability
Read scale-out
M SS
S
S
M
write clients read clientsread clients
write clients
Morereads?More
slaves!
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. | MySQL User Camp | Bangalore, India | 8 November 2013 |`9
Why Replication? – Redundancy
If master crashes, promote slave to master
C
B
A
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. | MySQL User Camp | Bangalore, India | 8 November 2013 |`10
Why Replication? – Redundancy
If master crashes, promote slave to master
C
B
ACrash
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. | MySQL User Camp | Bangalore, India | 8 November 2013 |`11
Why Replication? – Redundancy
If master crashes, promote slave to master
C
B
A
B is thenew master
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. | MySQL User Camp | Bangalore, India | 8 November 2013 |`12
Why Replication? – Disaster recovery
CB
BAAC
Image fromwww.ginkgomaps.com
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. | MySQL User Camp | Bangalore, India | 8 November 2013 |`13
But how do you copy changes to slave?
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. | MySQL User Camp | Bangalore, India | 8 November 2013 |`14
But how do you copy changes to slave?
Are there LOGS floating around?
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. | MySQL User Camp | Bangalore, India | 8 November 2013 |`15
Introducing replication LOGS...
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. | MySQL User Camp | Bangalore, India | 8 November 2013 |`16
Replication Logs
MySQL Master Server
– Changes data (Writes)
– Saves changes in Binary log
MySQL Slave Server– Copies changes to Relay log
– Reads Relay log
– Applies changes.
M
S
Binarylog
Relaylog
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. | MySQL User Camp | Bangalore, India | 8 November 2013 |`17
All Changes Written to Binary Log
A
binary log
Client
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. | MySQL User Camp | Bangalore, India | 8 November 2013 |`18
All Changes Written to Binary Log
A
binary log
Clientcreate table t (a int);
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. | `19
All Changes Written to Binary Log
create...
A
binary log
Client
Table t
create table t (a int);
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. | MySQL User Camp | Bangalore, India | 8 November 2013 |`20
All Changes Written to Binary Log
create...
A
binary log
Client
Table t
create table t (a int);insert into t values (1);
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. | MySQL User Camp | Bangalore, India | 8 November 2013 |`21
All Changes Written to Binary Log
create...insert...A
binary log
Client
Table t1
create table t (a int);insert into t values (1);
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. | MySQL User Camp | Bangalore, India | 8 November 2013 |`22
Slave Initiates Replication
B
relay log
A
binary log
Client
1. Slave sendsrequest to start replication
to master
2. Master sendsstream of replication data
to slave
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. | MySQL User Camp | Bangalore, India | 8 November 2013 |`23
create...
A
binary log
Client
B
relay log
Slave Copies, Applies Same Changes
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. | MySQL User Camp | Bangalore, India | 8 November 2013 |`24
Table t
create...
A
binary log
Client create...
B
relay log
Slave Copies, Applies Same Changes
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. | MySQL User Camp | Bangalore, India | 8 November 2013 |`25
create...
Slave Copies, Applies Same Changes
Table t Table t
create...
A
binary log
Client
B
relay log
create...
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. | MySQL User Camp | Bangalore, India | 8 November 2013 |`26
create...insert...
Table t1
create...insert...A
binary log
Client
Table t
B
relay log
create...
Slave Copies, Applies Same Changes
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. | MySQL User Camp | Bangalore, India | 8 November 2013 |`27
create...insert...
Table t1
create...insert...A
binary log
Client
Table t
B
relay log
create...insert...
1
Slave Copies, Applies Same Changes
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. | MySQL User Camp | Bangalore, India | 8 November 2013 |`28
OK makes sense !
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. | MySQL User Camp | Bangalore, India | 8 November 2013 |`29
But I see clients applying changes at Master, who applies at Slave?
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. | MySQL User Camp | Bangalore, India | 8 November 2013 |`30
But I see clients applying changes at Master, who applies at Slave?
Do you have some internal replication threads?
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. | MySQL User Camp | Bangalore, India | 8 November 2013 |`31
Introducing replication THREADS...
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. | MySQL User Camp | Bangalore, India | 8 November 2013 |`32
create...insert...
Replication Threads (4.0-5.5)
Bcreate...insert...A
binary log
Client
create...insert...
relay log
IO THREAD SQL THREAD
Writes into relay log at slaveReads from relay log.
Applies transactions at slave
network
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. | MySQL User Camp | Bangalore, India | 8 November 2013 |`33
insert...
Replication Threads (4.0-5.5)
Bcreate...insert...Insert...
A
binary log
Client
create...insert...Insert...
relay log
IO THREAD SQL THREADnetwork
ClientClientMore clients on master...Create... insert...
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. | MySQL User Camp | Bangalore, India | 8 November 2013 |`34
insert...
Replication Threads (4.0-5.5)
Bcreate...insert...Insert...
A
binary log
Client
create...insert...Insert...
relay log
IO THREAD SQL THREADnetwork
ClientClientMore clients on master...Create... insert...
Slave starts to lag behind master
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. | MySQL User Camp | Bangalore, India | 8 November 2013 |`35
insert...
Replication Threads (4.0-5.5)
Bcreate...insert...Insert...
A
binary log
Client
create...insert...Insert...
relay log
IO THREAD SQL THREADnetwork
ClientClientMore clients on master...Create... insert...
Table t123
Table t12
Slave starts to lag behind master
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. | MySQL User Camp | Bangalore, India | 8 November 2013 |`36
Lets Parallelize slave as well...
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. | MySQL User Camp | Bangalore, India | 8 November 2013 |`37
Lets Parallelize slave as well...
Introducing parallelization byDatabase
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. | MySQL User Camp | Bangalore, India | 8 November 2013 |`38
create...
Replication Threads (5.6+)
B
create(db1)insert(db2)insert(db3)
A
binary log
Client
create(db1)insert(db2)insert(db3)relay log
IO THREAD
COORDINATOR THREAD
Reads & assigns to worker(s)
db1
network
wORKERS
Worker threads applyconcurrently on diff dbs
db2
db3
ClientClient
insert... insert...
create..
insert...
insert..
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. | MySQL User Camp | Bangalore, India | 8 November 2013 |`39
Replication Threads (5.6+)
Setting up Parallelization by database
mysql> STOP SLAVE;
mysql> SET GLOBAL slave_parallel_workers=1;
mysql> START SLAVE;
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. | MySQL User Camp | Bangalore, India | 8 November 2013 |`40
Replication Threads (5.6+)
More databases? More workers!
mysql> STOP SLAVE;
mysql> SET GLOBAL slave_parallel_workers=2;
mysql> START SLAVE;
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. | MySQL User Camp | Bangalore, India | 8 November 2013 |`41
What if I have aa loaded
Database?
Image credits: http://www.easetechnology.co.uk/ask-an-expert/
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. | MySQL User Camp | Bangalore, India | 8 November 2013 |`42
Lets Parallelize slave somewhat similar to master
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. | MySQL User Camp | Bangalore, India | 8 November 2013 |`43
Introducing parallelization byMaster concurrency
Lets Parallelize slave somewhat similar to master
5.7.2
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. | MySQL User Camp | Bangalore, India | 8 November 2013 |`44
insert...
Replication Threads (4.0-5.5)
Bcreate...insert...Insert...
A
binary log
Client
create...insert...Insert...
relay log
IO THREAD SQL THREADnetwork
ClientClientMore clients on master...Create... insert...
Table t123
Table t12REVISITING THE OLD SLIDE !!!
Slave starts to lag behind master
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. | MySQL User Camp | Bangalore, India | 8 November 2013 |`45
create...
Replication Threads (5.7+)
B
create...insert...Insert...
A
binary log
Client
create...insert...Insert...relay log
IO THREAD
COORDINATOR THREAD
db1
network
wORKERS
Worker threads applyconcurrently
db2
ClientClient
insert... insert...
capture parallelization info on master
Reads & assigns to worker(s)
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. | MySQL User Camp | Bangalore, India | 8 November 2013 |`46
create...
Replication Threads (5.7+)
B
create...insert...Insert...
A
binary log
Client
create...insert...Insert...relay log
IO THREAD
COORDINATOR THREAD
db1
network
wORKERS
Worker threads applyconcurrently
db2
ClientClient
insert... insert... Reads & assigns to worker(s)
Save parallelization info in binary log
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. | MySQL User Camp | Bangalore, India | 8 November 2013 |`47
create...
Replication Threads (5.7+)
B
create...insert...Insert...
A
binary log
Client
create...insert...Insert...relay log
IO THREAD
COORDINATOR THREAD
db1
network
wORKERS
Worker threads applyconcurrently
db2
ClientClient
insert... insert... Reads & assigns to worker(s)
Send parallelization info to slave
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. | MySQL User Camp | Bangalore, India | 8 November 2013 |`48
create...
Replication Threads (5.7+)
B
create...insert...Insert...
A
binary log
Client
create...insert...Insert...relay log
IO THREAD
COORDINATOR THREAD
db1
network
wORKERS
Worker threads applyconcurrently
db2
ClientClient
insert... insert... Reads & assigns to worker(s)
Use parallelization info to assign
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. | `49
Leverage parallelization information obtained from the execution on the master:— Transactions that reach the prepare phase on the same data snapshot
are non-contending;— Write to the binary log on which snapshot id each transaction prepared;
This identifies the commit parent of each transaction.— The commit parent is stepped every time transactions commit.
Meanwhile, at the slave:— Transactions with the same commit parent can be executed in parallel;— Commit sequence at the slave may not be the same as that on the
master, but it is still a correct execution history.
Replication Threads (5.7+)
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. | `50
Higher Slave Throughput: Intra-Schema MTS
T1
T2
T3
Time
Commit
Prepare
Concurrent Execution HistoryConcurrent Execution History on the Masteron the Master
commit_parent++
Execution
Prepare
Commit
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. | `51
Higher Slave Throughput: Intra-Schema MTS
T1
T2
T3
Time
Commit
Prepare
Concurrent Execution HistoryConcurrent Execution History on the Masteron the Master
commit_parent++
Execution
Prepare
Commit
Parallelon the Slave.
Not parallel on the slave.
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. | MySQL User Camp | Bangalore, India | 8 November 2013 |`52
Replication Threads (5.7+)
mysql> STOP SLAVE;
mysql> SET GLOBAL slave_parallel_workers=3;
mysql> START SLAVE;
mysql> SET GLOBAL slave_parallel_type=[ 'logical_clock' | 'database' ];
Setting up Parallelization by master concurrency
database=>parallelize by databaselogical_clock=>parallelize by master concurrency
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. | MySQL User Camp | Bangalore, India | 8 November 2013 |`53
Too many threads here and there!
How do I monitor?
Knew that was coming! Check out replication
Performance_Schema
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. | MySQL User Camp | Bangalore, India | 8 November 2013 |`54
Performance Schema (5.5+)
Inspect internal execution of the server at runtime Exposed within performance_schema database Records various run time statistics via in-built instrumentation points Tracks latency for various events:
File I/O, Mutexes, Read/Write Locks, Table I/O, Table Locks, Stages, Statements, Idle etc..
Its a long growing list
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. | MySQL User Camp | Bangalore, India | 8 November 2013 |`55
connection execution
configuration status
Coordinator thread / SQL thread Worker thread(s)
replication
Replication Performance Schema Tables (5.7+)
configuration status
5.7.2
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. | MySQL User Camp | Bangalore, India | 8 November 2013 |`56
The REPLICATION P_S Tables
We have six performance schema tables for replication: (MySQL-5.7.2):
replication_connection_configuration replication_connection_status (IO thread status) replication_execute_configuration replication_execute_status replication_execute_status_by_coordinator replication_execute_status_by_worker
Tables for monitoring MTS
5.7.2
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. | MySQL User Camp | Bangalore, India | 8 November 2013 |`57
Monitoring The Coordinator Thread
mysql> select * from performance_schema.replication_execute_status_by_coordinator\G
*************************** 1. row ***************************
THREAD_ID: 13
SERVICE_STATE: ON
LAST_ERROR_NUMBER: 0
LAST_ERROR_MESSAGE:
LAST_ERROR_TIMESTAMP: 0000-00-00 00:00:00
No error in thread
5.7.2
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. | MySQL User Camp | Bangalore, India | 8 November 2013 |`58
Monitoring The Coordinator Thread
mysql> select * from performance_schema.replication_execute_status_by_coordinator\G
*************************** 1. row ***************************
THREAD_ID: 13
SERVICE_STATE: ON
LAST_ERROR_NUMBER: 1146
LAST_ERROR_MESSAGE:...'Table 'test.t' doesn't exist'...
LAST_ERROR_TIMESTAMP: 2013-11-04 13:37:23
Oops! There was an error
5.7.2
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. | MySQL User Camp | Bangalore, India | 8 November 2013 |`59
mysql> select * from performance_schema.replication_execute_status_by_worker\G
*************************** 1. row *************************** WORKER_ID: 0 THREAD_ID: 16 SERVICE_STATE: ONLAST_SEEN_TRANSACTION: 2b3e03a6-453f-11e3-8668-0021ccb3570d:3 LAST_ERROR_NUMBER: 0 LAST_ERROR_MESSAGE: LAST_ERROR_TIMESTAMP: 0000-00-00 00:00:00
*************************** 2. row *************************** WORKER_ID: 1 THREAD_ID: 17 SERVICE_STATE: ON ...
Monitoring Worker Threads
mysql> SET GLOBAL slave_parallel_workers= 2;
one row per worker thread
5.7.2
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. | MySQL User Camp | Bangalore, India | 8 November 2013 |`60
mysql> select * from performance_schema.replication_execute_status_by_worker\G
*************************** 1. row *************************** WORKER_ID: 0 THREAD_ID: 16 SERVICE_STATE: ONLAST_SEEN_TRANSACTION: 2b3e03a6-453f-11e3-8668-0021ccb3570d:3 LAST_ERROR_NUMBER: 0 LAST_ERROR_MESSAGE: LAST_ERROR_TIMESTAMP: 0000-00-00 00:00:00
*************************** 2. row *************************** WORKER_ID: 1 THREAD_ID: 17 SERVICE_STATE: ON ...
Monitoring Worker Threads
mysql> SET GLOBAL slave_parallel_workers= 2;
The newest transaction this worker is aware of
5.7.2
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. | MySQL User Camp | Bangalore, India | 8 November 2013 |`61
mysql> select * from performance_schema.replication_execute_status_by_worker\G
*************************** 1. row *************************** WORKER_ID: 0 THREAD_ID: 16 SERVICE_STATE: ONLAST_SEEN_TRANSACTION: 2b3e03a6-453f-11e3-8668-0021ccb3570d:3 LAST_ERROR_NUMBER: 0 LAST_ERROR_MESSAGE: LAST_ERROR_TIMESTAMP: 0000-00-00 00:00:00
*************************** 2. row *************************** WORKER_ID: 1 THREAD_ID: 17 SERVICE_STATE: ON ...
Monitoring Worker Threads
mysql> SET GLOBAL slave_parallel_workers= 2;
No error in this worker thread
5.7.2
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. | MySQL User Camp | Bangalore, India | 8 November 2013 |`62
We Want Your
Feedback
MySQL-5.6 is our latest GA release.
A lot more to explore in MySQL-5.6, keep
reading...
MySQL-5.7 is our current development
branch.
We want your valuable feedback.
Suggest Features, report bugs, contribute
patches.
Help make MySQL-5.7 even better!
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. | MySQL User Camp | Bangalore, India | 8 November 2013 |`63
Summary
Introduction to MySQL Replication
Replication Logs
Replication Threads
Multi-threaded Slave (MTS)– Need for Parallelization– Parallelize by database– Parallelize by master concurrency
Keeping track of replication threads– Performance Schema– Replication info. in Performance Schema
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. | MySQL User Camp | Bangalore, India | 8 November 2013 |`64
Read More About
MySQLReplication
Find MySQL Official Documentation at
http://dev.mysql.com/doc/refman/5.7/en/replication.html
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. | MySQL User Camp | Bangalore, India | 8 November 2013 |`65
Read More About Multi-
Threaded Slaves
Parallelization by database - Luis's blog - Andrei's blog
Parallelization by master concurrency - Rohit's blog
Replication Performance Schema - Official MySQL documentation - Shiv's blog
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. | MySQL User Camp | Bangalore, India | 8 November 2013 |`66
Questions!Email: [email protected] Blog: shivjijha.blogspot.in