Database Sync - Multi-Slave Environment


Database Sync for Multi-Slave Environments

Database Sync - Multi-Slave Environment

Background

Database Sync - Multi-Slave Environment

Background (continued)

Database Sync - Multi-Slave Environment

Goals

Database Sync - Multi-Slave Environment

Method

Database Sync - Multi-Slave Environment

Implementation - API

Database Sync - Multi-Slave Environment

Implementation - Example: Database

Database Sync - Multi-Slave Environment

Implementation - Example: Sync Parameters

gaugesgauges_daily
Unique IDid
Timestamp Column 
Insert OnlyFalse
Unique ID 
Timestamp Columndate
Insert OnlyTrue
to master: SELECT * FROM gaugesto replica: SELECT MAX(date) FROM gauges_daily
to master: SELECT * FROM gauges_daily WHERE date > %(max)s
UPDATE gauges
SET name=%(name)s, country=%(country)s, ...
WHERE id = %(id)s
 
INSERT INTO gauges (id, name, country, ...)
VALUES (%(id)s, %(name)s, %(country)s, ...)
INSERT INTO gauges_daily (id, date, value)
VALUES (%(id)s, %(date)s, %(value)s)
Database Sync - Multi-Slave Environment

Implementation - Example: Python Code

        db_master_conn = psycopg2.connect('dbname=tva user=postgres')
db_slave_conn = psycopg2.connect('dbname=tva2 user=postgres')
table_params = {'table': 'gauges',
'unique_id': 'id', 'timestamp': None, 'insert_only': False}
dbst = DBSyncTable(**table_params)
ins, upd = dbst.perform_sync(db_master_conn, db_slave_conn)

table_params = {'table': 'gauges_daily',
'unique_id': None, 'timestamp': 'date', 'insert_only': True}
dbst = DBSyncTable(**table_params)
ins, upd = dbst.perform_sync(db_master_conn, db_slave_conn)

Database Sync - Multi-Slave Environment

Exercises Left for the Reader

Database Sync - Multi-Slave Environment

Exercise - Read Sync Parameters from Database


Table: sync_info
tableunique_idtimestampinsert_onlylabelordercron_spec
gaugesid Falsegdaily110 0 * * *
gauges_daily dateTruegdaily210 0 * * *
Database Sync - Multi-Slave Environment

Questions?