This project is a scalable Role-Based Access Control (RBAC) API built with Node.js, Express, Prisma, PostgreSQL, Redis, and AWS ECS Fargate. It provides secure access control by dynamically assigning users to roles and permissions. It is deployed on AWS ECS Fargate with a Load Balancer, Redis ElastiCache, and PostgreSQL for optimized performance.
- Secure Authentication: JWT-based authentication for secure session handling
- Role & Permission Management: Assign multiple roles to users and define custom permissions
- Caching Layer: Redis ElastiCache for fast lookup of user roles & permissions
- Scalability: Auto-scaling ECS tasks based on CPU load, handling up to 5000 concurrent requests
- Fault Tolerance: Load Balancer for distributing traffic across multiple containers
- Optimized Queries: Prisma ORM with PostgreSQL for efficient database queries
- Backend: Node.js, Express.js, TypeScript
- Database: PostgreSQL (via Prisma ORM)
- Caching: Redis (AWS ElastiCache)
- Containerization: Docker
- Authentication: JWT (JSON Web Token)
- Orchestration: AWS ECS (Fargate)
- Load Balancing: AWS Application Load Balancer (ALB)
- Auto-Scaling: ECS Service Auto-Scaling based on CPU utilization
- Monitoring & Logging: AWS CloudWatch Logs
- Testing: Jest, Supertest, ApacheBench (ab) for load testing
- Version Control: Git, GitHub
Install Dependencies
- npm install
Set up environment variables
PORT=3000 HOST=0.0.0.0
JWT_SECRET=your_super_secret_key JWT_EXPIRES_IN=1h
DATABASE_URL=postgresql://postgres:123@localhost:5434/rbac_db?schema=public
REDIS_URL=redis://localhost:6379 REDIS_HOST=127.0.0.1 REDIS_PORT=6379 REDIS_TLS_ENABLED=false
- npm run dev
- docker build -t rbac-api .
- docker run -p 3000:3000 --env-file .env rbac-api
Apply database migrations
- npx prisma migrate dev --name init
Seed the database
- npm run seed
Start the API
- npm run dev
Authentication Method Endpoint Description POST /api/auth/register Register a new user POST /api/auth/login Login and get JWT token POST /api/auth/logout Logout user
Users Method Endpoint Description GET /api/users Get all users GET /api/users/:id Get user by ID POST /api/users Create a new user (Admin only) PATCH /api/users/:id Update user details (Admin only) DELETE /api/users/:id Delete a user (Admin only)
Roles & Permissions Method Endpoint Description GET /api/roles Get all roles POST /api/roles Create a new role (Admin only) POST /api/assignments/users/:userId/assign-role Assign a role to a user
- npm test
- ab -n 5000 -c 100 /api/auth/login
- 5000 concurrent requests
- Average response time: ~1200ms
- Zero failed requests
- Auto-scaled to 3 ECS tasks under load
This API is deployed using AWS ECS Fargate with AWS Application Load Balancer (ALB) and Redis ElastiCache. Here’s a summary of the AWS deployment strategy:
- Dockerized and stored in AWS Elastic Container Registry (ECR)
- Task definitions managed via AWS ECS
- PostgreSQL RDS as the primary database
- Redis ElastiCache for session storage & caching
- AWS Application Load Balancer (ALB) for traffic distribution
- ECS Auto-Scaling adjusts instances based on CPU & memory utilization
git clone https://github.com/mowems/rbac-system.git
cd rbac-systemEnsure Docker is installed and running before setting up the database. Modify the .env file with your own database credentials before running migrations. Only admins can create users and assign roles. The API is fully tested with Jest & Supertest.
This project is licensed under the MIT License.