A high-performance, horizontally scalable REST API built with Go and Fiber v3, featuring database sharding, connection pooling, and streaming replication.
Tested on MacBook Air M2 with 10,000 concurrent users:
| Metric | Value |
|---|---|
| Success Rate | 100% |
| Throughput | ~2,100 req/sec |
| P50 Latency | 1ms |
| P95 Latency | 3ms |
| P99 Latency | 11ms |
| Max Response | 105ms |
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β API Server (Fiber v3 with Prefork) β
β 7 Worker Processes β’ Sonic JSON β’ :8080 β
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β β β β β
βΌ βΌ βΌ βΌ βΌ
ββββββββββββ ββββββββββββ ββββββββββββ ββββββββββββ ββββββββββββ
βPgBouncer β βPgBouncer β βPgBouncer β βPgBouncer β βPgBouncer β
β Pool 0 β β Pool 1 β β Pool 2 β β Pool 3 β β Pool 4 β
β :6430 β β :6431 β β :6432 β β :6433 β β :6434 β
ββββββ¬ββββββ ββββββ¬ββββββ ββββββ¬ββββββ ββββββ¬ββββββ ββββββ¬ββββββ
β β β β β
βΌ βΌ βΌ βΌ βΌ
ββββββββββββ ββββββββββββ ββββββββββββ ββββββββββββ ββββββββββββ
β Shard 0 β β Shard 1 β β Shard 2 β β Shard 3 β β Shard 4 β
β Primary β β Primary β β Primary β β Primary β β Primary β
β :5440 β β :5441 β β :5442 β β :5443 β β :5444 β
ββββββ¬ββββββ ββββββ¬ββββββ ββββββ¬ββββββ ββββββ¬ββββββ ββββββ¬ββββββ
β β β β β
βΌ βΌ βΌ βΌ βΌ
ββββββββββββ ββββββββββββ ββββββββββββ ββββββββββββ ββββββββββββ
β Shard 0 β β Shard 1 β β Shard 2 β β Shard 3 β β Shard 4 β
β Replica β β Replica β β Replica β β Replica β β Replica β
β :5450 β β :5451 β β :5452 β β :5453 β β :5454 β
ββββββββββββ ββββββββββββ ββββββββββββ ββββββββββββ ββββββββββββ
- Fiber v3: High-performance HTTP framework with prefork mode for multi-core utilization
- Sonic JSON: 2-5x faster JSON encoding/decoding than standard library
- PgBouncer: Connection pooling in transaction mode (10K connections β 300 DB connections)
- PostgreSQL 16: Sharded across 5 instances with streaming replication
- Consistent Hashing: MD5-based shard routing for even data distribution
- GORM: ORM with optimized settings (skip default transaction, prepared statements disabled)
- Docker & Docker Compose
- Make
- Go 1.25+ (for local development)
# Start everything (infrastructure + API server with prefork)
make devThe API will be available at http://localhost:8080
# Check health
make health
# View shard statistics
make shards
# Show running containers
make psRun make help to see all available commands:
| Command | Description |
|---|---|
make up |
Start infrastructure (5 shards + replicas + pgbouncers) |
make down |
Stop all services |
make down-clean |
Stop all and remove volumes (clean slate) |
make ps |
Show running containers |
make stats |
Show resource usage (CPU, memory, network) |
make logs |
View API server logs |
make logs-all |
View all service logs |
| Command | Description |
|---|---|
make dev |
Start everything (infra + API with prefork) |
make restart |
Restart API server |
make rebuild |
Rebuild and restart API server |
| Command | Description |
|---|---|
make shards |
Show shard statistics (user counts per shard) |
make health |
Check API health endpoint |
make db-shell SHARD=0 |
Connect to PostgreSQL shell on specific shard |
| Command | Description |
|---|---|
make migrate-make NAME=<name> |
Create a new migration file |
make migrate-up |
Run all pending migrations |
make migrate-down |
Rollback the last migration |
make migrate-latest |
Run migrations to latest version |
Run make loadtest-help to see all load testing commands:
| Command | Description |
|---|---|
make loadtest-smoke |
Quick validation (10 users, 30s) |
make loadtest |
Standard load test (100 users, 60s) |
make loadtest-custom USERS=N |
Custom user count load test |
make loadtest-10k |
10,000 concurrent users test |
make loadtest-crud |
Full CRUD operations test |
make loadtest-stress |
High load test (500 users, 120s) |
make loadtest-spike |
Sudden traffic burst test |
make loadtest-soak |
Extended duration test (5 min) |
make loadtest-pagination |
Pagination endpoint test |
make loadtest-read |
Read-heavy workload (80% reads) |
make loadtest-write |
Write-heavy workload |
make loadtest-report |
Open latest HTML report |
make loadtest-clean |
Remove test results |
| Method | Endpoint | Description |
|---|---|---|
POST |
/api/v1/users/ |
Create a new user |
GET |
/api/v1/users/ |
List users (paginated) |
GET |
/api/v1/users/:id |
Get user by ID |
PUT |
/api/v1/users/:id |
Update user |
DELETE |
/api/v1/users/:id |
Delete user |
| Method | Endpoint | Description |
|---|---|---|
GET |
/health |
Health check |
GET |
/api/v1/shards/stats |
Shard statistics |
# Create a user
curl -X POST http://localhost:8080/api/v1/users/ \
-H "Content-Type: application/json" \
-d '{"name": "John Doe", "email": "john@example.com"}'
# List users
curl http://localhost:8080/api/v1/users/?limit=20&offset=0
# Get user by ID
curl http://localhost:8080/api/v1/users/123e4567-e89b-12d3-a456-426614174000
# Update user
curl -X PUT http://localhost:8080/api/v1/users/123e4567-e89b-12d3-a456-426614174000 \
-H "Content-Type: application/json" \
-d '{"name": "Jane Doe"}'
# Delete user
curl -X DELETE http://localhost:8080/api/v1/users/123e4567-e89b-12d3-a456-426614174000
# Check shard statistics
curl http://localhost:8080/api/v1/shards/stats | jqmessenger-clone/
βββ Makefile # Main build and run commands
βββ docker-compose.yml # Docker orchestration (5 shards + replicas)
βββ README.md # This file
β
βββ server/ # Go API Server
β βββ Dockerfile # Multi-stage build (dev + production)
β βββ Makefile # Server-specific commands
β βββ go.mod # Go modules
β βββ migration.yml # Migration configuration
β β
β βββ cmd/
β β βββ api/
β β βββ main.go # Application entry point
β β
β βββ config/
β β βββ config.go # Configuration management
β β
β βββ internal/
β β βββ application/ # Business logic layer
β β β βββ services/ # Application services
β β β
β β βββ bootstrap/ # Service initialization
β β β βββ application_service.go
β β β βββ shard_service.go
β β β
β β βββ domain/ # Domain models
β β β βββ entities/ # Entity definitions
β β β βββ repositories/ # Repository interfaces
β β β
β β βββ infra/ # Infrastructure layer
β β β βββ database/
β β β βββ shard/ # Sharding implementation
β β β
β β βββ persistence/ # Data access layer
β β β βββ repositories/ # Repository implementations
β β β
β β βββ presentation/ # HTTP layer
β β βββ handlers/ # Request handlers
β β βββ routes/ # Route definitions
β β
β βββ migrations/ # Database migrations
β β
β βββ tools/
β βββ migration/ # Migration tool
β
βββ database/ # Database configuration
β βββ pgbouncer/ # PgBouncer config
β βββ postgres/
β βββ init/ # Initialization scripts
β βββ replica/ # Replica setup scripts
β βββ pg_hba.conf # Authentication config
β
βββ loadtest/ # Load testing
β βββ Makefile # Load test commands
β βββ README.md # Load test documentation
β βββ gatling/
β βββ Dockerfile # Gatling container
β βββ entrypoint.sh # Test runner
β βββ conf/ # Gatling configuration
β βββ results/ # Test results (HTML reports)
β βββ simulations/ # Test scenarios
β βββ CreateUserSimulation.scala
β βββ UserSimulation.scala
β
βββ docs/ # π Documentation
β βββ INDEX.md # Documentation index
β βββ ARCHITECTURE.md # Complete system overview
β βββ MIGRATIONS.md # Database migrations guide
β βββ FOREIGN_KEYS.md # Relations in sharded DBs
β βββ DATABASE_SCALING.md # Database scaling guide
β βββ PARTITIONING_GUIDE.md # Table partitioning guide
β βββ PERFORMANCE_TUNING.md # Performance optimization
β βββ SHARDING_GUIDE.md # Sharding implementation
β
βββ scripts/ # Utility scripts
βββ loadtest-create-user.sh
βββ loadtest-env.sh
βββ loadtest-sharded.sh
βββ migrate-shards.sh
βββ partition-shards.sh
βββ tune-postgres.sh
| Variable | Default | Description |
|---|---|---|
PORT |
8080 |
API server port |
SHARDING_ENABLED |
true |
Enable database sharding |
SHARDING_COUNT |
5 |
Number of database shards |
GOMAXPROCS |
7 |
Number of prefork worker processes |
GOGC |
50 |
Garbage collection target percentage |
GOMEMLIMIT |
4GiB |
Go memory limit |
DB_WRITE_MAX_OPEN_CONNS |
500 |
Max open DB connections per shard |
DB_WRITE_MAX_IDLE_CONNS |
250 |
Max idle DB connections per shard |
| Setting | Value | Description |
|---|---|---|
pool_mode |
transaction |
Connection pooling mode |
max_client_conn |
10000 |
Max client connections |
default_pool_size |
200 |
Default pool size per database |
max_db_connections |
300 |
Max database connections |
reserve_pool_size |
50 |
Reserve pool for burst traffic |
make loadtest-smokemake loadtestmake loadtest-10k# Open HTML report in browser
make loadtest-report# Run with custom user count and duration
make loadtest-custom USERS=5000 DURATION=120The development mode includes hot reload via Air:
make devEdit any Go file in server/ and the server will automatically rebuild and restart.
# Create a new migration
make migrate-make NAME=add_messages_table
# Run migrations
make migrate-upcd server
make test
# With coverage
make test-coveragemake statsmake shardsExample output:
{
"total_users": 9398603,
"shard_count": 5,
"shards": [
{ "shard_name": "shard-0", "user_count": 2132135 },
{ "shard_name": "shard-1", "user_count": 2368920 },
{ "shard_name": "shard-2", "user_count": 2424517 },
{ "shard_name": "shard-3", "user_count": 2382946 },
{ "shard_name": "shard-4", "user_count": 90085 }
]
}# API server logs
make logs
# All service logs
make logs-all| Service | Port | Description |
|---|---|---|
messenger_server |
8080 | API Server |
pgbouncer-0 to pgbouncer-4 |
6430-6434 | Connection poolers |
shard-0 to shard-4 |
5440-5444 | Primary databases |
shard-0-replica to shard-4-replica |
5450-5454 | Read replicas |
redis |
6379 | Cache (optional) |
Start Here: docs/INDEX.md - Complete documentation index
| Topic | Guide | Description |
|---|---|---|
| ποΈ Architecture | ARCHITECTURE.md | Complete system overview, use cases, who uses this |
| π Sharding | SHARDING_GUIDE.md | How we distribute data across databases |
| π Migrations | MIGRATIONS.md | Database schema versioning explained |
| π Foreign Keys | FOREIGN_KEYS.md | Relationships in distributed databases |
| β‘ Performance | PERFORMANCE_TUNING.md | How we achieved 100% success rate |
| π Scaling | DATABASE_SCALING.md | Comprehensive scaling strategies |
| π¦ Partitioning | PARTITIONING_GUIDE.md | PostgreSQL table partitioning |
| π§ͺ Load Testing | LOADTEST_INSTRUCTIONS.md | Running performance tests |
Beginner: ARCHITECTURE β MIGRATIONS β FOREIGN_KEYS
Scaling: DATABASE_SCALING β SHARDING_GUIDE β PARTITIONING_GUIDE
Performance: PERFORMANCE_TUNING β DATABASE_SCALING β SHARDING_GUIDE
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
Built with β€οΈ using Go, Fiber, PostgreSQL, and Docker