title: How do you best offload a database insert, so a web response is returned quicker?
body: <h3>Setup</h3>
  
  <p>I have web service that takes its inputs through a REST interface.  The REST call does not return any meaningful data, so whatever is passed in to the web service is just recorded in the database and that is it.  It is an analytics service which my company is using internally to do some special processing on web requests that are received on their web page.  So it is very important the response take as little time to return as possible.</p>
  
  <p>I have pretty much optimized the code down as much as possible, to make the response as fast as possible.  However, the time the database stays open still keeps the connection open for longer than I want before a response is sent back to the web client.  </p>
  
  <p>The code looks basically like this, by the way it is ASP.NET MVC, using Entity Framework, running on IIS 7, if that matters.</p>
  
  <pre><code>public ActionResult Add(/*..bunch of parameters..*/) {
  
      using (var db = new Entities()) {
          var log = new Log {
              // populate Log from parameters
          }
          db.AddToLogs(log);
          db.SaveChanges();
      }
  
      return File(pixelImage, "image/gif");
  }
  </code></pre>
  
  <h3>Question</h3>
  
  <p>Is there a way to off load the database insert in to another process, so the response to the client is returned almost instantly?  </p>
  
  <p>I was thinking about wrapping everything in the <code>using</code> block in another thread, to make the database insert asynchronous, but didn't know if that was the best way to free up the response back to the client.</p>
  
  <p>What would you recommend if you were trying to accomplish this goal?</p>