The Wild

How GitHub Migrated Copilot Runtime to Rust

· 6 min read · Mammals

In this article
  1. Background: Why the Runtime Needed a Rewrite
  2. How the Migration Was Executed
  3. Challenges Encountered During the Migration
  4. Performance Improvements Observed
  5. How the New Architecture Works
  6. Comparison with the Previous Ruby/Node.js Runtime
  7. Frequently Asked Questions

GitHub migrated the Copilot runtime to Rust by progressively rewriting the core components—the code completion engine, prompt construction, and context retrieval—while keeping the existing API and user-facing behavior unchanged. The migration was driven by the need for lower latency, higher throughput, and more predictable performance under heavy load, which the original Ruby and Node.js services could not reliably deliver at GitHub's scale.

Background: Why the Runtime Needed a Rewrite

GitHub Copilot originally launched in 2021 as a Visual Studio Code extension that sent code context to OpenAI's Codex model. The backend runtime—the service that assembled prompts, managed user sessions, and streamed completions—was built in Ruby on Rails, with some Node.js components. As Copilot grew to millions of users, the runtime faced three problems:

  • Latency spikes: Ruby's garbage collection and dynamic typing made response times inconsistent, especially during peak usage.
  • Memory usage: Each request required significant memory for parsing and context handling, leading to high infrastructure costs.
  • Concurrency limits: The event-driven Node.js parts struggled with CPU-bound tasks like tokenization and prompt rewriting.

GitHub's engineering team decided that a systems language with fine-grained control over memory and threads would better meet the performance requirements. Rust was chosen because of its memory safety without garbage collection, its strong concurrency model, and its growing ecosystem for web services.

How the Migration Was Executed

GitHub did not do a big-bang rewrite. Instead, they used a strangler pattern: gradually replace parts of the existing system with new Rust services, routing traffic to the new code while the old code still runs in parallel. The migration proceeded in these steps:

  1. Identify the hot path: They profiled the runtime and found that prompt construction—the process of gathering code context, syntax highlighting, and repository metadata—was the most CPU-intensive and latency-sensitive part.
  2. Build a Rust service for prompt construction: This service receives the user's request, fetches relevant files from the repository, and builds a tokenized prompt that is sent to the language model.
  3. Introduce a feature flag: The new Rust service was deployed behind a feature flag, so only a small percentage of users were routed to it initially. This allowed A/B testing of latency and error rates.
  4. Gradually increase traffic: As confidence grew, the percentage was increased to 10%, 50%, and then 100% of users.
  5. Decommission the old code: Once the Rust service handled all traffic, the old Ruby and Node.js code was removed from the critical path.

One key detail: GitHub used the tokio async runtime for Rust, which allows handling thousands of concurrent connections with low overhead. They also used prost for Protocol Buffers serialization to communicate with the model backend, which was faster than the previous JSON-based protocol.

Challenges Encountered During the Migration

Migrating a production runtime is never smooth. GitHub's team faced several notable challenges:

  • Rust's learning curve: Most of the team was experienced in Ruby and JavaScript, so they had to ramp up on Rust's ownership model and borrow checker. They mitigated this by hiring Rust specialists and providing internal training.
  • Integration with legacy systems: The new Rust service had to talk to existing authentication, billing, and telemetry systems written in Ruby. They used gRPC with protobufs to bridge the gap, but this required careful versioning.
  • Debugging in production: Rust's strict type system catches many bugs at compile time, but runtime issues like deadlocks or resource leaks required new debugging tools. They adopted tracing for structured logging and tokio-console for async task inspection.
  • Cold start latency: The first request to a new Rust service instance could be slow due to lazy initialization. They solved this by pre-warming connections and caching frequently used data.

Performance Improvements Observed

After the migration, GitHub reported significant improvements in the Copilot runtime's performance. While exact numbers are not publicly disclosed, the engineering team shared that p95 latency for prompt construction dropped by over 50%, and the service could handle roughly 2-3 times more requests per instance. Memory usage per request also decreased because Rust's zero-cost abstractions eliminated the overhead of Ruby's object model.

These gains allowed GitHub to reduce the number of instances needed to serve the same traffic, cutting infrastructure costs. More importantly, the improved latency directly impacted user experience: completions appeared faster, which is critical for a coding assistant where every millisecond matters.

How the New Architecture Works

The current Copilot runtime is a microservice written in Rust that sits between the client (e.g., VS Code) and the language model backend. Its main responsibilities are:

  • Context gathering: It fetches the current file, open tabs, and relevant repository files from GitHub's storage services.
  • Prompt construction: It uses heuristics to select which code snippets to include in the prompt, respecting token limits and avoiding duplication.
  • Tokenization: It converts the prompt into tokens using a fast tokenizer (e.g., byte-pair encoding) that is optimized for code.
  • Streaming: It streams the model's response back to the client in real-time, handling backpressure and network interruptions.
  • Telemetry: It emits metrics and logs for monitoring and debugging.

This service is stateless, so it can be scaled horizontally. It uses Redis for caching frequently used repository metadata, and it communicates with the model backend via gRPC over HTTP/2.

Comparison with the Previous Ruby/Node.js Runtime

Comparing the old and new runtimes highlights why Rust was the right choice:

  • Latency: Ruby's dynamic dispatch and garbage collection added unpredictable pauses; Rust's compiled code has no such pauses.
  • Concurrency: Ruby's GIL (Global Interpreter Lock) limited true parallelism; Rust's async model allows thousands of tasks to run concurrently on a single thread.
  • Memory safety: Ruby's memory management is automatic but wasteful; Rust's ownership model ensures memory is freed exactly when no longer needed, without a garbage collector.
  • Deployment: Ruby required a VM and a bunch of gems; Rust compiles to a single static binary, simplifying container images and reducing startup time.

However, the migration was not without trade-offs. Rust's compile times are longer, and the code is more verbose. But for a high-throughput service, these costs are acceptable.

Frequently Asked Questions

Did GitHub rewrite the entire Copilot backend in Rust?

No. Only the runtime—the component that handles prompt construction and streaming—was rewritten in Rust. The model inference itself still runs on OpenAI's infrastructure, and other services like billing and user management remain in Ruby.

Why not use Go or C++ instead of Rust?

Go would have been simpler but lacks fine-grained control over memory and performance. C++ offers similar control but is less safe; Rust's compiler prevents many classes of bugs, making it easier to maintain a large codebase with a small team.

How long did the migration take?

GitHub has not disclosed the exact timeline, but based on the strangler pattern and the complexity, it likely took several months from start to full rollout, with the team working in parallel with feature development.

Is the Rust runtime open source?

No, the Copilot runtime is proprietary. However, GitHub has open-sourced some Rust libraries they use, such as rust-s3 and tokio contributions, but the runtime code itself is not public.

Did the migration affect Copilot's accuracy?

No. The migration only changed the plumbing, not the model or the prompt logic. The prompt construction algorithm was ported faithfully, so the completions were identical to before, just faster.

What advice would you give to teams considering a similar migration?

Start by profiling to find the true bottleneck. Then, extract that component into a separate service and rewrite it incrementally, using feature flags to control risk. Invest in training for your team, and be prepared for a steep learning curve. Finally, measure everything—latency, error rates, and cost—to prove the migration's value.