patroni - ha postgresql made easy

35
1 Alexander Kukushkin PGConf US 2017, Jersey City Patroni - HA PostgreSQL made easy

Upload: alexander-kukushkin

Post on 13-Apr-2017

90 views

Category:

Technology


9 download

TRANSCRIPT

Page 1: Patroni - HA PostgreSQL made easy

1

Please write the title in all capital letters

Alexander Kukushkin

PGConf US 2017, Jersey City

Please write title, subtitle and speaker name in all capital letters

Patroni - HA PostgreSQL made easy

Page 2: Patroni - HA PostgreSQL made easy

2

Please write the title in all capital letters

Put images in the grey dotted box "unsupported placeholder"

Use bullet points to summarize information rather than writing long paragraphs in the text box

ABOUT ME

Alexander KukushkinDatabase Engineer @ZalandoTechEmail: [email protected]: @cyberdemn

Page 3: Patroni - HA PostgreSQL made easy

3

Please write the title in all capital letters

Put images in the grey dotted box "unsupported placeholder"

ZALANDO AT A GLANCE

~3.6billion EURO

net sales 2016

~165million

visitspermonth

>12,000employees inEurope

50%return rate across all categories

~20millionactive customers

~200,000product choices

>1,500brands

15countries

Page 4: Patroni - HA PostgreSQL made easy

4

Please write the title in all capital letters

ZALANDO TECHNOLOGY

BERLIN

Page 5: Patroni - HA PostgreSQL made easy

5

Please write the title in all capital letters

ZALANDO TECHNOLOGY

BERLINDORTMUNDDUBLIN

HELSINKI

ERFURT

MÖNCHENGLADBACH

HAMBURG

Page 6: Patroni - HA PostgreSQL made easy

6

Please write the title in all capital letters

Put images in the grey dotted box "unsupported placeholder"

Use bullet points to summarize information rather than writing long paragraphs in the text box

ZALANDO TECHNOLOGY

● > 150 databases in DC

● > 130 databases on AWS

● > 1600 tech employees

● We are hiring!

Page 7: Patroni - HA PostgreSQL made easy

7

Please write the title in all capital letters

Put images in the grey dotted box "unsupported placeholder"

Use bullet points to summarize information rather than writing long paragraphs in the text box

POSTGRESQL

● Rock-solid by default

● Transactional DDL

● Standard-compliant modern SQL

● Blazing performance

● PostgreSQL is a community

The world’s most advanced open-source database

Page 8: Patroni - HA PostgreSQL made easy

8

Please write the title in all capital letters

Put images in the grey dotted box "unsupported placeholder"

RUNNING DATABASES AT SCALE

Page 9: Patroni - HA PostgreSQL made easy

9

Please write the title in all capital letters

Put images in the grey dotted box "unsupported placeholder"

RUNNING DATABASES AT SCALE

Page 10: Patroni - HA PostgreSQL made easy

10

Please write the title in all capital letters

Use bullet points to summarize information rather than writing long paragraphs in the text box

CLOUD DATABASES

● Rapid deployments

● Commodity hardware (cattle vs pets)

● Standard configuration and automatic tuning

Page 11: Patroni - HA PostgreSQL made easy

11

Page 12: Patroni - HA PostgreSQL made easy

12

Please write the title in all capital letters

Put images in the grey dotted box "unsupported placeholder"

Use bullet points to summarize information rather than writing long paragraphs in the text box

AUTOMATIC FAILOVER

“PostgreSQL does not provide the system software required to identify a failure on the primary and notify the standby database server.”

CC0 Public Domain

Page 13: Patroni - HA PostgreSQL made easy

13

Please write the title in all capital letters

Use bullet points to summarize information rather than writing long paragraphs in the text box

EXISTING AUTOMATIC FAILOVER SOLUTIONS

● Promote a replica when the master is not responding○ Split brain/potentially many masters

● Use one monitor node to make decisions○ Monitor node is a single point of failure

○ Former master needs to be killed (STONITH)

● Use multiple monitor nodes○ Distributed consistency problem

