Subscribe

Microservices vs monolith: use cases and benefits for your next software architecture

Microservices or a monolith? A Go benchmark puts a number on the cost of one network hop (4 ns in process vs 28 µs over loopback), then the use cases where each one really fits, and why I start with a modular monolith.

A function call inside one Go process took 4.4 nanoseconds on my laptop. The same call over HTTP on localhost took 28 microseconds, about 6,400 times slower, and that is before a real network, TLS or a load balancer gets involved. Every arrow in a microservices diagram is one of those calls. In this blog we will look at what microservices and monoliths actually trade, where each one fits, and how to make the call for a new project.

My view, so you know where I stand: start with a well structured monolith, and split out a service only when you can name the reason. The rest of the post is why.

What we are comparing

A monolith is one deployable unit. One binary (or one container image), one release, usually one database. It can still be well organised inside, with packages per business area; I wrote about that in Monolith first.

Microservices split the system into separately deployed services, each owning its data and talking to the others over the network, with HTTP/JSON, gRPC or a message queue. Each service can be released, scaled and rewritten on its own.

The difference that matters is not code size. It is that calls between parts go over a network, and data lives in more than one database.

Putting a number on one hop

I wanted a real number instead of "network calls are slower", so I wrote a tiny pricing module with one interface and two implementations. Local is a map lookup. Remote calls the same code through an HTTP handler.

type Pricer interface {
    Price(sku string) (Quote, error)
}

// Local is the monolith version: a method call.
func (l *Local) Price(sku string) (Quote, error) {
    a, ok := l.table[sku]
    if !ok {
        return Quote{}, fmt.Errorf("unknown sku %q", sku)
    }
    return Quote{SKU: sku, Amount: a}, nil
}

// Remote is the microservice version: same interface, over the network.
func (r *Remote) Price(sku string) (Quote, error) {
    res, err := r.Client.Get(r.BaseURL + "/prices/" + sku)
    if err != nil {
        return Quote{}, err
    }
    defer res.Body.Close()
    if res.StatusCode != http.StatusOK {
        return Quote{}, fmt.Errorf("pricing service: %s", res.Status)
    }
    var q Quote
    err = json.NewDecoder(res.Body).Decode(&q)
    return q, err
}

The benchmarks use b.Loop() (Go 1.24 and newer) and an httptest server on loopback:

func BenchmarkHTTPLoopback(b *testing.B) {
    srv := httptest.NewServer(Handler(NewLocal()))
    defer srv.Close()
    var p Pricer = &Remote{BaseURL: srv.URL, Client: &http.Client{}}
    for b.Loop() {
        if _, err := p.Price("tea-500g"); err != nil {
            b.Fatal(err)
        }
    }
}
$ go test -run '^$' -bench . -benchmem
goos: darwin
goarch: arm64
pkg: example.com/callcost
cpu: Apple M5
BenchmarkLocal-10               255158836            4.412 ns/op           0 B/op          0 allocs/op
BenchmarkHTTPLoopback-10           40726         28195 ns/op        5710 B/op         72 allocs/op
PASS
ok      example.com/callcost    2.804s

28 µs is still fast in human terms. Nobody notices one of them. But this is the best case: same machine, keep-alive connection, no TLS, a 30 byte body. Across availability zones you'd add something in the range of a millisecond per round trip, and a page that fans out to six services in sequence feels it. It also allocated 72 times per call against zero, that shows up in GC time on a busy service.

Speed isn't even the main cost. The local call can't fail halfway. The remote one can time out after the other side already did the work, and now you need to think about retries and idempotency for something that used to be a function call.

What microservices really give you

The usual list is scalability, maintainability, fault isolation and freedom of language. Let me go through them with less marketing.

Independent deployment is real, and it's the main benefit. Team A ships pricing on Tuesday without waiting for team B's checkout change. With 50 engineers in one repo and one release train, that matters a lot. With 5 engineers it basically doesn't.

Independent scaling is weaker than it sounds. A monolith scales too: you run more copies behind a load balancer. Splitting only helps when parts have very different resource needs, like an image resizer that wants 8 GB RAM next to an API that wants 256 MB. For most CRUD apps it's the same CPU profile everywhere.

Fault isolation is possible, but you have to build it. If checkout calls pricing synchronously and pricing hangs, checkout hangs too, unless you have timeouts, retries with backoff and a circuit breaker on every call. Without those, a split system fails in more ways than the monolith did.

Then different languages per service. It sounds like freedom. In practice every extra language is another set of CI images, libraries, security patches and on call knowledge. I would allow it for a strong reason (a Python ML model behind a Go API, say) and not otherwise.

And the costs that such lists usually skip: no database transaction across services (you get sagas or an outbox table instead), you need distributed tracing to debug one request, API versioning between your own services, local development that needs ten containers, and a lot more infrastructure to pay for.

Two public write ups are worth reading here. Segment's "Goodbye Microservices" (2018) describes merging over 100 services back into one. And Amazon Prime Video's 2023 post on their audio/video monitoring tool moved from distributed serverless parts to a single process and reported cutting infrastructure cost by 90 percent. Neither says microservices are bad; both say the split was in the wrong place for their load.

What a monolith gives you

One deploy. One place to look at logs. A rename in Go is a compiler error everywhere it's used, instead of a 500 in some other service at 2 AM. A database transaction covers the whole business operation, so "create order and reserve stock" is just BEGIN and COMMIT.

The weak points are real also. A big monolith with no internal boundaries becomes a ball of mud where everything imports everything. Build and test times grow. One bad deploy takes everything down, and one memory leak in a rarely used report endpoint can kill the checkout path because it's the same process.

Most of those come from bad structure, and not from being one deployable. Clear package boundaries fix a lot of it, the compiler can even enforce them.

Use cases

Microservices fit when:

  • several teams need to release on their own schedule, and the service lines match team lines (Conway's law, more or less)
  • one part has very different load, hardware or security needs, like a payment component you want isolated for PCI scope
  • a part is clearly separate already, such as a video transcoder or a search indexer fed by events

A monolith fits when the team is small (I'd say under 15 to 20 engineers), the domain is still changing every month, or most operations touch several areas at once and need one transaction. That is most new products.

There's also the middle road: a modular monolith first, then pull out the one or two modules that earn it. The pricing example above is shaped for that. Remote and Local satisfy the same interface, so moving pricing out later changes one line where it's wired up, not every caller.

How I would decide

Ask three questions before splitting. Who will own the new service, by name? What can it do on its own that it can't do inside the monolith (deploy, scale, isolate)? And how does it get its data without reading the other service's tables?

If all three have a clear answer, split it. If the answer is "it will be cleaner", it won't be; it'll be the same code with a network in the middle. For the service to service calls themselves, I compared HTTP and gRPC in this gRPC post.

The benchmark code is in one file with its test, and go test -run '^$' -bench . runs it in about 3 seconds.