Produced with backend/src/pruebas/carga.ts, which opens N real socket.io clients against the backend and measures what happens to a player, not how many connections a counter can hold.
Spanish version: LOAD.es.md.
Opening idle sockets is easy and proves nothing. What breaks the experience when there are people playing is something else:
| Measure | What it means for the player |
|---|---|
| Completeness | % of ticks received against whoever received the most. Below 100, somebody sees a frozen rocket and cannot cash out in time |
| Gap between ticks | The server emits every 100 ms. If they arrive every 300, the game stutters |
| Bet latency | The only thing the player waits for while watching. It is the path that touches the database |
Server→client latency per tick is not measured: round:tick carries no timestamp and adding one purely to measure would change the production protocol. Regularity of arrival detects the same thing without touching anything.
| Players | Completeness | Worst gap | Bet (median) | Bet (p95) | Rejections |
|---|---|---|---|---|---|
| 50 | 99.0 % | 115 ms | 39 ms | 2.1 s | 14 |
| 150 | 98.5 % | 112 ms | 347 ms | 33.6 s | 34 |
| 600 | 99.2 % | 1.4 s | 5.4 s | 17.5 s | 0 |
Connection: 600 out of 600 without a single failure, in 9 seconds.
1. A per-IP limit that would have thrown out legitimate players. It was fixed at 20 connections per 10 seconds. In the markets where this game is played most — Brazil, India, Latin America — mobile traffic leaves through carrier-grade NAT and hundreds of players share a public IP: the twenty-first was locked out having done nothing wrong. It is now configurable (MAX_CONN_POR_IP).
2. Four queries per bet where two suffice. Check the reference, debit, re-read the balance, write the ledger entry. With 300 simultaneous bets that was 1,200 queries at once. The debit now returns the balance in the same statement (UPDATE … RETURNING), and the pre-check for idempotency is unnecessary: if the reference already existed, the ledger entry collides with the unique index and the whole transaction is rolled back, charge included.
| 600 players | Before | After |
|---|---|---|
| Completeness | 25.3 % | 99.2 % |
| Rejected bets | 206 | 0 |
| Bet (median) | 10.3 s | 5.4 s |
| Bet (p95) | 37.8 s | 17.5 s |
Confirming a bet in 5 seconds with 600 players was not acceptable, and the step named here as pending — one transaction per batch instead of one per bet — is now in. Bets go into a batch that closes at 100 bets or 20 ms from the first, whichever comes first.
Measured in isolation, 600 concurrent bets against the same database, old path first so the new one gains nothing from a warm cache:
| 600 concurrent bets | One transaction each | Batched |
|---|---|---|
| Total | 241 ms | 17 ms |
| Median | 173 ms | 16 ms |
| p95 | 232 ms | 17 ms |
| Accepted | 600/600 | 600/600 |
93 % less. Reproduce it with npx tsx src/pruebas/lote.ts 600 — it runs both paths back to back and prints the difference.
The 5.4 s above has not been re-measured end to end. The figure that belongs in a conversation with an operator is the one from carga.ts, and a clean run of it needs a machine that is not also running the test client — otherwise the result says more about the laptop than about the game. A re-run on a loaded machine moved the median from 28.8 s to 18.4 s, which is the right direction and nothing more: both numbers are dominated by CPU contention, not by the database.
So: the write path is 14× faster and that is measured. What the player experiences with 600 of them at once is still the 5.4 s in the table until somebody measures it properly.
A batch charges everyone with one statement, and UPDATE ... FROM (VALUES ...) in PostgreSQL applies one matching row per target row and silently drops the rest. A player betting on both slots appears twice in the list, gets charged once, and plays the second bet for free — no error, no trace. The bets are grouped and summed per player before the statement, so each appears once.
Two more cases the batch has to survive:
Measured on one machine, same session, with the client betting the way a real client does — committing a seed and revealing it:
| Concurrent | Tick completeness | Bet confirmed (median) |
|---|---|---|
| 100 | 97.2 % | 165 ms |
| 300 | 94.3 % | 5,919 ms |
| 600 | 63.6 % | 15,784 ms |
About 100 is the comfortable ceiling. At 300 a bet takes almost six seconds, which a player staring at the screen reads as broken. At 600, completeness of 63.6 % is not a statistic — it is the rocket freezing for a third of the room and those players unable to cash out in time, which in a money game is the worst outcome there is.
And this was measured on a laptop with more CPU than the 0.5 the production plan has, so the real ceiling is lower than these numbers.
MAX_CONEXIONES (default 200) rejects connections above the limit with a server:full event, checked first in the connection handler — before the token is verified and before the database is touched, because when it is full the rejection has to cost nothing.
Verified: 120 clients against a cap of 50 let exactly 50 in, and those 50 saw 100 % completeness — a flawless game, where 300 uncapped gave everybody a degraded one. That is the whole point. Without a cap a surge hands the broken version to everyone; with one, some people see the game as it is and the rest are told the truth and come back on their own.
The client retries by itself after 20-45 seconds, spread at random. Telling five hundred rejected clients to come back in twenty seconds brings back five hundred at the same instant and rebuilds the surge; spread out, they trickle in as slots free up.
Roughly half of the queued bets come back as rejected — 21 of 40 at 100 players, 72 of 154 at 300. That is the harness colliding with itself, not a fault: a queued bet is placed when the next round opens, and the harness picks each socket with probability 0.5, so about half of those sockets bet again into a slot that is already taken. The ~50 % ratio is exactly that probability. A real player cannot do it — the button is disabled while a bet is live in that slot.
cd backend
MAX_CONN_POR_IP=5000 npm run dev # the per-IP limit would sink the test
npx tsx src/pruebas/carga.ts 600 90 # players, seconds
The test players are deleted when it finishes, and also if it crashes: leaving them behind dirties the operator reconciliation, which watches that balances agree with the ledger.