PATTERN Cited by 1 source
Client-side query rewriting¶
Client-side query rewriting is the practice of restructuring queries at the application layer to work around known inefficiencies in the database's query planner — rather than relying on the planner to optimize arbitrary query shapes.
Why it's needed¶
Graph database query planners (including JanusGraph's) may not optimize all Gremlin step combinations equally. Steps that look equivalent at the language level can produce vastly different execution plans — some falling back to slow, non-batched backend fetches.
Techniques (from Airbnb's identity graph)¶
-
Remove Path steps: Gremlin
PathandSimplePathsteps are not optimized as batched queries in JanusGraph. They fall back to slow, non-batched backend queries that occupy storage thread-pool connections. Replace with conditional queries that ensure acyclic results without path tracking. -
Restructure side-effect steps: Aggregation within side-effect steps may not be fully optimized by JanusGraph's query planning strategy, resulting in non-batched substeps. Modify to minimize computation within side-effect scope.
Trade-offs¶
- Pro: Can achieve dramatic latency improvements (Airbnb reports "huge improvement" in end-to-end read API latency).
- Con: Application code becomes coupled to engine-specific planner behavior — may need re-evaluation on engine upgrades.
- Con: Requires deep knowledge of the query planner's internals.
Seen in¶
- sources/2026-05-19-airbnb-scaling-identity-graph-unified-knowledge-graph-infrastructure — During the vendor-to-internal migration, identical Gremlin queries produced significantly different performance between engines. Client-side rewrites (removing Path steps, restructuring side-effect steps) were essential to achieving target latency on JanusGraph.
Related¶
- systems/janusgraph — the engine whose planner motivated these rewrites
- concepts/graph-traversal-fanout — high-fanout queries amplify planner inefficiencies
- patterns/vendor-to-internal-graph-migration — context where this pattern was applied