Hi @lewisl sorry for the late response. I’ve been out-of-office then sick then taking some time to run some tests of my own on this latency thing.
Can you run your performance test again, but this time run multiple requests, serially, in the same script? Then compare the duration for the first request to the subsequent ones.
For me, in Ohio, I am recording initial requests about 250-400ms longer than subsequent requests.
TLS handshakes can add 100ms or more to network latency
This is not unique to Fauna, but is common to any connection over HTTPS. A TLS handshake introduces a couple more round-trips across the network and cryptographic compute before you and the server even agree to swap data.
It scales with the network latency a lot since there are so-many trips required.
The drivers reuse connections whenever possible
Subsequent requests may show lower latency, because the clients can reuse the TLS connection once established.
Test script
here is a test script that I ran
const run = async () => {
const execute = async (i) => {
const start = performance.now()
let queryTime = null
await fauna.query(
q.Get(q.Ref(q.Collection("users"), "319254125213646912")),
{
observer: (response) =>
(queryTime = parseInt(
response.responseHeaders["x-query-time"]
)),
}
)
return {
queryTime,
duration: performance.now() - start,
}
}
try {
const initial = await execute()
console.log('Initial request', {
...initial,
externalLatency: initial.duration - initial.queryTime,
})
const NUM_REQUESTS = 30
const queryTimes = []
const durations = []
const externalLatencies = []
for (let i = 0; i < NUM_REQUESTS; i++) {
const subsequent = await execute(i)
queryTimes.push(subsequent.queryTime)
durations.push(subsequent.duration)
externalLatencies.push(
subsequent.duration - subsequent.queryTime
)
}
console.log("subsequent averages", {
queryTimes: mean(queryTimes),
durations: mean(durations),
externalLatencies: mean(externalLatencies),
})
console.log("subsequent median", {
queryTimes: median(queryTimes),
durations: median(durations),
externalLatencies: median(externalLatencies),
})
} catch (e) {
console.error(e)
throw new Error(e)
}
}
run()
Initial request {
queryTime: 4,
duration: 368.5752210021019,
externalLatency: 364.5752210021019
}
subsequent averages {
queryTimes: 2.033333333333333,
durations: 36.156075533231096,
externalLatencies: 34.122742199897765
}
subsequent median {
queryTimes: 2,
durations: 36.27919600903988,
externalLatencies: 34.2124989926815
}