I'm building an online multiplayer strategy game. The game has no offline mode and requires syncing up to a server. Part of this game is constructing buildings which will generate resources each second. These resources would be generated whether the player is online or offline.

If this was a purely offline game, this would be easy: get the delta between when the app was closed and when it was opened again and multiply by resources per second and just fast forward everything.

Since it's an online game, and other players can be affected by the resources an offline user has, these calculations have to occur on the backend.

My question is: if I have 10,000 players, each with 20 buildings generating resources on a per second basis, how on earth should I handle 200,000 things all at once and syncing to a database?

  • 1
    \$\begingroup\$ Hint: you can fast-forward your state when a different user logs on too. There's nothing special about the owning player that makes this solution applicable to them alone. \$\endgroup\$
    – DMGregory
    Jun 16, 2020 at 6:50

1 Answer 1


Recalculate (fast-forward) resource amount on each use.

  • On server, recalculate after db fetch and keep it cached for duration of current request. Do not store into db unless it's modified explicitly, e.g. withdrawn or deposited. Anything that affects growth rate must trigger update too.

  • On client, do it every time value is used (mostly for rendering, I presume), or do it periodically with e.g. 1 second interval. Carefully synchronize time between client and server to minimize desync issues.

(Throwing in some code because why not.)

class PassivelyGeneratedResource
    public readonly DateTime SnapshotTime;
    public readonly int SnapshotValue;
    public readonly double GrowthRatePerSecond;
    int? _cachedValue;
    public int CurrentValue => _cachedValue ?? (_cachedValue = Recalculate());

    void Recalculate() {...}
  • \$\begingroup\$ It seems the simplest solution was the least obvious. Thanks for this, it was very helpful. \$\endgroup\$
    – brian
    Jun 17, 2020 at 7:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .