-
Notifications
You must be signed in to change notification settings - Fork 933
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Participação de Fernando Comunello na Rinha de Backend. (#1679)
- Loading branch information
1 parent
0a4a0cc
commit e55e70b
Showing
5 changed files
with
197 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# Submissão para Rinha de Backend, Segunda Edição: 2024/Q1 - Controle de Concorrência | ||
|
||
## Duke Bank by Fernando Comunello | ||
|
||
Submissão feita com: | ||
- `Java 21` como linguagem de programação. | ||
- `Quarkus` como framework/plataforma. | ||
- `Vert.x & GraalVM` como runtime. | ||
- `PostgreSQL` como banco de dados relacional. | ||
- `NGINX` como Load Balancer. | ||
|
||
- [repositório da api](https://github.com/fercomunello/duke-bank-rb-2024) | ||
|
||
[@fercomunello](https://twitter.com/fercomunello) @ twitter | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
##### Rinha de Backend ###### | ||
# Revanche dos Javeiros # | ||
############################# | ||
*/ | ||
|
||
DROP SCHEMA IF EXISTS api CASCADE; | ||
CREATE SCHEMA api; | ||
|
||
SET search_path TO api; | ||
ALTER DATABASE rinhadb SET search_path TO api; | ||
|
||
SET TIME ZONE 'UTC'; | ||
|
||
CREATE TYPE TXTYPE AS ENUM ('c', 'd'); | ||
|
||
CREATE UNLOGGED TABLE bank_accounts ( | ||
id BIGSERIAL, | ||
credit_limit BIGINT NOT NULL DEFAULT 0 CHECK ( credit_limit >= 0 ), | ||
balance BIGINT NOT NULL DEFAULT 0, | ||
PRIMARY KEY (id) | ||
); | ||
|
||
CREATE UNLOGGED TABLE bank_transactions ( | ||
id BIGSERIAL, | ||
account_id BIGINT NOT NULL, | ||
type TXTYPE NOT NULL, | ||
amount BIGINT NOT NULL CHECK ( amount > 0 ), | ||
description VARCHAR(10) NOT NULL, | ||
issued_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(), | ||
PRIMARY KEY (id), | ||
FOREIGN KEY (account_id) REFERENCES bank_accounts (id) | ||
); | ||
|
||
CREATE INDEX idx_tx_account_id ON bank_transactions (account_id); | ||
CREATE INDEX idx_tx_issued_filter ON bank_transactions (issued_at DESC); | ||
|
||
GRANT INSERT ON bank_accounts TO duke; | ||
GRANT UPDATE ON bank_accounts TO duke; | ||
REVOKE DELETE ON bank_accounts FROM duke; | ||
|
||
GRANT INSERT ON bank_transactions TO duke; | ||
REVOKE UPDATE ON bank_transactions FROM duke; | ||
REVOKE DELETE ON bank_transactions FROM duke; | ||
|
||
INSERT INTO bank_accounts | ||
(credit_limit, balance) | ||
VALUES (1000 * 100, 0), -- 1 | $1,000.00 == 100000 | ||
(800 * 100, 0), -- 2 | $80,000.00 == 80000 | ||
(10000 * 100, 0), -- 3 | $10,000.00 == 1000000 | ||
(100000 * 100, 0), -- 4 | $100,000.00 == 10000000 | ||
(5000 * 100, 0); -- 5 | $5,000.00 == 500000 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
services: | ||
duke-bank-1: | ||
image: fercomunello/rinha-backend-duke-bank-2024q1:latest | ||
network_mode: host | ||
environment: | ||
- QUARKUS_HTTP_PORT=8081 | ||
- QUARKUS_DATASOURCE_REACTIVE_MAX_SIZE=1 | ||
- QUARKUS_HTTP_SO_REUSE_PORT=true | ||
- QUARKUS_HTTP_TCP_QUICK_ACK=true | ||
- QUARKUS_HTTP_TCP_CORK=true | ||
- QUARKUS_HTTP_TCP_FAST_OPEN=true | ||
- QUARKUS_LOG_MIN_LEVEL=OFF | ||
- QUARKUS_LOG_CONSOLE_ENABLE=false | ||
- QUARKUS_VERTX_EVENT_LOOPS_POOL_SIZE=1 | ||
- QUARKUS_DATASOURCE_REACTIVE_EVENT_LOOP_SIZE=1 | ||
- QUARKUS_DATASOURCE_REACTIVE_POSTGRESQL_PIPELINING_LIMIT=256 | ||
depends_on: | ||
duke-bank-postgres: | ||
condition: service_healthy | ||
deploy: | ||
resources: | ||
limits: | ||
cpus: 0.35 | ||
memory: 150M | ||
|
||
duke-bank-2: | ||
image: fercomunello/rinha-backend-duke-bank-2024q1:latest | ||
network_mode: host | ||
environment: | ||
- QUARKUS_HTTP_PORT=8082 | ||
- QUARKUS_DATASOURCE_REACTIVE_MAX_SIZE=1 | ||
- QUARKUS_HTTP_SO_REUSE_PORT=true | ||
- QUARKUS_HTTP_TCP_QUICK_ACK=true | ||
- QUARKUS_HTTP_TCP_CORK=true | ||
- QUARKUS_HTTP_TCP_FAST_OPEN=true | ||
- QUARKUS_LOG_MIN_LEVEL=OFF | ||
- QUARKUS_LOG_CONSOLE_ENABLE=false | ||
- QUARKUS_VERTX_EVENT_LOOPS_POOL_SIZE=1 | ||
- QUARKUS_DATASOURCE_REACTIVE_EVENT_LOOP_SIZE=1 | ||
- QUARKUS_DATASOURCE_REACTIVE_POSTGRESQL_PIPELINING_LIMIT=256 | ||
depends_on: | ||
duke-bank-postgres: | ||
condition: service_healthy | ||
deploy: | ||
resources: | ||
limits: | ||
cpus: 0.35 | ||
memory: 150M | ||
|
||
duke-bank-postgres: | ||
image: postgres:latest | ||
command: postgres -c config_file=/etc/postgresql/postgresql.conf | ||
environment: | ||
- POSTGRES_DB=rinhadb | ||
- POSTGRES_USER=duke | ||
- POSTGRES_PASSWORD=duke | ||
network_mode: host | ||
volumes: | ||
- ./postgresql.conf:/etc/postgresql/postgresql.conf | ||
- ./bank-schema.sql:/docker-entrypoint-initdb.d/bank-schema.sql | ||
healthcheck: | ||
test: ["CMD-SHELL", "pg_isready", "-U", "${POSTGRES_USER}", "-d", "${POSTGRES_DB}"] | ||
interval: 3s | ||
timeout: 10s | ||
retries: 10 | ||
deploy: | ||
resources: | ||
limits: | ||
cpus: 0.6 | ||
memory: 125M | ||
|
||
duke-bank-nginx: | ||
image: nginx:latest | ||
network_mode: host | ||
volumes: | ||
- ./nginx.conf:/etc/nginx/nginx.conf:ro | ||
depends_on: | ||
- duke-bank-1 | ||
- duke-bank-2 | ||
deploy: | ||
resources: | ||
limits: | ||
cpus: 0.2 | ||
memory: 125M |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
worker_processes auto; | ||
|
||
events { | ||
use epoll; | ||
} | ||
http { | ||
access_log off; | ||
sendfile on; | ||
|
||
upstream api { | ||
server 127.0.0.1:8081; | ||
server 127.0.0.1:8082; | ||
} | ||
server { | ||
listen 9999; | ||
|
||
location / { | ||
proxy_pass http://api; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# ----------------------------- | ||
# PostgreSQL configuration file | ||
# ----------------------------- | ||
|
||
listen_addresses = '*' | ||
|
||
random_page_cost = 1.1 | ||
effective_io_concurrency = 4 | ||
shared_buffers = 96MB | ||
work_mem = 4MB | ||
|
||
# PostgreSQL server will try to make sure that updates are physically written to disk by default. | ||
# This ensures that the database cluster can recover to a consistent state after an operating system or hardware crash. | ||
# We can disable it because we are not taking into account data recovery requirements. | ||
fsync = off | ||
full_page_writes = off | ||
|
||
# Turn of logging as we do not need troubleshooting features. | ||
log_checkpoints = off | ||
log_statement = none | ||
logging_collector = off | ||
debug_pretty_print = off | ||
|
||
# We can disable RLS (Row-Level-Security) because this is NOT a multitenancy application. | ||
row_security = off |