Mehmet Ergülcü

Java vs Go: does the gap widen when the database gets faster?

· 12 min

gojavaperformancevalkeypostgresqldocker

When I wrote the same API in Go and Spring Boot and measured it against PostgreSQL, the gap was small on most endpoints, and it was smallest wherever the work leaned on the database. So maybe the database was pinning both applications to the same speed.

The way to test that was obvious: make the database faster. I put an in-memory data store, Valkey, next to PostgreSQL and raced the same API against both. While I was at it I added three more things: logic endpoints that run in application code, tighter CPU limits, and a round with no warm-up.

Short answer: yes, the gap widened when the data store got faster. But the reason was not what I expected, it did not widen everywhere, and Valkey was not fast at everything. Details below.

The biggest difference: memory

The speed difference ranges from 1.0x to 4.6x depending on the work and the scenario. The memory difference is large in every scenario, and always in the same direction:

Go Spring Boot
Memory under load (1 CPU / 1 GiB) ~19 MiB ~660 MiB
Peak memory (0.5 CPU / 512 MiB limit) 105 MiB 438 MiB

Under load that is roughly a 35x difference.

This difference can go straight into the cloud bill. In the container world you largely pay for the memory and CPU you reserve: in Kubernetes the memory requests of your services decide how many copies fit on a machine, and on serverless platforms such as Cloud Run or AWS Lambda the price is computed directly from reserved memory and running time. Tens of times less memory per service means doing the same work on smaller or fewer machines. The gain grows where many small services run, or where services scale to zero and start often; the fast start-up (at 1 CPU, Go 0.67 s, Spring 6.6 s) adds to it.

In a real setup a safety margin and the CPU also come into play, so the saving is not simply 35x; still, it opens up a lot of room on the memory side. Spring ran with default JVM settings here; memory tuning can narrow part of the gap, which I did not try in this run.

What did I measure?

  • 14 endpoints (13 on Valkey, since /stats is PostgreSQL-specific). Among them: POST /base64, GET /factorial?n=1000, a per-store daily/monthly sales report (/stores/{id}/sales-report, computed in application code) and an all-stores report (/reports/sales, computed in the data store: a SQL GROUP BY on PostgreSQL, a Lua script on Valkey).
  • /health is application-only: it never touches the data store.
  • Two data stores: PostgreSQL 18 and Valkey 8, with the same 100,000-row seed data. Both get 8 CPU / 4 GiB.
  • Two limits: 1 CPU / 1 GiB and 0.5 CPU / 512 MiB per application. 4 scenarios in total.
  • Method: k6, 64 concurrent users, 180 s warm-up, 20 s per endpoint, alternating order, median of 3 rounds. In addition, every scenario has a cold round (fresh container, 60 s phases).
  • Measured: CPU time per request (not CPU %), data wait time and server-side total time.

Everything is repeatable with one command. The code is in the projects section. The whole run took about 6 hours.

Finding 1: when the data store got faster, Go opened up and Spring barely moved

At 1 CPU / 1 GiB, Go/Spring throughput ratio:

Endpoint PostgreSQL: Go Spring Ratio Valkey: Go Spring Ratio
GET /items/{id} 6,308 4,509 1.40x 8,022 4,158 1.93x
GET /items/search 5,780 4,114 1.40x 6,126 3,365 1.82x
POST /orders 2,648 2,271 1.17x 7,617 3,857 1.97x
POST /items 5,640 3,629 1.55x 7,544 3,637 2.07x
DELETE /items/{id} 6,396 3,415 1.87x 7,960 2,887 2.76x
GET /stores/{id}/sales-report 599 539 1.11x 807 582 1.39x

First, the opposite of what I expected: on these CRUD endpoints the database was never saturated. PostgreSQL used at most 175% of its 800% capacity. Both applications were pinned at their 1 CPU limit (94–97%). So the bottleneck was the application's own CPU in both cases. What changed was the CPU cost per request of talking to the data store:

Endpoint Go: PostgreSQL → Valkey Spring: PostgreSQL → Valkey
POST /items 0.177 → 0.133 ms 0.275 → 0.274 ms
POST /orders 0.379 → 0.132 ms 0.441 → 0.258 ms

