Search⌘ K
AI Features

Exploring Transactional Boundaries

Explore methods to create and propagate transactions across requests in Golang by leveraging context and dependency injection. Understand how to manage transaction lifecycles, avoid global variables, and implement scoped instances to isolate database interactions effectively.

Starting with the more important part first, we will tackle how to create a new transaction for each request into our modules, whether these come in as messages, a gRPC call, or the handling of a domain event side effect. As we are using grpc-gateway, all of the HTTP requests are proxied to our gRPC server and will not need any special attention.

Propagating transactions: Best practices and considerations

Creating a transaction is not the difficult part. The challenge will be ensuring the same transaction is used for every database interaction for the entire life of the request. With Go, our best option is going to involve using the context to propagate the transaction through the request. Before going into what that option might look like, we should also have a look at some of the other possible solutions:

  • We can toss out the option of using a global variable right away. Beyond being a nightmare to test, they will also become a problem to maintain or ...