Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions wiki/Architecture.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Architecture

## Overview

The system follows a shared architecture across all Rinha de Backend implementations: two API instances behind an Nginx reverse proxy, with a single PostgreSQL database and an observability stack.

## Services

| Service | Role | CPU | RAM |
|---------|------|-----|-----|
| webapi1 | Rust/Actix-web 4 API instance (SQLx 0.8 + Tokio) | 0.4 | 100MB |
| webapi2 | Rust/Actix-web 4 API instance (SQLx 0.8 + Tokio) | 0.4 | 100MB |
| nginx | Reverse proxy / load balancer (least-conn) | 0.2 | 20MB |
| postgresql | Database with stored procedures | 0.5 | 330MB |
| k6 | Load testing | (not counted) | (not counted) |
| grafana + influxdb | Observability 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. The database 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

## Implementation Details

- Minimal single-file Rust API (~140 lines)
- Actix-web 4 HTTP framework with Tokio async runtime
- SQLx 0.8 async PostgreSQL driver
- Multi-stage Docker build for minimal container image size
29 changes: 29 additions & 0 deletions wiki/CI-CD-Pipeline.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# CI/CD Pipeline

## Workflows

This repository uses four GitHub Actions workflows:

### build-check.yml

- **Trigger:** Pull requests to main
- **Steps:** Builds the Rust release binary, then runs Docker Compose and validates the healthcheck endpoint
- **Purpose:** Catch build failures and regressions before merging

### main-release.yml

- **Trigger:** Push to main branch
- **Steps:** Builds the release binary, builds and pushes a multi-platform Docker image (amd64/arm64) to GHCR, runs container healthcheck, then runs k6 load tests and uploads the stress test report as an artifact
- **Purpose:** Automated release of production-ready container images with load test validation

### codeql.yml

- **Trigger:** Push to main, pull requests to main, weekly schedule (Mondays)
- **Steps:** Runs CodeQL static analysis for Rust with security-and-quality queries
- **Purpose:** Continuous security and code quality analysis

### deploy.yml

- **Trigger:** Push to main branch
- **Steps:** Deploys the `docs/` directory to GitHub Pages using the actions/deploy-pages workflow
- **Purpose:** Publish project documentation and stress test reports to GitHub Pages
Comment on lines +27 to +29
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor | ⚡ Quick win

Align deploy workflow description with actual deploy.yml.

