Great Reminder of Persistence in Serverless

This post is more than 2 years old.

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 senior developer evangelist for Adobe. He focuses on document services, JavaScript, and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support. You can even buy me a coffee!

Lafayette, LA

Archived Comments

Comment 1 by Finnian Anderson posted on 7/25/2017 at 3:29 PM

Have to be careful with things like .sort() too as they do it in-place

Comment 2 (In reply to #1) by Raymond Camden posted on 7/25/2017 at 3:33 PM

To be honest, I have to check MDN every time to see what is inplace and what returns a copy.

Comment 3 (In reply to #2) by Finnian Anderson posted on 7/25/2017 at 4:34 PM

Me too. When I second guess I'm always wrong and create a bug like yours above