title: Redis vs SQL Server performance
body: <p>Application performance is one of the main reason of using cache over relational database. Because it stores data in memory in the form of key value pair, we can store frequently accessed data in cache which are not changes very frequently. Reading from cache is much faster than database. Redis is one of the best solution in distributed cache market.</p>
  <p>I was doing a performance test between Azure Redis cache and Azure SQL Server. I have created a simple ASP.NET Core application and inside that I have read data from SQL Server database as well as Redis multiple times and compare the read time duration between them. For database reading I have used Entity Framework Core and for Redis reading I have used 'Microsoft.Extensions.Caching.StackExchangeRedis'.</p>
  <p>Model</p>
  <pre><code>using System;
  
  namespace WebApplication2.Models
  {
      [Serializable]
      public class Student
      {
          public int Id { get; set; }
          public string Name { get; set; }
          public int Age { get; set; }
          public string Subject { get; set; }
  
          public Student()
          {
              Name = string.Empty;
              Subject = string.Empty;
          }
      }
  }
  </code></pre>
  <p>Entity Framework Core data context.</p>
  <pre><code>using Microsoft.EntityFrameworkCore;
  using WebApplication2.Models;
  
  namespace WebApplication2.Data
  {
      public class StudentContext : DbContext
      {
          public StudentContext(DbContextOptions&lt;StudentContext&gt; options)
              : base(options)
          {
          }
  
          public DbSet&lt;Student&gt;? Students { get; set; }
      }
  }
  </code></pre>
  <p>Startup class</p>
  <pre><code>public void ConfigureServices(IServiceCollection services)
  {
      services.AddControllersWithViews();
          
      string studentDbConnectionString = Configuration.GetConnectionString(&quot;StudentDbConnectionString&quot;);
      services.AddDbContext&lt;StudentContext&gt;(option =&gt; option.UseSqlServer(studentDbConnectionString));
  
      string redisConnectionString = Configuration.GetConnectionString(&quot;RedisConnectionString&quot;);
      services.AddStackExchangeRedisCache(options =&gt;
      {
          options.Configuration = redisConnectionString;
      });
  }
  </code></pre>
  <p>appsettings.json</p>
  <pre><code>{
    &quot;Logging&quot;: {
      &quot;LogLevel&quot;: {
        &quot;Default&quot;: &quot;Information&quot;,
        &quot;Microsoft&quot;: &quot;Warning&quot;,
        &quot;Microsoft.Hosting.Lifetime&quot;: &quot;Information&quot;
       }
    },
    &quot;AllowedHosts&quot;: &quot;*&quot;,
    &quot;ConnectionStrings&quot;: {
      &quot;StudentDbConnectionString&quot;: &quot;[Azure SQL Server connection string]&quot;,
      &quot;RedisConnectionString&quot;: &quot;[Azure Redis cache connection string]&quot;
    }
  }
  </code></pre>
  <p>Home controller</p>
  <pre><code>using Microsoft.AspNetCore.Mvc;
  using Microsoft.Extensions.Caching.Distributed;
  using System.Collections.Generic;
  using System.Diagnostics;
  using System.IO;
  using System.Linq;
  using System.Runtime.Serialization.Formatters.Binary;
  using WebApplication2.Data;
  using WebApplication2.Models;
  
  namespace WebApplication2.Controllers
  {
      public class HomeController : Controller
      {
          private readonly StudentContext _studentContext;
          private readonly IDistributedCache _cache;
  
          public HomeController(StudentContext studentContext, IDistributedCache cache)
          {
              _studentContext = studentContext;
              _cache = cache;
          }
  
          public IActionResult Index()
          {
              List&lt;Student&gt;? students = null;
              var counter = 10000;
  
              var sw = Stopwatch.StartNew();
              for (var i = 0; i &lt; counter; i++)
              {
                  students = _studentContext.Students.OrderBy(student =&gt; student.Id).ToList();
              }
              sw.Stop();
              ViewData[&quot;DatabaseDuraion&quot;] = $&quot;Database: {sw.ElapsedMilliseconds}&quot;;
  
              if (students != null &amp;&amp; students.Count &gt; 0)
              {
                  List&lt;Student&gt; studentsFromCache;
                  var key = &quot;Students&quot;;
                  _cache.Set(key, ObjectToByteArray(students));
  
                  sw.Restart();
                  for (var i = 0; i &lt; counter; i++)
                  {
                      studentsFromCache = (List&lt;Student&gt;)ByteArrayToObject(_cache.Get(key));
                  }
                  sw.Stop();
                  ViewData[&quot;RedisDuraion&quot;] = $&quot;Redis: {sw.ElapsedMilliseconds}&quot;;
              }
  
              return View();
          }
  
          private byte[] ObjectToByteArray(object obj)
          {
              var bf = new BinaryFormatter();
              using var ms = new MemoryStream();
              bf.Serialize(ms, obj);
              return ms.ToArray();
          }
  
          private object ByteArrayToObject(byte[] arrBytes)
          {
              using var memStream = new MemoryStream();
              var binForm = new BinaryFormatter();
              memStream.Write(arrBytes, 0, arrBytes.Length);
              memStream.Seek(0, SeekOrigin.Begin);
              object obj = binForm.Deserialize(memStream);
              return obj;
          }
      }
  }
  </code></pre>
  <p>Home\Index.cshtml view</p>
  <pre><code>@{
      ViewData[&quot;Title&quot;] = &quot;Home Page&quot;;
  }
  
  &lt;div class=&quot;text-center&quot;&gt;        
      &lt;p&gt;@ViewData[&quot;DatabaseDuraion&quot;]&lt;/p&gt;
      &lt;p&gt;@ViewData[&quot;RedisDuraion&quot;]&lt;/p&gt;
  &lt;/div&gt;
  </code></pre>
  <p>I have found SQL Server is faster than Redis.</p>
  <p><a href="https://i.stack.imgur.com/ZWhBu.jpg" rel="noreferrer"><img src="https://i.stack.imgur.com/ZWhBu.jpg" alt="SQL Server vs Redis" /></a></p>
  <p>The ASP.NET Core application is hosted in Azure App Service with the same location with Azure SQL Server and Azure Redis.</p>
  <p>Please let me know why Redis is slower than SQL Server?</p>