Your opponent's move arrived 1.5 seconds late. Now it takes 0.1
Retrucada is 1-on-1 truco, the Brazilian card game of bluffing and raising the stakes, played in a corner bar. I opened it in two browsers, one on each side of the table, and played a game against myself. It worked, but it felt sluggish: I played a card on one side and it took a while to show up on the other. Sometimes a long while. I measured it, found two causes and fixed both. Your opponent's move, which took 1.5 seconds to reach you, now takes 0.1.
How a move reaches you
Retrucada doesn't keep a connection open between your browser and the server all the time. When it's your opponent's turn, your browser asks "can I play yet?" and the server holds that question for up to 20 seconds, waiting for something to happen. When your opponent plays, the server answers, and your browser shows their card. This is called long polling.
The catch is in how the server "waits". It runs on AWS, in pieces that don't talk to each other: the piece holding your question doesn't hear about it when the piece that got your opponent's move saves the game. So it has to go and look at the game in the database every so often.
On my computer, where everything runs in a single process, the server tells it right away. That's why I had never noticed the problem.
Measuring
Every request that reaches the server is logged: when it arrived, what it asked for and how long it took. That's enough to rebuild a game: the moment one side played and the moment the other side got the answer.
The first thing that showed up was how long the waits lasted. They were all multiples of 2.4 seconds:
2.43 s 4.84 s 7.24 s 9.67 s 12.12 s 14.48 s
The server looked at the game, saw it wasn't your turn yet and slept for 2.4 seconds before looking again. If your opponent played right after a look, their card sat there until the next one. A median delay of 1.3 seconds from that alone.
The hidden move
The second cause was worse. The server only answered when it was your turn. But in truco your opponent sometimes plays twice in a row: they win the round and lead the next one. When that happened, you didn't see their first card until they played the second.
In the game I measured, this happened 11 times. In the worst one, my other browser went 12 seconds without seeing a card that was already on the table.
The fix
Two changes, both small:
- Look more often. The server now checks the game every 0.3 seconds instead of 2.4. It sounds wasteful, but it isn't: the server sits waiting either way, and the only extra cost is the database reads, which come to fractions of a cent per game.
- Answer on every move, not just on your turn. Your browser now tells the server how many moves it has already shown. If there's a new one, the server answers right away, even if it's still your opponent's turn. The browser shows the card and asks again. As a bonus, whoever creates a room sees the table as soon as their friend joins, even if the friend plays first.
AI agents playing through the API don't send that count and work as before: the server only answers them when it's their turn, which is what they need.
Before and after
I played a whole game again, in two browsers, after the fix:
| Before | After | |
|---|---|---|
| From a move to the other side seeing it (median) | 1.55 s | 0.12 s |
| 9 out of 10 moves arrive within | 3.04 s | 0.28 s |
| Worst case | 12.15 s | 0.30 s |
| Moves hidden behind the next one | 11 | 0 |
The worst case is now exactly one look: 0.3 seconds. These numbers measure the server; the internet between you and it adds a few hundredths.
Why not an open connection
The "proper" way to do this is a connection that stays open (a WebSocket), where the server pushes the move to you the instant it happens. It's more work: another piece on AWS, a list of who is connected, what to do when the connection drops mid-game. With a 0.1-second median, you can't feel the difference in a game of truco. If that ever changes, the measurement is ready to say so.
For programmers
The measurement became a command. It reads the API Gateway access logs in CloudWatch, tells the two sides apart by browser and works out, for each move, how long the other side took to get it:
pnpm latency # recent games with both sides waiting
pnpm latency <game> # median, p90, worst case and hidden moves
The server change is in wait in server/games.ts: the interval between reads went from 2,400 to 300 ms, and the new seen parameter makes it return as soon as the game has more moves than the browser has shown (-1 while the room waits for an opponent).
The lesson: measure where the game really runs. On my computer everything was instant, because there the server didn't need to keep checking the database. Only by playing in production, from both sides, could I see it.