Question

Serverless variables/server-side data in a 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

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Bobby Iliev
Site Moderator
Site Moderator badge
October 15, 2024

Hi Thomas 👋,

Great question! You’re absolutely correct in thinking about the potential limitations of storing the count variable directly in your serverless function’s runtime environment.

In a serverless environment like DigitalOcean Functions, each function execution is stateless. This means that the function could be executed by different instances at different times, and any variables (like your count variable) stored in memory will not persist across invocations.

When traffic scales up or down, additional instances of your function may be spun up, or some may be de-allocated. This will lead to situations where the value of the count variable might be lost, or you may get inconsistent results depending on which instance processes the request.

To make sure that your count variable is persistent and correct across all function invocations, you’ll need to store the value externally. Here are a couple of approaches you can consider:

  1. Use a Database: Storing the count in a database ensures that the value is persistent across multiple function executions. You can use a small database like:

    • DigitalOcean Managed Databases (MySQL, PostgreSQL, Redis)
    • DigitalOcean Spaces for object storage

    For this scenario, Redis might be a good option since it’s lightweight, fast, and ideal for storing simple key-value pairs like your integer count.

    Example Redis integration:

    • Store the count as a key in Redis.
    • Increment, decrement, or reset the value as needed with each function invocation.
  2. Use Object Storage or a Key-Value Store: If you’re looking for something even simpler, you could store the count value in DigitalOcean Spaces (S3-compatible object storage) or use a key-value store like Redis.

    You could read and write a small JSON file containing your count variable to Spaces after each request. While not as fast as a database, it would work for a low-traffic API.

In short, yes, you are correct in assuming that storing the count in the function itself is not reliable in a serverless environment. The best approach would be to store it in a database or key-value store to ensure persistence across multiple invocations.

Let me know if you need help setting this up with a specific database or service!

- Bobby

Try DigitalOcean for free

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

Sign up

Become a contributor for community

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

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.