A high-performance REST API built with Go for the Rinha de Backend 2024/Q1 challenge - a competition focused on handling concurrent financial transactions with minimal resources (1.5 CPU cores and 550MB RAM) ensuring the consistency of balance for multiple uses.
Build a REST API that can handle:
- Processing credit/debit transactions for clients with high concurrency
- Retrieving account statements under heavy load
- Managing concurrent transactions with proper balance/limit validation
- Handling 3k+ simultaneous requests with limited resources
This project demonstrates several concurrency patterns and safeguards:
-
Database Concurrency
- Optimistic locking with
SELECT FOR UPDATEto prevent race conditions - Connection pooling with
pgxpoolto manage concurrent database access - Transaction isolation to ensure data consistency
- Optimistic locking with
-
API Concurrency
- Request timeouts via middleware to prevent resource exhaustion
- Load balancing across multiple API instances
- Goroutine management for concurrent request handling
-
Resource Management
- Efficient memory usage with proper Go data structures
- Connection pool limits to prevent database overload
- Request throttling when under extreme load
- API Framework: Gin for high-performance HTTP routing
- Database: PostgreSQL with
pgxpoolfor efficient connection pooling - Load Balancer: Nginx for request distribution
- Architecture: Clean Architecture principles
- Domain-driven design
- Repository pattern
- Dependency injection
- Middleware support
The API successfully handles:
- 3k+ concurrent users
- Consistent response times under 100ms
- Zero data inconsistencies under load
- Full details in LEARNING.md
- Install Docker
- Run
docker compose up -d --buildormake run
- Gin for REST API with concurrent request handling
- Postgres for Database with transaction isolation
- pgxpool driver for concurrent Postgres connections
- Nginx for load balancing across API instances
