This is my first post so please bear with me if I've missed any guidelines.
I'm implementing a solution for throttling keys on the fly, and it revolves around a dictionary that has to be locked before every request (as multiple threads can ask for and modify the same key at the same time). As expected the number of items in the dictionary does not matter but the number of threads being used increases lock contention.
If I can't get a lock on the dictionary I just return OK, so as to not delay the caller. Each successfully taken lock takes around 5us, each unsuccessful about 600uS. So not only does lock contention cause a potentially throttled key to be allowed to proceed, it also takes a lot of time. Locking mechanism is using Monitor.TryEnter.
Test parameters: 20k items in dictionary, equal weighted
With 10 threads I get 140k tps, with 6% lock contention With 20 threads I get 90k tps, with 10% lock contention With 30 threads I get 80k tps, with 14% lock contention
While these are ridiculously high TPS numbers that will likely never be hit on a single box, the problem still stands when the number of accessing threads is high, even with low TPS.
What would the best way to improve this situation be? Perhaps I can have a sharding mechanism with individual locks for multiple dictionaries, and do a separate lookup to find which dictionary to use?
Thanks :)