question

Ozan Yilmaz avatar image
Ozan Yilmaz asked

Which approach should I use?

Hello,

I have 2 approaches about reading/writing data in Cloudscript in this kind of cases. Which one will be more efficient in terms of billing?

1)

    // ... checking if 'args' are sent correctly

    var getData = {
        "PlayFabId": currentPlayerId,
        "Keys": ["Key1", "Key2", "Key3", "Key4", "Key5", "Key6"]
    };
    var result = server.GetUserData(getData);

    if(result.Data["Key1"] != null) {
        // ... do something

        if(result.Data["Key2"] != null && parseInt(result.Data["Key2"].Value) == 1) {
            return 0;
        }
    }

    if(parseInt(args.Result) == 0) {
        var value1 = result.Data["Key3"].Value;
        var value2 = result.Data["Key4"].Value;

        // ... do something
    }
    else {
        var value1 = result.Data["Key5"].Value;
        var value2 = result.Data["Key6"].Value;

        // ... do something
    }

In the code above, I read all 6 keys at one time. I don't use some of them, but just in case, I read all of them at the beginning. Basically, 1 GetUserData call and 6 key-value pairs read.

2)

    // ... checking if 'args' are sent correctly

    var getData1 = {
        "PlayFabId": currentPlayerId,
        "Keys": ["Key1"]
    };
    var result1 = server.GetUserData(getData1);

    if(result1.Data["Key1"] != null) {
        // ... do something

        var getData2 = {
            "PlayFabId": currentPlayerId,
            "Keys": ["Key2"]
        };
        var result2 = server.GetUserData(getData2);
        if(result2.Data["Key2"] != null && parseInt(result2.Data["Key2"].Value) == 1) {
            return 0;
        }
    }

    if(parseInt(args.Result) == 0) {
        var getData3 = {
            "PlayFabId": currentPlayerId,
            "Keys": ["Key3", "Key4"]
        };
        var result3 = server.GetUserData(getData3);

        var value1 = result3.Data["Key3"].Value;
        var value2 = result3.Data["Key4"].Value;

        // ... do something
    }
    else {
        var getData3 = {
            "PlayFabId": currentPlayerId,
            "Keys": ["Key5", "Key6"]
        };
        var result3 = server.GetUserData(getData3);

        var value1 = result3.Data["Key5"].Value;
        var value2 = result3.Data["Key6"].Value;

        // ... do something
    }

In this code, I read the keys when they are needed. The worst case is 3 GetUserData call and 4 key-value pairs read. The best case is 2 GetUserData call and 2 key-value pairs read.

Which approach should I use? The first approach looks like spinning read/write meter unnecessarily, so the second approach looks more appropriate, but I wonder if calling GetUserData function has any affect on billing too?

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

·
Sarah Zhang avatar image
Sarah Zhang answered

>> The first approach looks like spinning read/write meter unnecessarily, so the second approach looks more appropriate, but I wonder if calling GetUserData function has any affect on billing too?

Yes, calling GetUserData API will affect the billing. Calling this API will cause the Profile read meter increment. You can check our Pricing documentation, especially this documentation -- https://docs.microsoft.com/en-us/gaming/playfab/features/pricing/meters/profile-reads for more details.

2 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Ozan Yilmaz avatar image Ozan Yilmaz commented ·

I asked the question wrong. Does calling many GetUserData function has any effect on billing? For example, same data, which one has more effect on billing? 1 GetUserData call with 4 keys or 4 GetUserData call with 1 key?

If I start coding like my second approach I need use more than 1 GetUserData in order to reduce the data I get, which is a positive affect on billing, but that will increase the the number of GetUserData calls.

0 Likes 0 ·
Sarah Zhang avatar image Sarah Zhang Ozan Yilmaz commented ·

Apologies for the delayed reply. The first solution would be better, “1 GetUserData call with 4 keys”. The execution time of CloudScript would affect the pricing too. This doc -- https://playfab.com/pricing/ mentioned this point.

0 Likes 0 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.