Database calls rate limit

Hello, calling the function Moralis.settings.setAPIRateLimit in the cloud code, does include also database calls, in order to control them into the limits that have been set?

For example to avoid malicious attempts to make excessive calls to the database, by running frontend code => that calls the cloud functions => that call the database

Thank you

no, that doesn’t handle the database calls, you could do some custom tracking for the cloud function to prevent that, you have in request object all the required info like the IP that makes the request

ok thank you, are there any examples in the documentation? I was not able to find them.

And how would you suggest to count the global number of calls an IP is doing, saving them on database as well (I’m not sure because it would be additional query for each request and I think it’s not a good practice)

you can save it temporary in a global variable (you have to take in consideration that an upgraded server will have multiple processes that run in parallel), or use redis (a little more complicated)

there isn’t an exact example in documentation, you have to understand how cloud functions work, how to extract the IP from the request parameter, and then you should be able to handle the rest at lest with the first option

ok, yes because the use of global variables is not recommended by the documentation itself, and it’s explained since there can be many instances managing the same cloud code, so it risks to provide incorrect / inconsistent data I think. If anyone found a good way to handle this, can please advise?

Thank you

you can still use global variables, it will be a different counter per process, you have to take that in consideration, to know that there will be different values for that global variable for every process, and the number of processes is equal to the number of CPU cores on that system