Using Type-Safe Infrastructure in Go for Event-Driven Backends
Tags: go, backend, typesafe, microservices
Published: 2024-01-03 15:49:34

By now you are likely familiar with the concept of type-safety in programming. If not — here's the TL;DR before we get into the novel concept of type-safe infrastructure:

## 🐞 Type Errors
Type errors occur when a program tries to do something with a value that doesn't make sense. For example, if you try to add a number to a true/false value (Boolean), the program doesn't know how to handle it, because the operation doesn't work. This is a common source of unexpected behavior.

## 🤝 Type Safety
Type-safety often refers to how a programming language helps prevent these mistakes by making sure you don't mix up different types, thereby avoiding error and making your program more reliable.

Go (Golang) is known for its strong type-safety. It helps prevent errors by strictly requiring you to define types for your variables and checks these types before the program runs. This means you're less likely to run into problems

It brings benefits like increasing predictability, reducing bugs, and helping ensure your application behaves as expected.

## 👻 Distributed systems makes type-safety HARD

In a microservices architecture, even with services all written in one language, type-safety is still challenging. 

Services may have different versions of data models or interpret data types slightly differently. If it's an event-driven systems, the event mechanisms (e.g. Pub/Sub) are blind to the data structures of the messages they handle.👩‍🦯

This is a common source of hard-to-catch errors that can be a nightmare to debug.🔥

## ✨ The solution: Type-Safe infrastructure

This innovation allows developers to integrate infrastructure components, such as databases or pub/sub, as safely-typed objects within the application.

### 💪 Benefits of Type-Safe Infrastructure for Microservices Backends

Using type-safe infrastructure helps to ensure your application's behaviour is consistent and provides several compelling advantages:

- **Faster iterations:** With type-safe infrastructure, you can identify infrastructure problems as early as in your code editor, instead of learning about issues via the — much slower — deployment cycle.
- **Fewer Mistakes:** By catching infrastructure earlier, you can drastically reduce post-deployment issues that affect users.
- **Easier Workflow:** Developing for the cloud is normally cumbersome and requires many manual steps to ensure you have a local environment or test environment that matches production. By making infrastructure a type-safe part of your code, it's possible to use [tools](https://encore.dev) to automatically provide production-like local infrastructure for development and testing.

## 🙋‍♂️ Getting Started with Type-Safe Infrastructure

At [Encore](https://encore.dev) we're pioneering the concept of type-safe infrastructure, and have created an [Open Source Infrastructure SDK](https://encore.dev/docs/primitives/) that makes adopting this new concept a smooth experience for developers.

With the SDK, you can start declaring infrastructure components as type-safe objects as part of your code. It's currently available for Go (Golang), and TypeScript is soon about to launch (February 2024). (👉 Join [Slack](https://encore.dev/slack) to get in the TypeScript Beta.)

### 👉 Benefits of using Encore for Type-Safe Infrastructure

Aside from getting end-to-end type-safety, Encore's Backend Development Platform comes with several other perks:

- Auto-generation of microservices boilerplate.
- Automated local environment setup with [local developer dashboard](https://encore.dev/docs/observability/dev-dash) and automated testing on dedicated local infra.
- Automated cloud deployments to your AWS or GCP account.
- Built-in tooling: Encore automatically hooks up your app with distributed tracing, provides key metrics, automatically generates your API docs, and more.

### 🕹 Getting Started

For developers eager to try out the simple workflow that type-safe infrastructure enables, check out this tutorial for [building a REST API with a PostgreSQL database](https://encore.dev/docs/tutorials/rest-api).👈 It will show you the fundamentals of the type-safe infrastructure workflow.

To figure out how to use Encore for your current project, [join the developer community on Discord](https://encore.dev/discord) for guidance, or check out the [docs](https://encore.dev/docs/how-to/try-encore).

## Wrapping up

Type-safe infrastructure is the next logical step in cloud-native backend development. Most of the cumbersome work of building distributed systems dependent on cloud services can now be automated. It's time to embrace the future and evolve!