Recently, I heard a lot about Gin, a lightweight and fast web framework in Go, so I wanted to experiment with it and compare it to FastAPI, a popular Python framework for building APIs. I was curious about how they stack up in terms of speed and ease of use.
What I Did
I built a simple REST API with both frameworks. The API just serves a list of music albums via a /albums endpoint.
FastAPI is known for its developer-friendly features and async support.
Gin is praised for its minimalism and blazing speed.
Code Snippets
Here’s a quick look at the endpoints I made:
FastAPI (Python):
`from fastapi import FastAPI
app = FastAPI()
albums = [
{„id“: 1, „title“: „Blue Train“, „artist“: „John Coltrane“},
{„id“: 2, „title“: „Jeru“, „artist“: „Gerry Mulligan“},
{„id“: 3, „title“: „Sarah Vaughan“, „artist“: „Sarah Vaughan“},
]
@app.get(„/albums“)
async def get_albums():
return albums`
Gin (Go):
`package main
import (
„github.com/gin-gonic/gin“
„net/http“
)
type Album struct {
ID int json:"id"
Title string json:"title"
Artist string json:"artist"
}
func main() {
router := gin.Default()
albums := []Album{
{ID: 1, Title: "Blue Train", Artist: "John Coltrane"},
{ID: 2, Title: "Jeru", Artist: "Gerry Mulligan"},
{ID: 3, Title: "Sarah Vaughan", Artist: "Sarah Vaughan"},
}
router.GET("/albums", func(c *gin.Context) {
c.IndentedJSON(http.StatusOK, albums)
})
router.Run("0.0.0.0:8080")
}`
The Benchmark
- Using wrk with 4 threads, 100 connections for 15 seconds, I tested both APIs.
- FastAPI handled about 959 requests/sec with average latency around 104 ms.
- Gin handled roughly 2700 requests/sec with latency as low as 6.7 ms.
What I Learned
- Gin is much faster — thanks to Go’s compiled nature and lightweight concurrency model.
- FastAPI offers ease of development and async features, making it great for many projects.
- If raw speed and low latency are critical, Gin is the winner.
- If rapid development and Python ecosystem matter more, FastAPI is a fantastic choice.
Conclusion
This experiment showed me the strengths of both frameworks in a hands-on way. Gin really impressed me with its performance, while FastAPI remains an excellent option for Python developers looking for quick and clean API development.
If you’re choosing between the two, consider your project needs — speed vs. development speed — and pick accordingly.