The duration of queries executed through the Neo4j driver is noticeably longer than executing them directly from the Neo4j browser.
- Query Execution time displayed in the Neo4j Browser
- Query Execution time using a Neo4j driver
- Contributing factors
- What is the approximate latency expected while using a Neo4j driver?
- Best practices to reduce response times
Query Execution time displayed in the Neo4j Browser
Let's consider an example involving the creation of two nodes and a relationship. Please note that the insights presented here apply to all Neo4j drivers, with Python being used as an illustrative example.
When the query is executed from the Neo4j browser, it takes approximately 82 milliseconds to complete.
Query Execution time using a Neo4j driver
Neo4j Python driver, utilizing the provided "create friendship" code from the connection instructions example available here:
In the case of the Python driver, the same operation takes slightly over 3 seconds to execute.
Several factors contribute to the disparity in query execution time:
The completion time displayed in the Neo4j browser represents the time the query takes to execute on the Neo4j server without accounting for any additional overhead.
The code will introduce additional overhead due to resource management, object initialization, and method invocation tasks.
In the Python code, the measured time includes the round-trips required for transaction management, which inherently introduces some delays.
Network latencies also play a significant role in the discrepancy, and they tend to increase with the geographic distance between the client and the Aura instance's region.
To clarify, the time displayed in the Neo4j browser solely pertains to the query execution time on the server itself.
In contrast, the time recorded while executing the query using a driver encompasses the following components:
- Query execution time on the server.
- The cumulative effect of round-trips required for transaction management.
- The time spent on streaming results.
- Additionally, the code will introduce some overhead for resource management, object initialization, and method invocation.
What is the approximate latency expected while using a Neo4j driver?
Since query execution and network latency are major contributing factors, we can estimate them using the following approach:
- Get the Query Execution time and call it queryExecTime. You can get this from your query logs or use the execution time displayed by the Neo4j Browser.
- Use netcat to check network time for a one-way trip:
From the client environment
nc -zv <dbid>.databases.neo4j.io 7687
ncat -zv <dbid>.databases.neo4j.io 7687
- roundTripTime = one-way X 2 (times 2)
- Overall response times we can expect:
- Ideal conditions, connection already established - 1 x roundTripTime + queryExecTime
- New connection, database name specified - 3 x roundTripTime + queryExecTime
- New connection, database name unspecified - 4 x roundTripTime + queryExecTime
(extra round trip for database discovery)
The above estimates consider only network times and do not include other factors (object creation, consuming results, etc.,).
Best practices to reduce response times
- Specify the database name (always 'neo4j' for Aura) in the session object. This will reduce one round trip for database discovery in new connections.
- Use traceroute to Identify the route and any additional, unexpected hops.
- If the one-way trip time for your driver is considerably high, it is advisable to run your applications from a server as close as possible to the Aura instance's region.
This approach will help minimize network latency.
- It is worth rewriting your application's logic to consume query results only when necessary, which may further optimize performance.
- If you wish to understand the 'Query execution time on the server' for your queries, you can get the query logs from the Aura console and inspect the elapsedTimeMs field - How To Download Query Log from Aura Instances
- For more comprehensive guidance on enhancing query performance, we recommend reviewing our article on Performance tuning with Neo4j AuraDB, which provides general query optimization techniques.