How I Optimized a Customer Identity Resolution System in Rust
How I made a Rust identity resolution engine 92% faster and 78% lighter on memory, and recovered 380K+ website visitors that were previously unidentifiable.
The context
During my internship as a data engineer, I worked at a company that builds data products for e-commerce brands. One of its goals was to personalize marketing emails. Personalization matters because, according to McKinsey, it can increase marketing ROI by 10 to 30%.
To personalize an email, you first need to know who the visitor is and what they did on the website. That is the job of customer identity resolution: turning anonymous traffic into people you can actually reach.
I did not build this system from scratch. My task was to optimize it.
What identity resolution does
Every event on an e-commerce website (a page view, an add to cart, a purchase) carries a few identifiers, which I call keys. Most of them tell you nothing about who the visitor is. They only say “this looks like the same visitor as before.”
I split keys into two types:
- Non-addressable keys, such as Shopify identifiers. They help link events together, but you cannot contact anyone with them.
- Addressable keys, such as an email, a hashed email or a phone number. These are the real target, because they let you reach a person.
The system links keys until it reaches an addressable one. At that point, everything the visitor did on the site can be tied to a real person.

How the system thinks about it
The system treats this as a graph. Each node is one key. Two events belong to the same person when they share a key. It works like combining sets: set one joined with set two gives set three, which holds everything both contained.
As more events arrive, small groups of keys merge into bigger ones, until each visitor ends up in one group.

The problem
The original engine used BFS (breadth-first search). To resolve identities, it walked the graph node by node, keeping a queue of everything left to visit.
This worked for small clients. At scale it did not. Some clients generate around 50 million events per day, and for them:
- processing took about one hour
- the job sometimes failed with an out-of-memory error
When the job failed, identity resolution stopped. No identity meant no personalization, and that meant lost ROI. This was a business problem, not only a technical one.
The solution: union-find (DSU)
I replaced BFS with DSU (Disjoint Set Union), also called union-find, and rewrote the engine in Rust.
The difference is simple:
- BFS walks the graph again and again, and holds a growing queue in memory.
- DSU does not walk anything. Each key points to a representative of its group. When two keys turn out to belong to the same person, the two groups are merged in a single fast step.
For this problem, merging groups is exactly what we need, so DSU fits it better than BFS.

How I tested it
I took a sample of two days of real events, about 5.7 million, and ran both the BFS and DSU versions on it several times to make sure the results were stable.
| BFS | DSU in Rust | |
|---|---|---|
| Runtime | baseline | 92% lower |
| Memory usage | baseline | 78% lower |

Rolling it out safely
I did not switch everything at once. I first deployed the new engine with a canary deployment on one client. The results held, and it also recovered 380K+ previously unidentifiable visitors. After validating it, we deployed DSU for all clients.
What I learned
The right data structure can matter more than any amount of tuning. BFS was not wrong, but it was built for traversing, and this problem is about merging. Once the algorithm matched the problem, the memory crashes and the long runtimes went away.
A second lesson was to prove the result before trusting it: benchmark on real data, then use a canary deployment before rolling out.
Comments