Datadog engineers migrated a critical production system, Stream Router, from an eventually consistent key-value store to PostgreSQL to overcome transaction size limits and improve performance. This migration involved a careful schema redesign and a test-driven refactoring process, significantly accelerated by AI tools like Claude and Cursor. Key architectural decisions included modularity, a comprehensive test suite, and a blue/green deployment strategy for a smooth transition.
Read original on InfoQ ArchitectureDatadog's Stream Router, an API for managing routing in its metrics pipeline, initially used an eventually consistent key-value (KV) database. As the routing table grew, this architecture faced severe limitations. The KV model required application code to reconstruct relational data, effectively making the application act as a relational database in-process. This led to thousands of sequential round trips, causing demanding operations to slow dramatically, sometimes taking up to 45 minutes, due to hitting transaction size limits and inefficient data retrieval.
To address the scaling issues, Datadog engineers redesigned the schema to explicitly reflect relationships between domain entities. This allowed them to leverage the capabilities of a relational database, using foreign keys to enforce relationships that were previously managed by complex application logic. This move from a KV model to a relational model (PostgreSQL) was a fundamental architectural shift aimed at improving data integrity, query efficiency, and overall performance.
System Design Lesson: The Power of Modularity and Testing
This case study highlights that strong architectural principles like modularity and comprehensive testing are not just good practices, but critical enablers for significant system migrations and refactoring efforts, especially when introducing new technologies or approaches like AI-assisted development. A well-tested, modular system is inherently more adaptable to change.
Post-migration, operations that once took 45 minutes now completed in approximately one second. Latencies dropped from hundreds of milliseconds to mere milliseconds. Data storage requirements decreased by up to 40x, and database costs were reduced by 90% due to more efficient handling of relationships and optimized query execution in PostgreSQL and DuckDB. CPU and memory usage also saw significant reductions, demonstrating the profound impact of choosing the right data model and database for the workload.
AI tools like Claude and Cursor were used to accelerate the code refactoring process. Engineers provided the AI with the old implementation, the new schema, and a failing test, allowing the models to generate initial code passes. While effective for repetitive refactoring, the AI struggled with niche optimizations like batching, `UNNEST` tricks, or common table expressions, often generating correct but suboptimal queries. Human engineers had to intervene to optimize these, after which the models could replicate the pattern.
This case study illustrates a common challenge with AI code generation: while it can automate boilerplate and straightforward transformations, deep architectural optimizations and understanding of system-specific performance nuances often still require human expertise. AI is a powerful assistant but not yet a complete replacement for a seasoned engineer's insight into complex system interactions and performance characteristics.