How to Build an AI App for 1 Million Users
How do you design an AI application that can scale to a million users? A system-design walkthrough of an Instagram-style AI chat feature: traffic estimates, interactive vs background workloads, AI gateways, model routing, queues, RAG, and why cost can break a system that looks healthy.
- AI system design
- Scalability
- AI gateway
- Model routing
- Queues & workers
- RAG
- Workload isolation
In short
Building an AI feature is easy until millions of people use it. Using a hypothetical Instagram-style AI chat as a case study, Harsh Gupta starts from a traffic estimate — tens of millions of requests a day, hundreds of requests per second on average — and shows why averages lie, why AI work cannot share the same path as likes and feed loads, and how the architecture grows as each bottleneck appears. Interactive chat streams through a short path; long jobs go to queues and specialized workers. An AI gateway and model router keep inference costs under control. The lesson is not to add more servers: it is to find what breaks first, isolate it, and scale that part — because handling a million users reliably and profitably is the real engineering problem.
Key takeaways
- 01Average RPS is only a baseline. Real traffic spikes — evenings, virality, a creator posting about the feature — so the architecture has to be ready for peaks, not the daily average.
- 02AI work is slow compared with likes and feed loads. If you couple them on the same resources, caption generation can starve the rest of the app.
- 03Use a queue only when the user is not waiting. Interactive chat should stay a short path: API, model router, LLM, stream tokens as they arrive.
- 04An AI gateway centralizes rate limiting, model routing, provider fallback, caching and cost tracking so inference policy lives in one place.
- 05Not every request needs the strongest model. Route simple tasks to small, cheap models and reserve larger ones for work that justifies the cost.
- 06A production AI system can look healthy on CPU and latency and still go bankrupt on tokens, retries and tool calls. Track cost per conversation, not just request rate.
- 07Scale the bottleneck, not the whole system. Different workloads — APIs, workers, models, storage — should scale independently.
Full transcript
How to architect an AI app at scale
If you had to build an AI feature at Instagram's kind of scale — Meta AI in DMs — how would you architect it? Building the feature is easy: call an API, get a response. The real engineering starts when millions of people use it.
Harsh does not draw the full architecture up front. As each problem appears, a new component gets added. The case study is hypothetical; the traffic numbers are illustrative, not Meta's internals.
Estimating traffic: 10 million users
Instagram has more than 500 million daily active users. Even if only 2% use the AI feature in a day, that is 10 million people. At three messages each, 30 million requests a day — roughly 350 requests per second on average. Treat it like a normal feed or like request and the system will crash.
Why average traffic lies
Traffic is never uniform. Evenings get busier, a feature can go viral, a creator posts about it, and 350 requests per second can spike to several thousand. The average is a baseline. The architecture has to be built for the peak.
Normal requests vs. AI requests
Loading photos, commenting and liking are fast: fetch from the database and return. Ask the AI for a caption and the path now includes an LLM, which can take seconds. Couple that work with the rest of the application and the AI feature starts consuming resources the rest of the system needs.
The restaurant analogy: workload contention
Think of the backend as a restaurant. Feed, likes and comments are cold drinks — fast to serve. Generating a caption is a slow-cooked dish. If one chef does both, everyone waits for the five-minute plate. The problem is not the chef. Different workloads are sharing the same resources.
Interactive AI vs. background AI
Workloads have different latency requirements. In chat, the user is sitting in front of the screen waiting. Other work — parsing a 500-page PDF, chunking, embedding, indexing — can happen in the background. Split them into two categories before you pick a queue.
Why interactive AI needs streaming
For interactive chat you want the path as short as possible: API, model router, LLM. As soon as tokens start arriving, stream them so the user is not waiting for the full answer. Rate limiting, caching, retrieval and routing can still sit on that path. What you should not add is an unnecessary queue that puts ten jobs ahead of a person who is waiting.
Background AI: queues and workers
A 500-page upload needs parsing, chunking, embeddings and a vector store. You should not keep an HTTP request open for that. Accept the job immediately — "your job has been accepted" — and let workers pick it up, process it and store the result.
When should you use a queue?
Do not look at AI and decide you need a queue. Look at the latency requirement. Is the user actively waiting? Interactive path, or long-running background work? Then decide whether queues and workers belong.
Load balancers and stateless APIs
As traffic grows, run multiple API instances behind a load balancer and keep those APIs stateless so any server can handle any request. Conversation and per-user state must not live in a server's local memory. Put that in a shared system.
Redis, caching and rate limiting
If every request hits the database, the database becomes the bottleneck. Redis caches frequently accessed data and helps with rate limiting. At this scale, rate limiting is not only security — it is cost control, because you do not want unlimited expensive inference.
Building an AI gateway
A dedicated gateway for AI traffic centralizes AI-specific concerns: rate limiting, model routing, authentication, provider selection, fallback, observability and caching. Inference policy lives in one place instead of being scattered across services.
Model routing: small vs. large models
Someone comments "Nice pic" and you ask the AI what to reply. That does not need the best model you have. A long conversation that needs a detailed response strategy is a different workload. Simple tasks go to small, fast, cheaper models; complex tasks go to stronger ones. Repeated questions can be cached so you skip the model call entirely.
The hidden cost of AI inference
The cost of an AI request is not one request. Input tokens, output tokens, which model ran, tool calls, retries — all of it counts. In agentic cases one user action can mean several model calls. In production you have to track cost per conversation, cost per user, tokens per request, model, tool calls and cache hit rate. The system can look healthy on latency and CPU and the AI bill can still destroy you.
Context management and summarization
A user with thousands of messages over six months cannot have the whole history sent to the LLM on every turn. That piles up input tokens and gets expensive. Store conversation state, summarize what matters, and pull only the relevant pieces for this request. The exact store depends on the workload.
Specialized workers and workload isolation
Background work should not share one generic worker pool. Document processing and image processing have different scaling needs. If PDFs go viral, scale document workers; if image jobs surge, scale those. That is workload isolation, and at scale it is essential.
Queue backpressure and worker scaling
Two thousand background jobs arrive in a second and workers can handle five hundred. Adding API servers will not help — the API is not the bottleneck. The queue absorbs the backlog, and queue depth tells you when to scale workers. That is backpressure: control the workload instead of letting the system overload.
Object storage vs. database storage
Photos, videos and PDFs do not belong on the API server's local disk. Large files go to object storage, metadata to the database. Do not turn the API tier into a file store.
RAG: vector search and document ingestion
If the AI has to answer from private documents, add a retrieval layer. Chunk at upload time, generate embeddings once, and at query time retrieve the relevant chunks. Do the expensive work once instead of repeating it on every request.
Handling AI provider failures
Things fail. A provider slows down to seconds. If AI work and normal work are tightly coupled, one slow dependency becomes a cascading failure. Use timeouts so you do not wait forever, retries with backoff so you do not retry blindly, circuit breakers when a dependency keeps failing, and fallbacks wherever you can.
Observability: monitoring AI systems
If you cannot answer why something is slow, expensive, or why the queue is growing, the architecture is only a diagram. Traditional apps watch CPU, memory, latency and errors. AI apps also need tokens, model latency, cost per request, model usage and cache hit rate.
Building the complete architecture
Put it together: users, load balancer, stateless APIs, Redis and the database. AI traffic goes through the gateway. The model router decides cache hit, small model or large model. Interactive requests stream. Long-running work is queued to specialist workers. Documents sit in object storage, embeddings in a vector database, context in its own store, and the whole system is observed.
The path was incremental. Traffic grew, so load balancers. The database bottlenecked, so cache. AI-specific traffic, so a gateway. Different model needs, so a router. Long jobs, so a queue. Different processing, so specialized workers. Growing context, so context management. Large files, object storage. Knowledge, a vector database. Failures, timeouts and circuit breakers. And observability so the diagram is not the only source of truth.
Seven common system design mistakes
Do not blindly add servers because there are a million users. Find the bottleneck first. Do not send every request through a queue — interactive work does not belong there. Do not keep one worker for everything; isolate workloads that scale differently. And complexity is not quality: you do not automatically need twenty microservices. Start simple, measure, and add complexity where it solves a real problem.
The biggest lesson: find the bottleneck
The system design question is not how much traffic a single server can handle. A better question: what will break first, and can you isolate and scale that part independently?
Applying the architecture to real problems
Traffic grew — scale the API layer. Database reads increased — cache and optimize the database. The queue is growing — scale the workers. Cost is rising — model routing, caching and token optimization. The provider failed — timeouts, retries or fallbacks. That is the architecture.
Scale the bottleneck, not the whole system
To handle a million users you do not need one giant server. You need a system where different workloads scale independently.
Reliability and the economics of AI
In AI systems every request can be expensive. You are not only designing for scale. You are designing for reliability and economics. Handling a million users is impressive. Handling a million users reliably and profitably is the real engineering problem.
Questions this video answers
Should every AI request go through a queue?
No. Queues are for work the user is not sitting and waiting on — document parsing, embeddings, re-indexing. Interactive chat should stay a short path and stream tokens. Decide from the latency requirement, not from the fact that the workload is AI.
Why not just add more servers when traffic grows?
More API servers only help if the API is the bottleneck. If workers can handle 500 jobs a second and 2,000 arrive, the queue and worker pool are the problem. Find what will break first and scale that part.
What does an AI gateway do?
It is a dedicated layer for AI traffic that centralizes rate limiting, model routing, authentication, provider selection, fallback, observability and caching so those concerns are not duplicated across services.
Why route some requests to smaller models?
A short reply to "Nice pic" does not need your strongest model. Simple tasks can use small, fast, cheaper models; complex analysis uses larger ones. Caching repeated questions avoids a model call entirely. That is how you keep inference cost from exploding.
Why can an AI system look healthy and still be too expensive?
CPU, memory and latency can look fine while tokens, tool calls, retries and the wrong model on easy tasks run up the bill. One user action can mean several model calls. Track cost per conversation, not only request rate.
What is workload isolation?
Different background jobs have different resource needs. Document workers and image workers should scale independently so a viral PDF upload does not starve image processing, and the other way around.
Is this how Instagram or Meta AI is actually built?
No. It is a hypothetical system-design exercise using an Instagram-style AI feature. The traffic figures are illustrative assumptions, not measured Meta traffic or a description of Meta's internal architecture.