Is it better to keep a cache of client data or invoke the server to retrieve it when needed?

I am obviously doing checks on the server. My question here is in terms of performance/suitability for checks on the client before sending a request to the server.

When I want to check if the client data meets a particular condition, should I be invoking the server to request the client’s data and using it to perform the client check or should I instead keep a cache of the client data, which in turn is updated whenever the client data on the server is changed by firing an event back to the client to update the cache?

Giving a pseudo-code example here:

local clientData = requestData:InvokeServer()
if clientData.money >= 50 then
     --code
end

or from a cache:

local cache = {
     money = 50
}

local function hasEnoughMoney()
     if cache.money >= 50 then
          --code
     end
end

dataUpdated.OnClientEvent:Connect(function(newData)
     cache = newData
end)

My idea is for the latter as if the data hasn’t changed I don’t see a need to bother the server with such a request as the serverside will handle any final checks regardless and the cache will be updated whenever the data changes on the server anyway, although I’d like to hear other thoughts.

A cache is better, since you’ll be able to access the data when you need it instead of waiting for the server. This should make it more responsive for users when they try to buy something.

2 Likes

I agree with Posatta here, its better if its simply for doing client-side checks, because the server should just be ignoring any changes to the client-side data anyway. You should update the cache whenever the server updates it though.

Thank you, that was my thoughts as well due to latency between the client and server.

2 Likes