image-description
Return to Blog

Dynamically Caching Audio Files For Superior Voice Experience

, Written by

The concept of caching is not new. Just peek into your backyard - the neighborhood squirrels are always working towards caching their acorns in empty tree stumps for the impending El Niño!

Fetching a resource over the network is both slow and expensive. As a result, the ability to cache and reuse previously fetched audio resources is a critical aspect of optimizing for performance.

In the Plivo platform - we have always allowed caching of the audio file for improved performance. What changed in recent years is the diversity of use cases and the way these use cases were consuming audio files. We progressively saw patterns where customers either used a single audio file for months or changed the audio file every five minutes. A singular audio caching philosophy could not suffice such diverse use cases. To address this, we are happy to introduce dynamic and user controlled caching of audio files. You choose to store your audio file on our servers for a duration of your choice.

It all starts with the Play API!

POST https://api.plivo.com/v1/Account/{auth_id}/Call/{call_uuid}/Play/
    {
      “Urls”:“https://s3.amazonaws.com/plivocloud/Trumpet.mp3”
    }
<Response>
      <Play>https://s3.amazonaws.com/plivocloud/Trumpet.mp3</Play>
</Response>

Modifying the behavior of caching your current audio file does not require any change in the Plivo application. In fact, the implementation is based on standard Client-Side Caching directives for RESTful communication over HTTP. All you need to do is ensure that each server response provides the correct HTTP header directives to instruct our servers on when and for how long to cache the audio resource.

The Cache-Control Response Header: “no-cache” “no-store” and “max-age”

“no-cache” indicates that the returned response can’t be used to satisfy a subsequent request to the same URL without first checking with the server if the response has changed. Responding with ‘Cache-Control: no-cache’ will ensure Plivo always checks if the file has changed.

By contrast, “no-store” is much simpler. It simply disallows the server and all intermediate caches from storing any version of the returned response. Responding with ‘Cache-Control: no-store’ will ensure Plivo doesn’t maintain a local copy of the file in its cache, and always requests your web-server for the resource.

Responding with ‘Cache-Control: max-age=<new max-age value in seconds>‘ informs Plivo to cache the returned audio file for a definite period of time. Plivo will consider its cached copy stale only after the specified number of seconds (max-age value).

Validating cached responses with ETags

Assume that max-age seconds have passed since the initial fetch and Plivo has initiated a new request for the same resource. First, Plivo checks the local cache and finds the previous response. Unfortunately, we can’t use the previous response because the response has now expired. At this point, Plivo could dispatch a new request and fetch the new full response. However, that’s inefficient because if the resource hasn’t changed, then there’s no reason to download the same information that’s already in the cache!

That’s the problem that validation tokens, as specified in the ETag header, are designed to solve. The ETag value of a resource is typically an MD5 hash or some other fingerprint of the contents of the file.

Plivo sets the If-None-Match request header to the ETag value of the resource in its cache, allowing your web server to respond with a new version or with a 304 Not Modified to instruct Plivo to use its cached version. This enables more efficient resource update checks as no data is transferred if the resource has not changed.

For most optimal voice quality on calls, Plivo uses multiple media servers closest to the physical location of the user to cut latency. Every Plivo media server maintains its own local cache. As a result, the first call landing on any new Plivo media server will always result in a cache miss.

As businesses strive to meet growing subscriber demand for optimization, effective caching has become a critical component of voice calls to load audio files quickly and consistently and deliver a seamless experience. Plivo is committed to empowering its customers with feature improvements to help along the way.

Do check out this feature and leave your comments below. If you are new to our platform, Sign Up to start building awesome applications in minutes.

comments powered by Disqus