Latency in computer science is the amount of time needed for an operation to complete. In web and mobile applications, latency is referred as the time elapsed between a request and a response. It is one of the most important metrics in any distributed system.
The Problem
One of major factors that cause a high latency in a distributed system is the long distance between the clients and the servers (assuming other factors are causing a low latency).
Selecting a server geographical location is most probably defined early, but as your application or system gets big and scale, you might serve different users across the world, so changing the server location to be as close as possible to clients would not be feasible.
The Solution
In this case, Content Delivery Network (CDN) comes in handy, the major thing a content delivery network would do is caching the content to different servers across the world (usually these servers are allocated in small data centers in different geographical locations) for a certain period of time, and for the next and same requests it will use the cached data if it exists instead of redirecting the request to the server, so another benefit here is reducing the number of requests that reach the application server.
The Flow or Traffic with CDN
Users or clients first hit the CDN and if the data is cached it will use that cached data and return it to the user or if it is not cached or expired it will be redirected to the server, gets the data from the server, cache it for the next and same request and serve it to the user or client.
Important Notes
For the first time accessing the data or making a request, the latency could be high, but after caching the content and before it expires and gets removed from the cache it will be significantly lower than the first time. So we did not solve the problem entirely, but we improved it.
Time To Live or (TTL) is a mechanism used in CDNs which limits the lifespan or lifetime of the cached data, having a high TTL would decrease the number of requests reaching the target server but it also could result in outdated or wrong data, so you have to choose a reasonable value.
SSL, TLS or HTTPS should be used in the CDN to secure the communication between the clients and the CDNs.