Saving user file uploads

Hi all,
a general discussion here. How does one implement saving user file uploads. For example, you have an app that allows users to upload pictures. How do you save that picture?

  1. Save the metadata of the file and the file itself as a blob in the database.
  2. Save the metadata of the file in the database. But save the file itself in the filesystem. For retrievals the app will construct the filepath from the metadata in the database and load from filesystem.

Is there a 3rd way?

The problem i have with 2. is that the database and filesystem can get out of sync after some time when say metadata is saved but the writing to filesystem failed. How can we guarantee the two actions are carried out in one atomic action?

  1. Store metadata into the database and upload the file to some cloudspace (Amazon, Google, whatever)

But, if you want to have guaranteed atomicity there is only option 1 for you. You can’t guarantee across system boundaries without doing some very heavy lifting, and still then there could happen things that roll back a successfull transaction because the message from the Filesystem to the Database confirming the write gets lost in the Void (simplified mechanics).

1 Like