D t b
R li ti
Fabian Mauchle Fachhochschule Rapperswil, 18.12.2008
Database Replication
with MySQL and PostgresSQL
Schedule
• Theory
– Why Replication
– Replication Layout and Types
– Conflicts
• Usage Scenarios
• Implementations
Fabian Mauchle Fachhochschule Rapperswil, 18.12.2008
• Test Setup and Scenario
• Conclusion
• Questions?
• Bibliography
2
Why Replication?
• Few Data, Few Users:
9
• Lots of Data, Few Users:
9
Fabian Mauchle Fachhochschule Rapperswil, 18.12.2008
• Few Data, Lots of Users:
9
• Lots of Data, Lots of Users:
?
4
How Replication?
• Use multiple servers! (with replication)
• Requirements
– Transparency
– Consistency (ACID)
Fabian Mauchle 5 Fachhochschule Rapperswil, 18.12.2008
Dimensions of
Database Replication
• Number
N
Fabian Mauchle 7 Fachhochschule Rapperswil, 18.12.2008
Dimensions of
Database Replication
• Bandwidth
• Delay
d
Fabian Mauchle 8 Fachhochschule Rapperswil, 18.12.2008
Replication Layout and Type
Synchronous
Asynchronous
Master-Slave
Fabian Mauchle Fachhochschule Rapperswil, 18.12.2008
Multi-Master
10
Replication Layout and Type
• Master-Slave
read update read
Fabian Mauchle 11 Fachhochschule Rapperswil, 18.12.2008
replicate
Replication Layout and Type
• Multi-Master
read update read update
Replication Layout and Type
• Asynchronous
Update
Fabian Mauchle 13 Fachhochschule Rapperswil, 18.12.2008
p
Commit OK
Commit
Replicate
Replication Layout and Type
• Synchronous
Update
Fabian Mauchle 14 Fachhochschule Rapperswil, 18.12.2008
p
Commit OK
Commit
Update
Commit
Commit OK
Conflicts
Conflicts
Update
Commit
Update
Commit
Fabian Mauchle 16 Fachhochschule Rapperswil, 18.12.2008
Commit OK
Commit
Replicate
Commit OK
Commit
Replicate
Conflicts
1. Avoid Conflicts!
2. Avoid Conflicts!
3. Use Conflict Handler
4. Write Conflict Handler
5. Resolve Manually
Fabian Mauchle 17 Fachhochschule Rapperswil, 18.12.2008
Conflicts
• Update Conflict
Id
Text
1
„A“
update
update
Id
Text
1
„B“
Id
Text
1
„C“
Conflicts
• Uniqueness Conflict
Id
Text
1
„A“
insert
insert
Fabian Mauchle 19 Fachhochschule Rapperswil, 18.12.2008
Id
Text
1
„A“
2
„B“
Id
Text
1
„A“
2
„C“
Conflicts
• Delete Conflict
Id
Text
1
„A“
delete
update
Fabian Mauchle 20 Fachhochschule Rapperswil, 18.12.2008
Id
Text
1
„A“
Id
Text
2
„C“
Usage Scenarios
• High Performance Cluster
– Synchronous Multi-Master (req. low distance)
– Asynchronous Multi-Master (req. conflict resolution)
Fabian Mauchle 22 Fachhochschule Rapperswil, 18.12.2008
read
update
Usage Scenarios
• Distributed Databases (large distance)
– Asynchronous Master-Slave (few updates)
– Asynchronous Multi-Master (req. conflict resolution)
Fabian Mauchle 23 Fachhochschule Rapperswil, 18.12.2008
Usage Scenarios
• Backup and Data Warehouse
– Asynchronous Master-Slave
read update read
Public Access
Backup
Data Warehouse
Main Application
Implementations
Fabian Mauchle 25 / 30 Fachhochschule Rapperswil, 18.12.2008
Implementations
Synchronous
Asynchronous
Fabian Mauchle 26 Fachhochschule Rapperswil, 18.12.2008
Synchronous
Asynchronous
Master-Slave
Use Multi-Master
9
Multi-Master
9
8
(see next slide)
• Multi-Master Replication by circular Master-Slave Replication
by (Maxia, 2006)
• No automatic conflict resolution
• MySQL replication internal
Implementations
Query:
update...
Fabian Mauchle 28 Fachhochschule Rapperswil, 18.12.2008
1. insert…
2. update...
Transaction-log
1. insert…
2. update...
Transaction-log
Replicate
Replay
Query
Server 1 (master)
Server 2 (slave)
Implementations
Fabian Mauchle 29 Fachhochschule Rapperswil, 18.12.2008
No built-in replication support!
Implementations
Synchronous
Asynchronous
Master-Slave
Use Multi-Master
-Slony-I
-Burcado
• PostgreSQL extensions
Burcado
Multi-Master
-Pgpool
Implementations
Subscribers Other Subscriber Origin User User sl_log sl_log CREATE CREATEFabian Mauchle Fachhochschule Rapperswil, 18.12.2008 Slonik ... CREATE TABLE modifications CREATE TRIGGER fills sl_log TRIGGER CREATE prevents 31 Source: (Momjian, 2008)
Test Setup and Scenario
Fabian Mauchle 32 / 30 Fachhochschule Rapperswil, 18.12.2008
Test Setup and Scenario
• Test against conflict behaviour
• Replication delay not considered
• Performance under high load not considered
VM1Test Setup and Scenario
• Database Table
CREATE TABLE test(
INT id PRIMARY KEY AUTO_INCREMENT,
CHAR(10) text NOT NULL UNIQUE,
Fabian Mauchle 34 Fachhochschule Rapperswil, 18.12.2008
CHAR(10) text NOT NULL UNIQUE,
INT counter NOT NULL);
MySQL Results
• Using Multi-Master layout by (Maxia, 2006)
• Remember: no conflict resolution!
Conflict
Result
Fabian Mauchle 35 Fachhochschule Rapperswil, 18.12.2008
Auto Increment
9
Update Conflict
Breaks Consistency
Uniqueness Conflict
Breaks Replication
Delete Conflict
Breaks Consistency
Conclusion
• Few Data, Few Users:
9
• Lots of Data, Few Users:
9
Fabian Mauchle Fachhochschule Rapperswil, 18.12.2008
• Few Data, Lots of Users:
9
• Lots of Data, Lots of Users:
9
but...
37
Conclusion
• Consider at design
• Select layout carefuly
• Try to avoid conflicts
• Closely monitor replication status
(especialy with MySQL Multi-Master Replication)
Fabian Mauchle 38 Fachhochschule Rapperswil, 18.12.2008
Bibliography
•
Keating, B. (2001). Challenges Involved in Multimaster Replication. Retrieved 2008,
from
www.dbspecialists.com/files/presentations/mm_replication.html
•
Maxia, G. (2006, 20 4). Advanced MySQL Replication. Retrieved 2008, from
www.onlamp.com/pub/a/onlamp/2006/04/20/advanced-mysql-replication.html
•
Momjian, B. (2008, 12). Replication Solutions. Retrieved 2008, from
http://momjian.us/main/writings/pgsql/replication.pdf
•
MySQL. (n.d.). Mysql Documentation. Retrieved 11 17, 2008, from
http://dev.mysql.com/doc/
•
pgReplicator. (n.d.). PostgreSQL Replicator. Retrieved 2008, from
htt //
li t
f
t
Fabian Mauchle Fachhochschule Rapperswil, 18.12.2008