Simple CRUD API with an in-memory database and a load balancer.
- Node.js
- TypeScript
- ESLint /Prettier
- Jest / supertest
- nodemon / dotenv / cross-env
# Install dependencies
npm install
# Run in develpment
npm run start:dev
# Run in production
npm run start:prod
# Run in cluster mode
npm run start:multi
# Run tests
npm run test
# Run ESLint
npm run lint
GET /api/users
POST /api/users
GET /api/users/:id
PUT /api/users/:id
DELETE /api/users/:id
{
id: string (uuid);
username: string;
age: number;
hobbies: string[];
}
This app supports horizontal scaling for application. It utilizes load balancer based on the Node.js Cluster API (Round-robin algorithm).
For example: host machine has 4 cores, PORT is 4000. On run npm run start:multi it works following way
- On
localhost:4000/apiload balancer is listening for requests - On
localhost:4001/api,localhost:4002/api,localhost:4003/api,localhost:4004/apiworkers are listening for requests from load balancer - When user sends request to
localhost:4000/api, load balancer sends this request tolocalhost:4001/api, next user request is sent tolocalhost:4002/apiand so on. - After sending request to
localhost:4004/apiload balancer starts from the first worker again (sends request tolocalhost:4001/api) - State of db should be consistent between different workers, for example:
- First
POSTrequest addressed tolocalhost:4001/apicreates user - Second
GETrequest addressed tolocalhost:4002/apishould return created user - Third
DELETErequest addressed tolocalhost:4003/apideletes created user - Fourth
GETrequest addressed tolocalhost:4004/apishould return 404 status code for created user
- First