Search code examples
parse-platformparse-cloud-code

How to calculate the number of requests in a CloudCode function?


I'm building my first app that uses Parse for storing user-data and sending Push Notifications. We're expecting the app to be no more than 10,000 users if it's successful due to its niche market, so it won't have a massive user-base although could get busy traffic from them at peak times. I'm not sure if my CloudCode functions are structured as efficiently as they could be and am worried about the number of requests/second spiralling out of control at peak times (if we hit our target number of users).

Is there any way of counting the number of requests in a CloudCode function call so I can figure roughly how many concurrent users the free service would allow? I know you're meant to be able to check the requests in the Analytics>Performance graphs but there is no data at all coming through (obviously there's only a few concurrent users at the moment but it's useless if it only starts showing data once under load, also the total requests for my app only seems to update once per day in the overview of the dashboard)

I've looked into the pricing FAQs and under 'What types of operations are counted as API requests?' it mentions: 'Queries, saves, logins, amongst other kinds of operations will be taken into account..' but i still can't figure what's going to be considered a request so I wondered if anyone could help me figure the number of requests that would be counted in the following afterSave function...

Parse.Cloud.afterSave("Job", function(request){

var userSendingJob = request.user;
var job = request.object;
var areaId = job.get("areaId");
var description = job.get("description");
var jobFee = job.get("fee");

Parse.Cloud.useMasterKey();

var userQuery = new Parse.Query(Parse.User);

//if areaId is contained in in the user's array of areaIds it will return the object
userQuery.containedIn("areas", [areaId]);
userQuery.lessThanOrEqualTo("workingFee", jobFee);
userQuery.find({
    success:function(results){
        for (var i = 0; i < results.length; i++) {
            var user = results[i];
            var relation = user.relation("jobs");
            relation.add(job);
            user.save();
        }

        response.success("all users have been updated with job"+results.length);
    },
    error:function(err){
        response.error("error updating users: "+err);
    }
});

So if just calculating the requests made here would I need to count the job.get("areaId"), job.get("description") etc as separate requests? Would I count the user.save() for each of the inner users that are found by the query? If so what happens if there are hundreds or even thousands of results returned by the query?

Or is the above just a handful of requests even with a large volume of saves on the inner loop of the query response?

Many thanks.

Sean


Solution

  • Every CURD operation performed by you on any data will be counted as a single request. What you can generally perform on Parse data can be easily mapped to Create, Update, Read and Delete operations.

    In your could function, your find query is counted as a single Read request no matter how many objects it returns. Also every Save operation is counted as a request, so the number of times you call save will be counted as one (except for Parse.Object.saveAll() function during batch updates) Lastly, job.get() is not a server request because it simply acts as a getter on the request object (you are not going to server to read anything)

    In Total you have results.length + 1 requests in your function as you have a single query and multiple saves. Bear in mind that calling a cloud function or job is also counted as one request.