Question

Variables in serverless functions/server-side data in serverless environment

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?


Submit an answer


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 In or Sign Up to Answer

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Featured on Community

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more