Step Over to Go Part 54 of 54
  1. 1 Goodbye .csproj, Hello go.mod
  2. 2 The Phrasebook: C# Concepts in Go Terms
  3. 3 You Know .NET, Now Meet the Go Toolchain
  4. 4 No Enums? No Problem (Sort Of)
  5. 5 Generics: Late to the Party
  6. 6 The Million Dollar Mistake, Differently
  7. 7 Your Class Is Now a Struct (And That's Fine)
  8. 8 Where Did My Properties Go?
  9. 9 if err != nil: Your New Reality
  10. 10 Panic and Recover: The Emergency Exit
  11. 11 The switch Statement You Always Wanted
  12. 12 Stack vs Heap: Go's Escape Analysis
  13. 13 Pointers Without the Pain
  14. 14 Slices Are Not Arrays (And Neither Is List<T>)
  15. 15 Channels: The sync You Didn't Know You Wanted
  16. 16 Context Is King
  17. 17 Async/Await vs Goroutines: A Mindset Shift
  18. 18 select: Multiplexing Like a Pro
  19. 19 Mutexes and WaitGroups: When Channels Aren't the Answer
  20. 20 The Empty Interface and Type Assertions
  21. 21 Composition Over Inheritance (For Real This Time)
  22. 22 Implicit Interfaces: Scary Then Brilliant
  23. 23 Benchmarks and Profiling Out of the Box
  24. 24 Mocking in a Language Without Mockito
  25. 25 Multi-Module Repos: Monorepo Thinking
  26. 26 Organising a Real Project
  27. 27 Testing Without a Framework
  28. 28 Configuration Without IOptions<T>
  29. 29 Database Access: database/sql vs Entity Framework
  30. 30 Debugging: Delve vs Visual Studio's Comfort
  31. 31 Generate All the Things
  32. 32 GORM and Friends: When You Do Want an ORM
  33. 33 HTTP Services: net/http vs ASP.NET Core
  34. 34 JSON: Struct Tags and the Marshal Dance
  35. 35 Linting and Formatting: gofmt Is Non-Negotiable
  36. 36 Logging: slog and the Structured Logging Story
  37. 37 Shrinking Binaries and Build Tags
  38. 38 Dockerfile for Go: Simpler Than You'd Think
  39. 39 GitHub Actions for Go Projects
  40. 40 Health Checks and Readiness Probes
  41. 41 Kubernetes and Go: A Natural Fit
  42. 42 AWS Lambda and Go: Cold Starts That Don't Hurt
  43. 43 Metrics, Traces, and Logs: The OpenTelemetry Way
  44. 44 Single Binary Deploys: The Killer Feature
  45. 45 Versioning Your Modules
  46. 46 What I Actually Miss From C#
  47. 47 What I'll Never Go Back For
  48. 48 Six Months In: Was It Worth It?
  49. 49 Forty-Eight Posts on Moving From C# to Go, in the Right Order
  50. 50 Range-Over-Func Iterators for People Who Miss LINQ
  51. 51 What I Got Wrong About Go
  52. 52 Attribute Routing Comes to Go's ServeMux, in Struct Tags
  53. 53 How a .NET Solution Structure Maps to a Go Project Layout
  54. 54 One Service, Two Runtimes, Measured
About this series

One Service, Two Runtimes, Measured

One Service, Two Runtimes, Measured

This series has said “Go deploys smaller and starts faster” in a dozen different ways, and the containers post put rough sizes on it from memory. It has never put the two runtimes on the same box, with the same limits, doing the same work, and written down what came out. This is that post. There is no winner in the title because on the measure most people argue about, requests per second, there is not one.

The whole thing is one repo, one-service-two-runtimes, and ./bench/bench.sh all reproduces every number here on any machine with Docker. The file under each chart is the output of that script.

The service

An orders API with three routes and no database. It is small enough to read in full and large enough to exercise the things a real service spends its time on: routing, JSON in and out, validation, a lock around shared state.

Route Does
GET /healthz 200 ok
POST /orders Validates {customer, lines:[{sku, qty, unit_pence}]}, totals it, stores it, returns 201 with the order
GET /orders/{id} 200 with the order, or 404 {"error":"not found"}

The store keeps the latest ten thousand orders and evicts the oldest, so a thirty second run measures the runtime rather than an ever-growing heap.

The Go version is net/http with the method-and-pattern routing from Go 1.22, encoding/json, and a mutex. The create handler is the whole shape of it:

mux.HandleFunc("POST /orders", func(w http.ResponseWriter, r *http.Request) {
    var n NewOrder
    if err := json.NewDecoder(http.MaxBytesReader(w, r.Body, 1<<20)).Decode(&n); err != nil {
        writeError(w, http.StatusBadRequest, "invalid json")
        return
    }
    if err := validate(&n); err != nil {
        writeError(w, http.StatusBadRequest, err.Error())
        return
    }
    o := &Order{ID: newID(), Customer: n.Customer, Lines: n.Lines, CreatedAt: time.Now().UTC()}
    for _, l := range n.Lines {
        o.TotalPence += l.Qty * l.UnitPence
    }
    store.Put(o)
    writeJSON(w, http.StatusCreated, o)
})

The C# version is a minimal API on CreateSlimBuilder with a source-generated JsonSerializerContext, which is the shape Microsoft's own templates use for Native AOT and which works just as well on the JIT:

app.MapPost("/orders", (NewOrder n) =>
{
    if (Validate(n) is { } err)
        return Results.Json(new Error(err), AppJsonContext.Default.Error, statusCode: 400);
    var total = 0;
    foreach (var l in n.Lines!) total += l.Qty * l.UnitPence;
    var o = new Order(NewId(), n.Customer!, n.Lines, total, DateTime.UtcNow);
    store.Put(o);
    return Results.Json(o, AppJsonContext.Default.Order, statusCode: 201);
});

One C# file produces two of the three builds. dotnet publish gives the framework-dependent build that runs on the JIT in the aspnet:10.0 image, the way most .NET services ship today. dotnet publish -p:PublishAot=true gives a native executable in the runtime-deps:10.0 image, with no runtime to start. The Go build is a static binary in a scratch image. The C# file is 71 non-blank lines and the Go file is 118, mostly because Go spells out the store and the error helpers that ASP.NET Core provides.

How it was measured

Everything runs under Docker Compose, which is also how the hot path clients are measured. Each service gets the same box: a container limited to two CPUs and 512 MB. The load generator is a small Go program in its own container on the same Compose network, so the host's port forwarding is not in the measured path. It opens 64 connections and each one loops over create, read back, and read a missing order, for thirty seconds, after a ten second warm-up. That is run three times per target and the middle run by request rate is the one reported, with the range under the chart.

The service's CPU is sampled every second during the load runs, and all three sit at the 200% limit for the whole run. That is the check that the load generator was not the thing being measured. The first version of this harness used k6, which needed nine CPUs and four gigabytes to drive the same load and left the Go service at 174%, and the three runtimes looked identical because the client was the bottleneck.

Cold start is measured from the host by a probe that stops and starts the container twenty times. For each start it records the time from docker start returning to the first 200 from /healthz, polled every millisecond, and then the time taken by the first real POST /orders after that, which is where a JIT pays for compiling the JSON path. Docker's own docker start takes about 230 ms on this machine for all three images and is reported separately so it can be ignored.

Memory is what docker stats reports for the container, which is the cgroup's view, read once a second after the cold starts with no load, and once at the end of the load runs. Image sizes are from docker image inspect (compressed, what a pull transfers) and docker image ls (unpacked, what sits on the node). Build time is one clean docker build --no-cache per target with the base images already pulled.

The box is a MacBook Pro with an Apple M4 Max, so the Linux VM under Docker Desktop is arm64 with sixteen virtual CPUs and eight gigabytes. Go is 1.27.3, the .NET SDK is 10.0.403, and both come from the official images. It is a laptop, the connections are plain HTTP without TLS, and there is no database. The numbers say which things are the same order of magnitude and which are not, and they are not a capacity plan for your service.

Throughput and latency

Requests per second, 64 connections, 2 CPUsThree horizontal bars, one per build of the service, showing sustained requests per second over thirty seconds.Requests per second30 s, 64 connections, after a 10 s warm-upGo 1.27Go 1.27: 155,230155,230.NET 10, JIT.NET 10, JIT: 157,996157,996.NET 10, Native AOT.NET 10, Native AOT: 159,846159,846
Requests per second over 30 s at 64 connections, the middle of three runs. The three runs ranged from 154,224 to 155,281 for Go, 156,373 to 166,866 for the JIT and 159,771 to 160,438 for AOT.Download the data (CSV)
Request latency under loadTwo panels of three bars: the median and the 99th percentile request time in milliseconds for each build.MedianGo 1.27Go 1.27: 0.3 ms0.3 ms.NET 10, JIT.NET 10, JIT: 0.3 ms0.3 ms.NET 10, Native AOT.NET 10, Native AOT: 0.3 ms0.3 ms99th percentileGo 1.27Go 1.27: 1.9 ms1.9 ms.NET 10, JIT.NET 10, JIT: 3.0 ms3.0 ms.NET 10, Native AOT.NET 10, Native AOT: 2.5 ms2.5 ms
Median and 99th percentile request time from the load generator's side, same run.Download the data (CSV)

With two CPUs pinned, all three builds serve between 155 and 160 thousand requests a second, each request being a JSON parse, a lock, a map operation and a JSON encode. The gap between them is three percent, which is inside the run-to-run range of the JIT build on its own, so I am not going to rank them. The medians are within four hundredths of a millisecond of each other, with .NET slightly lower. The one difference that held across all three runs is the tail: Go's 99th percentile stayed under two milliseconds every time, and both .NET builds sat between two and a half and three. The gap is a millisecond, and nobody will see it behind a database call.

The series has been careful never to claim Go is faster at serving requests, and this is the measurement behind that care. Kestrel and net/http both saturate two cores with the same work done per core, give or take the noise of a laptop. The runtime is not where this service's time goes, and the same would be true of yours the moment it talked to a database.

Memory

Container memory, idle and after loadTwo panels of three bars: resident memory of the container just after start, and after thirty seconds of load.Idle, just after startGo 1.27Go 1.27: 2.1 MB2.1 MB.NET 10, JIT.NET 10, JIT: 30 MB30 MB.NET 10, Native AOT.NET 10, Native AOT: 16 MB16 MBAfter 30 s of loadGo 1.27Go 1.27: 18 MB18 MB.NET 10, JIT.NET 10, JIT: 83 MB83 MB.NET 10, Native AOT.NET 10, Native AOT: 59 MB59 MB
Container memory from docker stats: a second after start with no load, and at the end of the load runs.Download the data (CSV)

Here the runtimes separate. The Go process idles at a few megabytes and grows to under twenty under load. The JIT build idles at thirty and grows to over eighty. Native AOT halves the idle figure and takes a chunk off the loaded one, but it is still several times Go, because the GC and the thread pool are the same runtime underneath.

The web SDK publishes with System.GC.Server set to true in the runtime config, which you can read out of the image with cat /app/service.runtimeconfig.json, and the server GC trades memory for throughput by collecting less often and keeping more heap committed. Since .NET 9 it starts with one heap and adds more as the allocation rate demands, which is why the loaded figure is a lot larger than the idle one. Setting DOTNET_gcServer=0 is the usual answer when memory matters more than the last few percent of throughput. I ran everything at the defaults, because that is what you get when you follow the template, and the point of the post is the defaults.

In practice this is the number that sets how many replicas fit on a node. Four or five Go replicas of this service fit in the memory of one JIT replica, and three fit in one AOT replica. Whether that matters depends entirely on whether you are paying per node or per team.

Cold start

Cold start, median of 20Two panels of three bars: milliseconds from container start to the first healthy response, and the time taken by the first real request after that.Start to first healthy replyGo 1.27Go 1.27: 1.6 ms1.6 ms.NET 10, JIT.NET 10, JIT: 52 ms52 ms.NET 10, Native AOT.NET 10, Native AOT: 2.7 ms2.7 msFirst POST /orders after thatGo 1.27Go 1.27: 1.0 ms1.0 ms.NET 10, JIT.NET 10, JIT: 12 ms12 ms.NET 10, Native AOT.NET 10, Native AOT: 2.4 ms2.4 ms
Median of 20 cold starts: from docker start returning to the first healthy reply, then the first real request after that.Download the data (CSV)

Go answers its health check under two milliseconds after the container starts, and Native AOT is a millisecond behind it. The JIT build takes around fifty milliseconds to be healthy and then another twelve on the first real request, which is Kestrel's pipeline, the routing, and the JSON path being compiled on the way through. After that first request the JIT build is as fast as the others, which is what the throughput chart shows.

Fifty milliseconds once per process start is nothing for a service that runs for days. It is something for a scale-to-zero platform that starts a process per request burst, for a test suite that starts the service hundreds of times, and for Lambda, where the C# hot path client found the same thing: AOT closes the gap, and the JIT does not.

What ships, and how long it takes to build

What ships and how long it takes to buildThree panels of three bars: compressed image size, unpacked image size, and clean build time.Image, compressedwhat crosses the wireGo 1.27Go 1.27: 2.5 MB2.5 MB.NET 10, JIT.NET 10, JIT: 88 MB88 MB.NET 10, Native AOT.NET 10, Native AOT: 48 MB48 MBImage, unpackedwhat sits on the nodeGo 1.27Go 1.27: 9.0 MB9.0 MB.NET 10, JIT.NET 10, JIT: 369 MB369 MB.NET 10, Native AOT.NET 10, Native AOT: 217 MB217 MBClean buildno cache, base images presentGo 1.27Go 1.27: 4 s4 s.NET 10, JIT.NET 10, JIT: 3 s3 s.NET 10, Native AOT.NET 10, Native AOT: 47 s47 s
Compressed image, unpacked image, and one clean build with the base images already present.Download the data (CSV)

The Go image is a binary and nothing else: under three megabytes compressed, nine on disk. The JIT image carries the ASP.NET Core shared framework and a Debian userland, and the application itself is a hundred kilobytes of IL inside it. The AOT image drops the framework but keeps the Debian base for libc and OpenSSL, and the binary is just under ten megabytes. Microsoft's chiseled Ubuntu variants of both base images cut that down a long way: runtime-deps:10.0-noble-chiseled is 4.6 MB compressed against 44 for the Debian default, which would put the AOT image around 15 MB over the wire, and the chiseled aspnet base is 50 MB against 89. The default tags are what the documentation reaches for first and they are what I measured, with the chiseled sizes in the repo's README for anyone who wants the other row.

Build time is the cost of AOT that the size and cold start charts do not show. The Go and JIT builds are a few seconds each. The AOT build is three quarters of a minute on this machine, every time, because ILCompiler compiles the whole framework's reachable surface to native code for each publish. On a CI runner with fewer cores it is longer. That is a cost paid on every commit, and it is the reason the AOT row is not simply the best of both.

What I take from it

The series has argued that the reason to move a service from .NET to Go is the deployment story, and the numbers put it at thirty-five times smaller over the wire, four to five times less memory under load, and a cold start measured in single milliseconds rather than tens. They also put throughput and latency at a draw, which is the claim I most wanted a number against.

Native AOT gets .NET most of the way on cold start and image size and some of the way on memory, for the price of most of a minute per build, source-generated JSON, and the set of libraries that have done the work to be trimmable. If you have a .NET team and a cold start problem, that is the move, and it is a much smaller one than a rewrite.

If you are choosing from nothing, the Go column is the one with no asterisks on it. That has been the series' position since the single binary post, and it is a relief to have it be a measurement rather than a feeling.

Both source files, the Dockerfiles, the load generator, the cold start probe and the script that ties them together are in the repo. Run it on an x86 box and send me the CSV.