pgraft Performance Tuning

Optimize pgraft PostgreSQL Raft extension for maximum performance and throughput.

Performance Optimization Strategies

High Throughput Configuration

# Optimize for high throughput
pgraft.heartbeat_interval = 50ms
pgraft.election_timeout = 500ms
pgraft.snapshot_threshold = 5000
pgraft.max_log_entries = 20000
pgraft.batch_size = 1000

Configuration optimized for maximum transaction throughput.

Low Latency Configuration

# Optimize for low latency
pgraft.heartbeat_interval = 25ms
pgraft.election_timeout = 250ms
pgraft.snapshot_threshold = 1000
pgraft.max_log_entries = 5000
pgraft.batch_size = 100

Configuration optimized for minimal response times.

Balanced Configuration

# Balanced performance
pgraft.heartbeat_interval = 100ms
pgraft.election_timeout = 1000ms
pgraft.snapshot_threshold = 2000
pgraft.max_log_entries = 10000
pgraft.batch_size = 500

Balanced configuration for most use cases.

PostgreSQL Configuration Tuning

Memory Configuration

# Memory settings
shared_buffers = 256MB
effective_cache_size = 1GB
work_mem = 4MB
maintenance_work_mem = 64MB
max_connections = 200

WAL Configuration

# WAL settings
wal_level = replica
max_wal_senders = 10
wal_keep_size = 1GB
checkpoint_completion_target = 0.9
wal_buffers = 16MB

Connection Settings

# Connection optimization
tcp_keepalives_idle = 600
tcp_keepalives_interval = 30
tcp_keepalives_count = 3
listen_addresses = '*'
port = 5432

Network Optimization

Network Settings

# Network optimization
pgraft.network_buffer_size = 1MB
pgraft.max_connections_per_node = 100
pgraft.connection_timeout = 30s
pgraft.keepalive_interval = 60s
pgraft.keepalive_count = 3

Cluster Topology

Recommended Topology

  • • Deploy nodes in same datacenter for low latency
  • • Use dedicated network for cluster communication
  • • Ensure symmetric network connectivity
  • • Monitor network latency between nodes

Performance Monitoring

Key Metrics to Monitor

Raft Metrics

  • • Election duration
  • • Log replication latency
  • • Commit index lag
  • • Snapshot frequency

PostgreSQL Metrics

  • • Transaction throughput
  • • Connection count
  • • WAL generation rate
  • • Buffer hit ratio

Performance Queries

# Get performance metrics
SELECT * FROM pgraft_metrics('my-cluster');

# Check log replication status
SELECT * FROM pgraft_cluster_status('my-cluster');

# Monitor PostgreSQL performance
SELECT * FROM pg_stat_activity;
SELECT * FROM pg_stat_database;

Performance Troubleshooting

Common Performance Issues

High Replication Latency

# Reduce heartbeat interval
SELECT pgraft_set_config('my-cluster', 'heartbeat_interval', '50ms');

# Check network connectivity
SELECT * FROM pgraft_cluster_status('my-cluster');

Frequent Elections

# Increase election timeout
SELECT pgraft_set_config('my-cluster', 'election_timeout', '2000ms');

# Check node health
SELECT * FROM pgraft_node_info('my-cluster');

High Memory Usage

# Reduce log entries
SELECT pgraft_set_config('my-cluster', 'max_log_entries', '5000');

# Force snapshot
SELECT pgraft_snapshot('my-cluster');

Performance Best Practices

Configuration Guidelines

  • • Set election_timeout to 3-5x heartbeat_interval
  • • Use odd number of nodes for proper majority
  • • Monitor and tune based on actual workload
  • • Test failover scenarios regularly

Operational Guidelines

  • • Deploy nodes across multiple availability zones
  • • Use dedicated network for cluster communication
  • • Implement comprehensive monitoring and alerting
  • • Regular performance testing and capacity planning