Skip to content

davidalecrim1/rinha-with-go-2024

Repository files navigation

Rinha de Backend with Go (Edition 2024/Q1)

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.

🎯 Challenge Overview

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

🔒 Concurrency Handling

This project demonstrates several concurrency patterns and safeguards:

  1. Database Concurrency

    • Optimistic locking with SELECT FOR UPDATE to prevent race conditions
    • Connection pooling with pgxpool to manage concurrent database access
    • Transaction isolation to ensure data consistency
  2. API Concurrency

    • Request timeouts via middleware to prevent resource exhaustion
    • Load balancing across multiple API instances
    • Goroutine management for concurrent request handling
  3. Resource Management

    • Efficient memory usage with proper Go data structures
    • Connection pool limits to prevent database overload
    • Request throttling when under extreme load

🚀 Technical Stack

  • API Framework: Gin for high-performance HTTP routing
  • Database: PostgreSQL with pgxpool for efficient connection pooling
  • Load Balancer: Nginx for request distribution
  • Architecture: Clean Architecture principles
    • Domain-driven design
    • Repository pattern
    • Dependency injection
    • Middleware support

🏗️ System Design

🧪 Load Testing Results

The API successfully handles:

  • 3k+ concurrent users
  • Consistent response times under 100ms
  • Zero data inconsistencies under load
  • Full details in LEARNING.md

Getting Started

  • Install Docker
  • Run docker compose up -d --build or make run

Stack

  • 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

References

About

High-performance REST API in Go for Rinha de Backend 2024/Q1—handling concurrent financial transactions with just 1.5 CPU cores & 550MB RAM. Features clean architecture, efficient DB access, and robust concurrency control for 3k+ simultaneous requests.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors