Quick revision
The short answer
I would model accounts, devices, phone numbers and addresses as nodes with usage edges, so finding a ring becomes a bounded traversal of up to three account-to-account links from a new event. A graph database or graph index answers such neighbourhood queries in milliseconds, provided we cap or exclude super nodes such as a shared office address, which would otherwise explode the traversal. PostgreSQL stays the source of truth and feeds the graph through change data capture, batch jobs precompute connected components and risk scores, real-time traversal runs only for new events, and we measure precision and latency on a pilot before committing to a new database.
What to remember
In this answer, we saw how to approach near real-time ring detection. Accounts and the identifiers they use become nodes and edges, so a ring is a bounded neighbourhood query of up to three account links. A graph database answers it in milliseconds when we bound the depth and cap or exclude super nodes, PostgreSQL remains the source of truth feeding the graph through change data capture, batch jobs precompute components and risk scores while live traversals run only for new events, and a pilot measuring precision, recall and latency decides whether the new database is worth it.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
I would model accounts, devices, phone numbers and addresses as nodes with usage edges, so finding a ring becomes a bounded traversal of up to three account-to-account links from a new event. A graph database or graph index answers such neighbourhood queries in milliseconds, provided we cap or exclude super nodes such as a shared office address, which would otherwise explode the traversal. PostgreSQL stays the source of truth and feeds the graph through change data capture, batch jobs precompute connected components and risk scores, real-time traversal runs only for new events, and we measure precision and latency on a pilot before committing to a new database.
Why this question is asked
Fraud rings hide in plain sight: each account looks normal, but dozens of them share the same two phones and a handful of devices. Finding that pattern means walking relationships, and SQL is not good at walking. A recursive query that takes minutes is fine for an investigator's report but useless for blocking a payment as it happens. Interviewers ask this scenario to see how we would bring graph techniques in without throwing away the systems that work, and whether we know the traps, especially super nodes. In this answer, we will model the problem as a graph, see why the recursive SQL is slow, write the bounded traversal, deal with super nodes, design the data flow from PostgreSQL, split the work between batch and real time, and plan how to measure success.
1Modelling the Problem as a Graph
A fraud ring is a group of accounts linked by shared identifiers. We model each account, device fingerprint, phone number, card fingerprint and normalised address as a node, and each use of an identifier as an edge: (account)-[:USES]->(device). Two accounts are linked when they use the same identifier, so account to device to account is one link between accounts, and 'within 3 hops' means up to three such links, which is six relationships in the graph.
This model turns the question into a neighbourhood query: starting from the account in a new event, which other accounts can we reach in up to three links, and how many of them are already known fraud?
| Node | Example | Edge to account |
|---|---|---|
| Account | acc-1001 | - |
| Device | fingerprint d-77 | USES (first_seen, last_seen) |
| Phone | +1 555 0100 (hashed) | USES |
| Address | normalised street address | USES |
| Card | card fingerprint, never the number | USES |
2Why the Recursive SQL Takes Minutes
In PostgreSQL the same data might be an account_identifier table with account_id, kind and value. A recursive CTE starts from one account, joins to all accounts sharing an identifier, then repeats for each of those. Each level multiplies the rows: if each account reaches 10 others, three levels produce 1,000 paths, but if one identifier is shared by 5,000 accounts, the next level has millions of rows. Recursive CTEs also revisit the same accounts through different paths unless we track visited nodes, which itself is expensive in SQL, and the planner cannot stop early when it has found enough. So the query time depends on the worst identifier it happens to touch.
3The Bounded Traversal in a Graph Database
In a graph database such as Neo4j, the traversal follows stored relationships, so its cost depends on the edges actually walked. We bound both the depth and the work.
Notice the two limits. The relationship range *2..6 stops after three account links, and the super node filter refuses to walk through identifiers we have marked as too widely shared. With those limits a typical query touches a few hundred nodes and returns in milliseconds. Variable-length patterns without a bound, or without the super node rule, are how graph queries become as slow as the SQL they replaced.
4Handling Super Nodes
A super node is a node with a huge number of relationships. In fraud data they are often innocent: a shared office address where 3,000 employees registered, a public Wi-Fi device fingerprint, a default phone number a form fills in, or a payment provider's test card. Traversing through one connects thousands of unrelated accounts, which both explodes the query and destroys precision, because everyone looks like a ring.
We handle them in three ways. A degree cap marks any identifier with more than, say, 50 accounts as a super node, computed by a batch job and updated as data arrives. An allow list removes known shared identifiers such as office addresses of business customers. And super nodes can still be used as a separate, weaker signal, for example 'shares an address with 3,000 accounts', instead of being walked through.
5Keeping PostgreSQL as the Source of Truth
We do not move account data into the graph database. PostgreSQL remains the system of record, and the graph is a derived index for relationship queries. Change data capture reads PostgreSQL's write-ahead log, for example with Debezium, and publishes each insert and update on the identifier tables to Kafka. A small loader consumes those events and creates or updates nodes and edges with idempotent MERGE statements, so replaying events is safe. If the graph is ever lost or corrupted, we rebuild it from PostgreSQL.
The lag of this pipeline, typically seconds, matters for real-time scoring: a device linked one second ago may not be in the graph yet. So the scoring service also passes the identifiers from the current event directly into the query, rather than relying only on what the graph already knows.
7 messages in order. Play them, or step through them one at a time.
6Splitting Work Between Batch and Real Time
Not every question needs a live traversal. A nightly batch job can compute connected components over the whole graph, groups of accounts that are linked at any distance, using a graph algorithm library or a batch framework, and store a component id and a risk score on each account. The online path can then read an account's precomputed score in one lookup.
Real-time traversal is reserved for new events: a signup, a new device on an existing account, or a payment over a threshold. That keeps the online load small and predictable, and the batch scores catch slow-forming rings that individual events would miss.
7Measuring Before Committing
A new database is a long-term commitment, so we prove the value first with a pilot. We load a few months of historical data, replay real events, and measure two kinds of numbers. Quality: of the accounts the traversal flags, how many were really fraud (precision), and of the known fraud cases, how many it would have caught (recall), compared with the current rules. Speed: p99 latency of the traversal under the expected event rate. If precision is too low, tuning the super node threshold and the depth usually matters more than the database. Only with these results do we decide between a graph database, a graph index on top of existing stores, or improved SQL with precomputed links.
8Follow-up questions
Could we make the PostgreSQL query fast enough instead?
Sometimes. Precomputing an account_link table of direct account-to-account links, excluding super nodes, turns three levels into three indexed joins on a much smaller table, and cycle tracking with arrays limits revisits. For moderate data and depth that can reach sub-second times. It becomes hard to maintain as more identifier types and depths are added, which is where a graph store helps.
What happens when an account is cleared after investigation?
We record the outcome as a property or relationship, such as a cleared case, so later scoring can discount links to accounts that were reviewed and found legitimate.
9Conclusion
In this answer, we saw how to approach near real-time ring detection. Accounts and the identifiers they use become nodes and edges, so a ring is a bounded neighbourhood query of up to three account links. A graph database answers it in milliseconds when we bound the depth and cap or exclude super nodes, PostgreSQL remains the source of truth feeding the graph through change data capture, batch jobs precompute components and risk scores while live traversals run only for new events, and a pilot measuring precision, recall and latency decides whether the new database is worth it.
Open with
I would model accounts, devices, phone numbers, addresses and cards as nodes, with a USES edge from each account to each identifier, so two accounts that share a device are two relationships apart and three hops between accounts is a bounded traversal of up to six relationships.
Then, if they want more
The recursive SQL is slow because each level multiplies rows and one widely shared identifier can produce millions of paths, without early stopping.
A graph database follows stored relationships, so a bounded neighbourhood query from a new account takes milliseconds, but only if we deal with super nodes: an office address or public device shared by thousands of accounts would connect everyone. I would mark identifiers above a degree threshold, say 50 accounts, as super nodes and not traverse through them.
PostgreSQL stays the source of truth; Debezium change data capture feeds Kafka and a loader merges nodes and edges into the graph, which we can always rebuild. A nightly batch computes connected components and risk scores, and the live traversal runs only for new events like signups or new devices.
Before committing to a new database, I would run a pilot on historical data and measure precision, recall and p99 latency against the current rules.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Redis documentation, MongoDB manual
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "A fraud team wants to detect rings of accounts that share devices, phone numbers or...": https://scrivio-interview-library.netlify.app/databases/nosql-fundamentals/2cec1c6ed5/