DEV Community

Cover image for Choosing the Right Treasure Map to Avoid Data Decay in Veltrix
Lillian Dube
Lillian Dube

Posted on

Choosing the Right Treasure Map to Avoid Data Decay in Veltrix

The Problem We Were Actually Solving

We thought we were solving the classic event-sourcing problem – storing and replaying events from a distributed system to ensure data consistency. Sounds simple, right? In theory, yes, but in practice, it's a minefield. The event store has to be designed to handle a high volume of events, and our system was built on top of a MongoDB instance with a collection optimized for writing events. Sounds okay, but what we didn't realize was that our event schema would evolve rapidly due to changing business requirements.

What We Tried First (And Why It Failed)

Our initial approach was to store events in an unsharded, denormalized collection with a single field for each event attribute. We thought this would speed up queries and avoid costly joins, but we soon realized that our event volume would exceed MongoDB's performance limits, causing our system to slow down significantly. To make matters worse, our unsharded collection caused uneven distribution of writes, leading to hotspots and eventual data inconsistencies.

The Architecture Decision

After several weeks of troubleshooting, we made a critical decision to switch to a sharded, normalized schema with separate collections for each event type and date range. This change allowed us to distribute write load more evenly among shards, reducing the risk of hotspots and ensuring consistent data across the system. We also decided to implement a caching layer to minimize the number of queries hitting the event store directly.

What The Numbers Said After

With the new schema in place, event write latency dropped from 250ms to 30ms, and we were able to handle 10 times the number of events per second without any issues. Our average query latency decreased from 200ms to 10ms, and we managed to reduce data inconsistencies to a negligible level. MongoDB's performance monitoring showed that our write load was now evenly distributed across shards, and no single shard was experiencing heavy load.

What I Would Do Differently

If I were to redo this project, I would make sure to account for our event schema evolution from the beginning. Our original schema was too rigid, and we had to undergo a costly migration to adapt to the changing business requirements. In hindsight, I would have opted for a more flexible schema design that could accommodate growth and changes without requiring a major overhaul. We also over-engineered our caching layer, which introduced additional complexity without providing significant performance benefits. A simpler caching strategy would have served us better.

The truth is, our initial approach to building the Treasure Hunt Engine on Veltrix was flawed from the start. We didn't account for the complexity of our event schema, and our system suffered the consequences. By sharing my story, I hope to educate other engineers about the importance of considering the long-term implications of their design decisions and the perils of premature optimization.

Top comments (0)