DriveSphere is a high-performance, concurrency-safe car rental platform built with the MERN stack. It features a robust booking engine capable of handling high-traffic scenarios, preventing double bookings through distributed locking, and managing real-time inventory with dynamic availability checks.
- Advanced Booking Engine:
- Concurrency Safe: Uses Redis Distributed Locks to prevent race conditions during high-demand periods.
- Atomic Transactions: MongoDB Sessions ensure data integrity during booking creation.
- Dynamic Availability: Inventory is calculated in real-time based on date ranges, allowing for precise back-to-back scheduling.
- Idempotency: Prevents duplicate bookings from network retries or double-clicks.
- Reservation System:
- "Hold" mechanism reserves vehicles for 10 minutes pending payment.
- Automated background cleanup (Cron Jobs) releases expired reservations.
- Payment Integration:
- Seamless integration with Stripe Checkout.
- Secure webhook handling for payment confirmation.
- Role-Based Access Control:
- Client: Browse, filter, book, and manage history.
- Car Owner: List assets, view earnings, manage fleet status.
- Admin: System-wide oversight and user management.
- Performance & Optimization:
- Intelligent Caching: Implements a read-through caching strategy using Redis for high-traffic endpoints (Inventory, User Bookings, Revenue Reports).
- Proactive Invalidation: Event-driven cache purging ensures data consistency across user and owner dashboards immediately after any state change.
- L3 Skeleton Screens: Premium shimmer effects and skeleton states integrated via Framer Motion for a perceived zero-latency UX.
- Frontend: React (Vite), Redux Toolkit, Framer Motion, TailwindCSS.
- Backend: Node.js, Express.js.
- Database: MongoDB (Mongoose) for persistence, Redis (Upstash) for locking & caching.
- Infrastructure: Stripe (Payments), Node-Cron (Background Tasks).
The booking system follows a "Check-Lock-Reserve-Pay" architectural pattern specifically designed to handle "Flash Sale" style spikes:
- Request Initiation: Client sends a booking request with date range.
- Idempotency Gate: Redis checks a unique key (
idempotency:booking:...) generated from the user's intent to reject duplicate requests immediately (409 Conflict). - Distributed Lock: Redis acquires a mutex on the specific
carId. This prevents two separate server instances from processing the same car simultaneously. If locked, returns423 Locked. - ACID Transaction (MongoDB):
- Availability Check: System queries for overlapping
confirmedor activependingbookings within the requested time window. - Reservation: If available, creates a
pendingbooking valid for 10 minutes.
- Availability Check: System queries for overlapping
- Payment Handoff: User is redirected to Stripe Checkout.
- Background Cleanup: A cron job (Node-Cron) runs every minute to identify
pendingbookings older than 10 minutes and marks them asexpired, immediately freeing up the vehicle for other users.
To achieve enterprise-grade speed, we implemented a multi-layered caching strategy:
- Inventory Search: Car listings are cached for 30 seconds with keys hashed by the user's search parameters (location, brand, specific dates).
- User/Owner Dashboards: High-cost aggregation queries (like Revenue Reports and Booking History) are cached for 5-10 minutes, reducing MongoDB compute usage by up to 80%.
We use a "Passive Cache with Active Purge" model. Whenever a critical action occurs (New Booking, Cancellation, Car Update), the system surgically identifies and deletes the relevant Redis keys:
cars_query_*: Cleared on any inventory change.owner_bookings_${id}: Cleared on new bookings.user_bookings_${id}: Cleared on cancellations.
Rather than using generic "Loading" spinners, we implemented L3 Skeleton Screens:
- Custom-built shimmer components using Framer Motion.
- Structural placeholders that match the exact layout of the data cards, reducing layout shifts (CLS) and improving perceived performance.
- Node.js v14+
- MongoDB Instance
- Redis Instance (Local or Upstash)
- Stripe Account
Create a .env file in the server directory:
# Server
PORT=8000
NODE_ENV=development
# Database
MONGO_URL=your_mongodb_connection_string
# Authentication
JWT_SECRET_KEY=your_jwt_secret
# Redis (Upstash or Local)
UPSTASH_REDIS_REST_URL=your_redis_url
UPSTASH_REDIS_REST_TOKEN=your_redis_token
# Stripe Payments
STRIPE_SECRET_KEY=sk_test_...
STRIPE_WEBHOOK_SECRET=whsec_...
# Client URL
FRONTEND_URL=http://localhost:5173-
Backend:
cd server npm install npm run dev -
Frontend:
cd client npm install npm run dev
We have included a specialized test script to verify the robustness of the booking engine.
Scenario: Two users attempt to book the same car for the same dates at the exact same millisecond.
Run the Test:
cd server
node test_concurrency.jsExpected Output:
✅ User 1: SUCCESS (201) - Booking ID: ...
🔒 User 2: LOCKED (423) - Vehicle is currently being processed...
Built for the DriveSphere Capstone Project.