Skip to content
R.
← All writing
9 min read

I removed Gin and GORM from my Go API. Only one of them made it faster.

Replacing Gin with net/http and GORM with sqlc in LabFlow, one change at a time, measured with the same k6 load: a 24% smaller binary from dropping Gin, faster requests from dropping GORM, and a bug a struct tag had been hiding.

GoPostgreSQLsqlcPerformanceArchitecture

In the last post I load tested LabFlow, my Go rewrite of a lab management system, and fixed the three problems that made it fail 79% of requests. That version used Gin for HTTP and GORM for the database, because that’s what I reached for first.

Then I read Efron Licht’s “Gin is a very bad software library”. His argument is that Gin brings a huge dependency tree and a 133-method context for things the standard library already does. I agreed with it in theory. So I removed Gin and GORM from LabFlow, one at a time, and measured what each one actually cost.

Short version: removing Gin made the binary a quarter smaller and made no difference to speed. Removing GORM made the API faster, because it changed which SQL runs. And the standard-library version is more code, not less.

How I measured

I built three versions of the same API, each one change apart:

  • Gin + GORM, the version from the last post.
  • net/http + GORM: Gin replaced, database code unchanged.
  • net/http + sqlc: GORM replaced with sqlc and pgx, handlers unchanged.

A bench script builds each version from its git commit and runs the same k6 test against each one: dashboard reads, new test requests and logins at the same time. Each run gets a freshly seeded database and the same limit of 2 CPUs and 512 MB. The versions take turns round by round, so if the laptop slows down, all three are affected equally.

Two things I learned about benchmarking on a laptop on the way:

  • The first rounds are always slow. In my first attempt, the same two builds were hundreds of milliseconds apart in one round and 8 ms apart in another. Now there’s a warm-up round that isn’t recorded, and the load is below the point where the container saturates.
  • Say what you threw away. In the final run, one of the four recorded rounds hit a slow period on the machine. All three builds fell short of the target load and had p95s of 4 to 6 seconds. I excluded that round for every build. The raw data and the reason are in the repo.

Before trusting any numbers, I also checked that the versions do the same work. A parity script runs the old and new build side by side on one database and sends them the same 21 requests. Every read must return identical JSON. That check found a real bug, which I’ll get to.

The results

220 requests per second, median of three rounds, 0% errors for all three:

Gin + GORMnet/http + GORMnet/http + sqlc
Browse p504.3 ms4.4 ms2.9 ms
Browse p9536.4 ms34.4 ms29.1 ms
Submit p9550.5 ms54.2 ms47.2 ms
API CPU, same load108%109%100%
Third-party Go packages321252239
Binary size45.5 MB34.7 MB33.1 MB

The middle column is the interesting one. Taking out Gin removed 69 packages and 11 MB, and latency and CPU didn’t move.

Removing Gin: smaller, not faster

Since Go 1.22 the standard router matches methods and path parameters, so routing was the easy part:

// Gin
reservations := apiGroup.Group("/reservations")
reservations.GET("/:xid", middleware.RBACMiddleware(access, "read_reservation"), h.GetReservationByXid)

// net/http
requires("read_reservation", "GET /api/reservations/{xid}", h.GetReservationByXid)
// requires = Chain(handler, jwt, apiLimiter, RBACMiddleware(access, perm))

The rest of what the handlers used from Gin fit in a 150-line httpx package: writing JSON, chaining middleware, reading the user ID from the request context, query defaults, the client IP, and a response writer that records the status code for metrics. I also rewrote CORS, the access log and panic recovery to behave like the Gin versions, so the comparison stays fair.

To see why speed didn’t change, I benchmarked just the HTTP layer, with no database: the same middleware and the same 1 KB reservation JSON through both stacks.

ResponseGinnet/http
1 KB reservation JSON6.7 µs7.1 µs (±10%)
11-byte JSON2.6 µs2.2 µs (±9%)

The difference is within noise either way. Each request costs a few microseconds in the router and about a millisecond in the database, so the router could be twice as fast and the user would never notice. If you want Go’s standard library for speed, this isn’t the reason.

