Latency

The time delay between a request being sent and a response being received.

Latency is the time delay between initiating a request and receiving a response. In API development, it measures how long it takes for data to travel from client to server and back.

Types of Latency

TypeDescription
Network LatencyTime for data to travel over the network
Server LatencyTime for server to process request
Database LatencyTime for database queries
Total LatencyEnd-to-end response time

Measuring Latency

  • P50 (Median): 50% of requests faster than this
  • P95: 95% of requests faster than this
  • P99: 99% of requests faster than this
  • Average: Mean response time (can be misleading)

Factors Affecting Latency

  • Geographic distance between client and server
  • Network congestion and bandwidth
  • Server processing power
  • Database query complexity
  • Payload size

Reducing Latency

  • CDN: Serve content from edge locations
  • Caching: Reduce database queries
  • Connection Pooling: Reuse database connections
  • Async Processing: Move heavy tasks to background
  • Compression: Reduce payload size
  • HTTP/2: Multiplexing, header compression

Code Examples

Measuring API Latency

async function measureLatency(url) {
  const start = performance.now();
  await fetch(url);
  const end = performance.now();
  
  const latency = end - start;
  console.log(`Latency: ${latency.toFixed(2)}ms`);
  return latency;
}

// Measure multiple times for accuracy
const results = await Promise.all([
  measureLatency("/api/users"),
  measureLatency("/api/users"),
  measureLatency("/api/users")
]);

const avg = results.reduce((a, b) => a + b) / results.length;
console.log(`Average: ${avg.toFixed(2)}ms`);