Skip to content
All articles

Backend Development

A realtime multiplayer Okey server: .NET 9, SignalR and an authoritative server

In table games, cheating starts on the client. For a commercial Okey and 101 game, I moved every rule to the server. Here's how I balanced in-memory game state with persistent data in PostgreSQL.

Fahrettin Baştürk4 min read

Turn-based tile games like Okey, 101 and pot-based variants look simple from the outside: four players, one table, drawing and discarding tiles in turn. But once you put them online with an in-game economy, the engineering question changes completely. As a commercial freelance job, I built the server for a game written in Unity, and I set one rule from the very start: the client is trusted with nothing.

Why an authoritative server?

The client runs on the player's computer or phone, which means it can be modified. If you leave tile dealing, hand validation or win checks to the client, someone will eventually find a way to deal themselves four jokers. So all game rules, tile distribution and win validation run on the server. The client only says "I want to discard this tile"; the server decides whether that's allowed.

Why SignalR?

The classic HTTP request-response model falls short even for a turn-based game. When one player discards a tile, the other three need to see it instantly. SignalR provides bidirectional, realtime communication on ASP.NET Core and manages the transport (WebSocket or its fallbacks) on its own. There are two hubs: SocialHub for social interactions and TableHub for tables and games.

Game state in memory, persistent data in the database

The most critical architectural decision was separating state management from persistence. Writing every tile move to the database during a game is both slow and unnecessary. Active game states (GameMatchState) are kept in memory in a thread-safe ConcurrentDictionary. Tile arrangements and turn tracking resolve in milliseconds.

When the game ends, things change: now there's data that must be persistent and consistent. Rewards, experience points and pot payouts are written inside a single database transaction.

await using var tx = await db.Database.BeginTransactionAsync();
winner.Balance += pot;
winner.Xp += xpReward;
db.Ledger.Add(new LedgerEntry(winner.Id, pot, "win", match.Id));
await db.SaveChangesAsync();
await tx.CommitAsync();

Even if the server crashes right at that moment, either the whole reward is paid out or none of it is. Player balances never end up inconsistent.

Keeping rules apart: the strategy pattern

Classic Okey, 101 and pot games share a common skeleton but have different rules. Managing them all in one class with if (mode == ...) blocks turns into code nobody dares to touch within a few months. Each game mode is a separate class implementing the IGameRuleHandler interface. The table asks the rule object without knowing which rules it's playing by: is this hand valid, is this discard legal, what's the score?

Keeping heavy work away from the game

Large aggregate queries like leaderboards shouldn't slow down the game server's main job. They're handed to BackgroundService workers based on PeriodicTimer; they run at intervals and keep results ready. No tile is ever delayed because a leaderboard is being computed while players are at the table.

What I learned

  • In realtime games, the question "what stays in memory and what goes to disk?" decides both performance and consistency.
  • Not trusting the client isn't suspicion; it's a design principle.
  • Any operation involving money or rewards should be written with transaction boundaries in mind.
  • Splitting rules into separate classes makes adding a new game mode no longer scary.

In Turkey, the Okey table is more than a game; it's a place for conversation, laughter and a little stubbornness. Building that same table fairly in a server's memory made this job enjoyable beyond the technical side. A good game server should be like a good referee: invisible, impartial and always there.

Fahrettin Baştürk

Software Engineer & Consultant

Share
  • C#
  • .NET
  • SignalR
  • Game Server

Working on a similar system or problem?Tell me about your project