TechByteByByte

Scalability

Closing Level 6: how AI applications scale from 10 requests a day to millions, and the infrastructure patterns — queues, async workers, connection pooling, horizontal scaling — that make that growth possible.

#AI Engineering#Scalability#Level 6

Begin with the problem

A system that handles ten requests may collapse at ten thousand because of rate limits, shared state, queues, and slow dependencies. Scaling means controlling demand as well as adding machines.

traffic → rate limit/queue → stateless workers → dependencies → autoscale + backpressure

What you will learn

  • Distinguish horizontal scaling, vertical scaling, batching, and queuing.
  • Move shared state outside individual workers.
  • Plan for provider limits, backpressure, load tests, and overload behavior.

Current production grounding: Kubernetes documents workload autoscaling and controlled Deployments for operating containerized services.

These links document current public features and practices. They do not reveal a provider’s private implementation or guarantee that every product uses identical defaults.

1. The Engineering Problem

What works for 10 requests a day breaks down at 100,000 — not because the model gets worse, but because the system around it was never designed to handle real concurrency, provider rate limits, or the operational load of that much traffic.

This module covers what changes at each stage of growth, so you build the right amount of infrastructure at the right time — not too little, and not prematurely too much.


2. The Scaling Progression

~10 requests/day:      a SINGLE server, SYNCHRONOUS calls --
                      sufficient, no real infrastructure
                      needed beyond the basics

~1,000 requests/day:      basic CACHING (Module 15), simple RATE
                         LIMITING -- still simple

~100,000 requests/day:        HORIZONTAL scaling (multiple
                             STATELESS instances), a QUEUE for
                             async/slow work, LOAD BALANCING,
                             CONNECTION POOLING -- real
                             infrastructure now required

Millions of requests/day:         DISTRIBUTED caching, MULTIPLE
                                 provider/model fallback (Module
                                 14), BATCH processing
                                 pipelines, careful provider
                                 rate-limit management ACROSS
                                 regions

Building million-request-scale infrastructure for a 10-request-a- day prototype is wasted effort — Module 29 (Anti-Patterns) covers this directly. The engineering skill is building JUST ENOUGH for your CURRENT real scale, with a clear path to add more as growth demands it.


3. Stateless Services — The Foundation of Horizontal

Scaling

STATEFUL service:      holds request-specific data IN MEMORY between
                      requests -- CANNOT simply add more
                      instances, since a user's SECOND request might
                      hit a DIFFERENT instance with no memory of
                      their FIRST

STATELESS service:         stores no request-specific
                          state in memory -- state lives in a
                          shared database or cache (Module 19,
                          Application Memory) -- ANY instance can
                          handle ANY request

This directly connects to your Agents course’s Module 12 (Agent State) — an agent’s state must be PERSISTED externally (not held in the orchestration process’s memory) for the orchestration layer itself to scale horizontally.


4. Queues and Async Workers — Decoupling Slow Work

WITHOUT a queue: a SLOW model call or agent task blocks
                the request-response cycle, holding a connection
                open and consuming resources the WHOLE time.

WITH a queue: the request is accepted immediately, placed
             on a queue, and processed by a SEPARATE worker pool --
             the client gets a fast acknowledgment and can poll or
             be notified when the slower work completes.

5. A Real-World Analogy — The Call Center

A SMALL call center with 5 agents handles ALL calls
directly, synchronously.

A LARGE call center handles volume with: a QUEUE (hold
music, "your call is important"), MULTIPLE agents working in
PARALLEL (horizontal scaling), and a callback SYSTEM for
long tasks rather than keeping a caller on hold
indefinitely (async processing).

The SAME underlying "answer the customer's question" task requires
different infrastructure at different volumes.

6. Rate Limiting and Provider Limits — An External

Constraint

Your OWN system's rate limiting: protects your infrastructure from
                                 overload or abuse

Provider rate limits: an external constraint you don't
                      control -- at real scale, you MUST manage
                      request rate against the provider's actual
                      limits, often across MULTIPLE provider
                      accounts or regions

7. Concurrency and Connection Pooling

CONNECTION POOLING: reusing, already-established
                    connections to a database or external service,
                    rather than the real overhead of establishing a
                    NEW connection for every single request

CONCURRENCY LIMITS:, deliberate caps on how many
                        simultaneous requests your system processes
                        -- protecting against your OWN system, or a
                        DOWNSTREAM dependency, being overwhelmed

8. A worked developer example

TechCorp’s infrastructure evolution as their support assistant’s traffic grew:

