Quick revision
The short answer
I would start with a relational database for persistence and an in-memory Java graph for queries. 2,000 services and tens of thousands of edges fit in a few megabytes, so BFS and DFS over adjacency maps answer blast radius and shortest path in microseconds, while an edge table with time windows and weights (call volume, error rate, latency) in PostgreSQL stores history and handles simple queries with recursive CTEs. A graph database earns its operational cost only when queries become deep, variable and ad hoc across a much larger graph, so we add one when that demand is clear.
What to remember
In this answer, we saw that a 2,000-service graph with tens of thousands of edges is small, so the interactive queries, blast radius as BFS over reversed edges and shortest path as BFS or Dijkstra, run fastest over an in-memory adjacency map rebuilt from the database. A relational edge table with time windows and weights such as call volume, error rate and latency stores history and serves one-hop, time-window and simple recursive queries. A graph database helps only when deep, variable, ad hoc queries over a much larger graph are in clear demand, so we start simple and add it when the signals appear.
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 start with a relational database for persistence and an in-memory Java graph for queries. 2,000 services and tens of thousands of edges fit in a few megabytes, so BFS and DFS over adjacency maps answer blast radius and shortest path in microseconds, while an edge table with time windows and weights (call volume, error rate, latency) in PostgreSQL stores history and handles simple queries with recursive CTEs. A graph database earns its operational cost only when queries become deep, variable and ad hoc across a much larger graph, so we add one when that demand is clear.
Why this question is asked
Service dependency graphs built from traces are becoming standard tooling for incident response and planning: when payments fails, which user journeys break, and how does a request get from the edge to the ledger? The storage choice is the kind of decision where teams over-engineer easily, because "graph data" sounds like it needs a graph database. Interviewers ask it to see whether we size the problem first and choose the simplest store that meets the queries. In this answer, we will size the graph, define the queries, design the in-memory model, add the relational store, consider when a graph database helps, and handle time windows and weights.
1Sizing the Graph First
2,000 services is a small graph by computing standards. Even if each service calls 15 others on average, that is 30,000 edges. In Java, an adjacency map from service id to a list of edge records costs perhaps a few hundred bytes per edge with its metadata, so the whole graph takes around 10 MB. A BFS over it visits at most 2,000 nodes and 30,000 edges, which takes well under a millisecond.
Size the history too. If we keep one snapshot of edges per hour for 90 days, that is 2,160 snapshots of 30,000 edges, about 65 million rows. That is ordinary for a relational database with a good index, and far too much to keep in memory as separate graphs. So the current graph is small and the history is medium-sized; those two facts drive the design.
2The Queries We Need to Answer
Blast radius asks: if service X fails, which services are affected? Because callers break when their dependencies break, that means following edges backwards, from X to its callers, to their callers, and so on: a reachability query on the reversed graph, usually with a depth limit and a filter on edge weight, since a caller that sends one request a day to X is barely affected.
Shortest call path asks: how does a request get from the API gateway to the ledger service? On an unweighted graph, BFS finds the path with the fewest hops. If we weight edges by latency or error rate, we want the path with the lowest total weight, which is Dijkstra's algorithm. Other likely queries are "what changed in X's dependencies since last week?" and "which services depend on this deprecated one?", which are mostly time-window comparisons and one-hop lookups.
| Query | Algorithm | Best served by |
|---|---|---|
| Blast radius of a failure | BFS on reversed edges, depth limit | in-memory graph |
| Shortest call path | BFS, or Dijkstra with weights | in-memory graph |
| Direct callers of a deprecated service | one-hop lookup | SQL on the edge table |
| What changed since last week | compare two time windows | SQL on the edge table |
| Ad hoc deep pattern queries | variable-length path matching | graph database, if demand appears |
3The In-Memory Graph in Java
The query service loads the current window's edges from the database every few minutes into immutable adjacency maps, one for callees and one for callers, and swaps them in through a volatile reference. Queries then run as plain BFS over the maps.
Notice that the visited check is built in: affected.add returns false for a service we have already reached, so cycles in the call graph, which do happen with callbacks and mutual calls, cannot make the search loop. Shortest path is the same BFS over callees, recording each node's predecessor so we can rebuild the path, or Dijkstra with a PriorityQueue when edges carry latency weights.
4The Relational Store for History and Simple Queries
The source of truth is an edge table in PostgreSQL: caller, callee, the window it covers, and the measured weights. Each aggregation run from the tracing pipeline inserts a new window of edges. Indexes on (callee, window_start) and (caller, window_start) make one-hop and time-window queries fast, and the table can be partitioned by month so old windows are cheap to drop.
Recursive CTEs cover simple traversals and are handy for ad hoc analysis, but they are slower and clumsier than BFS in Java for the interactive queries, which is why the query service keeps its in-memory copy. The database gives us durability, history, backups and familiar tooling without adding a new system.
5When a Graph Database Is Worth It
A graph database such as Neo4j stores nodes and edges natively and offers a query language for variable-length path patterns, for example "services reachable from the gateway within 6 hops through edges with error rate above 2 percent, excluding batch jobs". If many people need to write such deep, variable, ad hoc queries interactively, or the graph grows to include hosts, deployments, owners, data sets and libraries with millions of nodes, a graph database can make the work much easier.
Its cost is real, though: another system to run, secure, back up, monitor and keep in sync with the source of truth, plus a query language the team must learn. For 2,000 services and a known set of queries, it would add more operational work than it saves. So the decision is to start with the simplest stores that meet today's queries and record the signals that would change it: query patterns the in-memory service cannot express, analysts writing long recursive SQL every week, or a graph that no longer fits comfortably in memory.
- Does the current graph fit in memory?2,000 nodes, 30,000 edges: about 10 MBYesIn-memory BFS and DFS for queriesNoConsider a graph engine
- Need history and durability?YesEdge table in PostgreSQLNoMemory only
- Deep, ad hoc path queries in demand?YesAdd a graph database with a clear ownerNoStay with the simple stores
3 steps in order. Play them, or step through them one at a time.
6Time Windows and Weights
The call graph changes every day: deployments add and remove calls, traffic shifts, and nightly batch jobs create edges that do not exist during the day. An edge without a time window is misleading: a call that stopped a month ago would still appear in the blast radius. So every edge belongs to a window, and queries pick one: the last hour for incidents, the last 7 days for planning, with an edge counted if it carried traffic in that range.
Weights make answers useful. Call volume separates a critical dependency from a once-a-day health check, error rate shows which edges are already unhealthy, and latency weights let shortest-path queries find the slowest path rather than the fewest hops. Tracing is sampled, so volumes are estimates; we store the sampling rate or scaled counts so comparisons stay fair across services.
7Follow-up questions
How would you compute blast radius when edges have different criticality?
Filter or weight edges by how much the caller depends on the callee: for example, ignore edges below a call-volume threshold, or below a share of the caller's traffic, and stop at services that have a fallback for that dependency. The result can be ranked by user-facing traffic that passes through each affected service.
How fresh does the in-memory graph need to be?
For incident response, a few minutes behind is fine, since the graph is a map of normal behaviour. The query service reloads the latest window every few minutes and swaps the new graph in atomically, and it reports the window's timestamp with every answer so users know what they are looking at.
Would you ever skip the database and keep only memory?
Only for a prototype. Without a durable store, every restart loses history, and comparing this week with last week, which is one of the most valuable queries, becomes impossible. The edge table is cheap and keeps the in-memory graph rebuildable.
8Conclusion
In this answer, we saw that a 2,000-service graph with tens of thousands of edges is small, so the interactive queries, blast radius as BFS over reversed edges and shortest path as BFS or Dijkstra, run fastest over an in-memory adjacency map rebuilt from the database. A relational edge table with time windows and weights such as call volume, error rate and latency stores history and serves one-hop, time-window and simple recursive queries. A graph database helps only when deep, variable, ad hoc queries over a much larger graph are in clear demand, so we start simple and add it when the signals appear.
Open with
I would size it first. 2,000 services with maybe 15 callees each is about 30,000 edges, roughly 10 megabytes in Java, so the interactive queries are easiest as plain graph algorithms in memory: blast radius is a BFS over reversed edges, from the failed service to its callers and their callers, with a visited set, a depth limit and a call-volume threshold; shortest call path is BFS for fewest hops or Dijkstra if edges are weighted by latency.
Then, if they want more
For persistence I would use PostgreSQL with an edge table: caller, callee, window start, call volume, error rate and p99 latency, indexed by callee and window and partitioned by month. That gives history, so we can compare this week with last week, and simple queries, including recursive CTEs with a cycle guard for ad hoc use.
The query service reloads the latest window every few minutes and swaps an immutable graph in. Time windows and weights matter because the graph changes daily and a dead edge or a rare health check should not appear critical.
A graph database like Neo4j is worth its operational cost only when people need deep, variable, ad hoc path queries or the graph grows to millions of nodes, so I would start with the simple stores and add one when that demand is clear.
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 official documentation for the version you use. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
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 "Your company wants a dependency graph of 2,000 services built from tracing data, with...": https://scrivio-interview-library.netlify.app/java/algorithms-and-data-structures/582c89b7c4/