ABOUT THE PROJECT
We are developing an AI SaaS platform for content generation. Architecturally, the project is a "Wrapper" orchestrator that connects the user interface with the GPU cluster (RunPod Serverless). We do not have the task of writing the infrastructure from scratch. We use ready-made templates.
Stack: Frontend on Next.js 14 + SupaStarter, Backend/DB on Supabase (PostgreSQL), Storage Cloudflare R2.
We do not need a Fullstack developer who will "fix a button." We need an architect who will build a reliable Backend, design the queue system, and ensure data consistency during asynchronous generation.
Deadline: 8-10 weeks
The system works as follows:
User sends a request for generation (text, parameters, model).
Backend receives the request, checks the balance, and queues the task.
Asynchronous worker takes the task from the queue and sends it to RunPod.
RunPod generates a video of 2-3 minutes and sends the result back.
Backend saves the video in cloud storage (R2), updates the status and balance.
Frontend sees that the video is ready and shows it to the user.
All of this must work reliably: if something fails, the system will recover, money will not be lost, and videos will not be lost.
WHAT NEEDS TO BE DONE
1. Design the Architecture
Draw how everything moves: from the front request to the finished video.
Determine where the data is stored and how it is updated.
Consider what will happen if RunPod fails, a payment is lost, or a webhook arrives twice.
2. Design the Database
Table users (credit balance).
Table video_generations (all video orders and their statuses).
Table balance_ledger (journal of all payments and expenses — for auditing).
Table payment_transactions (all credit purchases).
Constraints and indexes to ensure the system does not break during errors.
3. Define all API Endpoints
Endpoints for video generation (request and status check).
Endpoints for managing balance and payments.
Endpoints for order history.
Endpoints for webhooks (notifications from RunPod and the payment system).
4. Write FastAPI Backend
API for the frontend (accepting requests, validation, returning results).
Handlers for webhooks (when RunPod says "video is ready," when the payment system says "money has arrived").
Logic for checking balance, reserving credits, and deducting after success.
5. Set Up Queue (Redis/BullMQ)
When a user starts generation, the task goes into the queue.
Background worker takes tasks from the queue and sends them to RunPod.
The queue distributes the load to avoid overloading RunPod.
6. Write Background Worker
The worker listens to the queue.
Takes a task, sends it to RunPod with a callback URL.
Waits for the result via webhook.
7. Set Up Docker
Production Docker image with ComfyUI, all models, and Python code.
Optimize for the container to start quickly on RunPod.
Use Network Volume so that heavy models do not take up space in the image.
8. Integration with RunPod
Set up how the backend sends tasks to RunPod.
Set the callback URL so that RunPod can send the result back.
Handle errors (if RunPod fails, the task hangs, the result is lost).
9. Integration with Cloudflare R2
10. Integration with Payment System
Connect Stripe / Crypto gateway / both.
Receive webhooks on successful payment.
Update the user's balance.
11. Handling Edge Cases & Failures
If RunPod fails in the middle of generation — refund the user.
If the payment arrives twice (duplicate webhooks) — credit only once.
If the user sends 10 requests simultaneously and wants to spend 50 credits, but has 30 — do not allow them to spend more.
If the webhook is lost on the internet — periodically check the payment status.
12. Monitoring & Logging
Log all events (requests, generation, payments, errors).
Have the ability to trace what happened with each video and each payment.
You are creating a complete, reliable system where all parts (generation, billing, asynchronicity, error handling) work together.
There is no need to write from scratch. It is necessary to design correctly and then implement.