Forget perfect microservices — build this instead
There is a certain comfort in a well-drawn architecture diagram.
Boxes connected by arrows. Service boundaries that look clean. Each domain neatly isolated, each team owning their slice. It reads like engineering maturity.
Then you try to run it locally and suddenly you need eight terminal tabs, a running Kafka broker, three databases, a service mesh, and someone on your team who remembers what that one environment variable does.
We’ve been sold complexity as a proxy for correctness. It isn’t.
The trap that starts with good intentions
In reality nobody really architects a distributed mess on purpose. It starts sensibly — a monolith gets hard to deploy, two teams step on each other’s work, so you split things up. Reasonable.
But then the splits keep coming. One service becomes three because someone read a blog post about bounded contexts.
Three becomes seven because a new team joined and needs “ownership.” Seven becomes fifteen because every database table is now its own microservice.
What most people fail to understand is that they have now traded one hard problem (a big codebase) for ten harder ones: distributed tracing, inter-service latency, partial failures, schema versioning across boundaries, cascading timeouts, and a deployment pipeline that takes forty minutes.
So, technically, you’ve solved the wrong thing.
What to Build Instead
The answer isn’t “write a monolith forever.” It’s: build for the scale you have, with paths to the scale you want.
Start with a modular monolith
A modular monolith is not a compromise — it is an architecture decision. You organize code into packages with clear, enforced boundaries, but everything deploys as one binary.
In Go, this means strict package separation where each domain owns its types, queries, and business logic. Cross-package calls go through explicit interfaces, never direct struct access.
/internal
/payments
handler.go
service.go
repository.go
types.go
/accounts
handler.go
service.go
repository.go
types.go
/notifications
handler.go
service.go
repository.go
types.go
/internal
/payments
handler.go
service.go
repository.go
types.go
/accounts
handler.go
service.go
repository.go
types.go
/notifications
handler.go
service.go
repository.go
types.go
Here’s what that interface boundary looks like in practice:
<button data-code="// internal/payments/service.go
type AccountService interface {
GetBalance(ctx context.Context, accountID string) (int64, error)
Debit(ctx context.Context, accountID string, amount int64) error
}
type Service struct {
repo Repository
accounts AccountService
logger *slog.Logger
}
func NewService(repo Repository, accounts AccountService, logger *slog.Logger) *Service {
return &Service{repo: repo, accounts: accounts, logger: logger}
}
func (s *Service) ProcessPayment(ctx context.Context, req ProcessPaymentRequest) (*Payment, error) {
balance, err := s.accounts.GetBalance(ctx, req.AccountID)
if err != nil {
return nil, fmt.Errorf("get balance: %w", err)
}
if balance
// internal/payments/service.go
type AccountService interface {
GetBalance(ctx context.Context, accountID string) (int64, error)
Debit(ctx context.Context, accountID string, amount int64) error
}
type Service struct {
repo Repository
accounts AccountService
logger *slog.Logger
}
func NewService(repo Repository, accounts AccountService, logger *slog.Logger) *Service {
return &Service{repo: repo, accounts: accounts, logger: logger}
}
func (s *Service) ProcessPayment(ctx context.Context, req ProcessPaymentRequest) (*Payment, error) {
balance, err := s.accounts.GetBalance(ctx, req.AccountID)
if err != nil {
return nil, fmt.Errorf("get balance: %w", err)
}
if balance < req.Amount {
return nil, ErrInsufficientFunds
}
if err := s.accounts.Debit(ctx, req.AccountID, req.Amount); err != nil {
return nil, fmt.Errorf("debit account: %w", err)
}
payment := &Payment{
ID: uuid.New().String(),
AccountID: req.AccountID,
Amount: req.Amount,
Status: StatusCompleted,
CreatedAt: time.Now(),
}
return s.repo.Save(ctx, payment)
}
// internal/payments/service.go
type AccountService interface {
GetBalance(ctx context.Context, accountID string) (int64, error)
Debit(ctx context.Context, accountID string, amount int64) error
}
type Service struct {
repo Repository
accounts AccountService
logger *slog.Logger
}
func NewService(repo Repository, accounts AccountService, logger *slog.Logger) *Service {
return &Service{repo: repo, accounts: accounts, logger: logger}
}
func (s *Service) ProcessPayment(ctx context.Context, req ProcessPaymentRequest) (*Payment, error) {
balance, err := s.accounts.GetBalance(ctx, req.AccountID)
if err != nil {
return nil, fmt.Errorf("get balance: %w", err)
}
if balance < req.Amount {
return nil, ErrInsufficientFunds
}
if err := s.accounts.Debit(ctx, req.AccountID, req.Amount); err != nil {
return nil, fmt.Errorf("debit account: %w", err)
}
payment := &Payment{
ID: uuid.New().String(),
AccountID: req.AccountID,
Amount: req.Amount,
Status: StatusCompleted,
CreatedAt: time.Now(),
}
return s.repo.Save(ctx, payment)
}
The payments package has no idea how AccountService is implemented. It could be a direct call to internal/accounts, or later, an HTTP client wrapping a separate service. The call site doesn’t change. That’s the point.
This gives you team isolation and domain clarity without the distributed systems tax.
Wire It at the Top, Not Inside Packages
All the wiring lives in one place — main.go or a cmd/ entrypoint. Packages never reach into each other directly.
// cmd/api/main.go
func main() {
logger := slog.New(slog.NewJSONHandler(os.Stdout, nil))
db, err := pgxpool.New(ctx, os.Getenv("DATABASE_URL"))
if err != nil {
logger.Error("connect to db", "err", err)
os.Exit(1)
}
accountRepo := accounts.NewRepository(db)
accountSvc := accounts.NewService(accountRepo, logger)
paymentRepo := payments.NewRepository(db)
paymentSvc := payments.NewService(paymentRepo, accountSvc, logger)
notificationRepo := notifications.NewRepository(db)
notificationSvc := notifications.NewService(notificationRepo, logger)
srv := api.NewServer(paymentSvc, accountSvc, notificationSvc, logger)
srv.Run(":8080")
}
// cmd/api/main.go
func main() {
logger := slog.New(slog.NewJSONHandler(os.Stdout, nil))
db, err := pgxpool.New(ctx, os.Getenv("DATABASE_URL"))
if err != nil {
logger.Error("connect to db", "err", err)
os.Exit(1)
}
accountRepo := accounts.NewRepository(db)
accountSvc := accounts.NewService(accountRepo, logger)
paymentRepo := payments.NewRepository(db)
paymentSvc := payments.NewService(paymentRepo, accountSvc, logger)
notificationRepo := notifications.NewRepository(db)
notificationSvc := notifications.NewService(notificationRepo, logger)
srv := api.NewServer(paymentSvc, accountSvc, notificationSvc, logger)
srv.Run(":8080")
}
One binary. One deployment. Zero service mesh.
When you need to extract payments into its own service, you swap accountSvc for an HTTP client that satisfies the same AccountService interface — nothing else changes.
Use Async Boundaries Deliberately
Not every cross-domain call needs to be synchronous. When a payment completes and the notification service needs to know about it, does it need to wait for an acknowledgment before returning to the caller? Almost never.
This is where a message queue earns its keep — not as a universal integration bus, but as a deliberate boundary between things that don’t need to happen in lockstep.
// internal/payments/service.go
type EventPublisher interface {
Publish(ctx context.Context, topic string, payload []byte) error
}
func (s *Service) ProcessPayment(ctx context.Context, req ProcessPaymentRequest) (*Payment, error) {
// ... validation and debit logic from above
payment, err := s.repo.Save(ctx, payment)
if err != nil {
return nil, fmt.Errorf("save payment: %w", err)
}
event := PaymentCompletedEvent{
PaymentID: payment.ID,
AccountID: payment.AccountID,
Amount: payment.Amount,
Timestamp: time.Now(),
}
data, _ := json.Marshal(event)
if err := s.publisher.Publish(ctx, "payments.completed", data); err != nil {
// Log and continue — notifications are not critical path
s.logger.Warn("failed to publish payment event", "payment_id", payment.ID, "err", err)
}
return payment, nil
}
// internal/payments/service.go
type EventPublisher interface {
Publish(ctx context.Context, topic string, payload []byte) error
}
func (s *Service) ProcessPayment(ctx context.Context, req ProcessPaymentRequest) (*Payment, error) {
// ... validation and debit logic from above
payment, err := s.repo.Save(ctx, payment)
if err != nil {
return nil, fmt.Errorf("save payment: %w", err)
}
event := PaymentCompletedEvent{
PaymentID: payment.ID,
AccountID: payment.AccountID,
Amount: payment.Amount,
Timestamp: time.Now(),
}
data, _ := json.Marshal(event)
if err := s.publisher.Publish(ctx, "payments.completed", data); err != nil {
// Log and continue — notifications are not critical path
s.logger.Warn("failed to publish payment event", "payment_id", payment.ID, "err", err)
}
return payment, nil
}
The EventPublisher interface could be backed by Redpanda, an in-memory channel, or a simple PostgreSQL outbox row — the service doesn’t care.
The notification service consumes events on its own schedule. No synchronous coupling, no shared database query, no orchestration nightmare.
The key word here is deliberate. One queue for one well-understood integration — not an event-driven mesh where nobody can trace what triggers what.
Keep the Data Together Longer Than Feels Comfortable
Premature schema separation is one of the most expensive mistakes in distributed systems design.
Once two services have separate databases with a shared entity — say, a user_id both reference — you’ve created a consistency problem that doesn’t go away.
Stay co-located. Use sqlc and a single PostgreSQL instance. Keep transactions meaningful:
// internal/payments/repository.go
type Repository struct {
db *pgxpool.Pool
q *db.Queries // generated by sqlc
}
func (r *Repository) SaveWithAudit(ctx context.Context, payment *Payment) error {
return pgx.BeginTxFunc(ctx, r.db, pgx.TxOptions{}, func(tx pgx.Tx) error {
qtx := r.q.WithTx(tx)
if err := qtx.InsertPayment(ctx, db.InsertPaymentParams{
ID: payment.ID,
AccountID: payment.AccountID,
Amount: payment.Amount,
Status: string(payment.Status),
}); err != nil {
return fmt.Errorf("insert payment: %w", err)
}
if err := qtx.InsertAuditLog(ctx, db.InsertAuditLogParams{
EntityID: payment.ID,
EventType: "payment.created",
CreatedAt: time.Now(),
}); err != nil {
return fmt.Errorf("insert audit log: %w", err)
}
return nil
})
}
// internal/payments/repository.go
type Repository struct {
db *pgxpool.Pool
q *db.Queries // generated by sqlc
}
func (r *Repository) SaveWithAudit(ctx context.Context, payment *Payment) error {
return pgx.BeginTxFunc(ctx, r.db, pgx.TxOptions{}, func(tx pgx.Tx) error {
qtx := r.q.WithTx(tx)
if err := qtx.InsertPayment(ctx, db.InsertPaymentParams{
ID: payment.ID,
AccountID: payment.AccountID,
Amount: payment.Amount,
Status: string(payment.Status),
}); err != nil {
return fmt.Errorf("insert payment: %w", err)
}
if err := qtx.InsertAuditLog(ctx, db.InsertAuditLogParams{
EntityID: payment.ID,
EventType: "payment.created",
CreatedAt: time.Now(),
}); err != nil {
return fmt.Errorf("insert audit log: %w", err)
}
return nil
})
}
This transaction — payment row and audit log in a single commit — is trivially correct.
The moment these live in separate services with separate databases, you’re writing sagas, outbox patterns, and idempotency keys to get back to the same guarantee. That’s real engineering hours spent rebuilding what PostgreSQL gave you for free.
In summary, every moving part you add is a liability on your operational balance sheet. It needs to be maintained, monitored, documented, and debugged.
It will fail at 2am. It will confuse your next hire.
The question is never “can we add this complexity?” You can always add it. The question is: does the value it delivers outweigh what it costs to operate?
Thanks for reading, read these too
I Stopped Writing Tests This Way — Everything Improved
Get this right once and you’ll never write tests badly in any of your Go projects again.
The biggest mistake most teams make with Go microservices
The Architecture Mistake That Makes Go Microservices Hard to Maintain
The Go Patterns we stopped using in production
These Go Patterns Look Smart Until Production Traffic Arrives