On POST /items Spring did 3,629 req/s with PostgreSQL and 3,637 with Valkey: no change, because its CPU per request did not change either. Go's cost dropped by a quarter and throughput went from 5,640 to 7,544. On POST /orders (the transactional order) Go spends 2.9x less CPU per request, Spring 1.7x. I did not investigate why Go's PostgreSQL client costs so much more than its Valkey client; the measurement shows the difference, I do not have the explanation.

Caution: Valkey did not help every endpoint. GET /items (the paged list) fell from 3,931 to 3,004 on Go and from 3,552 to 1,928 on Spring. This is most likely my Valkey data model: the list takes IDs from a sorted set and then fetches the records one by one. A different model could look different. Do not conclude “Valkey speeds everything up”.

Finding 2: when you leave the computation to the data store, the language stops mattering

/stats is the clearest example, and there are two more heavy endpoints:

Endpoint Store Go Spring Ratio Median latency
GET /stats PostgreSQL 238 239 0.99x ~270 ms
GET /reports/sales PostgreSQL 31 29 1.08x ~2 s
GET /reports/sales Valkey 9 9 1.00x ~6.6 s

/reports/sales scans every store's sales records and produces daily and monthly totals. Because the computation runs in the data store, both applications spent 100% of the request time waiting. The application code only turned the result into JSON.

Valkey coming out about 3.4x slower than PostgreSQL here is interesting: a Lua script runs on a single thread in Valkey and no other command is processed meanwhile. PostgreSQL can spread the same query over several cores. “In-memory” does not mean “everything is fast”; Valkey is great for many small commands, not for one heavy computation.

Finding 3: logic work: Go is ahead again, far ahead on factorial

Endpoints that never touch the data store (1 CPU, PostgreSQL scenario):

Endpoint Go (rps) Spring (rps) Ratio Go (CPU ms/request) Spring (CPU ms/request)
GET /health 13,664 9,258 1.48x 0.073 0.108
POST /base64 8,601 7,062 1.22x 0.117 0.141
GET /cpu 466 371 1.26x 2.153 2.706
GET /factorial?n=1000 5,039 1,210 4.16x 0.199 0.825

Apart from factorial the difference is around 1.2–1.5x. On factorial, Go spends a quarter of the CPU per request. I did not profile why; the difference between the two languages' big-number implementations (big.Int / BigInteger) and the JVM's behavior under a CPU quota come to mind, but those are guesses.

Looking at CPU percentage is useless here, because both applications fill their limit under load (94–97%). The real difference shows in “CPU time per request”. I measured that separately in this report.

A note: average response time tells the same story but adds nothing. With 64 users sending requests back to back, average latency is roughly 64 / throughput anyway (on /cpu: Go 137 ms, Spring 171 ms). If throughput differs by 1.26x, the average differs by 1.26x.

Finding 4: halving the CPU hurt Spring much more

The same ratios, with 0.5 CPU / 512 MiB per application instead of 1 CPU (PostgreSQL):

Endpoint 1 CPU: Go/Spring 0.5 CPU: Go/Spring
GET /health 1.48x 3.88x
GET /items/{id} 1.40x 2.26x
POST /orders 1.17x 1.71x
POST /items 1.55x 1.86x
DELETE /items/{id} 1.87x 2.76x
GET /factorial 4.16x 4.62x

On light work the gap grew two- to threefold. On /health Go did 7,313 req/s while Spring stayed at 1,886. Spring's round-to-round variation also grew (/health between 1,555 and 2,665).

I did not measure the cause; the most plausible explanation is that the JIT compiler and the garbage collector share the same half core with the application. Go has no JIT.

The other numbers point the same way:

Go Spring
Cold start, 1 CPU (PostgreSQL) 0.67 s 6.6 s
Cold start, 0.5 CPU (PostgreSQL) 0.64 s 12.6 s
Cold start, 0.5 CPU (Valkey) 0.80 s 15.2 s
Memory under load, 1 CPU ~19 MiB ~660 MiB
Peak memory, 512 MiB limit 105 MiB 438 MiB