Current text says this repo directly uses actions/deploy-pages and only triggers on push, but .github/workflows/deploy.yml uses a reusable workflow and also supports workflow_dispatch.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@wiki/CI-CD-Pipeline.md` around lines 27 - 29, Update the CI-CD doc text to
match the actual deploy.yml behavior: replace the claim that it directly uses
actions/deploy-pages and only triggers on push with a note that the repo invokes
a reusable deployment workflow (deploy.yml) and that the workflow supports both
push to main and manual runs via workflow_dispatch; also keep the intent to
publish the docs/ directory to GitHub Pages but describe that deployment is
performed through the reusable deploy.yml workflow.

26 changes: 26 additions & 0 deletions wiki/Challenge.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Challenge

## Rinha de Backend 2024/Q1

The Rinha de Backend is a Brazilian backend programming challenge. The 2024/Q1 edition simulates a fictional bank called "Rinha Financeira" that manages up to 5 named clients, each seeded at startup with a credit limit and initial balance.

## Endpoints

Two API endpoints are required:

| Endpoint | Method | Description |
|----------|--------|-------------|
| `/clientes/{id}/transacoes` | POST | Submit a debit or credit transaction for a client (IDs 1-5) |
| `/clientes/{id}/extrato` | GET | Get a client's current balance, credit limit, and recent transactions |

## Constraints

The challenge imposes strict resource limits across all containers combined:

- **1.5 CPU total** shared across all services
- **550MB RAM total** shared across all services
- The system is stress tested using Grafana k6 with concurrent users submitting transactions and querying statements

## Source

Full specification: [github.com/zanfranceschi/rinha-de-backend-2024-q1L(https://github.com/zanfranceschi/rinha-de-backend-2024-q1.md)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor | ⚡ Quick win

Fix broken source link syntax on Line 26.

The link is malformed (L(), so it won’t render as clickable Markdown.

Suggested fix
-Full specification: [github.com/zanfranceschi/rinha-de-backend-2024-q1L(https://github.com/zanfranceschi/rinha-de-backend-2024-q1.md)
+Full specification: [github.com/zanfranceschi/rinha-de-backend-2024-q1](https://github.com/zanfranceschi/rinha-de-backend-2024-q1)
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
Full specification: [github.com/zanfranceschi/rinha-de-backend-2024-q1L(https://github.com/zanfranceschi/rinha-de-backend-2024-q1.md)
Full specification: [github.com/zanfranceschi/rinha-de-backend-2024-q1](https://github.com/zanfranceschi/rinha-de-backend-2024-q1)
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@wiki/Challenge.md` at line 26, The Markdown link on Line 26 is malformed
(contains "L("); update the link syntax in Challenge.md by replacing the broken
fragment "rinha-de-backend-2024-q1L(" with a proper Markdown link such as
[github.com/zanfranceschi/rinha-de-backend-2024-q1](https://github.com/zanfranceschi/rinha-de-backend-2024-q1.md)
so the URL renders as a clickable link.

40 changes: 40 additions & 0 deletions wiki/Getting-Started.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Getting Started

## Prerequisites

- Docker with Docker Compose

## Clone and Run

```bash
git clone https://github.com/jonathanperis/rinha2-back-end-rust.git
cd rinha2-back-end-rust
docker compose up nginx -d --build
```

## Access

The API is available at `http://localhost:9999`

## Endpoints

| Endpoint | Method | Description |
|----------|--------|-------------|
| `/clientes/{id}/transacoes` | POST | Submit debit or credit transaction |
| `/clientes/{id}/extrato` | GET | Get account balance statement |

## Example Requests

### Create Transaction

```bash
curl -X POST http://localhost:9999/clientes/1/transacoes \
-H "Content-Type: application/json" \
-d '{"valor": 1000, "tipo": "c", "descricao": "deposito"}'
```

### Get Statement

```bash
curl http://localhost:9999/clientes/1/extrato
```
22 changes: 22 additions & 0 deletions wiki/Performance.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Performance

## Resource Constraints

The challenge allows a total of 1.5 CPU and 550MB RAM across all containers.

## Actual Usage

| Metric | Limit | Actual | Margin |
|--------|-------|--------|--------|
| RAM | 550MB | ~250MB | 60% below limit |
| Response time | - | < 800ms | All requests |

## Results

- All requests completed under 800ms
- Total RAM usage of approximately 250MB, which is 60% below the 550MB limit
- Built for learning purposes

## Stress Testing

Load tests are run using the shared [rinha2-back-end-k6L(https://github.com/jonathanperis/rinha2-back-end-k6.md) test suite, which simulates concurrent users performing debits, credits, validations, and statement queries.
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor | ⚡ Quick win

Fix broken stress-test suite link on Line 22.

The link is malformed and currently won’t render correctly.

Suggested fix
-Load tests are run using the shared [rinha2-back-end-k6L(https://github.com/jonathanperis/rinha2-back-end-k6.md) test suite, which simulates concurrent users performing debits, credits, validations, and statement queries.
+Load tests are run using the shared [rinha2-back-end-k6](https://github.com/jonathanperis/rinha2-back-end-k6) test suite, which simulates concurrent users performing debits, credits, validations, and statement queries.
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
Load tests are run using the shared [rinha2-back-end-k6L(https://github.com/jonathanperis/rinha2-back-end-k6.md) test suite, which simulates concurrent users performing debits, credits, validations, and statement queries.
Load tests are run using the shared [rinha2-back-end-k6](https://github.com/jonathanperis/rinha2-back-end-k6) test suite, which simulates concurrent users performing debits, credits, validations, and statement queries.
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@wiki/Performance.md` at line 22, The markdown link on the line starting with
"Load tests are run using the shared
[rinha2-back-end-k6L(https://github.com/jonathanperis/rinha2-back-end-k6.md)
test suite..." is malformed; update it to use proper markdown link syntax and a
correct URL by replacing the broken token
"[rinha2-back-end-k6L(https://github.com/jonathanperis/rinha2-back-end-k6.md)"
with a well-formed link such as
"[rinha2-back-end-k6](https://github.com/jonathanperis/rinha2-back-end-k6)"
(keep the rest of the sentence unchanged), ensuring the repository name
"rinha2-back-end-k6" is the link text and the URL does not include the stray
".md" or extra characters.

24 changes: 24 additions & 0 deletions wiki/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# rinha2-back-end-rust

Rust/Actix-web 4 implementation for the Rinha de Backend 2024/Q1 challenge. Manages a fictional bank API with transaction processing and balance statements under strict resource constraints (1.5 CPU, 550MB RAM total across all containers).

## Wiki Pages

| Page | Description |
|------|-------------|
| [Challenge](Challenge) | What is Rinha de Backend 2024/Q1 |
| [Architecture](Architecture) | Stack, services, resource constraints |
| [Getting StartedL(Getting-Started.md) | Prerequisites and how to run |
| [Performance](Performance) | Results, benchmarks, resource usage |
| [CI/CD PipelineL(CI-CD-Pipeline.md) | GitHub Actions workflows |
Comment on lines +11 to +13
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor | ⚡ Quick win

Fix malformed links in the wiki index (navigation is currently broken).

Several links use L( instead of ](, so they won’t render correctly.

Suggested fix
-| [Getting StartedL(Getting-Started.md) | Prerequisites and how to run |
+| [Getting Started](Getting-Started.md) | Prerequisites and how to run |
@@
-| [CI/CD PipelineL(CI-CD-Pipeline.md) | GitHub Actions workflows |
+| [CI/CD Pipeline](CI-CD-Pipeline.md) | GitHub Actions workflows |
@@
-*[GitHubL(https://github.com/jonathanperis/rinha2-back-end-rust.md) · [Jonathan Peris](https://jonathanperis.github.io/)*
+*[GitHub](https://github.com/jonathanperis/rinha2-back-end-rust) · [Jonathan Peris](https://jonathanperis.github.io/)*

Also applies to: 24-24

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@wiki/index.md` around lines 11 - 13, Fix the malformed markdown links by
replacing the incorrect "L(" sequence with the proper closing bracket and paren
"](" in the link labels found (e.g., change "[Getting
StartedL(Getting-Started.md)" to "[Getting Started](Getting-Started.md)" and
"[CI/CD PipelineL(CI-CD-Pipeline.md)" to "[CI/CD Pipeline](CI-CD-Pipeline.md)");
scan the file for other occurrences of "L(" (such as the "Performance" or any
duplicated instances) and correct them so all link text is enclosed in [] and
targets in ().


## Key Features

- Minimal single-file API implementation (~140 lines of Rust)
- Actix-web 4 with Tokio async runtime and SQLx 0.8
- PostgreSQL stored procedures for server-side business logic
- All requests under 800ms at 250MB RAM usage

---

*[GitHubL(https://github.com/jonathanperis/rinha2-back-end-rust.md) · [Jonathan Peris](https://jonathanperis.github.io/)*
Loading