When Clean Architecture Adds More Layers Than a Go Backend Needs
A Go backend where changing a user's email touched twelve files, and the simpler feature-based layout I use instead on small projects.
· 2 min read · Erik Kubica
I once worked on a Go backend where updating a customer's email address meant touching twelve files across eight layers of abstraction.
The team had read Robert C. Martin's Clean Architecture and implemented the diagrams quite literally, which is an understandable thing to do. The result looked like this:
internal/
├── delivery/http/controllers/user_controller.go
├── delivery/http/presenters/user_presenter.go
├── usecases/boundaries/update_email_input_boundary.go
├── usecases/boundaries/update_email_output_boundary.go
├── usecases/interactors/update_email_interactor.go
├── domain/entities/user_entity.go
├── domain/values/email_value_object.go
├── domain/repositories/user_repository_interface.go
├── infrastructure/persistence/postgres/user_repository.go
├── infrastructure/persistence/models/user_db_model.go
└── infrastructure/mappers/user_mapper.go
That was over four hundred lines of interfaces, DTOs, and mapper functions to execute a single SQL statement:
-- What all that indirection actually executes in Postgres:
UPDATE users SET email = $1, updated_at = NOW() WHERE id = $2;
Swapping the Database
When I asked why there were five layers of interfaces between the HTTP handler and Postgres, the answer was: "What if we need to switch from PostgreSQL to DynamoDB? And what if we want to mock the database in unit tests?"
Those are fair questions. In my 13 years of client work I have not seen a project switch its core relational database to a document store, so on the projects I know, the extra layer mostly cost development time. On a large system with several teams and real portability requirements, the trade-off may look different.
The mocking part bothered me more:
// Clean architecture mock that passes tests while masking real SQL bugs
mockRepo.On("UpdateEmail", ctx, 42, "new@example.com").Return(nil)
The mock returns nil, and the suite runs in 4 milliseconds with full coverage. Then in production a user tries to change their email to an address that already exists, and the application hits an unhandled PostgreSQL unique key violation (SQLSTATE 23505). The mock never exercised the database, so it could not have caught that.
A Simpler Layout I Use
In my own Go backends I group code by feature rather than by architectural layer:
internal/users/
├── handler.go // HTTP parsing, status codes, JSON encoding
└── store.go // Direct SQL queries executed via pgx or sqlc
The handler validates the input and calls store.UpdateEmail(ctx, userID, email). The whole feature is about 65 lines.
Here is how the two compare for this one feature:
| Metric | Layered Clean Architecture | Feature Layout |
|---|---|---|
| Files to Update Email | 12 files | 2 files |
| Lines of Boilerplate | 420 lines | 0 lines |
| Memory Allocations | 18 heap allocs / req | 1 heap alloc / req |
| Real Postgres Test Time | Faked with mocks | 35 ms in testcontainers |
Testing against a real database container caught problems for me that mocks did not.
Clean Architecture's ideas are useful, and larger codebases with many contributors can benefit from stricter boundaries. For smaller Go services, I have had better results with fewer layers, plain SQL, and tests that run against a real database. It might not fit your situation, so it is worth weighing against the size of your team and codebase.