What Is Latency and How to Reduce It
What is latency defined
Many people have likely heard the term latency being used before but what is latency exactly? In terms of network latency, this can be defined by the time it takes for a request to travel from the sender to the receiver and for the receiver to process that request. In other words, the round trip time from the browser to the server. It is obviously desired for this time to remain as close to 0 as possible, however, there can be a few things at play preventing your website latency times to remain low.
Latency vs bandwidth vs throughput
Although latency, bandwidth, and throughput all work together hand-in-hand, they do have different meanings. It’s easier to visualize how each term works when referencing it to a pipe:
- Bandwidth determines how narrow or wide a pipe is. The narrower it is, the less data is able to be pushed through it at once and vice-versa.
- Latency determines how fast the contents within a pipe can be transferred from the client to the server and back.
- Throughput is the amount of data which can be transferred over a given time period.
If the latency in a pipe is low and the bandwidth is also low, that means that the throughput will be inherently low. However, if the latency is low and the bandwidth is high that will allow for greater throughput and a more efficient connection. Ultimately, latency creates bottlenecks within the network thus reducing the amount of data which can be transferred over a period of time.
Causes of network latency
The question of what is latency has been answered, now where does latency come from? There are 4 main causes that can affect network latency times. These include the following:
- Transmission mediums such as WAN or fiber optic cables all have limitations and can affect latency simply due to their nature.
- Propagation is the amount of time it takes for a packet to travel from one source to another (at the speed of light).
- Routers take time to analyze the header information of a packet as well as, in some cases, add additional information. Each hop a packet takes from router to router increases the latency time.
- Storage delays can occur when a packet is stored or accessed resulting in a delay caused by intermediate devices like switches and bridges.
Ways to reduce latency
Latency can be reduced using a few different techniques as described below. Reducing the amount of server latency will help load your web resources faster, thus improving the overall page load time for your visitors.
- HTTP/2: The use of the ever prevalent HTTP/2 is another great way to help minimize latency. HTTP/2 helps reduce server latency by minimizing the number of round trips from the sender to the receiver and with parallelized transfers. KeyCDN proudly offers HTTP/2 support to customers across all of our edge servers.
- Fewer external HTTP requests: Reducing the number of HTTP requests not only applies to images but also for other external resources such as CSS or JS files. If you are referencing information from a server other than your own, you are making an external HTTP request which can greatly increase website latency based on the speed and quality of the third party server.
- Using a CDN: Using a CDN helps bring resources closer to the user by caching them in multiple locations around the world. Once those resources are cached, a user’s request only needs to travel to the closest Point of Presence to retrieve that data instead of going back to the origin server each time.
- Using prefetching methods: Prefetching web resources doesn’t necessarily reduce the amount of latency per se however it improves your website’s perceived performance. With prefetching implemented, latency intensive processes take place in the background when the user is browser a particular page. Therefore, when they click on a subsequent page, jobs such as DNS lookups have already taken place, thus loading the page faster.
- Browser caching: Another type of caching that can be used to reduce latency is browser caching. Browsers will cache certain resources of a website locally in order to help improve latency times and decrease the number of requests back to the server. Read more about browsing caching and the various directives that exist in our cache-control article.
Other types of latency
Latency occurs in many various environments including audio, networks, operations, etc. The following describes two additional scenarios where latency is also prevalent.
Fibre optic latency
Latency in the case of data transfer through fibre optic cables can’t be fully explained without first discussing the speed of light and how it relates to latency. Based on the speed of light alone (299,792,458 meters/second), there is a latency of 3.33 microseconds (0.000001 of a second) for every kilometer of path covered. Light travels slower in a cable which means the latency of light traveling in a fibre optic cable is around 4.9 microseconds per kilometer.
Based on how far a packet must travel, the amount of latency can quickly add up. Cable imperfections can also degrade the connection and increase the amount of latency incurred by a fibre optic cable.
This form of latency is the time difference between a sound being created and heard. The speed of sound plays a role in this form of latency which can vary based on the environment it travels through e.g solids vs liquids. In technology, audio latency can occur from various sources including analog to digital conversion, signal processing, hardware / software used, etc.
How to measure network latency?
Network latency can be tested using either Ping, Traceroute, or MTR (essentially a combination of Ping and Traceroute). Each of these tools is able to determine specific latency times, with MTR being the most detailed.
The use of MTR allows a user to generate a report that will list each hop in a network that was required for a packet to travel from point A to point B. The report will include details such as Loss%, Average latency, etc. See our traceroute command article to learn more about MTR and traceroute.
Essentially, latency is measured using one of two methods:
- Round trip time (RTT)
- Time to first byte (TTFB)
The round trip time can be measured using the methods above and involves measuring the amount of time it takes between when a client sends a request to the server and when it gets it back. On the other hand, the TTFB measure the amount of time it takes between when a client sends a request to a server and when it receives its first byte of data. You can use our performance test tool to measure the TTFB of any asset across our network of 16 test locations.
If you’re curious to know how latency affects throughput in a hypothetical situation, you can use a throughput calculator. Simply define the application, latency value, distance, etc. Once complete, look at the “unoptimized” bar for an estimate of the max throughput you can expect.
This article has hopefully helped answer the question of what is latency and provided readers with a better understanding of what causes it. Latency is an inevitable part of today’s networking ecosystem and is something we can minimize, but not completely eliminate. However, the suggestions mentioned above are important steps to take in reducing your website’s latency and helping to improve page load times for your users. After all, in today’s internet age, the importance of website speed comes down to milliseconds and can be worth millions of dollars in gained or lost profits.