Introducing async to my serverless text-to-speech player for jokes and quotes

Using AWS components, Cloudflare, and public APIs.

James Montgomery

3 minute read


In this post, I revisit my serverless “jokes and quotes” player. The purpose was to remove the tight coupling between the client request and audio playback API source. To explore the issue, I have introduced DynamoDB to store pre-generated results.

Challenge overview

Logically, all the components of my existing solution depend on each other to succeed to deliver audio to the client. You can visualise it as follows:

Flow of the synchronous version.

The challenge was to accomplish a solution which operated in the following fashion:

The challenges of introducing async.

Separating the audio generation from the client call introduces some challenges.

  • The external API generates a single random piece of text. My current code doesn’t contain the means to randomise results.
  • The additional duties increase Lambda execution time. Not only will this increase the cost and complexity of the solution, but it could also be slower than the architecture it seeks to replace.
  • The external API result doesn’t have any ID in the response that I could conveniently re-use. Therefore, I require a schema for whichever storage solution I use.

Solution detail

If there is one thing I’ve taken away from DynamoDB posts out there, its “know your access patterns” before starting. I decided on these patterns:

  • Give me all entry IDs of a specific type.
  • Play an entry with a specific ID.

As noted above, I had no re-usable ID, so I elected to convert the text into a SHA256 hash.

After some experimentation, I arrived with a record in the following format:

  • Primary partition key: String. For example “QG” for Quote Garden.
  • Primary sort key: String. For storing the hash of the source text.
  • Attributes for audio (the base64 encoded Polly result) and voiceid (the AWS Polly voice name such as Joanna or Matthew).

Data access

The current implementation queries each partition, using a projection expression to return the hash IDs only. I use these to randomly construct the contents of the arrays in the JSON response to a GET query on /random:

    "RS": [
    "SV443": [
    "ICH": [
    "QG": [
    "lastSeen": "2020-04-24T13:44:04"

Getting a specific entry

I’ve created an API route for each partition which expects the SHA256 ID as a query string parameter in the URL. For example:

Note the waterfall with all entries cached.

In case the entry is not present, I return a “404” style response with an audio clip reserved for that scenario.

Speed and cost of execution

The /random API route is expensive from an I/O perspective. Here is what happens if a few queries happen in quick succession:

Note the waterfall effects of dynamic and mixed.

I’ve elected to cover this route and others with Cloudflare caching. Generally speaking, each API call duration is about 500ms when working with a warm Lambda, with caching it is usually less than 50ms. Additionally, each cache hit means one less execution for my AWS resources to perform.

Note the waterfall effects of dynamic and mixed with some caching.

We can observe the Cloudflare cache status via a header in the response (“cf-cache-status”) and its effects on response time below:

Note the waterfall effects of dynamic and mixed with some caching.

Note the effects of all entry routes with a cache hit:

Note the waterfall with all entries cached.

And the effects of the /random route cached as well:

Note the waterfall with all entries cached.

Whenever we achieve 100% caching within Cloudflare, the response time is much improved.

The /random route caches for the plan minimum (2 hours) whereas I can cache the specific entries for much longer.


I’ve learned a lot researching and experimenting for this. There is scope for plenty of future experiments with this subject matter.