Home
Back to Overview

Parallel Processing & Performance

Process multiple requests simultaneously for better performance

Learn how to handle multiple conversations concurrently

The Performance Problem

❌ Sequential Processing (Slow)

Request 1 ────────→ Response 1 (2s)
↓
Request 2 ────────→ Response 2 (2s)
↓
Total: 4 seconds

✅ Parallel Processing (Fast)

Request 1 ────────→ Response 1 (2s) ──┐
(Both running at the same time)
Request 2 ────────→ Response 2 (2s) ──┘
Total: 2 seconds
2x speedup!

Core Concept: Context Sequences

Model (Loaded Once, Shared)

Neural network weights stored in memory

↓

Sequence 1

Independent Context
Conversation History A:
User: "Hello"
AI: "Hi there!"
User: "How are you?"
→ Processing independently

Sequence 2

Independent Context
Conversation History B:
User: "What's 2+2?"
AI: "4"
User: "Explain"
→ Processing independently

Key Insight: The model weights are loaded once and shared across all sequences. Each sequence maintains its own independent conversation history, allowing multiple conversations to run in parallel without interfering with each other.

Memory Efficient

Model weights (often 4-8GB+) are loaded once and shared. Only conversation histories are duplicated, which is much smaller (~few MB per sequence).

Independent History

Each sequence maintains its own conversation context. User A's conversation doesn't affect User B's, even though they share the same model.

Parallel Processing

Multiple sequences can process requests simultaneously. With GPU support, this provides true parallelism and significant performance improvements.

Same Model

All sequences use the same underlying model, ensuring consistent behavior and quality across all parallel conversations.

How Parallel Processing Works

Sequential Processing

Slow
await fn1(); // Wait 2s
await fn2(); // Wait 2s more
Total: 4 seconds
Functions run one after another

Parallel Processing

Fast
await Promise.all([
fn1(), // Start immediately
fn2() // Start immediately
]);
Total: 2 seconds
Functions run simultaneously
⚡ 2x speedup!

Execution Timeline

Time →
0s
1s
2s
3s
4s
Sequence 1
Processing...
Response 1 ✓
Sequence 2
Processing...
Response 2 ✓
Both complete at ~2s instead of 4s!
50% time reduction through parallelization

How It Works

Promise.all() starts all promises simultaneously and waits for all to complete. With multiple context sequences, each sequence processes independently while sharing the same model weights. This enables true parallel execution on GPUs, dramatically reducing total processing time.

GPU Batch Processing

Without Batching (Inefficient)

GPU: [Token 1] ... wait ...
GPU: [Token 2] ... wait ...
GPU: [Token 3] ... wait ...
GPU underutilized

With Batching (Efficient)

GPU: [Tokens 1-1024]
GPU fully utilized!

Trade-offs

Small Batch (128)

  • ✓ Lower memory
  • ✓ More flexible
  • ✗ Slower throughput

Large Batch (2048)

  • ✓ Better GPU utilization
  • ✓ Faster throughput
  • ✗ Higher memory usage

Sweet spot: Usually 512-1024 for consumer GPUs

Architecture Patterns

Different ways to structure parallel processing for LLM applications

Pattern 1: Multi-User Service

Multiple users share a pool of model instances. A load balancer distributes requests across available sequences.

User A
User B
User C
↓
Load Balancer
↓
Seq 1
Seq 2
Seq 3
↓
Shared Model

Pattern 2: Multi-Agent System

Different specialized agents work on the same task simultaneously, each using their own model instance.

Task
Planner
Critic
Executor
(All run in parallel)

Real-World Scenarios

Scenario 1: Chatbot Service

Challenge: 100 users, each waiting 2s per response
Sequential: 100 × 2s = 200s (3.3 minutes!)
Parallel (10 seq): 10 batches × 2s = 20s
10x speedup!

Scenario 2: Batch Analysis

Task: Analyze 1000 documents
Sequential: 1000 × 3s = 50 minutes
Parallel (8 seq): 125 batches × 3s = 6.25 minutes
8x speedup!

💻 Resource Requirements

Memory (RAM/VRAM)

  • 1 sequence: ~4-8GB
  • 4 sequences: ~8-16GB
  • 8 sequences: ~16-32GB

More sequences = more memory needed

GPU vs CPU

  • GPU: True parallel
  • CPU: Interleaved
  • GPU recommended for >2 sequences

When NOT to Parallelize

  • ✗ Dependent operations
  • ✗ Limited memory
  • ✗ Single user
  • ✗ Simple tasks

Limitations & Considerations

Context Capacity

Sequences share total context space. More sequences = less history per sequence

CPU vs GPU

With GPU: True parallel. CPU only: Interleaved processing (still helps!)

Not Always Faster

Only helps with independent requests, not dependent sequential steps

Key Takeaways

⚡

Parallelism is essential for production AI agent systems

🔄

Sequences share model but maintain independent state

📦

Promise.all enables concurrent JavaScript execution

💾

Memory is the limit - more sequences need more VRAM

Next: Streaming & Response Control