Design a Neural Machine Translation system

    A neural machine translation service translates text across many language pairs using encoder-decoder Transformers, served at low latency with caching and batching.

    AI & Machine Learning, 6 components, about 30 min

    1. Scope

    In scope

    • • Translate text between languages
    • • Many-to-many language support
    • • Low-latency serving
    • • Caching frequent translations

    Out of scope

    • • Training NMT models
    • • Speech translation
    • • Document layout preservation

    2. Functional requirements

    Must have

    • • Translate a sentence accurately
    • • Support many language pairs
    • • Fast response
    • • Handle sub-word/unknown tokens

    Nice to have

    • • Auto-detect source language
    • • Alternative translations
    • • Formality control

    3. Non-functional requirements

    Latency< 200 ms
    QualityHigh BLEU across pairs
    ThroughputHigh via batching
    Coverage100+ languages

    4. Back-of-envelope estimation

    Assumptions

    • • Shared multilingual model
    • • BPE tokenization
    • • Batch similar-length sentences
    • • Cache popular phrases

    Derived numbers

    Cache hit rateHigh for common phrases
    Batch efficiencyLength-bucketed batching
    ModelShared vocab, multilingual

    5. API design

    POST/api/translate

    { text, source?, target } → translation

    6. High-level design

    What each box in the reference diagram is responsible for:

    Translation API — Tokenize, check cache, route to model, detokenize.
    Transformer Encoder — Encodes source into contextual representations.
    Transformer Decoder — Generates target tokens autoregressively.
    Lang Dictionary — Vocab/BPE merges, fallback handling.
    Translation Cache — Caches frequent source→target pairs.

    7. Data model

    Cache: hash(text+pair) → translation

    8. Deep dives

    Sub-word tokenization (BPE)

    Languages have huge, open vocabularies and rare words. Byte-Pair Encoding splits text into sub-word units so any word (including unseen ones) is represented as a sequence of known pieces. A shared sub-word vocabulary across languages enables cross-lingual transfer in a single multilingual model.

    Encoder-decoder Transformer

    The encoder reads the full source sentence with self-attention (capturing long-range dependencies); the decoder generates the target one token at a time, attending to both the encoder output and previously generated tokens. Self-attention is what makes Transformers handle word-order and context far better than older RNN/phrase-based systems.

    Batching for throughput

    Autoregressive decoding underuses the accelerator on single requests. Group incoming sentences of similar length into batches (length bucketing) to pad minimally and maximize GPU/TPU utilization — big throughput gains at a small latency cost.

    Caching

    Many translations repeat (UI strings, common phrases). A cache keyed by (text, language pair) serves these instantly, offloading the model and cutting cost.

    9. Bottlenecks

    • Decoding latency → batching, quantization, accelerators
    • Rare/long sentences → BPE + length handling
    • Many language pairs → single shared multilingual model

    10. Trade-offs

    One multilingual model or many pairs

    Choose: One multilingual model with a target-language token.

    Instead of: A model per pair: N² models.

    Shared models transfer from high-resource to low-resource languages.

    Caching

    Choose: Cache translations for frequent short strings.

    Instead of: Translate everything fresh.

    Short phrases repeat a lot.

    Latency versus quality

    Choose: Smaller distilled models for interactive use, larger for document translation.

    Instead of: One size for all.

    Different contexts tolerate different latency.

    11. Failure modes

    Hallucinated content in translation

    Meaning changed silently.

    Mitigation: Quality estimation models, length-ratio checks, and fallbacks.

    Low-resource language quality poor

    Misleading translations.

    Mitigation: Back-translation for data, show confidence, and gather feedback.

    Batching mixes very long and short inputs

    Wasted compute and latency.

    Mitigation: Bucket batches by length.

    12. What interviewers probe

    Answer each one yourself before opening it.

    How do you evaluate translation quality?Show a strong answer

    Automatic metrics (BLEU, chrF, learned metrics like COMET) plus human evaluation for important pairs.

    How do you handle long documents?Show a strong answer

    Segment by sentence or paragraph with context windows, preserving formatting markup.

    Where does tokenization matter?Show a strong answer

    Shared subword vocabularies (BPE or SentencePiece) handle many scripts; vocabulary size trades sequence length against model size.

    How would you add speech translation?Show a strong answer

    Cascade speech recognition → translation → speech synthesis, or end-to-end speech models for lower latency.

    Sign in to track this guide in your curriculum progress.