Search code examples
c#.netdisk

How to handle large numbers of concurrent disk write requests as efficiently as possible


Say the method below is being called several thousand times by different threads in a .net 4 application. What’s the best way to handle this situation? Understand that the disk is the bottleneck here but I’d like the WriteFile() method to return quickly.

Data can be can be up to a few MB. Are we talking threadpool, TPL or the like?

public void WriteFile(string FileName, MemoryStream Data)
{
   try
   {
      using (FileStream DiskFile = File.OpenWrite(FileName))
      {
         Data.WriteTo(DiskFile);
         DiskFile.Flush();
         DiskFile.Close();
      }
   }
   catch (Exception e)
   {
      Console.WriteLine(e.Message);
   }
}

Solution

  • Since you say that the files don't need to be written in order nor immediately, the simplest approach would be to use a Task:

    private void WriteFileAsynchronously(string FileName, MemoryStream Data)
    {
        Task.Factory.StartNew(() => WriteFileSynchronously(FileName, Data));
    }
    
    private void WriteFileSynchronously(string FileName, MemoryStream Data)
    {
        try
        {
            using (FileStream DiskFile = File.OpenWrite(FileName))
            {
                Data.WriteTo(DiskFile);
                DiskFile.Flush();
                DiskFile.Close();
            }
        }
    
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
        }
    }
    

    The TPL uses the thread pool internally, and should be fairly efficient even for large numbers of tasks.