Building an AI app is easy; building one that serves thousands of users without issues is hard. If you are building an AI SaaS (Software as a Service), your GPU backend needs to do two things including scale up in high traffic and scale down in low traffic to save costs. This tutorial provides a complete setup for a scalable GPU backend for AI SaaS.
We will use vLLM for serving, Kubernetes with KEDA for scaling, and Semantic Caching to reduce costs.
Many developers are now moving their workloads to dedicated GPU providers like Perlod Hosting, which offers bare-metal performance for AI hosting and saves costs.
Architecture Review: Build a Scalable GPU Backend for AI SaaS
Your GPU backend needs a smart architecture that can handle many users at once, keep latency low, and avoid wasting expensive GPU time.
The architecture below shows how a single request travels through your system, from the user to the GPU and back. A cache layer like Redis sits in front of your models to answer repeated or similar questions instantly, while a load balancer routes the rest to GPU Dedicated Servers running vLLM.
Additionally, an autoscaling engine such as KEDA watches the traffic and spins GPUs up or down based on demand, so your AI app stays fast, stable, and cost-efficient even as usage grows.
Here is a simple architecture example to build the GPU backend for AI SaaS:
- User sends a request, for example, Summarize this text.
- Redis Cache Layer checks: Have we answered a similar question recently?
Yes? Return the saved answer (Cost: $0, Time: 10ms).
No? Send it to the GPU.
- The Load Balancer sends the request to a GPU node.
- vLLM Engine runs the model and generates text.
- KEDA Scaler watches the line of waiting users. If the line gets too long, it adds more GPUs automatically.
Now that you have understood the architecture, proceed to the following steps to build a scalable GPU backend for AI SaaS.
Step1. Prerequisites for SaaS AI GPU Hosting
Before deploying vLLM, you must ensure your server has the right physical hardware and the software to let Docker talk to your GPU.
Hardware requirements include:
- NVIDIA GPU: You need at least 8GB VRAM to run a quantized 7B model. For production, 24GB+, like an A10G or A100, is recommended.
- Drivers: Ensure CUDA-compatible drivers are installed. The version must match your GPU. For a quick check, you can run:
If you see your GPU details, your drivers are working.
Tip: If you are unsure whether to use consumer cards or enterprise-grade, check out this comparison on RTX 4090 vs A100 for AI Training to see which fits your budget and workload best.
Operating System: Ubuntu 22.04 or Ubuntu 24.04 LTS is the industry standard for AI serving.
Also, you need Python 3.8+ installed on your system.
Software Tools You Need to Install:
- Docker and NVIDIA Container Toolkit
- Kubernetes.
- KEDA Autoscaler for Kubernetes.
- Prometheus Metrics collector for KEDA.
- Redis Stack.
- Python libraries: sentence-transformers, redis, and numpy.
By default, Docker containers cannot see your GPU. You need the NVIDIA Container Toolkit to do this.
If you haven't installed Docker yet, run these commands to get the latest version:
sudo apt updatesudo apt install ca-certificates curl -ysudo install -m 0755 -d /etc/apt/keyringssudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.ascsudo chmod a+r /etc/apt/keyrings/docker.asc echo \ "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \ $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \ sudo tee /etc/apt/sources.list.d/docker.list > /dev/null sudo apt update sudo apt install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin -y
Then, install the NVIDIA Container Toolkit with the commands below:
curl -fsSL https://nvidia.github.io/libnvidia-container/gpgkey | sudo gpg --dearmor -o /usr/share/keyrings/nvidia-container-toolkit-keyring.gpg \ && curl -s -L https://nvidia.github.io/libnvidia-container/stable/deb/nvidia-container-toolkit.list | \ sed 's#deb https://#deb [signed-by=/usr/share/keyrings/nvidia-container-toolkit-keyring.gpg] https://#g' | \ sudo tee /etc/apt/sources.list.d/nvidia-container-toolkit.list sudo apt updatesudo apt install nvidia-container-toolkit -y sudo nvidia-ctk runtime configure --runtime=dockersudo systemctl restart docker
Now, verify that Docker can actually see your GPU with the command below:
docker run --rm --gpus all nvidia/cuda:12.0-base-ubuntu22.04 nvidia-smi
Step2. Set up vLLM Serving Engine for SaaS AI Apps
The first step in your GPU backend is to set up a fast and reliable serving engine, and vLLM is one of the best options for this. It is a high-throughput, open-source engine that uses smart memory tricks like PagedAttention to keep your GPUs busy and your latency low.
In this step, we will turn vLLM into an HTTP server that looks and behaves like the OpenAI API, so your SaaS can plug into it with simple REST calls. We package everything in a Dockerfile, which the vLLM setup runs consistently across the local machine, staging, and production clusters.
Create your project folder and navigate to it:
sudo mkdir ai-saas-backendcd ai-saas-backend
Create a Dockerfile from your project directory with the following command:
Add the following content to the file:
FROM vllm/vllm-openai:latest WORKDIR /app RUN pip install redis sentence-transformers numpy EXPOSE 8000 ENTRYPOINT ["python3", "-m", "vllm.entrypoints.openai.api_server", \ "--model", "TheBloke/Mistral-7B-Instruct-v0.2-AWQ", \ "--quantization", "awq", \ "--dtype", "float16", \ "--port", "8000"]
Flags Explanations:
- FROM: Pull the base system with all NVIDIA drivers pre-installed.
- ENTRYPOINT: This command runs automatically. We use api_server to make it look exactly like GPT-4's API.
- --quantization awq: This tells the GPU to use a compressed version of the model. It uses less memory and runs faster with almost no loss in quality.
Build the Docker image with the command below:
docker build -t my-ai-backend:latest .
This takes 5-15 minutes on the first run.
Once you are done, test the vLLM server locally:
docker run --rm --gpus all -p 8000:8000 my-ai-backend:latest
You should see logs like "Uvicorn running on 0.0.0.0:8000" and "Started vLLM engine successfully."
In another terminal, you can test the API with the command below:
curl -X POST http://localhost:8000/v1/completions \-H "Content-Type: application/json" \-d '{"model": "Mistral-7B-Instruct-v0.2","prompt": "Explain quantum computing in simple terms:","max_tokens": 100}'
If you see generated text, vLLM is working.
Note: This step assumes you already have a model ready to serve. If you are still in the training phase and looking for a cost-effective way to fine-tune your models before deploying them here, check out this guide on Kaggle GPU Training with Best Configs.
Step 3. Implement Semantic Caching for Cost Savings in SaaS AI Apps
Semantic caching is a game-changer for AI SaaS apps because it goes beyond simple text matching to understand the intent behind user questions. This means your system can reuse answers for similar queries without firing up expensive GPUs every time, which cuts costs and speeds up responses.
In this step, we build a cache layer using Redis with vector search, where queries get turned into embeddings for quick similarity checks.
First, start Redis in Docker with the commands below:
docker run -d --name redis-stack -p 6379:6379 redis/redis-stack-server:latest docker exec redis-stack redis-cli ping
Create a middleware.py file that acts as a middle layer between user requests and vLLM; check the cache first, then forward if needed.
Add the following sample script to the file:
1import time2import numpy as np3from redis import Redis4from redis.commands.search.field import VectorField5from redis.commands.search.indexDefinition import IndexDefinition, IndexType6from sentence_transformers import SentenceTransformer7 8910REDIS_HOST = 'localhost' 11redis_client = Redis(host=REDIS_HOST, port=6379, decode_responses=False)12 1314embedder = SentenceTransformer('all-MiniLM-L6-v2')15 16def init_redis_index():17 """Initialize Redis vector index if it doesn't exist."""18 try:19 redis_client.ft("cache_idx").info()20 print("Index already exists.")21 except:22 23 schema = (VectorField("embedding", "FLAT", {24 "TYPE": "FLOAT32", 25 "DIM": 384, 26 "DISTANCE_METRIC": "COSINE"27 }),)28 definition = IndexDefinition(prefix=["cache:"], index_type=IndexType.HASH)29 redis_client.ft("cache_idx").create_index(schema, definition=definition)30 print("Created new Redis vector index.")31 32def get_embedding(text):33 """Turns text into a 384-dim vector."""34 return embedder.encode(text).astype(np.float32).tobytes()35 36def check_cache(user_query):37 """Checks if a similar question exists."""38 vector = get_embedding(user_query)39 40 41 query = f"*=>[KNN 1 @embedding $vec AS score]"42 43 try:44 result = redis_client.ft("cache_idx").search(45 query, 46 query_params={"vec": vector}47 )48 49 50 if result.total > 0 and float(result.docs[0].score) < 0.1:51 52 return result.docs[0].answer.decode('utf-8')53 except Exception as e:54 print(f"Cache search error: {e}")55 56 return None57 58def save_to_cache(user_query, llm_answer):59 """Saves new Q&A pair with embedding."""60 vector = get_embedding(user_query)61 key = f"cache:{int(time.time()*1000)}"62 63 64 redis_client.hset(key, mapping={65 "embedding": vector,66 "answer": llm_answer67 })68 69 redis_client.expire(key, 86400)70 7172if __name__ == "__main__":73 init_redis_index()74 75 print("Saving test Q&A...")76 save_to_cache("Who is the president of USA?", "The current president is Donald Trump.")77 78 print("Checking cache for similar question...")79 80 answer = check_cache("Who runs the United States?")81 82 if answer:83 print(f"Cache HIT! Answer: {answer}")84 else:85 print("Cache MISS.")
If 20% of your users ask similar questions, this script reduces your GPU bill by 20% and makes your app feel instant.
Test the Caching Script with:
pip install sentence-transformers redis numpy python middleware.py
In your output, you must see something similar to this:
Created new Redis vector index.Saving test Q&A...Checking cache for similar question...Cache HIT! Answer: The current president is Donald Trump.
Advanced Tip: This tutorial focuses on the infrastructure for serving standard LLMs. However, if you are building a system that needs to read your own data (RAG), you can check this guide on Building RAG Pipelines Using GPU Servers.
Step 4. Deploy vLLM with Kubernetes for SaaS AI Apps
Once your Docker image is ready, the next step is to run it in a reliable, scalable environment, and Kubernetes is the standard choice for that.
Install kubectl with the commands below:
curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"sudo install -o root -g root -m 0755 kubectl /usr/local/bin/kubectlkubectl version --client
For local testing, install minikube with the following commands:
curl -LO https://github.com/kubernetes/minikube/releases/latest/download/minikube-linux-amd64sudo install minikube-linux-amd64 /usr/local/bin/minikubeminikube start --gpus=1
Before deploying, push your image to Docker Hub or a private registry:
docker login docker tag my-ai-backend:latest yourusername/my-ai-backend:latest docker push yourusername/my-ai-backend:latest
Create a Deployment YAML file with the command below:
sudo nano deployment.yaml
Add the following content to the file:
1apiVersion: apps/v12kind: Deployment3metadata:4 name: llm-inference5spec:6 replicas: 17 selector:8 matchLabels:9 app: llm-inference10 template:11 metadata:12 labels:13 app: llm-inference14 spec:15 containers:16 - name: vllm-container17 18 image: yourusername/my-ai-backend:latest19 resources:20 limits:21 nvidia.com/gpu: 1 22 ports:23 - containerPort: 800024 livenessProbe:25 httpGet:26 path: /health27 port: 800028 initialDelaySeconds: 6029---30apiVersion: v131kind: Service32metadata:33 name: llm-service34spec:35 selector:36 app: llm-inference37 ports:38 - protocol: TCP39 port: 800040 targetPort: 800041 type: LoadBalancer
The most important line is nvidia.com/gpu: 1, which tells Kubernetes to schedule this pod only on nodes that have a free NVIDIA GPU available, which prevents crashes and wasted deployments.
Apply the deployment with the command below:
kubectl apply -f deployment.yaml
View logs and get the service IP or URL with:
kubectl logs -f deployment/llm-inferencekubectl get svc llm-service
Step 5. Smart GPU Autoscaling with KEDA for AI SaaS Products
Traditional Kubernetes autoscaling is built around CPU metrics, which do not work well for AI inference workloads where GPUs do the heavy lifting and CPUs often stay almost idle. To keep your AI SaaS responsive and cost-efficient, you need to scale based on real demand signals, such as how many user requests are currently waiting in the queue.
KEDA (Kubernetes Event-Driven Autoscaling) solves this by reading metrics from Prometheus and dynamically adjusting the number of vLLM pods based on queue depth instead of CPU usage.
Install KEDA and Helm package manager with the commands below:
kubectl apply -f https://github.com/kedacore/keda/releases/download/v2.12.1/keda-2.12.1.yaml kubectl get pods -n keda curl https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3 | bash
Install Prometheus Metrics Collector:
helm repo add prometheus-community https://prometheus-community.github.io/helm-chartshelm repo update helm install prometheus prometheus-community/kube-prometheus-stack
Then, create a YAML file for KEDA Scaler with the command below:
sudo nano keda-scaler.yaml
Add the following script to the file:
1apiVersion: keda.sh/v1alpha12kind: ScaledObject3metadata:4 name: gpu-scaler5spec:6 scaleTargetRef:7 name: llm-inference8 minReplicaCount: 19 maxReplicaCount: 1010 triggers:11 - type: prometheus12 metadata:13 14 serverAddress: http://prometheus-kube-prometheus-prometheus.default:909015 16 query: sum(rate(vllm_request_duration_seconds_count[1m]))17 threshold: "5"
This gives you automatic, demand-based GPU scaling without manual intervention, which keeps latency low while controlling cloud costs.
Deploy the Scaler with the command below:
kubectl apply -f keda-scaler.yaml
Monitor scaling and watch pods scale up or down in real time:
You now have a fully functional, production-ready GPU backend for AI SaaS.
Conclusion
Building a scalable AI SaaS is about architecture. In this guide, you have learned to build a scalable GPU backend for AI SaaS that serves fast using vLLM's PagedAttention, saves money using Redis Semantic Caching, and scales automatically using KEDA to add resources only when users are actually waiting.
We hope you enjoy this guide on SaaS AI GPU Hosting. Subscribe to our X and Facebook channels to get the latest articles and updates on AI and GPU hosting.