An open API service indexing awesome lists of open source software.

https://github.com/prrathnayake/uber_backend

A modular backend system built in modern C++, inspired by Uber's architecture. This clone demonstrates core backend features such as database integration, multithreaded task execution, and centralized loggin designed for educational, portfolio, or experimental use.
https://github.com/prrathnayake/uber_backend

conan conan-recipe cpp database-management datapattern kafka uber-clone

Last synced: about 1 month ago
JSON representation

A modular backend system built in modern C++, inspired by Uber's architecture. This clone demonstrates core backend features such as database integration, multithreaded task execution, and centralized loggin designed for educational, portfolio, or experimental use.

Awesome Lists containing this project

README

          

# πŸš— Uber Backend Clone (C++ Microservices) – Under Development

A **modular C++ backend system** inspired by Uber’s architecture. This project simulates a microservices environment to manage users, rides, and locations, using secure, scalable, and modern design patterns.

---

![Untitled Diagram(10)](https://github.com/user-attachments/assets/02bb3d93-590f-4973-9c9c-e2b3b13d0ac0)

---

## πŸ“¦ Features

- 🧩 **Microservice Architecture**
- Independent services: `UserManager`, `RideManager`, and `LocationManager`
- πŸ’Ύ **Multi-DB Support**: MySQL, PostgreSQL, SQLite (per service)
- 🧡 **Thread Pool**: Asynchronous background task execution
- πŸͺ΅ **Color-Coded Logger**: Singleton-based centralized logging
- πŸ” **JWT Authentication**: Secure login/session using JSON Web Tokens
- πŸ“¬ **Message Brokers**:
- Kafka: For inter-service events (`user_created`, etc.)
- RabbitMQ: For async job/event delegation with an in-memory fallback to simplify local testing
- πŸ“¦ **Shared Libraries** via [`cpp_base`](https://github.com/prrathnayake/cpp-base)
- βš™οΈ **Secure Configuration**: Via `.env` file + environment variables
- 🐳 **Dockerized**: Each microservice has its own Dockerfile & entrypoint
- πŸ” **CI/CD Ready**: GitHub Actions build and push Docker images
- 🌐 **gRPC Services**: LocationManager boots its own gRPC endpoint alongside HTTP handlers
- πŸ“„ **Ride Settlement Runbook**: End-to-end booking β†’ wallet flow captured in [`docs/ride_lifecycle_settlement.md`](docs/ride_lifecycle_settlement.md)

---

## πŸ—οΈ Tech Stack

- **Language**: C++17 / C++20
- **Databases**: MySQL, PostgreSQL, SQLite
- **Message Brokers**: Apache Kafka, RabbitMQ
- **Networking**: gRPC + Protobuf
- **Build**: CMake + Conan
- **Containerization**: Docker + docker-compose
- **CI/CD**: GitHub Actions

---

## πŸ“‚ Project Structure

```plaintext
uber-backend/
β”œβ”€β”€ UserManager/ # Handles users + JWT auth
β”‚ β”œβ”€β”€ include/ # Headers
β”‚ β”œβ”€β”€ src/ # Source
β”‚ └── sql_scripts/ # SQL init files
β”‚
β”œβ”€β”€ RideManager/ # Handles ride matching and booking
β”œβ”€β”€ LocationManager/ # Geolocation services (H3-based)
β”‚
β”œβ”€β”€ sharedUtils/ # Logger, thread pool, config loader
β”œβ”€β”€ sharedResources/ # Shared gRPC, Kafka, RabbitMQ, DB
β”œβ”€β”€ proto/ # gRPC proto definitions
β”œβ”€β”€ docker/ # Dockerfiles per service
β”‚
β”œβ”€β”€ entrypointUserManager.sh # Entrypoint: UserManager
β”œβ”€β”€ entrypointRideManager.sh # Entrypoint: RideManager
β”œβ”€β”€ entrypointLocationManager.sh # Entrypoint: LocationManager
β”‚
β”œβ”€β”€ .env # Environment variables
β”œβ”€β”€ docker-compose.yml # Multi-service orchestration
β”œβ”€β”€ CMakeLists.txt
β”œβ”€β”€ conanfile.py
└── README.md
```

# 🧠 Microservice Architecture – Uber Backend Clone (C++)

---

## πŸ–₯️ Server Lifecycle

Each microservice follows this initialization flow:

1. **Database Initialization**
Initializes a dedicated SQL database for the service. This stores all persistent data relevant to the service domain.

2. **HTTP Handler Launch**
Sets up HTTP servers and clients, routes incoming requests to proper controllers, and manages outgoing requests.

3. **Kafka Handler Activation**
Initializes Kafka producers and consumers for event-driven communication between microservices.

---

## πŸ” Component Descriptions

- **πŸ—„οΈ Database**
Dedicated storage per service using MySQL, PostgreSQL, or SQLite.

- **🌐 HTTP Handler**
Manages HTTP servers and clients. Routes requests to appropriate handlers.

- **πŸ“₯ HTTP Server**
Listens for external or internal HTTP requests and serves responses based on API logic.

- **πŸ“€ HTTP Client**
Sends HTTP requests to other microservices to fetch or send data.

- **🧭 Kafka Handler**
Coordinates Kafka producer and consumer setup within the service.

- **πŸ“© Kafka Consumer**
Subscribes to topics and listens for inter-service messages. Integrates incoming data into the service's workflow.

- **πŸ“¨ Kafka Producer**
Publishes events/messages to Kafka topics for other microservices to consume.

---

## πŸš— Microservices Breakdown

---

### 1. UserManager Server
**Purpose:** Manage all user-related operations

**Responsibilities:**
- Handle user registration and login
- Manage user profiles (name, email, phone)
- Generate JWT tokens and hash passwords (bcrypt/Argon2)
- Publish events to Kafka/RabbitMQ (e.g., user signup)
- Expose HTTP endpoints:
- `/register`
- `/login`
- `/profile`

---

### 2. RideManager Server
**Purpose:** Handle ride requests, assignments, and tracking

**Responsibilities:**
- Accept and manage ride requests
- Assign drivers based on proximity and availability
- Track ride status (`requested β†’ accepted β†’ in-progress β†’ completed`)
- Publish/consume Kafka events (e.g., ride started, completed)
- Expose HTTP APIs:
- `/requestRide`
- `/rideStatus`
- `/cancelRide`

---

### 3. LocationManager Server
**Purpose:** Handle real-time geolocation tracking

**Responsibilities:**
- Track and update driver/rider locations
- Use Uber H3 for geospatial indexing
- Find nearby drivers or riders
- Publish/consume location updates via Kafka/RabbitMQ
- Provide real-time location services

---

## πŸ§ͺ UserManager workflow smoke test

Before committing, run the automated UserManager workflow script to ensure the
core CRUD paths behave as expected and that transient files are handled safely:

```bash
scripts/test_usermanager_workflow.sh
```

The helper waits for the service to accept TCP connections on
`USER_SERVICE_HOST:USER_SERVICE_PORT` (defaults to `localhost:8081`), then
executes a signup β†’ login β†’ profile update β†’ password change β†’ delete flow. It
fails fast when HTTP responses deviate from the expected contracts or when the
response body is missing, preventing `cat: response.txt: No such file or
directory` style errors.

Set `USER_SERVICE_HOST`/`USER_SERVICE_PORT` to target a remote deployment if
needed.

---

## πŸ”§ Common Subsystems Used in All Servers

- πŸ—ƒοΈ **Dedicated SQL Database** (MySQL/PostgreSQL/SQLite)
- 🌐 **HTTP Server** for exposing REST APIs
- πŸ“‘ **Kafka Handler** for event messaging (Producer + Consumer)
- πŸ“¬ **RabbitMQ Handler** (optional command queue with thread-safe in-memory fallback)
- 🌐 **gRPC Bootstrap Helpers** (background server lifecycle management)
- πŸ” **gRPC Client/Server** (optional internal communication)
- 🧡 **Thread Pool** for async task execution
- πŸ“‹ **Singleton Logger** with colored output
- πŸ” **.env / Env Variable Config Loader**

---

### πŸš€ Getting Started

## πŸ› οΈ Build & Run (Manual)

> πŸ›‘ **Important:** Kafka and RabbitMQ must be running before you start the service binaries.

You can start Kafka and RabbitMQ using Docker (recommended) or your local installation.

### βœ… Local build

```bash
conan build . --output-folder=build --build=missing
```

Then run the binaries from `./build/Release/bin/` as needed.

### πŸ§ͺ Test deployment with Docker Compose

```bash
docker build -f docker/Dockerfile.base -t uber_base:latest .
docker compose -f docker/docker-compose.test-deploy.yml --env-file .env build
docker compose -f docker/docker-compose.test-deploy.yml --env-file .env up -d
```

Bring the stack down when finished:

```bash
docker compose -f docker/docker-compose.test-deploy.yml down
```

## 🐳 Docker Compose Setup

This project supports a fully containerized microservices environment using **Docker Compose**.

---

### πŸ“¦ Services

| Service | Description | Port |
|--------------------|------------------------------------------------|-----------------------------------|
| `mysql_user` | MySQL for UserManager | `${USERMANAGER_PORT}` |
| `mysql_ride` | MySQL for RideManager | `${RIDEMANAGER_PORT}` |
| `mysql_location` | MySQL for LocationManager | `${LOCATIONMANAGER_PORT}` |
| `kafka` | Kafka broker (KRaft mode) | `${KAFKA_EXTERNAL_PORT:-9092}` |
| `rabbitmq` | RabbitMQ broker + management UI | `${RABBITMQ_AMQP_PORT:-5672}`/`${RABBITMQ_HTTP_PORT:-15672}` |
| `redis` | Redis cache | `${REDIS_PORT:-6379}` |
| `usermanager` | UserManager C++ microservice | `${USERMANAGER_APP_PORT}` |
| `ridemanager` | RideManager C++ microservice | `${RIDEMANAGER_APP_PORT}` |
| `locationmanager` | LocationManager C++ microservice | `${LOCATIONMANAGER_APP_PORT}` |

---

### πŸ”§ Environment Configuration

Set your values in a `.env` file at the project root:

```env
# MySQL shared config
MYSQL_ROOT_PASSWORD=yourRootPassword
MYSQL_USER=uber
MYSQL_PASSWORD=securepass

# Databases
USERMANAGER_DB=userdb
RIDEMANAGER_DB=ridedb
LOCATIONMANAGER_DB=locationdb

# DB Ports
USERMANAGER_PORT=3307
RIDEMANAGER_PORT=3308
LOCATIONMANAGER_PORT=3309

# Application Ports
USERMANAGER_APP_PORT=8081
RIDEMANAGER_APP_PORT=8082
LOCATIONMANAGER_APP_PORT=8083

# gRPC Ports
LOCATION_MANAGER_GRPC_PORT=50051

# RabbitMQ
RABBITMQ_HOST=localhost
RABBITMQ_PORT=5672
RABBITMQ_USERNAME=guest
RABBITMQ_PASSWORD=guest
RABBITMQ_VHOST=/
```

> πŸ’‘ **Tip:** When running inside the provided Docker Compose stack, set service hosts (e.g., `USER_MANAGER_HOST`, `RIDE_MANAGER_HOST`, `LOCATION_MANAGER_HOST`, `KAFKA_HOST`, `RABBITMQ_HOST`, `REDIS_HOST`) to the matching container names (`usermanager`, `ridemanager`, `locationmanager`, `kafka-bus`, `rabbitmq-bus`, `redis-cache`).

### πŸ”§ Run Docker Compose

```bash
docker compose -f docker/docker-compose.test-deploy.yml --env-file .env up -d
```

---

## 🀝 Community & Project Standards

- Review the [Code of Conduct](CODE_OF_CONDUCT.md) to understand the expectations for participating in this community.
- See the [Contributing Guide](CONTRIBUTING.md) for instructions on setting up your environment, coding standards, and submitting pull requests.
- Consult the [Security Policy](SECURITY.md) for guidance on how to responsibly disclose vulnerabilities.
- This project is distributed under the terms of the [MIT License](LICENSE).