Great Reminder of Persistence in Serverless

Today I ran into a great bug. By “great” I mean it totally confused me but as soon as I realized what I was doing wrong, I knew it would be a great thing to blog about as a reminder to others. I don’t think this applies just to OpenWhisk, but to serverless in general.

Ok, so what did I do? I was working on a sample action that simply returned an array of cats. As an added feature, you can filter the cats by passing an argument. Here’s the code:

let cats = ["Luna","Cracker","Pig","Simba","Robin"];

function main(args) {

    if(args.filter && args.filter !== '') {
        cats = cats.filter((cat) => {
            return (cat.indexOf(args.filter) >= 0);

    return {

Pretty simple, right? Either return cats as is, or filter based on a quick indexOf check. (On reflection, I should probably lcase the filter and the cat name to make the filter a bit easier to use, but whatevs.)

So I fired this up on OpenWhisk, web-enabled it, and ran a quick test. First, I hit:

And I got:

	cats: [

Woot. Ok, now filter it like so:

And the result is perfect:

	cats: [

Now let’s try a filter returning nothing:

cats: [ ]


Alright, so final test, let’s return everything again with the original URL:

cats: [ ]

Um. Err. What the heck, right? So I added a console.log. Reran my code and it was fixed.


And then I filtered again.

And it broke again.

Whatever is the opposite of woot!

And then this is where I remember a blog post I did back in February: Serverless and Persistence. Specifically this part:

First off - when your action is fired up, OpenWhisk does not kill it immediately when done. Rather, it keeps the container used for your code around to see if the action will be fired again soon.

And then I looked back at my code, specifically the filter. Realization dawned.


In case it wasn’t obvious the issue was two fold:

  • OpenWhisk was keeping my code ‘warm’, in other words, up and ready for repeated calls.
  • My code was modifying, and destroying, the original array when a filter was used.

Incredibly obvious when you think about it. My fix was to just make a copy of array (and I set the original to a const for good measure).

Raymond Camden's Picture

About Raymond Camden

Raymond is a developer advocate for Extend by Auth0. He focuses on serverless and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support.

Lafayette, LA