I have a ConcurrentBag in .Net 4.5, in which I store about 4,000 rows from a database. I keep DTO.
My entire application relies on this. I have functions that return the whole list, as well as functions that return a single item. So many places in my code, I make LINQ queries in collections, etc.
I pushed all this to production, in a place that receives significant traffic, and immediately a 100% processor. I used the iis diagnostic tool and, of course, there were another 50 threads waiting for the ConcurrentBag at the dead end.
The documentation says that this collection is thread safe, but either itβs wrong, or the performance of this collection is not good, therefore it is not indirect for threads.
This collection, unfortunately, is not readable. If one of the functions that scans the ID returns null, it will go to the web service and add it.
I also converted it to ConcurrentDictionary and had the same problem. Locks for days in the .Values ββproperty.
What is the fastest and most reliable solution in the most extreme scenarios?
private ConcurrentBag<Students> _students; public static ConcurrentBag<DestinyHash> GetStudents() { if (_students == null) { _students = new ConcurrentBag<Students>(); } return _students; } public static Student GetStudentByID(int id) { if (GetStudents().Any(x => x.id == id)) { return ... } _students.Add(getStudentFromDb(id)); return... }
An example of use is placed throughout the application.
Helper.GetStudents().FirstOrDefault(x => x.name == "foo" && x.status == "bar"); Helper.GetStudentByID(50);