VolumeWhat TechCorp Added
10/day (internal beta)Single server, direct synchronous model calls
1,000/day (early rollout)Redis cache (Module 15), basic per-user rate limiting
100,000/day (company-wide)Multiple stateless application instances behind a load balancer, a queue for long agent tasks, connection pooling to the vector database
Millions/day (multi-product)Distributed cache, model provider fallback (Module 14) across two providers, careful rate-limit budgeting split across accounts

9. How Is This Used in the Industry?

🤖 How Is This Used in the Industry?

Production teams build infrastructure incrementally, matching Section 2’s progression to their actual, observed traffic — rather than either under-building (hitting real scaling walls unexpectedly) or over-building (wasting engineering effort on infrastructure a prototype doesn’t need yet).


10. Common Mistakes

Incorrect idea: Building full million-request-scale infrastructure for a prototype with minimal traffic.

Why it is incorrect: As shown directly in Section 2, this is wasted effort.

Incorrect idea: Holding agent or conversation state in an in-process variable, preventing horizontal scaling.

Why it is incorrect: As shown directly in Section 3, this blocks adding more instances.

Incorrect idea: Ignoring provider rate limits until hitting them in production.

Why it is incorrect: As shown directly in Section 6, this is an external constraint that needs proactive management at real scale.


11. Code — A Scale-Tier Infrastructure Recommender

What this shows: turning Section 2’s scaling progression into a working decision function — exactly the kind of guidance a team would want when planning infrastructure investment ahead of expected traffic growth, directly Section 8’s worked developer example made concrete.

from dataclasses import dataclass
from enum import Enum

class ScaleTier(Enum):
    TIER_1 = "10_per_day"
    TIER_2 = "1k_per_day"
    TIER_3 = "100k_per_day"
    TIER_4 = "millions_per_day"

@dataclass
class ScaleRequirement:
    tier: ScaleTier
    requests_per_day: int
    genuinely_needs: list

# Section 2's progression, made into structured, queryable data
SCALE_PROGRESSION = [
    ScaleRequirement(ScaleTier.TIER_1, 10, ["A single server, synchronous calls -- sufficient"]),
    ScaleRequirement(ScaleTier.TIER_2, 1_000, ["Basic caching", "Simple rate limiting"]),
    ScaleRequirement(ScaleTier.TIER_3, 100_000, ["Horizontal scaling (multiple stateless instances)",
                                                   "A queue for async/slow work", "Load balancing", "Connection pooling"]),
    ScaleRequirement(ScaleTier.TIER_4, 5_000_000, ["Distributed caching", "Multiple provider/model fallback",
                                                     "batch processing pipelines", "Careful provider rate-limit management across regions"]),
]

def recommend_scale_tier(requests_per_day: int) -> ScaleRequirement:
    """Given a real expected daily volume, recommend which
    tier's infrastructure requirements apply -- directly
    supporting infrastructure planning ahead of expected growth."""
    applicable = [tier for tier in SCALE_PROGRESSION if requests_per_day >= tier.requests_per_day]
    return applicable[-1] if applicable else SCALE_PROGRESSION[0]

for volume in [50, 5000, 250_000, 8_000_000]:
    tier = recommend_scale_tier(volume)
    print(f"{volume:,} requests/day -> {tier.tier.value}")
    for req in tier.genuinely_needs:
        print(f"  - {req}")
    print()

Expected Output:

50 requests/day -> 10_per_day
  - A single server, synchronous calls -- sufficient

5,000 requests/day -> 1k_per_day
  - Basic caching
  - Simple rate limiting

250,000 requests/day -> 100k_per_day
  - Horizontal scaling (multiple stateless instances)
  - A queue for async/slow work
  - Load balancing
  - Connection pooling

8,000,000 requests/day -> millions_per_day
  - Distributed caching
  - Multiple provider/model fallback
  - batch processing pipelines
  - Careful provider rate-limit management across regions

What this confirms: each different volume correctly maps to its appropriate infrastructure tier — exactly Section 8’s TechCorp progression, made into a working, reusable planning tool rather than an informal “we’ll figure it out when we get there” approach.


12. Production Considerations

  • Stateless service design (Section 3) should be a default architectural choice from the start — retrofitting it after scaling problems emerge is more disruptive than building it in from day one
  • Monitor actual provider rate-limit headroom (Module 12) proactively — hitting a limit unexpectedly in production is an avoidable incident

13. Trade-offs

  • Queues and async processing (Section 4) add architectural complexity and a small latency overhead for the acknowledgment step — worthwhile once task duration or volume justifies decoupling
  • Building for a future scale tier before reaching it wastes real engineering effort that could address more immediate needs

14. Chapter Summary

AI application scaling follows a progression — from a single synchronous server at low volume, through basic caching and rate limiting, to horizontal scaling with queues and load balancing at real production volume, up to distributed infrastructure and multi-provider management at very high scale.

