FrameworkBenchmarks icon indicating copy to clipboard operation
FrameworkBenchmarks copied to clipboard

Paralles multiple queries in golang webserver

Open hotrungnhan opened this issue 6 months ago • 1 comments

I was simply asking why the Go/Fiber framework appears slower than Node/Express in tests involving multiple queries, if it's single query test is dramatically faster. How come ?

From what I observed, it seems Fiber doesn’t yet implement true parallel query execution—each query is handled sequentially within a goroutine. Meanwhile, Express benefits from Node.js’s event-driven, non-blocking architecture, allowing it to handle multiple queries asynchronously by default.

So the term “multiple queries” in this benchmark can be a bit misleading. I'm not checking for other go framework, they may have same issue.

Reference:

Fiber: server.go#L243-L253

Express: postgresql-app.js#L84-L94

I suggest for this changes

func queriesHandler(c *fiber.Ctx) error {
	n := QueriesCount(c)
	worlds := AcquireWorlds()[:n]
	var wg sync.WaitGroup
	wg.Add(n)

	for i := 0; i < n; i++ {
		go func(i int) {
			defer wg.Done()
			w := &worlds[i]
			db.QueryRow(context.Background(), worldselectsql, RandomWorld()).Scan(&w.ID, &w.RandomNumber)
		}(i)
	}

	wg.Wait()
	c.JSON(&worlds)
	ReleaseWorlds(worlds)
	return nil
}

hotrungnhan avatar Jun 20 '25 20:06 hotrungnhan

This benchmark is open source, You are welcome to add changes (PRs)

joanhey avatar Jul 16 '25 16:07 joanhey