Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stream (.NET) handling best-practices

Tags:

.net

streaming

The question is entitled with the word "Stream" because the question below is a concrete example of a more generic doubt I have about Streams:

I have a problem that accepts two solutions and I want to know the best one:

  1. I download a file, save it to disk (2 min), read it and write the contents to the DB (+ 2 min).
  2. I download a file and write the contents directly to the DB (3 min).

If the write to DB fails I'll have to download again in the second case, but not in the first case.

Which is best? Which would you use?

like image 504
Jader Dias Avatar asked Dec 17 '22 09:12

Jader Dias


2 Answers

Unless the increased latency is really killing you, I'd usually go for Option 1 unless there's a good reason you don't want the data on the file system (e.g. concerns about security, capacity, ...).

Or maybe Option 3 as suggested by Max Schmeling, save to the filesystem at the same time as writing to the database.

Disk space is cheap, and it's often useful to have a backup of downloaded data (e.g. to test changes to your database writing code, as evidence of the contents of data downloaded, ...).

like image 127
Joe Avatar answered Dec 21 '22 22:12

Joe


To detail Jekke's reply:

Depending on the file system creates many occasions for failure (you must create a valid file name, make sure the file system isn't full, make sure the file can be opened and written to by you but not by anyone else, what about concurrent use, etcetera).

The only benefit of writing to file I can think of is that you'll know the download completed successfully prior to doing anything with the database. If you can hold the contents in memory, do that instead. If you can't and really insist on not going to the database in case of an interrrupted download, at least use .NET's built-in support to help you with the tricky bits (e.g. IsolatedStorageFileStream).

like image 43
reinierpost Avatar answered Dec 21 '22 22:12

reinierpost