Architectural Challenges and Practices for Scaling AI Applications

    This article examines why generative AI applications often slow down or queue up during sudden traffic spikes, and how to address it. It breaks the problem down into mismatched traffic, resource, and scheduling models, then walks through a layered architecture covering traffic shaping, priority queues, continuous batching, GPU memory management, and queue-driven autoscaling. It also covers SLO definition, retries, graceful degradation, circuit breaking, observability, and capacity planning, along with common pitfalls to avoid.

    1. From Viral Growth to Congestion: Mapping the Phenomenon to Technical Signals

    A generative AI application may reach over ten million downloads within a week of public beta, climb the app charts quickly, and then slow down or start queuing requests because instantaneous concurrency is too high. This pattern is common in the industry. Capital markets often react as well (for example, a cloud vendor’s Hong Kong-listed shares rose 4.6% in one day, adding HK$56 billion in market value in a single session), which essentially reflects market expectations that large-model applications are entering a stage of large-scale adoption.

    From an engineering perspective, it helps to set aside the specific product and brand and keep only three reusable technical signals:

    1. An extremely steep traffic curve: New users and request volume grow exponentially in a short period, and the traditional weekly or monthly capacity expansion cycle simply cannot keep up.
    2. A specialized resource profile: Unlike ordinary web requests, generative inference is compute-intensive. A single request occupies a GPU or accelerator for a long time and consumes a large amount of video memory (VRAM).
    3. High sensitivity to user experience: Users are very sensitive to time to first token (TTFT) and total generation time. Once requests start queuing, retention drops quickly.

    For this reason, “server congestion” is not simply a matter of “not enough machines.” It is the combined result of a mismatch among the traffic model, the resource model, and the scheduling model.

    2. Root Cause Analysis: Why Congestion Happens

    2.1 Traffic Side: Cold Start Combined with the Herd Effect

    A new application launch is often accompanied by concentrated promotion, so users arrive in the same time window. Social sharing adds a “herd effect,” and the peak can differ from the average by one to two orders of magnitude. If capacity is provisioned for the average, the peak will inevitably break through it.

    2.2 Inference Side: The Inherent Tension Between Throughput and Latency

    Generative inference has two key metrics:

    • Throughput (tokens/s): The total number of tokens the system can produce per unit of time.
    • Latency (TTFT / TPOT): Time to first token and time per output token.

    Improving throughput often relies on larger batches, but the larger the batch, the longer an individual request waits, and the higher the latency. If a system chases throughput at peak time, what users experience is a spinning indicator.

    2.3 Architecture Side: Synchronous Blocking and Resource Fragmentation

    Many initial implementations use a synchronous model in which “one request occupies one long-lived connection plus one block of VRAM until generation finishes.” This leads to:

    • Connection slots being fully occupied by long requests, so healthy requests cannot get in;
    • VRAM being “pinned” by a few very long requests, creating fragmentation;
    • Retry storms that further amplify the load.

    3. Goals and Principles: Define “Acceptable Service” First

    Before starting any optimization, you must define service level objectives (SLOs), for example:

    • TTFT P95 < 1.5s;
    • Generation speed P95 > 20 tokens/s;
    • Error rate < 0.5% during peak periods;
    • Queue timeouts return a clear message rather than waiting indefinitely.

    The principles can be summarized in four points: tiered capacity, elastic scaling, asynchronous decoupling, and full-link observability.

    4. Architecture: Layered Design and Key Components

    4.1 Ingress Layer: Traffic Shaping and Tiering

    Apply “coarse filtering” at the entry point to keep invalid traffic out of the expensive inference layer:

    • Rate limiting based on token bucket or leaky bucket, with different quotas by user tier and API type;
    • Lowering the priority of anonymous or low-value requests, or queuing them directly;
    • Enabling static resources and caching to reduce origin fetches.

    Illustrative pseudocode (to express the rate-limiting idea only):

    1. if not rate_limiter.allow(user_id, quota=plan.quota):
    2. return enqueue_or_reject(user_id, reason="busy")
    3. route_to_inference(request)

    4.2 Scheduling Layer: Queues and Priorities

    Introduce a message queue or priority queue to turn synchronous requests into asynchronous tasks:

    • High priority (paid or core scenarios) can jump the queue;
    • Low priority enters a delayed queue and is actively degraded at peak times;
    • Queue length serves as one of the core signals for elastic scaling.

    4.3 Inference Layer: Batching and VRAM Governance

    • Adopt a continuous batching approach, where a slot is released as soon as a request completes;
    • Set upper limits on input and output length to prevent very long requests from occupying resources for extended periods;
    • Use paged VRAM management (such as the PagedAttention-style approaches common in the industry) to reduce fragmentation;
    • Scale horizontally across multiple instances, with the container platform handling fast scheduling.

    4.4 Elasticity Layer: From “CPU-Based” to “Queue + GPU Utilization”

    Traditional CPU-based scaling is ineffective for inference scenarios. Instead:

    • Scaling signals: queue backlog, GPU utilization, VRAM usage, TTFT degradation;
    • Scaling actions: add inference instances, warm up model replicas;
    • Scale-in protection: set a cooldown window to avoid oscillation.

    4.5 Data and State: Statelessness

    Keep session context external, in a cache or object storage, and keep inference instances stateless so they can be scaled or migrated on demand.

    5. Stability Details: Retries, Degradation, and Circuit Breaking

    • Retries: Retry only idempotent requests, using exponential backoff plus jitter to avoid retry storms;
    • Degradation: At peak times, disable non-core features (such as long-text summarization or stylized generation) to protect the main path;
    • Circuit breaking: Fail fast when the downstream inference timeout rate exceeds a threshold, and return a friendly message;
    • Timeout budgets: Set decreasing timeouts at each layer to prevent “layer-by-layer waiting.”

    6. Observability: No Measurement, No Optimization

    You must build full-link metrics covering “user - gateway - queue - inference - storage”:

    • Business metrics: active users, success rate, queue time;
    • Technical metrics: TTFT, TPOT, queue depth, GPU utilization, VRAM fragmentation rate;
    • Logs and tracing: correlate by request ID to locate slow nodes.

    You can build this system using general capabilities from providers such as Baidu AI Cloud (for example, log services, monitoring and alerting, message queues, container platforms, and object storage), without binding to any proprietary interfaces.

    7. Capacity Planning: Replace Intuition with Data

    1. Estimate QPS and total token volume using historical peak × growth factor;
    2. Use load testing to obtain a single instance’s “latency-throughput” curve and find the inflection point;
    3. Reserve 30%–50% headroom to handle bursts;
    4. Run regular drills (chaos engineering) to verify that rate limiting and degradation actually work.

    8. Common Pitfalls

    • Adding machines without changing the architecture: under synchronous blocking, the benefit of scaling diminishes;
    • Looking only at average latency: averages hide the long tail;
    • Ignoring the input length distribution: a few very long requests can drag down the whole system;
    • Unlimited retries: turning a local failure into a global avalanche.

    9. Conclusion

    The viral growth of a generative AI application is a dual test of product and technology. Turning “congestion” into a governable engineering problem—through tiered rate limiting, asynchronous queues, continuous batching, metrics-driven elasticity, and full-link observability, carried on general cloud infrastructure such as Baidu AI Cloud—is what keeps user experience stable under a traffic flood and makes the leap from a “traffic miracle” to “technical resilience.”