Keeping Variable Data on Heroku with Python Flask Application

Question:

Say I have a Hobby plan or above on Heroku which has the feature "never sleeps".

From the time my Python Flask app starts running on Heroku, can I assign and keep variables in this Python app throughout the time the app is running on Heroku?

My plan is to periodically make GET requests to an API and store the JSON data in a string. Every time a new GET request is made, the same string reference variable will be reassigned the new JSON string data.

In this way, could this work? As long as what they say is true and the app never sleeps, can I just keep this data in a Python variable to then make a GET request from my end to this Python Flask app to retrieve the JSON data?

In other words, can I avoid storing the JSON data properly either in a database or file in the project folder tree?

Asked By: Jiehfeng

||

Answers:

No, you cannot expect data to remain in local memory indefinitely. Even non-sleeping dynos restart frequently and unpredictably (at least once per day). Even if dynos didn’t restart like this, you’d still have problems.

Your best bet is to store this data outside of your application’s local memory, e.g. in a proper client-server database or in something like Redis.

Answered By: Chris
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.