This document summarizes an organization's journey to creating a highly available PostgreSQL cluster using etcd for consensus and automation. They initially used MongoDB but required more data exploration capabilities. They investigated tools like repmgr with pgpool II and PostgreSQL streaming replication but these did not meet their requirements for automated failover without human interaction. They then tried Consul which worked for a prototype but had resource issues. They ultimately switched to etcd due to its consensus algorithm and features like TTL locks, which allowed them to build a production-ready automated HA PostgreSQL cluster with etcd in just one day.
3. How we started using PostgreSQL
MongoDB was a primary datastore
launched project to understand financial
metrics
required data exploration, which is brutal in
MongoDB
4. Our database product
our platform runs databases
these databases scale
automatically as a customer
increases data size
11. Learned from tools investigation
PostgreSQL should not be the canonical
store of its own state, investigated:
serf - not consensus based
consul - runs with consensus
etcd - run with conensus
12. Consul
we built the prototype on Consul
using:
locking
sessions
health checks
code at: https://github.com/MongoHQ/consul_ha
17. Consul
until, we ran into massive swap
allocation.
40 GB swap allocation.
fine for prototypes, not for production.
18. Results from Consul
HA PostgreSQL is possible
but, we need a tool which uses our
resources more wisely.
19. Switch to etcd
because of what we’d learned in
Consul, the switch to etcd took a
day to have a working sample
20. Modern etcd diagram
Start
Connect to
etcd?
Is data
directory
empty?
yes
Win race to set
initialization
key?
yes
Initialize
database
Take over
lead TTL
key
Start
PostgreSQL
as a
leaderless
Secondary
no
yes
Leader
owns key?
pg_basebackup
from leader
Do I own
leader
key?
Acquire
leader lock?
yes
Update
leader
TTL lock
yes
Promote
to leader
Is leader
key
owned?
no
Am I
following
the correct
leader?
yes
Am I the
healthiest
member?
no
Am I the
leader?
no
Wait 30
seconds
yes
yes
no
yes
Start
Postgres
wait 5
seconds
no
wait 5
seconds
no
follow
proper
leader
no
yes
Running Loop
Start
Postgres
Start-up Process
26. Improved with etcd
removed tight coupling in classes:
HA decision process
etcd state interaction
PostgreSQL handler
27. Issues with etcd
overly aggressive about consensus
instructions for optimization at
https://coreos.com/docs/cluster-
management/debugging/etcd-
tuning/
28. Issues with etcd
overly aggressive about
consensus
we quit running etcd along side
PostgreSQL because we wanted
expanding PostgreSQL clusters