A scalable, distributed ride-sharing application built with microservices architecture, real-time communication, and asynchronous message queuing.
- Overview
- Features
- Architecture
- Tech Stack
- Project Structure
- Installation & Setup
- Running the Application
- API Endpoints
- Database Schema
- Communication Patterns
- Deployment
- Contributing
RideSphere is a modern ride-sharing platform that implements a microservices architecture with independent services for user management, captain (driver) management, and ride coordination. The system uses an API Gateway pattern for request routing, RabbitMQ for asynchronous communication, MongoDB for data persistence, and JWT for authentication.
The architecture enables:
- Scalability: Each microservice can be scaled independently
- Resilience: Failure of one service doesn't cascade to others
- Decoupling: Services communicate through message queues
- Real-time Updates: Long polling for ride notifications
- Security: JWT-based authentication and password hashing with bcrypt
- User registration and authentication
- Secure password storage with bcrypt hashing
- JWT-based session management
- Profile management
- Ride request creation
- Captain registration and authentication
- Availability toggle (online/offline status)
- Real-time ride notifications via long polling
- Accept/reject ride requests
- JWT-based session management
- Ride request creation by users
- Ride acceptance by available captains
- Real-time notification system via RabbitMQ
- Ride status tracking (requested β accepted β started β completed)
- Persistent ride history
- API Gateway for unified routing
- Asynchronous Communication via RabbitMQ
- Token Blacklisting for secure logout
- Request Logging with Morgan
- Error Handling across all services
- Database Connection Pooling
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Client Applications β
ββββββββββββββββββββββββββββ¬βββββββββββββββββββββββββββββββββββ
β
βΌ
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β API Gateway (Port 3000) β
β - Request Routing & Forwarding β
β - Request/Response Logging β
β - Header Management β
ββββ¬ββββββββββββββ¬ββββββββββββββ¬βββββββββββββββββββββββββββββββ
β β β
βΌ βΌ βΌ
ββββββββββ ββββββββββ ββββββββββ
β User β βCaptain β β Ride β
βService β βService β βService β
β(3001) β β(3002) β β (3003) β
βββββ¬βββββ βββββ¬βββββ βββββ¬βββββ
β β β
ββββββββββββΌβββββββββββββ
β
βΌ
βββββββββββββββββββββββ
β Message Queue β
β (RabbitMQ) β
β - new-ride queue β
β - event publishing β
βββββββββββββββββββββββ
β
βΌ
βββββββββββββββββββββββ
β Database β
β (MongoDB) β
β - users β
β - captains β
β - rides β
β - blacklist_tokens β
βββββββββββββββββββββββ
| Service | Port | Responsibility |
|---|---|---|
| User Service | 3001 | User authentication, registration, profile management |
| Captain Service | 3002 | Captain authentication, availability, ride notifications |
| Ride Service | 3003 | Ride creation, acceptance, status management |
| API Gateway | 3000 | Request routing, logging, unified entry point |
- Node.js - JavaScript runtime
- Express.js - REST API framework
- MongoDB - NoSQL database
- Mongoose - MongoDB ODM
- RabbitMQ - Message broker (asynchronous communication)
- JWT - Authentication tokens
- bcrypt - Password hashing
- amqplib - RabbitMQ client library
- express-http-proxy - API Gateway routing
- Morgan - HTTP request logger
- dotenv - Environment variable management
- Docker (optional for containerization)
- MongoDB Atlas or local MongoDB instance
RideSphere/
βββ gateway/ # API Gateway Service
β βββ app.js # Express app setup
β βββ package.json
β βββ server.js
β
βββ user/ # User Microservice
β βββ app.js
β βββ server.js
β βββ db/
β β βββ db.js # MongoDB connection
β βββ models/
β β βββ user.model.js # User schema
β βββ controllers/
β β βββ user.controller.js # User logic
β βββ routes/
β β βββ user.routes.js # User endpoints
β βββ middleware/
β β βββ auth.middleware.js # Authentication
β βββ service/
β β βββ rabbit.js # RabbitMQ client
β βββ package.json
β
βββ captain/ # Captain Microservice
β βββ app.js
β βββ server.js
β βββ db/
β β βββ db.js
β βββ models/
β β βββ captain.model.js # Captain schema
β β βββ blacklisttoken.model.js
β βββ controllers/
β β βββ captain.controller.js
β βββ routes/
β β βββ captain.routes.js
β βββ middleware/
β β βββ authMiddleWare.js
β βββ service/
β β βββ rabbit.js
β βββ package.json
β
βββ ride/ # Ride Microservice
β βββ app.js
β βββ server.js
β βββ db/
β β βββ db.js
β βββ models/
β β βββ ride.model.js # Ride schema
β βββ controllers/
β β βββ ride.controller.js
β βββ routes/
β β βββ ride.routes.js
β βββ middleware/
β β βββ auth.middleware.js
β βββ service/
β β βββ rabbit.js
β βββ package.json
β
βββ backend-check/ # Load testing & health checks
β βββ app.js
β βββ gateway.service.js
β βββ stress.service.js
β βββ test/
β βββ package.json
β βββ test.js
β
βββ .env.example # Environment variables template
βββ README.md # This file
- Node.js (v14+)
- npm or yarn
- MongoDB (local or Atlas)
- RabbitMQ server running
git clone https://github.com/Pranjal360Agarwal/RideSphere.git
cd RideSphereCreate a .env file in each service directory with the following variables:
# MongoDB Connection
MONGO_URI=mongodb+srv://username:password@cluster.mongodb.net/ridesphere?retryWrites=true&w=majority
# JWT Secret
JWT_SECRET=your_jwt_secret_key_here
# RabbitMQ Configuration
RABBIT_URL=amqp://guest:guest@localhost:5672
# Service Ports (optional, if overriding defaults)
PORT=3001 # for user service
PORT=3002 # for captain service
PORT=3003 # for ride service# Install gateway dependencies
cd gateway && npm install && cd ..
# Install user service dependencies
cd user && npm install && cd ..
# Install captain service dependencies
cd captain && npm install && cd ..
# Install ride service dependencies
cd ride && npm install && cd ..If using MongoDB Atlas:
- Create a cluster at MongoDB Atlas
- Create a database user and whitelist your IP
- Update
MONGO_URIin.envfiles
If using local MongoDB:
# Start MongoDB service
mongod# Using Docker (recommended)
docker run -d --name rabbitmq -p 5672:5672 -p 15672:15672 rabbitmq:3-management
# Or install locally: https://www.rabbitmq.com/download.htmlTerminal 1 - Start User Service:
cd user && npm startTerminal 2 - Start Captain Service:
cd captain && npm startTerminal 3 - Start Ride Service:
cd ride && npm startTerminal 4 - Start API Gateway:
cd gateway && npm startnpm install -g pm2
# Start all services
pm2 start gateway/server.js --name "gateway" --env production
pm2 start user/server.js --name "user-service" --env production
pm2 start captain/server.js --name "captain-service" --env production
pm2 start ride/server.js --name "ride-service" --env production
# Monitor
pm2 monit
# View logs
pm2 logsCreate docker-compose.yml:
version: "3.8"
services:
mongodb:
image: mongo:latest
ports:
- "27017:27017"
environment:
MONGO_INITDB_DATABASE: ridesphere
rabbitmq:
image: rabbitmq:3-management
ports:
- "5672:5672"
- "15672:15672"
gateway:
build: ./gateway
ports:
- "3000:3000"
environment:
- NODE_ENV=production
user-service:
build: ./user
ports:
- "3001:3001"
environment:
- NODE_ENV=production
depends_on:
- mongodb
- rabbitmq
captain-service:
build: ./captain
ports:
- "3002:3002"
environment:
- NODE_ENV=production
depends_on:
- mongodb
- rabbitmq
ride-service:
build: ./ride
ports:
- "3003:3003"
environment:
- NODE_ENV=production
depends_on:
- mongodb
- rabbitmqRun with:
docker-compose up -d| Method | Endpoint | Description | Auth |
|---|---|---|---|
| POST | /register |
User registration | β |
| POST | /login |
User login | β |
| GET | /profile |
Get user profile | β |
| POST | /logout |
User logout | β |
| Method | Endpoint | Description | Auth |
|---|---|---|---|
| POST | /register |
Captain registration | β |
| POST | /login |
Captain login | β |
| GET | /profile |
Get captain profile | β |
| POST | /logout |
Captain logout | β |
| PUT | /toggle-availability |
Toggle online/offline status | β |
| GET | /wait-for-ride |
Long polling for new rides | β |
| Method | Endpoint | Description | Auth |
|---|---|---|---|
| POST | /create-ride |
Create a new ride request | β (User) |
| PUT | /accept-ride |
Accept a ride request | β (Captain) |
{
_id: ObjectId,
name: String,
email: String (unique),
password: String (hashed),
createdAt: Date,
updatedAt: Date
}{
_id: ObjectId,
name: String,
email: String (unique),
password: String (hashed),
isAvailable: Boolean (default: false),
createdAt: Date,
updatedAt: Date
}{
_id: ObjectId,
user: ObjectId (reference to User),
captain: ObjectId (reference to Captain),
pickup: String,
destination: String,
status: String (enum: ["requested", "accepted", "started", "completed"]),
createdAt: Date,
updatedAt: Date
}{
_id: ObjectId,
token: String,
createdAt: Date
}- REST APIs between gateway and services
- Request-response pattern for immediate operations
- Publisher: Ride Service (when ride is created)
- Subscribers: Captain Service (all connected captains receive notifications)
- Purpose: Real-time notifications for available captains
Flow:
User creates ride β Ride Service β Publish to "new-ride" queue
β All captains receive notification
- Captains subscribe to ride notifications only when
isAvailable = true - Long polling mechanism with 30-second timeout
- JWT Tokens: 1-hour expiration for secure sessions
- Token Blacklisting: Logout invalidates tokens in database
- Password Hashing: bcrypt with 10 salt rounds
- Middleware Protection:
authMiddlewarevalidates JWT on protected routes
- Environment Variables: Sensitive data in
.envfiles - Cookie-based Storage: Tokens stored as HTTP-only cookies
- MongoDB Connection: SSL/TLS with MongoDB Atlas
- CORS: Cross-origin handling via express
- Input Validation: Express middleware for JSON/URL-encoded data
- Error Handling: Generic error messages to prevent information disclosure
- Horizontal Scaling: Deploy multiple instances of each service
- Load Balancing: Use Nginx or cloud load balancers
- Database Indexing: Ensure indexes on
email,user,captainfields - Connection Pooling: MongoDB connection pooling via Mongoose
- Caching: Implement Redis for frequently accessed data (future enhancement)
- Morgan: HTTP request logging
- PM2: Process monitoring and auto-restart
- RabbitMQ Management UI: Queue monitoring (http://localhost:15672)
# Check MongoDB is running
mongod --version
# For Atlas, verify connection string and IP whitelist# Check RabbitMQ service
sudo systemctl status rabbitmq-server
# Or with Docker
docker logs rabbitmq- Ensure
JWT_SECRETis set consistently across all services - Check token expiration (1 hour default)
- Verify token is being sent in Authorization header
# Clear node_modules and reinstall
rm -rf node_modules
npm install
# Check for port conflicts
lsof -i :3000 # Check if port 3000 is in use1. User POST /user/register with email & password
2. Password hashed with bcrypt
3. User document created in MongoDB
4. JWT token generated & stored in cookie
5. User can now create rides
1. Captain logs in (POST /captain/login)
2. Captain toggles availability (PUT /captain/toggle-availability)
3. Captain calls GET /captain/wait-for-ride (long polling)
4. Request held for 30 seconds waiting for ride notifications
5. When user creates ride:
- Ride Service publishes to "new-ride" queue
- Captain Service receives message
- All waiting captains get notified with ride data
1. User creates ride (POST /ride/create-ride)
- Ride stored in MongoDB with status "requested"
- Message published to "new-ride" queue
2. Available captains receive notification
3. Captain accepts ride (PUT /ride/accept-ride)
- Ride status updated to "accepted"
- Captain ID assigned to ride
4. Ride in progress
- EC2 Instances: One per service + Gateway
- RDS: MongoDB Atlas alternative
- SQS/SNS: Alternative to RabbitMQ
- Load Balancer: ALB for API Gateway
- CloudWatch: Monitoring and logging
# Create Heroku apps
heroku create ridesphere-gateway
heroku create ridesphere-user
heroku create ridesphere-captain
heroku create ridesphere-ride
# Add MongoDB Atlas add-on
heroku addons:create mongolab:sandbox --app ridesphere-user
# Deploy
git push heroku main- Cloud Run: Serverless deployment
- Cloud SQL: Managed MongoDB alternative
- Pub/Sub: Alternative to RabbitMQ
- Cloud Load Balancing: Request routing
User Registration:
curl -X POST http://localhost:3000/user/register \
-H "Content-Type: application/json" \
-d '{
"name": "John Doe",
"email": "john@example.com",
"password": "password123"
}'User Login:
curl -X POST http://localhost:3000/user/login \
-H "Content-Type: application/json" \
-d '{
"email": "john@example.com",
"password": "password123"
}'Create Ride:
curl -X POST http://localhost:3000/ride/create-ride \
-H "Content-Type: application/json" \
-H "Authorization: Bearer <JWT_TOKEN>" \
-d '{
"pickup": "123 Main St",
"destination": "456 Oak Ave"
}'Accept Ride:
curl -X PUT http://localhost:3000/ride/accept-ride \
-H "Content-Type: application/json" \
-H "Authorization: Bearer <JWT_TOKEN>" \
-d '{
"rideId": "ride_object_id"
}'- Fork the repository
- Create your feature branch (
git checkout -b feature/AmazingFeature) - Commit your changes (
git commit -m 'Add some AmazingFeature') - Push to the branch (
git push origin feature/AmazingFeature) - Open a Pull Request
- Use consistent indentation (2 spaces)
- Follow Node.js naming conventions
- Add comments for complex logic
- Test all endpoints before submitting PR
Pranjal Agarwal
- GitHub: @Pranjal360Agarwal
- Project: RideSphere
For issues, questions, or suggestions, please:
- Check existing GitHub Issues
- Create a new issue with detailed information
- Include error messages, environment details, and steps to reproduce
- Real-time location tracking with WebSockets
- Payment integration (Stripe/Razorpay)
- Rating and review system
- Ride sharing/pooling
- Admin dashboard
- Mobile app (React Native)
- AI-based driver matching
- Real-time notifications (Push notifications)
- Analytics dashboard
- Caching layer (Redis)
If you have any questions or feedback, please feel free to contact me at pranjal360agarwal@gmail.com. You can also connect with me on LinkedIn or Twitter. Thank you for visiting my project!