Skip to content

FollG/go-database-replication

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

14 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸš€ Go Database Replication

Robust and scalable Go application with MySQL master-slave replication running in Docker containers! πŸ’Ύ

Go Version MySQL Docker

πŸ“– Overview

This project demonstrates high-availability database architecture using MySQL master-slave replication with a Go backend. Perfect for learning about distributed systems and database scaling! 🎯

✨ Features

  • πŸ— Master-Slave Replication - 1 Master + 3 Slave nodes
  • 🐳 Docker Containerization - Easy deployment and scaling
  • πŸ”„ Automatic Failover - Built-in replication monitoring
  • ⚑ High Performance - Read scalability across multiple slave nodes
  • πŸ”’ Data Consistency - ACID compliance with replication
  • πŸ“Š Health Monitoring - Built-in database health checks

πŸ— Architecture

graph TB
    A[Go Application] --> B[MySQL Master]
    B --> C[Slave 1]
    B --> D[Slave 2]
    B --> E[Slave 3]
    
    style A fill:#74c0fc
    style B fill:#ff8787
    style C fill:#51cf66
    style D fill:#51cf66
    style E fill:#51cf66
Loading

πŸ›  Technology Stack

Component Technology Purpose
Backend Go High-performance API server
Database MySQL Primary data storage
Containerization Docker Environment isolation
Orchestration Docker Compose Multi-container management

πŸš€ Quick Start

Prerequisites

Installation

  1. Clone the repository

    git clone https://github.com/FollG/go-database-replication.git
    cd go-database-replication
  2. Start the environment

    # Start all services (Master + 3 Slaves + Go Application)
    chmod +x \
       ./build/stop.sh \
       ./build/start.sh \
       ./build/status.sh \
       ./build/setup-replication.sh
    
    ./build/start.sh
  3. Verify services are running

    ./build/status.sh

    βœ… You should see 5 running containers!

  4. Check replication status

    # Connect to master and check slave status
    docker-compose exec mysql-master mysql -uroot -psecret -e "SHOW SLAVE HOSTS;"

πŸ“ Project Structure

TODO

βš™οΈ Configuration

Environment Variables

Variable Default Description
DB_MASTER_HOST mysql-master Master database host
DB_MASTER_PORT 3306 Master database port
DB_SLAVE_HOSTS slave1,slave2,slave3 Comma-separated slave hosts
MASTER_DB_USER ./configs.*.yaml Database username
SLAVE_DB_USER ./configs.*.yaml Database username
MASTER_DB_PASSWORD ./configs.*.yaml Database password
SLAVE_DB_PASSWORD ./configs.*.yaml Database password

πŸ§ͺ Testing Replication

Write to Master

# This will create data on the master node
curl -X POST http://localhost:8080/api/users/ \
  -H "Content-Type: application/json" \
  -d '{"name": "MisterFish", "email": "misterfish@gmail.com"}'

Read from Slaves

# This will read from slave nodes (round-robin)
curl http://localhost:8080/api/users

πŸ› Troubleshooting

Common Issues

  1. Connection Refused

    # Check if containers are running
    docker-compose ps
    
    # Check master logs
    docker-compose logs mysql-master
  2. Replication Not Working

    # Check slave status
    docker-compose exec mysql-slave1 mysql -uroot -psecret -e "SHOW SLAVE STATUS\G"
  3. Application Can't Connect

    # Verify network
    docker network ls
    docker network inspect go-database-replication_default

🀝 Contributing

We welcome contributions! πŸŽ‰ Here's how you can help:

  1. 🍴 Fork the project
  2. 🌿 Create your feature branch (`git checkout -b feature/AmazingFeature`)
  3. πŸ’Ύ Commit your changes (`git commit -m 'Add some AmazingFeature'`)
  4. πŸ“€ Push to the branch (`git push origin feature/AmazingFeature`)
  5. πŸ”ƒ Open a Pull Request

πŸ“œ License

This project is licensed under the MIT License - see the LICENSE file for details.

πŸ™ Acknowledgments

  • MySQL Documentation for replication setup
  • Docker community for excellent containerization guides
  • Go community for best practices and libraries

⭐ Don't forget to star the repository if you found it helpful! ⭐

Made with ❀️ and β˜• by FollG

Releases

No releases published

Packages

No packages published