How to use cache with your Google Cloud Functions on Node.js
Google Cloud Functions

How to use cache with your Google Cloud Functions on Node.js

While developing my latest web & iOS app Word Whirl, I ran into a small computation problem where users would experience lag between playing a word and receiving a response from the server. For example, if a player submits the word "candle", they might have had to wait up to 0.5s to know whether the word was accepted or not.

Word Whirl Game

The Google Cloud function responsible from determining a word's point value should have a response time of milliseconds for the game to feel smooth. The root cause of the long response times was the amount of computation the function had to perform for every single word submission. The Word Whirl word list is over 270,000 words long, and without caching the list, the point value of every word in that list was being calculated every time a word was submitted. This calculation took up to half a second.

Even though Google Cloud functions are designed to run in an isolated environment, information can be shared between invocations through caching. This behavior is not guaranteed, but is more than sufficient for my purposes. If I could cache my 270,000 word-value lists, I would only have run that calculation once per game per user. In fact, if I could initialize and cache the word-point list prior to the user playing a word, they would never experience lag while playing.

Each function runs in its own isolated secure execution context, scales automatically, and has a lifecycle independent from other functions.
-Google Cloud Docs


Caching with Google Cloud functions is easy. Here's what I did:

  1. Create a global variable in your index.js file called cache.

Initialize the variable with the data you want to cache, in my case I am initializing the word-point array as an empty array. Name the second key of your variable TTL and assign it an initial value of the current date.

const cache = {
    valueArray: [],
    ttl: new Date(),
};        

TTL stands for Time to Live and holds the variable used to determine whether the data stored in the cache is valid. If the TTL expires, the cached data is no longer valid to use. In Word Whirl, there is a new game every 120 seconds, so I know my cached word-value array is only useful for that long. When a new game starts, the word-value array will be completely different because the game board changes and so does the point value of every possible word.

2. Read or write to your cache

Whenever you want to interact with your cached data, you must check if the data is still valid. If the data is valid, you can use the cache and skip heavy computation or a network request. If the cache has expired, calculate what the new cache should be and overwrite the cache with the new data and new TTL.

    
    if (cache.valueArray.length > 0 && cache.ttl > new Date()) {
        valueArray = cache.valueArray;
    } else {
        cache.valueArray = valueArray;
        cache.ttl = dateTimeLeftInTheFuture;
    }
        

In this simplified code snippet, I am first checking whether the word-point array is longer than 0 elements. If it is not, then there is no cache and I must calculate the cache. The other check I am making is whether the cache's TTL is in the future relative to right now. If it is in the future, then the TTL is still valid and I can safely use the cache. If either of these statements are false, then the cache is overwritten with the new word-point array that must be calculated and the new TTL.

Results from adding cache to Word Whirl

The game's performance was substantially increased after I added caching. The average response time for a word submission decreased from about 0.5s to less than 60ms. In this chart, the blue/red lines generally show the execution time for when a new cache is calculated, and the green line generally shows the execution time for when the cached data is used.

No alt text provided for this image

If you've gotten this far into the article, you might as well play some Word Whirl and experience the cached data. Here are links to both the web app and iOS app:

Word Whirl web app:

Word Whirl on iOS:

Thanks for reading!

Jeremy Swedberg

Jeremy Swedberg

To view or add a comment, sign in

More articles by Jeremy Swedberg

Insights from the community

Others also viewed

Explore topics