Skip to content

Commit 1218d64

Browse files
committed
Initial commit
0 parents  commit 1218d64

28 files changed

+1021
-0
lines changed

.gitattributes

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Auto detect text files and perform LF normalization
2+
* text=auto

.idea/.gitignore

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/compiler.xml

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/encodings.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/jarRepositories.xml

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/vcs.xml

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Dockerfile

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
FROM openjdk:21-jdk-slim as builder
2+
3+
WORKDIR /app
4+
COPY pom.xml .
5+
COPY src ./src
6+
7+
RUN apt-get update && apt-get install -y maven && \
8+
mvn clean package -DskipTests && \
9+
mv target/*.jar app.jar
10+
11+
FROM openjdk:21-jdk-slim
12+
13+
WORKDIR /app
14+
15+
ENV JAVA_OPTS="-server \
16+
-XX:+UseG1GC \
17+
-XX:+UseStringDeduplication \
18+
-XX:+DisableExplicitGC \
19+
-XX:+UnlockExperimentalVMOptions \
20+
-XX:+UseJVMCICompiler \
21+
-XX:MaxGCPauseMillis=10 \
22+
-XX:G1HeapRegionSize=4m \
23+
-Xmx120m \
24+
-Xms120m \
25+
-XX:MaxDirectMemorySize=20m \
26+
-XX:+TieredCompilation \
27+
-XX:TieredStopAtLevel=1 \
28+
-Xlog:disable \
29+
-XX:-PrintGC \
30+
-XX:-PrintGCDetails"
31+
32+
COPY --from=builder /app/app.jar .
33+
34+
EXPOSE 8080
35+
36+
ENTRYPOINT ["sh", "-c", "java $JAVA_OPTS -jar app.jar"]

README.md

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
# Rinha de Backend - Nerfa mais a JVM
2+
3+
<details align="left">
4+
5+
<div>
6+
7+
<h1>
8+
Signatures:
9+
</h1>
10+
11+
⢀⣴⣿⣿⣿⣿⣿⣶⣶⣶⣿⣿⣶⣶⣶⣶⣶⣿⡿⣿⣾⣷⣶⣶⣾⣿⠀
12+
⣠⣿⣿⢿⣿⣯⠀⢹⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡇⣿⡇⣿⣿⣿⣿⣿⡇
13+
⠀⣰⣿⣿⣷⡟⠤⠟⠁⣼⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⢸⡇⣿⣿⣿⣿⣿⡇
14+
⠀⣿⣿⣿⣿⣿⣷⣶⣿⣿⡟⠁⣮⡻⣿⣿⣿⣿⣿⣿⣿⣿⢸⡇⣿⣿⣿⣿⣿⡇
15+
⠘⣿⣿⣿⣿⣿⣿⣿⣿⠏⠀⠀⣿⣿⣹⣿⣿⣿⣿⣿⣿⡿⢸⡇⣿⣿⣿⣿⣿⡇
16+
⠀⠙⢿⣿⣿⣿⡿⠟⠁⣿⣿⣶⣿⠟⢻⣿⣿⣿⣿⣿⣿⡇⣼⡇⣿⣿⣿⣿⣿⠇
17+
⠀⠀⠈⠋⠉⠁⣶⣶⣶⣿⣿⣿⣿⢀⣿⣿⣿⣿⣿⣿⣿⣇⣿⢰⣿⣿⣿⣿⣿⠀
18+
⠀⠀⠀⠀⠀⠙⠿⣿⣿⣿⡄⢀⣠⣾⣿⣿⣿⣿⣿⣿⣿⣽⣿⣼⣿⣿⣿⣿⠇⠀
19+
⠀⠀⠀⠀⠀⠀⠀⠈⠉⠒⠚⠿⠿⠿⠿⠿⠿⠿⠿⠿⠿⠛⠿⠿⠿⠿⠿⠋⠀⠀
20+
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
21+
⠀⠀⠀⣿⣙⡆⠀⠀⡇⠀⢸⠀⠀⢸⠀⠀ ⢸⡇⠀⠀⢸⣏⡉ ⠙⡏⠁⠀
22+
⠀⠀⠀⣿⣉⡷⠀⠀⢧⣀⣼ ⠀⢸⣀ ⢸⣇⡀ ⢸⣏⣁⠀ ⠀⡇⠀
23+
24+
25+
</div>
26+
27+
</details>
28+
29+
![Boleto](assets/javapagou.png)
30+
31+
## Estratégia de Performance
32+
33+
Esta implementação foi otimizada para **máxima performance** e **menor custo por transação**
34+
35+
### Otimizações Principais
36+
37+
- **Undertow** ao invés de Tomcat (30% mais rápido)
38+
- **Processamento assíncrono** em lotes com filas otimizadas
39+
- **Health Check inteligente** com cache de 5s e fallback automático
40+
- **Pool de conexões HTTP** otimizado com keep-alive
41+
- **Repository em memória** com estruturas lock-free (LongAdder)
42+
- **JVM tuning agressivo** para baixa latência
43+
44+
45+
46+
### Estratégia de Escolha de Processador
47+
48+
1. **Health Check periódico** a cada 5s respeitando rate limit
49+
2. **Score inteligente**: tempo de resposta + disponibilidade
50+
3. **Preferência pelo default** (menor taxa) com tolerância 1.5x no tempo
51+
4. **Circuit breaker** automático com retry e fallback
52+
5. **Fallback inteligente** quando processador principal falha
53+
54+
### ⚡ Otimizações de Performance
55+
56+
- **Virtual Threads** para I/O não-bloqueante
57+
- **G1GC** com pausa máxima de 10ms
58+
- **String deduplication** para economia de memória
59+
- **Tiered compilation** nivel 1 para startup rápido
60+
- **Buffer sizes** otimizados (1KB-4KB)
61+
- **Keep-alive** em todas as conexões HTTP
62+
63+
### Configurações de Recursos
64+
65+
- **CPU**: 1.5 cores total (0.6 por backend + 0.3 nginx)
66+
- **Memória**: 350MB total (125MB por backend + 100MB nginx)
67+
- **Threads**: 20 workers + 4 I/O por instância
68+
- **Connections**: Pool de 50 conexões HTTP com keep-alive
69+
70+
### Tecnologias Utilizadas
71+
72+
- **Java 21** com Virtual Threads
73+
- **Spring Boot 3.3** com WebFlux
74+
- **Undertow** como servidor web
75+
- **Nginx** com least_conn balancing
76+
- **Caffeine** cache local
77+
- **Repository in-memory** otimizado
78+
79+
### 🏃‍♂ Como Executar
80+
81+
```bash
82+
# 1. Subir os Payment Processors primeiro
83+
cd payment-processor
84+
docker-compose up -d
85+
86+
# 2. Subir o backend
87+
docker-compose up -d
88+
89+
# 3. Testar
90+
curl -X POST http://localhost:9999/payments \
91+
-H "Content-Type: application/json" \
92+
-d '{"correlationId": "550e8400-e29b-41d4-a716-446655440000", "amount": 19.90}'
93+
94+
curl http://localhost:9999/payments-summary
95+
```
96+
97+
### Objetivo
98+
99+
**Maximizar o lucro** processando o máximo de pagamentos possível com a **menor taxa** (processador default), mantendo **alta disponibilidade** e **baixa latência** (p99 < 10ms para bônus de performance).
100+
101+
---
102+
103+
**Feito com ❤️ por e bastante ☕ por BulletOnRails para a Rinha **

assets/javapagou.png

1.84 MB
Loading

0 commit comments

Comments
 (0)