Home Ask Login Register

Developers Planet

Your answer is one click away!

user2950593 February 2016

Heroku erases images each time the application shuts down and restarts after being inactive for x minutes?

I saw this question:After git push heroku - uploaded files on Heroku are lost

each time the application shuts down and restarts after being inactive for x minutes), your application is recreated and all stored data is lost.(C)

Right now I have user which can upload two photos.I got email confirmation of new users. So I can check that user registered and uploaded photo 4 and 14 hours ago. I've made my last commit and pushed it to heroku around 19 hours ago. And this 4 images, that new users uploaded are lost now.But I can see images if I just now register the user. So it seems to be really true that my app was inactive for x minutes and then it restarts and deletes images.

I read some questions like this Rails] Images erased after a new commit on heroku There it says that I should use external server like aws s3( I have no idea what it is and how much it will cost and how to connect it) So is it really true? what are my other options? may be I should simply use digital ocean(won't be there the same problem?) or something else. Will this problem continue in paid account?

I use rails and upload files using carrierwave gem, I can't upload code here cause I am writing from another laptop.


eugen February 2016

Heroku's filesystem is readonly. You can't expect anything you upload to persist there, you need to use an external storage mechanism, something like Amazon's S3 for example.

See the links for more details.

https://devcenter.heroku.com/articles/s3 https://devcenter.heroku.com/articles/dynos#isolation-and-security

sheltond February 2016

The filesystem that your Heroku instance runs on is not read-only, but it is transient - i.e. files that you store there will not persist after an instance restart.

This is a deliberate design decision by Heroku, to force you to think about where you store your data and how it impacts on scalability.

You're asking about Digital Ocean - I haven't used them but I assume from your question that they allow you to store to a persistent local filesystem.

The question that you then have to ask is: what happens if you want to run more than one instance of your app? Do they share the same persistent filesystem? Can they access each other's files? How do you handle file locking to avoid race-conditions when several app instances are using the same filesystem?

Heroku's model forces you to either put stuff in a database or store it using some external service. Generally, any of these sorts of systems will be reasonably scalable - you can have multiple Heroku instances (perhaps running on different machines, different data-centers, etc), and they will all interact nicely.

I do agree that for a simple use-case where you just want to run a single instance of an app during development it can be inconvenient, but I think this is the reasoning behind it - to force you to design this sort of thing in, rather than developing your whole app on the assumption that it can store everything locally and then find out later that you need to completely redesign to make it scalable.

Richard Peck February 2016

What you're looking at is something called the ephemeral file system in Heroku:

Each dyno gets its own ephemeral filesystem, with a fresh copy of the most recently deployed code. During the dyno’s lifetime its running processes can use the filesystem as a temporary scratchpad, but no files that are written are visible to processes in any other dyno and any files written will be discarded the moment the dyno is stopped or restarted

In short, it means that any files you upload will only last for the time the dyno is running. When the dyno shuts down, the files will be removed unless they were part of the local git repo.

The way to resolve the issue is to store the files on a third-party service - typically S3. This will store the files on a system independent of Heroku.

Both Paperclip & Carrierwave support S3 (Simple Storage Service) - through a gem called fog. S3 gives you a "free" tier, allowing you to store a certain amount of data (I've forgotten how much) for free.

I would strongly recommend setting up an S3 account and linking it to your Heroku app. This way, any files you upload will be stored off-site.

Post Status

Asked in February 2016
Viewed 1,190 times
Voted 6
Answered 3 times


Leave an answer

Quote of the day: live life