I am using DitigalOcean Functions to make a serverless API. The server has an integer variable, “count”. The function processes a post request, and depending on the data in the request, it will increment the count variable, decrement the count variable, reset the count variable to 0, or return the value of the count variable.
From my testing, the API works exactly as intended but I’m wondering if it would be better to connect the function to a database and store the value of the variable there.
If I compare the serverless function to a traditional program, the value of the count variable will be lost when the process representing the program stops executing. In a serverless environment, is it possible that some requests will be handled by a different processes and therefore return different results for the value of the server-side variable? When traffic to the API changes, and more resources need to be allocated or de-allocated to the function, will the process terminate and be restarted, therefore resetting the count variable?
To put my question another way: If I need to store a small amount of data (a single integer variable) how can I ensure that this data will be persistent in a serverless environment?
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
Sign up for Infrastructure as a Newsletter.
Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.