Introduction
In the world of web development, performance is king. Fast-loading applications keep users happy, reduce server load, and improve overall user experience. One of the most effective ways to achieve this is through intelligent caching. When a client requests data that hasn't changed since their last request, wouldn't it be great if we could just tell them "you already have this"? That's exactly what the HTTP 304 Not Modified
status code is for.
However, implementing 304 Not Modified
effectively can be tricky, especially when in the real world data lives in a database. How do you efficiently determine if the data has truly changed without re-fetching and re-processing everything? This is where Delta comes in. As SimonCropp says
Delta is an approach to implementing a
304 Not Modified
leveraging DB change tracking.
The approach uses a last updated timestamp from the database to generate anETag
. All dynamic requests then have thatETag
checked/applied.
Tech Stack choice
I chose this combination because:
1.Dot net is the most performant framework
2.EF Core has gotten better and provides a slew of performance steps
3.PostgreSQL is a powerful, open source object-relational database that safely stores and scales the most complicated data workloads.
4.Delta An efficient approach to implementing a 304 Not Modified
leveraging DB change tracking
Steps to use Delta
- Postgres requires
track_commit_timestamp
flag to be enabled. Log into thepsql
with a user that has admin privileges.
psql -U <insert_your_user_name>
ALTER SYSTEM SET track_commit_timestamp = on;
Restart the Postgres service.
- Add Delta.EF to your .NET solution.
dotnet add package Delta.EF --version 6.4.2
- Enable row versioning in your AppDbContext.
public class ApplicationDbContext : DbContext
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options) { }
public DbSet<Product> Products { get; set; }
protected override void OnModelCreating(ModelBuilder builder)
{
var product = builder.Entity<Product>();
product.HasKey(p => p.Id);
}
}
- Update your
Program.cs
file.
var builder = WebApplication.CreateBuilder(args);
AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true);
builder.Services.AddDbContextPool<ApplicationDbContext>(options =>
{
options.UseNpgsql(builder.Configuration.GetConnectionString("DefaultConnection"), opts =>
{
opts.EnableRetryOnFailure(maxRetryCount: 5,
maxRetryDelay: TimeSpan.FromSeconds(30),
errorCodesToAdd: null);
opts.CommandTimeout(60);
});
if (Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") != "Development") return;
options.EnableDetailedErrors();
options.EnableSensitiveDataLogging();
});
var app = builder.Build();
// activate Delta
app.UseDelta<ApplicationDbContext>();
Results
Ran two endpoints
- Get a resource item.
Before enabling Delta
After enabling Delta
The response time reduced from 7ms to 6ms
- Stress tested using a
getAll
resource endpoint returning 1000 items.
Before enabling Delta
After enabling Delta
The response time reduced from 13ms to 7ms
Conclusion
The Delta approach, by cleverly leveraging database change tracking, offers a powerful and efficient way to implement 304 Not Modified
. It moves the responsibility of determining "what has changed" to the database, where it's the most efficient, freeing up your application server to focus on serving dynamic content. By embracing this strategy, you can significantly improve the performance, scalability, and user experience of your web applications. Consider integrating Delta into your caching strategy and unlock the full potential of 304 Not Modified
!
Feel free to leave question or a comment about this.
Star Delta or sponsor Simon Cropp to give the package the platform it deserves.
Top comments (0)