Stateless service design is the foundation that makes horizontal scaling possible at all. The right infrastructure investment matches your actual, current traffic — neither under-building (hitting real walls) nor over-building (wasting effort on infrastructure not yet needed).


15. Visual Cheat Sheet

10/day        -->  single server, synchronous
1K/day        -->  + caching, rate limiting
100K/day      -->  + horizontal scaling, queue, load balancer,
                    connection pooling
Millions/day  -->  + distributed cache, multi-provider fallback,
                    batch pipelines, cross-region rate limits

16. Top Takeaways

  1. Infrastructure needs grow in stages — build for your current, real traffic, not a hypothetical future scale.
  2. Stateless service design is the foundation that makes horizontal scaling possible.
  3. Queues decouple slow work from the request-response cycle, necessary once task duration or volume grows.
  4. Provider rate limits are an external constraint requiring proactive management at real scale, not reactive firefighting.
  5. Over-building infrastructure for traffic you don’t yet have is a real waste of engineering effort.

17. Interview Questions

Q: 1. Why is stateless service design foundational to horizontal scaling?**

Ans: A stateful service holds request-specific data in memory, meaning a user’s second request needs to hit the SAME instance that handled their first — this prevents simply adding more instances behind a load balancer, since any given instance might lack the state a specific user’s request depends on.

A stateless service stores state externally (a shared database or cache), so any instance can handle any request, making horizontal scaling straightforward.

  • Why it matters: Retrofitting statelessness after a system is built statefully is more disruptive than designing for it from the start.
  • Real-world example: Directly connects to your Agents course’s Module 12 — agent state must be persisted externally for the orchestration layer to scale horizontally.
  • Common mistake: Holding conversation or agent state in an in-process variable “for simplicity” during initial development, creating a scaling blocker later.
  • Interviewer is testing: Whether the candidate understands this as a foundational architectural choice, not a later optimization.
  • Likely follow-up: “Where would you store state instead?” → A shared database or cache (Module 19, Application Memory), accessible to any instance.

Q: 2. Why might a team deliberately choose NOT to build for million-request-scale infrastructure when launching a new AI feature?**

Ans: Building full-scale infrastructure (distributed caching, multi-provider fallback, cross-region rate-limit management) for a feature with minimal initial traffic is wasted engineering effort — that time would be better spent validating the feature actually works well and is wanted, then scaling infrastructure as real traffic growth demands it.

  • Why it matters: Over-engineering for hypothetical future scale is a common anti-pattern that delays actually shipping and validating a product.
  • Real-world example: Section 8’s TechCorp progression — the internal beta didn’t need horizontal scaling or distributed caching.
  • Common mistake: Assuming “production-grade” always means the most scalable possible architecture, regardless of actual current need.
  • Interviewer is testing: Whether the candidate can right-size engineering effort to current requirements.
  • Likely follow-up: “How would you know when it’s time to add the next tier of infrastructure?” → Monitoring actual traffic and latency/error trends (Module 12) against Section 2’s progression, proactively planning before hitting a wall, not reactively after an incident.

18. Scenario-Based Question

Scenario: TechCorp’s support assistant, originally built for internal use (roughly 200 requests/day), is approved for company-wide rollout, expected to reach 80,000 requests/day within a month. The current implementation holds each user’s conversation state in the application server’s in-memory session dictionary.

  • Problem Analysis: Section 3’s foundational requirement — the current stateful design cannot scale horizontally to handle the expected volume increase.
  • How to Think: This is an architectural migration that needs to happen BEFORE the traffic increase, not discovered as an outage during it.
  • Investigation: Confirm the current in-memory state can’t survive a request being routed to a different instance if more instances were simply added.
  • Root Cause: Stateful design incompatible with the horizontal scaling Section 2’s 100K/day tier requires.
  • Solution: Migrate conversation state to a shared, external store (Module 19) before adding additional application instances; then deploy horizontal scaling with a load balancer, queue for slow agent tasks, and connection pooling — exactly Section 8’s 100K/day tier requirements.
  • Trade-offs: The state migration requires real, upfront engineering time before the rollout can safely proceed — necessary given the alternative is a production outage or broken user sessions once traffic actually spikes.
  • Production Considerations: This scenario directly demonstrates why Section 12 emphasizes stateless design as a foundational choice — discovering this gap during an urgent rollout deadline is a far worse position than having built it in from the start.

19. Next Step

Next: Module 18 — Data Engineering for AI — Level 7 begins here: AI applications are data-dependent, and this module covers ingestion pipelines, validation, versioning, and governance for that data.

Author
TechByteByByte Editorial Team
Reviewed by
TechByteByByte Admin
Published
Last reviewed