Page 14: Patroni - HA PostgreSQL made easy

14

Please write the title in all capital letters

Put images in the grey dotted box "unsupported placeholder"

DISTRIBUTED CONSISTENCY PROBLEM

https://www.flickr.com/photos/kevandotorg

Page 15: Patroni - HA PostgreSQL made easy

15

Please write the title in all capital letters

Put images in the grey dotted box "unsupported placeholder"

Use bullet points to summarize information rather than writing long paragraphs in the text box

PATRONI APPROACH

● Use Distributed Configuration System (DCS): Etcd, Zookeeper or Consul

● Built-in distributed consensus (RAFT, Zab)

● Session/TTL to expire data (i.e. master key)

● Key-value storage for cluster information

● Atomic operations (CAS)

● Watches for important keys

Page 16: Patroni - HA PostgreSQL made easy

16

Please write the title in all capital letters

Use bullet points to summarize information rather than writing long paragraphs in the text box

DCS STRUCTURE

● /service/cluster/○ config○ initialize○ members/

■ dbnode1■ dbnode2

○ leader○ optime/

■ leader○ failover

Page 17: Patroni - HA PostgreSQL made easy

17

Please write the title in all capital letters

Use bullet points to summarize information rather than writing long paragraphs in the text box

● initialize○ "key": "/service/testcluster/initialize",

"value": "6303731710761975832"

● leader/optime○ "key": "/service/testcluster/optime/leader",

"value": "67393608"

● config○ "key": "/service/testcluster/config",

"value": "{\"postgresql\":{\"parameters\":{\"max_connections\":\"200\"}}}"

KEYS THAT NEVER EXPIRE

Page 18: Patroni - HA PostgreSQL made easy

18

Please write the title in all capital letters

Use bullet points to summarize information rather than writing long paragraphs in the text box● leader

○ "key": "/service/testcluster/leader",

"value": "dbnode2",

"ttl": 22

● members○ "key": "/service/testcluster/members/dbnode2",

