Let's craft software. Contact me
Avatar

Héctor Valls

Sr. Software Engineer

Cover Image for Service Weaver: Write monolith, deploy microservices

Service Weaver: Write monolith, deploy microservices

Recently, Google released Service Weaver, a Go-based framework for developing distributed applications.

Service Weaver allows developers writing their code as a modular monolith but deploying the different components as different distributed services in Google Kubernetes Service or other cloud. When developing, different modules or components communicate each other through regular method call. However, when they are deployed to the cloud, they communicate via Remote Procedure Call (RPC). This is transparent for the developer and it is made by Service Weaver using code generation.

Let's take this basic CRM example:

CRM diagram

Each component must be an interface and the implementation must embed type weaver.Implements[T]:

type InvoiceService interface {
	GetInvoices(context.Context) ([]string, error)
}

type invoiceService struct {
	weaver.Implements[InvoiceService]
}

func (r *invoiceService) GetInvoices(_ context.Context) ([]string, error) {
	// Code goes here...
}

In order to create an InvoiceService instance, we call weaver.Get method:

invoiceService, err := weaver.Get[InvoiceService](root)
invoices, err := invoiceService.GetInvoices(ctx)

Service Weaver decides what implementation of InvoiceService must be used depending on each case:

  • If we run the application with go run ., it will use the "local" implementation with regular method calls.
  • If we deploy it like in Google Kubernetes Service with weaver gke deploy weaver.toml, it will use generated RPC clients to call service's methods as cross-machine calls.

You can see the full code of the example in the Github repo.

Service Weaver is still in early development stage. You can learn more about it in the official website.