Search Unity

Discussion Is it better to have lots of small key-value pairs or few big ones?

Discussion in 'Cloud Save' started by Filip8429, Nov 11, 2023.

  1. Filip8429


    Dec 16, 2018
    I guess the answer depends on your project but are there any best practices or rules of thumb?
  2. IainUnity3D


    Unity Technologies

    Oct 5, 2022
    No strictly right/wrong or answer here - as you say, what to use might vary depending on your use case - but using separate key/value pairs gives you some more flexibility.

    Example use case:

    If you have something you might update not as often, for example for storing what a player is wearing you could either have one key per slot (e.g. HEAD, CHEST, LEGS, HANDS, FEET, etc) or use a single item for all those values (e.g. and just call it OUTFIT). In either case, the value could be an object. Which one is best in a scenario like that might come down to which you find easiest to work with in your game.

    Some questions which might help you decide what is best for you:

    Do you read or write some values more frequently?

    If you are reading/writing some specific values much more often than others - e.g. player experience points, number of coins they have, a timestamp, inbox / in-game message counter, etc - that you frequently access, then you might want to consider making them distinct items. Making them distinct properties will make them faster to read/write from as there will be less data on the network, this is especially true for things like mobile games where people may have limited network availability.

    Do you want to modify player data from Cloud Code or a Game Server?

    Another consideration might be if you have (or plan to have) Cloud Code or a Game Server also modify player data, sever side. If you have all of your player data in one big item you might find you run into write lock issues more often as it makes it more likely if the same object is being written to often. This is not a big deal, as you can always just have your code attempt a retry, but it might simplify things for you if your data is split across different keys.

    Do you want to be able to query/filter based on a key/value?

    If you think you might want to use Cloud Save Queries to search for or filter based on a value - e.g. all players with a particular value in a field like REGION, LANGUAGE, GUILD/CLAN, or search by players with an XP / LEVEL or LAST ACTIVE value with in a particular range, etc - then you might want to consider using distinct key/values for those items, so you can create Indexes in Cloud Save for those fields to allow searching / filtering on them.
    Marvin_Wack and Filip8429 like this.