type Query {
users: [User]
user(id: ID!): User
products: [Product]
product(id: ID!): Product
orders: [Order]
order(id: ID!): Order
}
type Mutation {
registerUser(input: RegisterUserInput): RegisterUserResult
createProduct(input: CreateProductInput): Product # admin route, x-api-key required in headers, should match API_SECRET in .env
placeOrder(products: [OrderProductInput]): Order # protected route, should have a Bearer token in Authorization header which is signed using API_SECRET in .env and contains {userId: <your_user_id>} - received as access_token from registerUser mutation
}
type User {
_id: ID!
username: String!
}
type Product {
_id: ID!
name: String!
price: Int!
quantity: Int!
}
type OrderProduct {
_id: ID!
quantity: Int!
}
type Order {
_id: ID!
userId: ID!
products: [OrderProduct]
}
input RegisterUserInput {
username: String!
password: String!
}
type RegisterUserResult {
access_token: String!
user: User!
}
input CreateProductInput {
name: String!
price: Int!
quantity: Int!
}
input OrderProductInput {
_id: String!
quantity: Int!
}
query Users {
users {
_id
username
}
}
query Products {
products {
_id
name
price
quantity
}
}
mutation RegisterUser {
registerUser(input: { username: "arth", password: "password" }) {
access_token
}
}
# requires x-api-token (check API_SECRET env var)
mutation CreateProduct {
createProduct(input: { name: "chocolate", price: 20, quantity: 10 }) {
_id
name
price
quantity
}
}
# protected router, requires Bearer token auth, access_token is received from registerUser mutation
mutation PlaceOrder {
placeOrder(
products: [
{
_id: "Add a valid id received from createProduct or products query"
quantity: 10
}
]
) {
_id
products {
_id
quantity
}
userId
}
}
A robust, scalable e-commerce system using microservices architecture.
The system is composed of the following key components:
- API Gateway: Acts as the single entry point for all client requests, routing them to appropriate microservices.
- User Service: Manages user authentication and profile information.
- Product Service: Handles product catalog and inventory management.
- Order Service: Processes and manages customer orders.
- Apache Kafka: Facilitates event-driven communication between microservices.
- MongoDB: Provides persistent storage for each microservice.
- Redis: For Caching frequently accessed data (ex: getting a list of products).
- User registration and authentication
- JWT-based secure access
- Product creation and management
- Inventory tracking
- Real-time inventory updates
- Secure order placement
- Inventory reservation system to prevent overselling
Real-time updates across services using Kafka Improved system resilience and scalability
- Flexible and efficient data querying
- Consolidated API for client applications
- Frequently accessed resource such as list of products is cached in redis and invalidated when inventory is updated.
Docker-based deployment for easy scaling and management
- Backend: Node.js with Express.js
- API Gateway: Apollo Server (GraphQL)
- Database: MongoDB
- Message Broker: Apache Kafka
- Authentication: JSON Web Tokens (JWT)
- Containerization: Docker and Docker Compose
- Caching: Redis (K/V)
- REST APIs for synchronous communication
- Kafka for asynchronous event-driven updates
- JWT authentication for secure API access
- Environment-based configuration for sensitive data
- Each microservice can be independently scaled
- Used Kafka for decoupling services and handling high loads
The whole system can be run with a single command.
docker-compose up
Go to each service's directory to run them specifically.