“value": "{\"role\":\"master\",\"state\":\"running\",\"xlog_location\":67393608,

\"conn_url\":\"postgres://172.17.0.3:5432/postgres\",

\"api_url\":\"http://172.17.0.3:8008/patroni\"}",

"ttl": 22

KEYS WITH TTL

Page 19: Patroni - HA PostgreSQL made easy

19

Please write the title in all capital letters

Use bullet points to summarize information rather than writing long paragraphs in the text box

● Initialization race

● initdb by a winner of an initialization race

● Waiting for the leader key by the rest of the nodes

● Bootstrapping of non-leader nodes (pg_basebackup)

BOOTSTRAPPING OF A NEW CLUSTER

Page 20: Patroni - HA PostgreSQL made easy

20

Please write the title in all capital letters

Use bullet points to summarize information rather than writing long paragraphs in the text box

● Update the leader key or demote if update failed

● Write the leader/optime (xlog position)

● Update the member key

● Add/delete replication slots for other members

EVENT LOOP OF A RUNNING CLUSTER (MASTER)

Page 21: Patroni - HA PostgreSQL made easy

21

Please write the title in all capital letters

Use bullet points to summarize information rather than writing long paragraphs in the text box● Check that the cluster has a leader

○ Check recovery.conf points to the correct leader

○ Join the leader race if a leader is not present

● Add/delete replication slots for cascading replicas

● Update the member key

EVENT LOOP OF A RUNNING CLUSTER (REPLICA)

Page 22: Patroni - HA PostgreSQL made easy

22

Please write the title in all capital letters

Use bullet points to summarize information rather than writing long paragraphs in the text box

● Check whether the member is the healthiest

○ Evaluate its xlog position against all other members

● Try to acquire the leader lock

● Promote itself to become a master after acquiring the lock

LEADER RACE

Page 23: Patroni - HA PostgreSQL made easy

23

Please write the title in all capital letters

Use bullet points to summarize information rather than writing long paragraphs in the text box

LEADER RACE

CREATE (“/leader”, “A”, ttl=30, prevExists=False)

CREATE (“/leader”, “

B”, ttl=30, prevExists=False)

Success

Fail

promote

A

B

Page 24: Patroni - HA PostgreSQL made easy

24

Please write the title in all capital letters

LIVE DEMO

Page 25: Patroni - HA PostgreSQL made easy

25

Please write the title in all capital letters

Use bullet points to summarize information rather than writing long paragraphs in the text box

PATRONI FEATURES

● Manual and Scheduled Failover

● Synchronous mode

● Attach the old master with pg_rewind

● Customizable replica creation methods

● Linux watchdog support (coming soon)

● Pause (maintenance) mode

● patronictl

Page 26: Patroni - HA PostgreSQL made easy

26

Please write the title in all capital letters

Use bullet points to summarize information rather than writing long paragraphs in the text box

● Change Patroni/PostgreSQL parameters via Patroni REST API

○ Store them in DCS and apply dynamically on all nodes

● Ensure identical configuration of the following parameters on all members:

○ ttl, loop_wait, retry_timeout, maximum_lag_on_failover

○ wal_level, hot_standby

○ max_connections, max_prepared_transactions, max_locks_per_transaction,

max_worker_processes, track_commit_timestamp, wal_log_hints

○ wal_keep_segments, max_replication_slots

● Inform the user that PostgreSQL needs to be restarted (pending_restart flag)

DYNAMIC CONFIGURATION

Page 27: Patroni - HA PostgreSQL made easy

27

Please write the title in all capital letters

Put images in the grey dotted box "unsupported placeholder"

Use bullet points to summarize information rather than writing long paragraphs in the text box

BUILDING HA POSTGRESQL BASED ON PATRONI

● Client traffic routing

○ patroni callbacks

○ confd + haproxy, pgbouncer

● Backup and recovery

○ WAL-E, barman

● Monitoring

○ Nagios, zabbix, zmonImage by flickr user https://www.flickr.com/photos/brickset/

Page 28: Patroni - HA PostgreSQL made easy

28

Please write the title in all capital letters

SPILO: DOCKER + PATRONI + WAL-E + AWS/K8S

Page 29: Patroni - HA PostgreSQL made easy

29

Please write the title in all capital letters

SPILO DEPLOYMENT

Page 30: Patroni - HA PostgreSQL made easy

30

Please write the title in all capital letters

AUTOMATIC FAILOVER IS HARD

Page 31: Patroni - HA PostgreSQL made easy

31

Please write the title in all capital letters

Use bullet points to summarize information rather than writing long paragraphs in the text box

WHEN SHOULD THE MASTER DEMOTE ITSELF?

● Chances of data loss vs write availability

● Avoiding too many master switches (retry_timeout, loop_wait, ttl)

● 2 x retry_timeout + loop_wait < ttl

● Zookeeper and Consul session duration quirks

Page 32: Patroni - HA PostgreSQL made easy

32

Please write the title in all capital letters

Use bullet points to summarize information rather than writing long paragraphs in the text box

CHOOSING A NEW MASTER

● Reliability/performance of the host or connection○ nofailover tag

● XLOG position○ highest xlog position = the best candidate

○ xlog > leader/optime - maximum_lag_on_failover■ maximum_lag_on_failover > size of WAL segment (16MB) for disaster recovery

Page 33: Patroni - HA PostgreSQL made easy

33

Please write the title in all capital letters

Use bullet points to summarize information rather than writing long paragraphs in the text box

ATTACHING THE OLD MASTER BACK AS REPLICA

● Diverged timelines after the former master crash

● pg_rewind○ use_pg_rewind

○ remove_data_directory_on_rewind_failure

Page 34: Patroni - HA PostgreSQL made easy

34

Please write the title in all capital letters

Use bullet points to summarize information rather than writing long paragraphs in the text box

USEFUL LINKS

● Spilo: https://github.com/zalando/spilo

● Confd: http://www.confd.io

● Etcd: https://github.com/coreos/etcd

● RAFT: http://thesecretlivesofdata.com/raft/

Page 35: Patroni - HA PostgreSQL made easy

35

Questions?https://github.com/zalando/patroni