This project is a backend for an ecommerce application, built using the microservices architecture in Go. It leverages gRPC for inter-service communication and exposes a unified API via a GraphQL gateway. The system is designed for scalability, resilience, and clear separation of concerns.
go-microservices/
├── account/ # Account microservice (user management)
│ ├── client.go
│ ├── repository.go
│ ├── server.go
│ ├── service.go
│ ├── account.proto
│ ├── cmd/account/main.go
│ └── ...
├── catalog/ # Catalog microservice (product management)
│ ├── client.go
│ ├── repository.go
│ ├── server.go
│ ├── service.go
│ ├── catalog.proto
│ ├── cmd/catalog/main.go
│ └── ...
├── order/ # Order microservice (order processing)
│ ├── client.go
│ ├── repository.go
│ ├── server.go
│ ├── service.go
│ ├── order.proto
│ ├── cmd/order/main.go
│ └── ...
├── graphql/ # GraphQL gateway (API aggregation)
│ ├── main.go
│ ├── graph.go
│ ├── schema.graphql
│ ├── ...
├── docker-compose.yaml
├── go.mod
└── ...
Each microservice (account, catalog, order) follows a clean, layered architecture:
-
Repository Layer
- Handles all data persistence and retrieval (e.g., Postgres for account/order, Elasticsearch for catalog).
- Exposes an interface for CRUD operations.
-
Service Layer
- Implements business logic, orchestrates repository calls, and enforces domain rules.
- Exposes a service interface for use by the server layer.
-
Server Layer (gRPC Server)
- Exposes the service as a gRPC API.
- Handles incoming gRPC requests, translates them to service calls, and returns responses.
-
Client Layer (gRPC Client)
- Provides a typed client for other services or the GraphQL gateway to interact with the microservice over gRPC.
-
Proto Definitions
- Each service defines its own
.protofile for gRPC contracts.
- Each service defines its own
- A request to create an order:
- Hits the GraphQL gateway.
- Gateway calls the order service via gRPC client.
- Order service validates the account and products by calling account and catalog services via their gRPC clients.
- Order service persists the order using its repository.
- The
graphql/directory contains the API gateway, which exposes a single GraphQL endpoint for clients. - The gateway aggregates data from all microservices by using their gRPC clients.
- The GraphQL schema (
schema.graphql) defines the API surface, including queries and mutations for accounts, products, and orders. - This approach provides a unified API for frontend clients, hiding the complexity of the underlying microservices.
- Service Independence: Each microservice can be developed, deployed, and scaled independently.
- gRPC Communication: Fast, strongly-typed, and efficient inter-service communication.
- Resilient Patterns:
- Retry logic for connecting to databases and other services.
- Graceful error handling and resource cleanup.
- Stateless Services: All services are stateless and can be horizontally scaled.
- Data Isolation: Each service manages its own data store, reducing coupling and blast radius.
- API Gateway: The GraphQL gateway can be scaled independently to handle API traffic spikes.
- The
docker-compose.yamlfile defines all services and their dependencies (databases, etc.). - Services:
account,catalog,order: The three core microservices.graphql: The GraphQL API gateway.account_db,catalog_db,order_db: Databases for each service.
- This setup allows for easy local development and testing of the entire system.
- Clone the repository:
git clone https://github.com/saurabhdhingra/go-microservices.git cd go-microservices - Start all services:
docker-compose up --build
- Access the GraphQL Playground:
- Visit
http://localhost:8080/playgroundin your browser.
- Visit
- Add new microservices by following the same layered pattern.
- Update the GraphQL gateway to expose new APIs.
- Scale services independently based on load.
- Go (1.24+)
- gRPC for service-to-service communication
- GraphQL (via gqlgen)
- PostgreSQL (account, order)
- Elasticsearch (catalog)
- Docker Compose for orchestration
This architecture enables rapid development, clear separation of concerns, and robust scaling for modern ecommerce backends.
