Writing directly to a database on every HTTP request works fine for low-traffic sites, but fails catastrophically under heavy polling bursts. In live voting events, thousands of connections submit votes concurrently. If each vote fires a separate write transaction through Entity Framework Core, database connection pools are quickly exhausted, leading to timeouts and lag.
Decoupling Writes with a Channel Queue
To optimize this, we designed a producer-consumer thread-safe queue. The incoming HTTP request writes to an in-memory queue and returns an immediate 202 Accepted status code. The client connection is released immediately. Behind the scenes, an ASP.NET Core BackgroundService reads these records, batches them into chunks of 100, and performs bulk writes via Entity Framework Core inside a single roundtrip transaction.
DbConnection Retention
By shifting from transactional writes to batch inserts, we reduced the write overhead by over 90%. EF Core's change tracker runs once per batch, and locks on the Votes table are kept minimal, ensuring the application remains blazing fast under intense loads.