Building Microservices with Go

A microservice is not a small monolith; it is a decision about where the seams between pieces of a system should run. The rest - the transport, the deployment, the discovery - is comparatively easy once the boundaries are right, and very hard to fix afterwards. This page works through that decision first, and then builds a working service on top of it with Go.

1. Where to Draw the Boundary

The most common mistake is to split along the technical layers: one service for the API, one for the business logic, one for the database. That produces a distributed monolith - every request crosses three network hops and every release needs all three, so you have all the operational cost of microservices and none of the benefit.

Split along business capabilities instead. Ask which part of the domain changes for which reason.

Good boundaryBad boundary
Order service owns orders, order lines, and order statusDatabase service owns “the orders table”
Notification service owns email, SMS, and push deliveryService layer owns “business logic”
Catalog service owns products, pricing, and stockAPI service owns “request handling”

Two rules keep the boundaries honest:

  1. Each service owns its data. No service reads another service’s tables. If service A needs data from service B, it calls B’s API. A shared database is a distributed monolith in waiting.
  2. A service is changed by one team. If two teams always have to release together, they are one service, whatever the code says.

Data consistency across services

Once each service owns its data, you cannot use a distributed transaction for every operation. The usual pattern is the saga: each step is a local transaction, and a failure triggers compensating actions for the steps that already succeeded.

type Saga struct {
    steps    []Step
    done     []Compensation
}

type Step struct {
    Run         func(ctx context.Context) error
    Compensate func(ctx context.Context) error
}

func (s *Saga) Execute(ctx context.Context) error {
    for _, step := range s.steps {
        if err := step.Run(ctx); err != nil {
            // roll back what already happened, newest first
            for i := len(s.done) - 1; i >= 0; i-- {
                _ = s.done[i](ctx)
            }
            return err
        }
        s.done = append(s.done, step.Compensate)
    }
    return nil
}

Compensations cannot undo an already-visible effect, so a saga is only as good as its compensating actions. Order a car, then fail to charge the card: the compensation is to release the car, not to pretend the order never happened.

2. Transport: HTTP and gRPC

HTTP with JSON is the pragmatic default. It is debuggable with curl, every language can call it, and it fits request/response flows. Use it for public APIs and for endpoints with a handful of callers.

gRPC with protobuf pays off when the contract is stable, the payload is large or the call is frequent, and both sides are under your control. It gives you a strict schema, generated client and server code, and streaming that HTTP/JSON does not have.

A protobuf contract looks like this:

syntax = "proto3";

package orders;

message CreateOrderRequest {
  string customer_id = 1;
  repeated OrderLine lines = 2;
}

message OrderLine {
  string sku = 1;
  int32 quantity = 2;
}

message Order {
  string id = 1;
  string status = 2;
  int64 total_cents = 3;
}

service OrderService {
  rpc CreateOrder(CreateOrderRequest) returns (Order);
  rpc WatchOrders(google.protobuf.Empty) returns (stream Order);
}

The Go server is a small amount of glue:

type orderServer struct {
    pb.UnimplementedOrderServiceServer
    orders *OrderStore
}

func (s *orderServer) CreateOrder(ctx context.Context, req *pb.CreateOrderRequest) (*pb.Order, error) {
    if len(req.Lines) == 0 {
        return nil, status.Error(codes.InvalidArgument, "an order needs at least one line")
    }
    order, err := s.orders.Create(ctx, req.CustomerId, req.Lines)
    if err != nil {
        return nil, status.Errorf(codes.Internal, "create order: %v", err)
    }
    return order, nil
}

Two rules keep a gRPC service healthy: always wrap errors in status.Error so the client sees a code rather than a string, and set deadlines on every call so a slow dependency cannot pin a goroutine forever.

ctx, cancel := context.WithTimeout(ctx, 2*time.Second)
defer cancel()
resp, err := client.Get(ctx, &pb.GetRequest{Id: id})

3. Service Discovery

In a containerised system the address of a service changes every time it is redeployed, so something has to keep a map from name to address. Consul, etcd, and Kubernetes DNS are the usual answers; the pattern is the same.

type Resolver struct {
    mu      sync.RWMutex
    addrs   map[string][]string
    current string
}

func (r *Resolver) Next(service string) (string, error) {
    r.mu.RLock()
    defer r.mu.RUnlock()

    addrs := r.addrs[service]
    if len(addrs) == 0 {
        return "", fmt.Errorf("no instances for %s", service)
    }
    addr := addrs[r.current%len(addrs)]
    r.current++
    return addr, nil
}

Round-robin over a snapshot of the instance list is enough for a small fleet. For a large one, add health checking, and prefer a service registry that already does it.

In Kubernetes you usually do not write any of this: you create a Service and resolve the name through DNS.

apiVersion: v1
kind: Service
metadata:
  name: orders
spec:
  selector:
    app: orders
  ports:
    - port: 8080
      targetPort: 8080

4. Configuration

Configuration comes from the environment, and the twelve-factor approach says to read it at start-up rather than reading it deep inside the business logic.

type Config struct {
    HTTPAddr    string        `env:"HTTP_ADDR" default:":8080"`
    DatabaseDSN string        `env:"DATABASE_DSN" required:"true"`
    Timeout     time.Duration `env:"TIMEOUT" default:"5s"`
    MaxRetries  int           `env:"MAX_RETRIES" default:"3"`
}

func Load() (Config, error) {
    var cfg Config
    if err := envconfig.Process("APP", &cfg); err != nil {
        return Config{}, fmt.Errorf("load config: %w", err)
    }
    return cfg, nil
}

Parse and validate the configuration once, at start-up, and pass the resulting struct down. A service that discovers a missing variable only when the code path is first executed fails in production, not in review.

5. What to Take Away

  • Split on business capability, and let each service own its data.
  • Use sagas with explicit compensations instead of distributed transactions.
  • Pick HTTP/JSON for openness and gRPC for a stable, hot, internal contract.
  • Give every outbound call a deadline, and always return a gRPC status code.
  • Read configuration once at start-up and fail fast when it is invalid.

Practice

MicroservicesService BoundariesSagagRPCConfiguration

Approach: The question asks you to design a split for a specific system, so reason from the change axis: the three proposed services - order, payment, notification - are already split along technical layers, which is exactly the mistake the first part of the question sets up. Decide the boundary first, then state the consistency mechanism that follows from it.

Steps:

  1. “Order service” and “payment service” as separate services is a technical split: they are two halves of one business transaction, so they will always have to be released together and will always call each other.
  2. The correct boundary is a single order capability that owns the order, its lines, and its state, including the payment authorisation as part of the order’s own workflow, because the order and the payment change for the same reason.
  3. Notification is a genuinely separate capability: it changes on its own schedule, and a slow email provider must not slow down order creation, so it consumes an event rather than a synchronous call.
  4. Because order and payment now live in one service, the payment authorisation becomes a local transaction plus a compensating action if the subsequent step fails, rather than a distributed transaction across a service boundary.
  5. Notification is reached by publishing an event (for example an order-created message on a queue) so that delivery is retried and eventually consistent, and so that the order service does not block on the notification service.

Answer: Merge order and payment into one order service that owns the order lifecycle and performs the payment authorisation with a compensating action on failure, and split notification into its own service that consumes an order-created event asynchronously. The boundary follows business capability, the data stays owned by the service that changes it, and the cross-service consistency is handled by an event plus a compensation rather than a distributed transaction.

Found an issue with this content?

Tell us what could be improved.