Saturday, March 2, 2024
HomeGolangGoroutines and I/O - Technical Dialogue

Goroutines and I/O – Technical Dialogue

Not too long ago, I’ve been requested throughout an interview “What number of goroutines would you create to ship 1M http requests on the machine with 16 CPU?”. So, after a while considering I noticed I don’t know a solution as a result of principally it depends upon many alternative causes:

  • latency of the requests (what number of goroutines shall be parking ready)
  • measurement of paylod and response (CPU loading wanted for marshaling/unmarshling)
  • and many others.

so, the reply on high of my head is “it relies upon” and “must be examined”

I’d respect to listen to your reasoning about this query.

Is it a trick query? It’s my understanding that a brand new goroutine is created for every request (assuming we’re utilizing the usual library’s HTTP server).

EDIT: Oops. Sending requests; not receiving :upside_down_face:

The query was to “ship requests”, to not “obtain requests”.

Anyway, for “what number of requests would you do in parallel” questions, I normally begin of with the next assumptions:

  1. There may be infinite bandwidth accessible (community and disk!)
  2. There may be zero latency
  3. There isn’t any connection restrict on the distant
  4. Now we have infinite file descriptors
  5. We absolutely personal the CPU

Based mostly on this assumptions I normally begin with 2 to three instances numprocs and tweak them primarily based on experiments and observations.

I do comparable concerns for every other “how giant ought to the pool be” form of questions.

Explaining the chain of ideas, the foundational assumptions and the reasoning for the primary quantity is normally rather more necessary to the interviewer than the concrete reply you give.

If I have been the interviewer and asking the query, and also you’d simply reply “32 to 48, then tweak”, that’d be a detrimental. I’d maybe attempt to push you to offer me some reasoning, but when I’ve to ask actually “why do you assume thats acceptable”, that be a no go.

And if the interviewer is actually simply excited about you saying the quantity as it’s written on their questionaire, then the job wasn’t value your time doing the interview anyway…


It was a questionaire for 15min and seven questions offered by a recruiter simply to be preliminary verified by tech result in begin discussing tech interview :slight_smile: That’s as a result of I used to be confused and began digging attempting to understand what I’m lacking.
Thanks for thorough clarification

Hello @Mikhail_Bolshakov, IMHO, this query’s reply is: we now have not sufficient informations, as a result of coroutines scheduler will not be deterministic, and we aren’t speaking a few fastened algorithm the place we may calculate roughly the right quantity (divide-et-impera for example). Doing a lot assumptions is way from the fact. Concept is for fastened algorithms the place you possibly can impose abstracts idea. Right here it’s best to AT LEAST know the time spent in scheduling on that CPU, making ready and sending the request, receiving the consequence and discard it…is completely ineffective and no actual engineer would ask one thing like this in a severe query.


Yeah, for a pre-screening this query is certainly underspecified and much too open ended.

1 Like

To ship 1M concurent http requests you have to precisely 1M goroutines.
Whether or not the mandatory assets, comparable to bandwidth, processor, reminiscence, can be found or not is one other dialogue. Anyway, the bandwidth depends upon how a lot data you ship, the reminiscence most likely depends upon how a lot data you obtain, the variety of processors doesn’t matter as they’re reused (goroutines should not threads!). The variety of descriptors within the working system should be elevated to help 1M requests and so forth. However the variety of goroutines is all the time equal to the variety of concurrent requests.
If we’re not speaking about concurrent requests, however 1M requests in a time interval, then most likely some calculations have to be executed in accordance with what I stated above.

1 Like

@geosoft1 I don’t assume so. Goroutine isn’t simply an async name, it isn’t simply an abstraction like promise in js . They aren’t free. They’ve bodily parameters comparable to stack measurement. So you need to listen on what atmosphere (cpu, reminiscence and many others) your code is operating on when coping with goroutines. Whereas your assumption might be cheap in principle, it’s inpractical in my perspective.

16 goroutines are sufficient. Add the request to the duty queue.



Please enter your comment!
Please enter your name here

Most Popular

Recent Comments