by jiggawatts · 2y ago · 0 comments
var a = await AlphaAsync();
var b = await BetaAsync();
var g = await GammaAsync();
will be slower than the equivalent synchronous code. It does the same work, in the same order, and will not run any part in parallel. It can theoretically scale to more concurrent connections and in-flight requests, but I had a tough time achieving a measurable benefit even in benchmark tests.To get things running in parallel, the tasks have to be started but not immediately awaited:
var ta = AlphaAsync();
var tb = BetaAsync();
var tg = GammaAsync();
...
var a = await ta; var b = await tb; var g = await tg;
I've only ever seen this turn up in real code maybe once or twice. People just don't do it, and the samples and documentation doesn't show this pattern either.In fairly extensive tests I found that it is actually pretty rare for threads to be the limiting resource, so async provided no benefit at all.
You pretty much need this specific scenario:
- A large auto-scale pool
- Tuned well to keep load at 80-90%.
- High concurrent connections per instance.
- Slow dependencies that return small volumes of data.
- A much larger back end than the front end. Think 100 VMs at the front and 10K at the back.
Violate any of the above and the benefits seem to evaporate. I’m sure that this scenario is common at FAANG sized orgs, but is extremely rare elsewhere.