Week 18: PostgreSQL & Redis in Production
Theme: Operate stateful databases on Kubernetes: backup, HA, migrations.
Learning Objectives
- Deploy PostgreSQL on Kubernetes using CloudNativePG or Zalando operator
- Configure streaming replication for HA
- Perform zero-downtime schema migrations
- Deploy Redis Sentinel or Redis Cluster for high availability
Reading Materials
- CloudNativePG Documentation
- Zalando PostgreSQL Operator
- PostgreSQL Streaming Replication
- golang-migrate
- Redis Sentinel
- Redis Cluster
- Redis on Kubernetes Best Practices
Finger Exercises (choose 2)
- Deploy a PostgreSQL cluster with CloudNativePG: 1 primary + 1 replica. Connect and run a simple schema.
- Run a schema migration using golang-migrate: add a
userstable, then add an index. Roll back. - Deploy Redis with Sentinel (3 nodes). Test failover by killing the primary.
Project
- Deploy PostgreSQL with CloudNativePG:
- 1 primary + 1 synchronous replica + 1 asynchronous replica
- Automated backups to S3 every 6 hours
- Point-in-time recovery enabled
- Connection pooling with PgBouncer (sidecar or standalone)
- Integrate with the Go app:
- Replace in-memory storage with PostgreSQL
- Use golang-migrate for schema migrations
- Use connection pooling (max 20 connections per app instance)
- Deploy Redis with Sentinel:
- 3 Sentinel nodes + 1 primary + 2 replicas
- Used for session caching and rate limiting
- Implement backup/restore procedures:
- Full backup every 6h, WAL archiving continuous
- Restore test: delete the database, restore from backup, verify data integrity
- Document:
DATABASE.mdwith connection pooling config, backup schedule, failover procedure
Time Budget
| Activity | Time |
|---|---|
| Reading | 2 hrs |
| Finger exercises | 1.5 hrs |
| Project | 2.5 hrs |
| Total | 6 hrs |