HTTP package use of goroutines and memory overhead

Hello I am writing a database driven program that can be expected to have thousands of requests per second and I am using Go’s main HTTP package . My question is does Golang HTTP package fires of 1 Goroutine per request or does it fire many ? The reason why I ask is because I read this article Golang 1 million requests per minute and am unsure if something like this could benefit me for my particular project . My code looks like this … almost all of my code performs some action that involves the database . I know that in an environment when you have many requests that there could be a lot of Go-routines being spawn and that can overload a servers CPU or memory so I am trying to be proactive before I start .

``

func Profile(w http.ResponseWriter, r *http.Request) {
if parseToken(tokenValue,mySigningKey,w) == false {
	return
}
          
ctx, cancel := context.WithTimeout(context.Background(),connectionTimeout)
defer cancel()
r.ParseForm()
id := r.FormValue("id")

var result string

db.QueryRowContext(ctx,"select json_build_object('profiledash', array_to_json(array_agg(t))) from " +
	"(select id,fullname,city::text || ', ' || state::text AS location,profession,picture,answers from profiles where id=$1 )t",id).Scan(&result)
io.WriteString(w,result)

 }

So, you want to be proactive about performance?

The best thing you can do is set yourself up so you can improve performance over time. In general, the process is:

  1. Instrument your code
  2. Monitor it’s performance (e.g., with prometheus)
  3. Improve performance where it matters most

This process should extend from development, through production, and keep going until the program isn’t running anymore.

The article was actually following this process. The latency graphs are from their production environment. While the article doesn’t say how they chose to work on improving that feature, the graphs show that it was working before they attempted improvements.

Other than getting this infrastructure in place, I would figure out how to simulate the “thousands of requests per second” as realistically as possible. This is more than just hitting an end-point at that rate. You also need to worry about frequency of request types, number of clients, ordering of requests that a client makes, etc. This simulation will give you the performance numbers to direct your improvements during development, so make it as realistic as possible.

My question is does Golang HTTP package fires of 1 Goroutine per request or does it fire many ? The reason why I ask is because I read this article Golang 1 million requests per minute and am unsure if something like this could benefit me for my particular project .

The HTTP server provided by net/http has years of performance improvements. It’s more likely that your code will be the bottleneck, as was the case in the referenced article. Using the performance improvement process described above will help you find spots in your project where to look to improve performance. Once you find the problem areas, then you can find the technique to improve them.

1 Like

Thanks a lot for the in depth reply, I will begin to do those 3 points you mentioned above . As it is better to be prepared and have good practices implemented from the beginning .

1 Like

Glad my response was helpful.

I also liked it, so I expanded into an article: How to be proactive about performance.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.