rinha2-back-end-python

Architecture

Tech Stack

TechnologyVersionPurpose
Python3.14Language runtime
Flask3.1.3Web framework
Gunicorn25.3.0WSGI HTTP server (4 workers, 2 threads)
psycopg2-binary2.9.12PostgreSQL adapter
PostgreSQL16.7Database with stored procedures
NGINX1.27Reverse proxy / load balancer (least_conn)
Docker-Containerization (python:3.14-slim base)
k6-Load / stress testing

Overview

NGINX (:9999, least_conn)
├── webapi1-python (:8080, 0.4 CPU, 100MB) — Gunicorn 4w x 2t
├── webapi2-python (:8080, 0.4 CPU, 100MB) — Gunicorn 4w x 2t
└── PostgreSQL (0.5 CPU, 330MB)
    ├── InsertTransacao() — atomic balance + validation
    └── GetSaldoClienteById() — statement with JSONB

Services

ServiceRoleCPURAM
webapi1Python API instance (Gunicorn 4w x 2t)0.4100MB
webapi2Python API instance (Gunicorn 4w x 2t)0.4100MB
nginxReverse proxy / load balancer (least_conn)0.220MB
postgresqlDatabase with stored procedures0.5330MB
k6Load testing(not counted)(not counted)
grafana + influxdbObservability dashboards(not counted)(not counted)

Load Balancing

Nginx uses least_conn strategy to distribute requests across the two API instances.

Database

Business logic is implemented in PostgreSQL stored procedures (InsertTransacao, GetSaldoClienteById). The database uses UNLOGGED tables and is tuned for maximum write performance:

  • synchronous_commit=0 — no wait for WAL flush
  • fsync=0 — skip fsync on writes
  • full_page_writes=0 — skip full page writes

Connection Management

The API uses psycopg2 SimpleConnectionPool with 1-10 connections per instance for efficient database access.

Gunicorn Configuration

--workers=4 --threads=2 --worker-class=sync --bind=0.0.0.0:8080 --timeout=30