Architecture Overview
- Style: Microservices using Service-Oriented Architecture (SOA).
- Services: 3 isolated services with individual codebases, connected to seperate MongoDB instances.
- Containerization: Docker.
- Communication: Event-driven architecture (message broker).
- Message Broker: RabbitMQ (default), easily swappable with Kafka or NATS.
- Deployment: Docker Compose.
Microservices
-
User Service
- Framework: Express.js
- Responsibilities: Manage user data
- MongoDB Collection: USERS
- API Endpoints:
- POST /users – Register a new user
- GET /users/{id} – Fetch user details
- Event Handling:
- Consumes: LoanCreated – update user loanHistory, bookHistory and currentBooks .
- Consumes: LoanReturned – update currentBooks.
-
Book Service
- Framework: Fastify
- Responsibilities: Manage books and availability
- MongoDB Collection: BOOKS
- API Endpoints:
- POST /books – Add a new book
- GET /books/{id} – Fetch book details
- Event Handling:
- Consumes: LoanCreated – mark book as unavailable, update currentHolder and currentLoan.
- Consumes: LoanReturned – mark book as available, set currentHolder and currentLoan to None.
-
Loan Service
- Framework: Koa
- Responsibilities: Manage loan transactions
- MongoDB Collection: LOANS
- API Endpoints:
- POST /loans – Register a new user
- GET /loans/{id}/return – Fetch user details
- Event Handling:
- Publishes: LoanCreated – (userId, bookId, loanId)
- Publishes: LoanReturned – (userId, bookId, loanId)
Data Model
- USERS:
Field Type Description id ObjectId (PK) Unique user ID name String Full name of the user email String (Unique) User's email address joinedAt Date Timestamp of user registration currentBooks Array of Strings ID list of all active loaned books associated with a user (if any) loanHistory Array of Strings ID list of all loans associated with a user (if any) bookHistory Array of Strings ID list of all books ever loaned to a user (if any) status Enum User account status (active, suspended) - BOOKS:
Field Type Description id ObjectId (PK) Unique book ID title String Title of the book author String Author name isbn String (Unique) International Standard Book Number available Boolean Indicates if the book is currently available currentLoan String (Nullable) ID of the current active loan associated with a book (if any) currentHolder String (Nullable) ID of the user the book was currently loaned out to (if any) tags Array of Strings Genres or category labels addedAt Date Date the book was added to the system - LOANS:
Field Type Description _id ObjectId (PK) Unique loan ID userId String (FK) References the ID of a user bookId String (FK) References the ID of a book loanedAt Date Date the loan was initiated dueDate Date Deadline for returning the book returnedAt Date (Nullable) Date the book was returned status String Loan status (active, returned, overdue)
Infrastructure
- Message Broker: RabbitMQ:
- Services communicate via a central broker.
- Each service subscribes to a queue/topic.
- Events are JSON-based messages.
- Deployment:
- Docker compose setup with user-service, book-service, loan-service, three MongoDB containers, RabbitMQ container.