title: Asp.net core Web API parallel call , batch processing with some data lost
body: <p>Created one web API in asp.net core 3.x which is responsible to save data in to Azure service bus queue and that data we will process for reporting.</p>
  <p>API load is too high so we decided to save data in-memory for each request. Once data limit increased up to certain limit (&gt;50 count) next request (51) will get all data from memory and save in to service bus in one go and clear the memory cache.</p>
  <p>for sequential request all logic works fine but when load coming in parallel few data lost and i think it is because of one batch request is taking some time and after that all data problem start.</p>
  <p>I did some research and found article and used <strong>SemaphoreSlim</strong>. It's working fine but is that good approach? As you see in below code I am blocking each request but actually I want to lock when I am processing the batch. I tried to move the lock inside <strong>if condition</strong> but it was not working.
  <a href="https://medium.com/swlh/async-lock-mechanism-on-asynchronous-programing-d43f15ad0b3" rel="nofollow noreferrer">https://medium.com/swlh/async-lock-mechanism-on-asynchronous-programing-d43f15ad0b3</a></p>
  <pre><code> using (await lockThread.LockAsync())
              {
                  var topVisitedTiles = _service.GetFromCache(CacheKey);
                  if (topVisitedTiles?.Count &gt;= 50)
                  {
                      topVisitedTiles?.Add(link);
                      await _service.AddNewQuickLinkAsync(topVisitedTiles);
                      _service.SetToCache(CacheKey, new List&lt;TopVisitedTilesItem&gt;());
                      return Ok(link.Title);
                  }
                  topVisitedTiles?.Add(link);
                  _service.SetToCache(CacheKey, topVisitedTiles);
              }
              return Ok(link.Title);
  </code></pre>
  <p>I got something from research that <strong>concurrentbag</strong> and <strong>blockingcollection</strong> help but I am not aware how can i use in my case. Your small direction will help me.</p>