Advice on efficient scripting for a specific task

Nytrinzix

Ars Tribunus Militum
1,989
I am extending an existing application with additional scripting that will be running very frequent calculations. I'd like to describe in loose terms my application's specifics and see if you can help me determine an efficient way to do this within a stated time limit.

My application will ping an API every second - or at worst, every few seconds - and return a large JSON dataset with thousands of data points. Once returned, I need to iterate over this captured data and run some addition calculations on the provided data followed by a division operation (almost like an averaging operation), then store the result in a database, and if a particular condition is met I'll also be triggering an alert of sorts. Once this cycle is done, the next API call will be made to capture the next set of data.

However, the next set of data will be extremely similar to the first, only incrementally bigger - an additional 1 (or more seconds) of data will be appended to the data set. Basically, every time the script is run (at least until the end of the day), the data set will be 1 (or more) seconds larger than the previous, but the historical data is "set in stone" and won't change, so each call is 99% similar to the one before it, save for the new time-series data.

The alert trigger, if applicable during a given "run", needs to be kicked off before the next firing of the above script.

Now, take that process, and multiply it by about 10,000. I will be running this script on around 10,000 different data sets in parallel every second (or every few seconds at worst).

---


My initial question is: since the dataset is very similar on each firing of the script, save for the last 1 or more seconds that include the latest data, would it be better to store the returned API called data locally and on every subsequent iteration, only fetch the latest data from the API and combine it with the stored data in the DB? I know the answer to this likely is based on "how fast" the API is - it's pretty fast.

I'm willing to deploy whatever VPS resources are needed for this - I use Digital Ocean so I'm trying to determine whether I need a CPU optimized VPS, a Memory optimized VPS, or just maximize for everything.

---

Any help is appreciated!
 

minnmass

Ars Tribunus Militum
2,087
Subscriptor
How "almost like an averaging operation" is it? And, how exact do you need the result to be?

If you have the current average of a collection and the count of values in the collection, updating the average to account for one more value doesn't require re-calculating the entire average (eg., this SE/Math answer). It may be possible to redefine the problem such that the client can store the last request's ID (so the next request can get the diff) and a running "average" and count of elements (or some similarly-simple data) to more cheaply update the trigger value and persist the updated data.

Since computers are limited in the values they can store (especially floating point values), adding values one-by-one to the average will tend to introduce errors that might not be present if calculated the normal "add 'em up then divide once" way. It would be worth the time to run a few days' worth of API responses through the re-defined algorithm to see if the inaccuracies affect alerting.

My initial question is: since the dataset is very similar on each firing of the script, save for the last 1 or more seconds that include the latest data, would it be better to store the returned API called data locally and on every subsequent iteration, only fetch the latest data from the API and combine it with the stored data in the DB?

Assuming sufficient local resources, it will always be faster to use local data than data from the network (see, eg., this article about relative latencies. This is why browsers cache CSS, JS, images, etc..

Storing the previous API responses (especially in a parsed, not-JSON format) and appending the diff will probably have lower memory requirements than parsing the full response every time. It'll also probably have lower local CPU requirements, since it won't have to parse the old data again.

It'll also be worth giving serious thought to the collection that's going to contain the historical data: if you're using a library that allows for an array-like structure to grow, you might run into unexpected memory pressure when the collection is being grown; setting an initial size or using a Linked List-type collection would be worth considering/investigating.