The real gains were elsewhere:

  • A security bug. Gin’s ClientIP() trusts X-Forwarded-For from any client by default. My rate limiter used it as the key, so anyone could send a fake header and get a fresh rate limit. The new code only trusts that header from the reverse proxy’s address.
  • Types instead of any. In Gin the user ID was c.Get("id"), so the rate limiter had a type switch for int, int64, float64 and string. Now it’s a typed context value, and that code is gone.
  • Control. Every line between the socket and my handler is now code I can read. Gin brought in 69 packages I didn’t choose, including a QUIC library, a YAML parser and a MessagePack codec that this API never used.

Removing GORM: faster, because of the SQL

With sqlc you write the SQL yourself, and it generates typed Go functions from it, checked against the schema at build time. The permission lookup from the last post went from this:

db.Preload("Roles.Accesses").First(&user, userID)

to this:

-- name: ListUserPermissions :many
SELECT r.role_id, r.role_name, a.access_id, a.access_name
FROM user_roles ur
JOIN roles r ON r.role_id = ur.role_id
LEFT JOIN role_accesses ra ON ra.role_id = r.role_id
LEFT JOIN accesses a ON a.access_id = ra.access_id
WHERE ur.user_id = @user_id
ORDER BY r.role_id, a.access_id;

I counted the statements per request with Postgres statement logging:

RequestGORMsqlc
Current user + permissions52
Reservation list53
One reservation52
Test catalog21
Create reservation44

Each Preload is a separate query, so one line of GORM was five round trips to the database. That also corrects my last post: I wrote that the permission check cost three queries. I hadn’t counted. It was five.

Fewer round trips explain most of the gain: the median dashboard request dropped from 4.3 to 2.9 ms. CPU use dropped by about 7% at the same load, because GORM’s reflection-based scanning and query building are gone.

The bug the parity check found

The first parity run showed one difference. Reservations created by the GORM build had feedback_required: true. The sqlc build created them with false.

The column’s database default is FALSE. But the GORM model had a struct tag that said default:true, and GORM writes literal tag defaults into every INSERT. So the database default was ignored, and nobody knew, because the only thing that said true was a struct tag.

This is the strongest argument for sqlc I found. With GORM, the SQL that runs depends on struct tags, naming conventions and defaults that aren’t in the query. With sqlc, the SQL in the repo is the SQL that runs. I kept the old behavior and wrote it down:

-- feedback_required is TRUE although the column default is FALSE: the GORM
-- model's default:true tag was written into every INSERT, so existing data
-- and the app expect TRUE.

What it cost

The standard-library version isn’t smaller to maintain:

  • More code. Hand-written Go grew from 5,882 to 6,289 lines. On top of that come 269 lines of SQL and about 2,000 generated lines, though those are written for you.
  • Mapping. sqlc returns its own row structs, so there’s a file that copies rows into the domain models, field by field.
  • Dynamic queries are awkward. sqlc can’t take a column name as a parameter, so sorting the reservation list by a user-chosen column became a CASE expression for each allowed column. GORM did that in one line.
  • Things you now do yourself. GORM set created_at and updated_at, wrapped every write in a transaction, and turned nil fields into NULL on update. I had to find each of those and decide whether to keep it.

Because the rewrite touched every query, I also added integration tests that run each repository method against a real Postgres in CI. The old code had none, because GORM made it feel safe.

Would I do it again?

  • GORM → sqlc: yes. It was faster, and I can now read every query the API runs, and code review covers the SQL too.
  • Gin → net/http: yes, but not for speed. Do it for fewer dependencies, a smaller binary and code you can read end to end. If your team already knows Gin and you don’t care about those, it isn’t slowing you down.
  • Measure one change at a time. If I had swapped both at once, I would have credited the router for a gain that came from the queries.

The code, both pull requests, the benchmark runs and the parity script are in the LabFlow repository.

I’m a backend-leaning fullstack engineer (Go, TypeScript, Postgres), open to remote roles.

If this is the kind of problem your team works on, get in touch.