如何执行大量的阻塞/同步I/O操作

huangapple go评论71阅读模式
英文:

How to run large amount of blocking/sync I/O operations

问题

我有一个大约10万用户的列表,我需要为每个用户执行同步的WCF调用,并且需要尽快完成整个列表。每个WCF调用大约需要200毫秒到1秒,这取决于服务器的状态。我在考虑使用Parallel.For,但是否有更好的方法?

英文:

I have a list of around 100k users and I need to perform a sync WCF call for each of them and I need to complete the list as fast as possible. Each WCF call is around 200ms-1s depending the server status. I was thinking of using Parallel.For but is there better way?

答案1

得分: 0

Blocking IO在并行化方面并不容易,因为它会占用线程,而线程相对较昂贵。此外,如果所有工作都发送到同一后端,增加并行性通常会降低性能,因为它可能会增加服务器上的许多开销并导致瓶颈。

在我看来,真正的问题在于工作单元的大小,听起来你需要对每个记录执行一次API调用;1000批次,每批次(比如说)100条记录通常比100,000个单独操作的性能要好得多。如果是我的话,我会考虑将其重构为批处理API。如果底层服务无法更改,另一个选择是在尽可能靠近后端的位置运行编排服务(最好是在同一台服务器上),这样至少可以接收批次并在本地展开它们,从而最小化延迟开销。但请注意,如果延迟在当前问题中不是一个重要因素,那么这将不会有帮助:如果200毫秒到1秒的延迟仅仅是由于服务器实现缓慢,那么你可能无法做太多来解决这个问题(除非可能...投入时间来加快它)。

这里的一个次要问题是异步;异步可能很重要(尤其是服务器端),但在这种情况下,我不会把重点放在这里。

英文:

Blocking IO is not awkward to parallelize because it ties up threads, and threads are relatively expensive. Additionally, increasing parallelism can often degrade performance if all the work is going to the same backend, as it can increase a lot of overheads at the server and hit bottlenecks.

IMO the real problem here is the size of the unit-of-work, where it sounds like you need to perform an API call per rexord; 1000 batches of (say) 100 records is usually vastly more performance than 100k single operations. If it was me, I'd be looking to refactor this into a batched API. If the underlying service can't be changed, another option is to run an orchestration service as close to the backend as possible (same box, ideally), that can at least receive batches and unroll them locally, minimizing latency overheads. Note however that if latency isn't a significant factor in the current issue, this won't help: if the 200ms-1s is simply due to a slow server implementation, then you probably can't do much to work around that (except maybe ... invest the time to make it less slow).

A secondary concern here would be async; async can be important (especially server-side), but in this instance it isn't where I'd focus.

huangapple
  • 本文由 发表于 2023年7月23日 15:01:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76746993.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定