Spring fit into 512 MiB, but using 85% of it. A bit more data or more connections would have pushed the limit. I did not test that.

Finding 5: the cold first minute

In normal rounds, a 180 s warm-up load prepares the JVM. A service that has just started in real life has no such luxury. In the extra round the container starts fresh and the phases are measured directly, with no warm-up.

Ratio of Spring's cold throughput to its warm throughput:

Scenario /health /items /factorial
PostgreSQL, 1 CPU 0.52x 0.73x 0.82x
PostgreSQL, 0.5 CPU 0.73x 0.71x 0.86x
Valkey, 1 CPU 0.38x 0.61x 0.84x
Valkey, 0.5 CPU 0.30x 0.44x 0.17x

Because the phases run in sequence, /health and /items are measured at their coldest, the later ones after the JVM has warmed up a bit. So the first rows show the warm-up effect most cleanly.

The most extreme example: in the Valkey 0.5 CPU scenario, /factorial without warm-up gave Spring 97 req/s (Go 2,768). This is a single measurement, so I will not say “Spring is 28x slower in the first minute”; what I mean is that on half a core the JVM can struggle badly during its warm-up period.

Go was unaffected by the missing warm-up in the PostgreSQL scenarios (0.95–1.05x). In the Valkey 1 CPU scenario Go also dropped 10–30% when cold, and in Valkey 0.5 CPU some rows rose to 1.1–1.2x. Since each is a single measurement, I cannot tell what caused these swings.

(I did not interpret the DELETE and /reports/sales rows in this section: in the cold round the number of rows to delete grows with the phase length, so those two rows do not show warm-up cleanly.)

Build time

Go Spring Boot
Clean build (including dependencies) 24 s 128 s
Incremental build 20 s 7 s

Go wins the clean build, Spring wins the incremental one.

So which one?

Three questions, three answers:

  • If the data store is the bottleneck (heavy query, heavy report) the language does not matter. Fix the query, the index, the data model first. The proof is /stats and /reports/sales.
  • If the data store is fast (small reads/writes, an in-memory store, good indexes) the application's CPU becomes the bottleneck and Go does 1.4 to 2.8 times the work of Spring. If you want more work out of the same CPU, the difference is real.
  • If CPU and memory are tight (0.5 CPU, a small container, a scale-to-zero service) Spring's cost grows: slow startup, poor performance during warm-up, a memory share.

There are still cases where I would pick Spring Boot: if the team knows Java, if you will use the ecosystem (security, data access, observability), if application CPU is not the bottleneck anyway. What this measurement does not say is that Spring is “slow”. What it says is that it has a cost under certain conditions, and there is value in knowing those conditions beforehand.

How to read these results

  • One machine. Docker Desktop sees 16 CPU / 7.4 GiB. k6, the data store and the application share cores on the same machine. The numbers may shift on yours; the ratios and the method are what matter.
  • fsync is off on PostgreSQL and persistence is off on Valkey. The aim is to measure the application, not the store. Do not use these settings in production.
  • My Valkey model. The data structures, the Lua scripts and the client choice (go-redis in Go, Jedis in Java, pool of 20 in both) are one design. Another design could change the result.
  • Closed loop. 64 users send the next request as soon as one finishes. Real traffic arrives in waves; average latency and saturation can behave differently.
  • Spring's measured time is incomplete. The server-side timing starts when the request enters the handler; queueing and CPU throttling before the handler are not counted.
  • Untuned. Both applications run with near-default settings. Things I did not try: virtual threads, Tomcat thread count, tuning the JVM to the CPU quota, GraalVM native image for Spring; GOMEMLIMIT and GOGC for Go.
  • The cold round is a single measurement, the others are medians of 3 rounds. The machine's power state (performance mode) is my own statement.

Run it yourself

# quick trial (a few tens of minutes)
docker compose run --rm runner --quick --stores postgres,valkey

# the full run from this post (about 6 hours)
docker compose run --rm runner --stores postgres,valkey --limits 1:1g,0.5:512m

The results are collected in a single results/index.html. If yours come out different, I would like to know.

© 2026 Mehmet Ergülcü SvelteKit · Tailwind · <1 KB JS