Case study boundary
Use this as a learning model.
This page explains architectural patterns and constraints inspired by Gmail. It should not be read as the exact private production architecture or as a one-size-fits-all recommendation.
Evidence
Public info + inference
Learn first
Concepts behind this architecture
Search Index
Precompute searchable structures so users can find data quickly.
API Gateway
A controlled front door for authentication, routing, rate limits, and API policy.
Bloom Filter
Quickly test whether an item is definitely absent or maybe present.
GPU Inference
Run trained models efficiently enough to serve user requests in real time.
Overview
Gmail is the world’s largest email service with 1.8B+ users, processing and storing hundreds of billions of emails. Its architecture must handle high-throughput email delivery (receiving and sending), massive full-text search across years of email history, real-time spam and phishing detection that blocks 99.9%+ of threats, and smart features like auto-categorization, Smart Reply, and Smart Compose. Gmail pioneered the “search, don’t sort” approach to email with its label-based organization and conversation threading. Built on Google’s Bigtable and Spanner infrastructure, Gmail provides strong consistency and global availability.
Scale
1.8B+
Users
300B+
Emails sent/received per day
~15B
Spam blocked per day
15 GB
Storage per free account
Requirements
Functional
- Send and receive emails with attachments
- Conversation threading and label-based organization
- Full-text search across all emails
- Spam, phishing, and malware detection
- Auto-categorization (Primary, Social, Promotions, Updates)
- Smart Reply and Smart Compose suggestions
- Calendar and Drive integration
Non-Functional
- Email delivery latency <5 seconds for 99th percentile
- Spam detection accuracy >99.9%
- Search results in <500ms across years of email
- High availability (99.99%+ uptime)
- Strong consistency — no lost or duplicated emails
- Compliance: encryption in transit, DLP, retention policies
High-Level Architecture
Key Components
SMTP / MX Service
Receives inbound emails via SMTP from other mail servers. Google operates MX (Mail Exchange) servers at multiple locations worldwide. Handles TLS encryption, DKIM/SPF/DMARC validation, and initial connection-level spam filtering (IP reputation).
Spam Filter
Multi-layer spam and phishing detection that blocks 99.9%+ of threats. Combines rule-based filters, ML classifiers (trained on user reports), URL reputation scanning, attachment sandboxing, and real-time threat intelligence. Processes every inbound and outbound email.
Delivery Service
Routes emails to the correct user’s mailbox. Handles conversation threading (grouping replies into threads), label application, and triggers for filters/rules. Writes email content to Bigtable and metadata to Spanner.
Search Service
Full-text search across all of a user’s emails using an inverted index. Supports operators like from:, to:, subject:, has:attachment, before:, after:, label:, is:starred. Index is updated in near real-time as new emails arrive.
Label & Category Service
Manages Gmail’s label system (labels replace folders — an email can have multiple labels). Auto-categorization ML model classifies emails into Primary, Social, Promotions, and Updates tabs based on sender patterns and content.
Smart Features
AI-powered features including Smart Reply (suggesting short responses), Smart Compose (autocomplete while typing), nudges (reminders about emails needing a response), and important-first inbox prioritization.
Notification Service
Delivers push notifications to mobile devices and manages email notification settings. Batches notifications for less important categories and respects quiet hours.
Data Model
Spam Detection Pipeline
Gmail blocks ~15 billion spam emails per day with a 99.9%+ detection rate — one of the most effective spam filters ever built.
Multi-layer approach:
- Connection-level: IP reputation, SPF/DKIM/DMARC validation, rate limiting. Rejects obvious spam before the email body is even received.
- Content analysis: ML classifiers analyze email headers, body text, URLs, and attachments. Models are trained on billions of user-reported spam signals.
- URL scanning: Every URL in every email is checked against Google’s Safe Browsing database. Suspicious URLs are sandboxed — clicked links go through a Google redirect that re-checks the destination in real time.
- Attachment scanning: Attachments are scanned for malware using multiple antivirus engines and sandboxed execution. Encrypted attachments and password-protected ZIPs receive extra scrutiny.
- Behavioral signals: New sender patterns, bulk sending characteristics, and engagement rates (do users delete without reading?) feed into the spam model.
Adversarial training: Spammers constantly evolve tactics. Gmail’s models are retrained daily on fresh data, and the system can deploy emergency rules within minutes of detecting a new spam campaign.
Bigtable Email Storage
Gmail stores email content in Bigtable, Google’s distributed wide-column store, which provides the performance characteristics email needs.
Data model: Each user’s emails are stored in a single Bigtable row range, sorted by a key combining user_id and reverse timestamp. This means the most recent emails are physically stored together, making inbox loading a single sequential read.
Column families: Email data is split into column families — headers (small, frequently read), body (larger, read on demand), and attachments (large blobs stored separately with references). This allows loading the inbox (headers only) without reading full email bodies.
Compaction and storage: Bigtable’s LSM-tree storage engine handles Gmail’s write-heavy workload (billions of new emails daily) efficiently. Background compaction merges sorted runs and reclaims space from deleted emails.
Metadata in Spanner: While email content lives in Bigtable, structured metadata (labels, thread relationships, search index pointers) lives in Spanner for its strong consistency and SQL query capabilities. A label change needs to be instantly visible across all devices — Spanner’s external consistency guarantees this.
Smart Reply and Smart Compose
Gmail’s Smart Reply suggests three short response options, while Smart Compose autocompletes sentences as users type. Both are on-device and server-side ML features.
Smart Reply: A sequence-to-sequence model reads the incoming email and generates three diverse response suggestions (e.g., “Thanks!”, “Sounds good, I’ll be there.”, “Let me check and get back to you.”). The model is trained on anonymized response patterns — not on reading users’ actual emails. A diversity mechanism ensures the three suggestions cover different intents.
Smart Compose: As the user types a reply, a language model predicts the next few words and displays them as gray text. The model conditions on the email being replied to (for context) and the user’s typing so far. Pressing Tab accepts the suggestion.
On-device inference: Both features run a lightweight model on-device for privacy (the email content doesn’t leave the device for these features). A larger server-side model provides higher-quality suggestions when available.
Personalization: The models adapt to individual writing style over time — learning common phrases, greetings, and sign-offs that a specific user tends to write.
Architectural Tradeoffs
Labels over folders
Pros
- +An email can have multiple labels — more flexible than folders
- +Search-centric approach reduces need for manual organization
- +Enables auto-categorization without moving emails
Cons
- −Unfamiliar to users coming from folder-based email
- −Can lead to label proliferation
- −IMAP mapping to labels is imperfect
Bigtable + Spanner over single database
Pros
- +Bigtable optimized for large sequential reads (email loading)
- +Spanner provides strong consistency for metadata operations
- +Each optimized for its access pattern
Cons
- −Two systems to maintain and keep in sync
- −Cross-system transactions are complex
- −Operational overhead of running both
Aggressive spam filtering with ML
Pros
- +99.9%+ spam detection rate
- +Adapts to new spam techniques automatically
- +Protects users from phishing and malware
Cons
- −False positives — legitimate emails occasionally marked as spam
- −ML models are opaque — hard to explain why an email was filtered
- −Constant retraining required to stay ahead of adversaries
Learn From Gmail
Copy a learning context prompt for an AI assistant. It keeps assumptions visible and asks for tradeoffs, cost drivers, failure modes, and what to measure before adding complexity.