Serverless architecture is rising in popularity, in a happy union that joins business needs and pay-as-you-go services with a break from excess configuration on the technical side. Last week we revisited AWS Lambda. This week we look at IBM Bluemix OpenWhisk.

OpenWhisk is open source

OpenWhisk is an event-driven open source platform for developers. The code can be found on GitHub. The aim is to free up developers to spend more time on business logic. The fact that it is open source is a key differentiator between it and AWS Lambda: you are not locked in to a vendor. You can also look at the milestones of the project and see the features being developed, as well as submitting your own as pull requests.

It is currently an experimental offering in beta, and you can either install vagrant and clone the repository:

git clone https://github.com/openwhisk/openwhisk.git
cd openwhisk/tools/vagrant
vagrant up

Or get a free account with IBM Bluemix.

Runtimes

OpenWhisk supports NodeJS, Java 8, Python 2.7 and (unlike many competitors) Swift. If you want to use something else, say, legacy C code, you can. Any thing you can package as a binary command in a docker file (the rule of thumb is, a statement with standard input and standard output that would run in bash) then it can be managed. For more information on this see the OpenWhisk tech talk.

How it works

Under the covers, OpenWhisk uses Kafka and Docker among others. It is event driven: the application is structured where events are flowing through the system and event handlers listen. There are four key concepts.

Action

An action is a stateless function, which acts as an event handler. It should be short running: the default is 5 minutes, after which the function is disposed of. In order for the platform to know what to do with it, it needs to have a public main function that is the entry point, and return Json. For example:

import com.google.gson.JsonObject
public class HelloWorld {
   public static JsonObject main(JsonObject args) {
      JsonObject response = new JsonObject();
      return response;
   }
}

Or in Javascript:

function main(params) {
    return {payload: 'Hello ' + params.name};
}

To create the action:

wsk action create hello hello.js

The main function can call additional functions but must follow the conventions to be picked up as the entry point.

Importantly, actions can be chained together in a sequence, like a piping operation. This is a key feature if you want to reuse code. It allows you to reuse implementations of actions by composing them with other actions. You can build up more powerful general solutions. For example, you could call a news API with one action, and filter it with another before returning the results as Json. This allows you to move key processing away from the client.

For more information on actions, including how to invoke them, see the documentation or the OpenWhisk tech talk.

Trigger

This is the name for a class of events, it also might be described as a ‘feed’. However in OpenWhisk, a feed refers to a trigger along with control operations (such as starting or stopping a feed of events). A trigger is the events themselves.

Triggers can be fired by using a dictionary of key-value pairs: sometimes referred to as the event. This results in an activation ID. They can be fired explicitly or by an external event source, by a feed.

To create a trigger:

wsk trigger create sayHello

To fire a trigger:

wsk trigger fire sayHello --param name "World"

See the trigger documentation.

Package

This refers to a collection of actions and feeds which can be packaged up and shared in an ecosystem. You can make your feeds and actions public and share them with others. An example is some of the IBM Watson APIs, which are exposed as OpenWhisk packages.

Rule

A rule is a mapping from a trigger to an action. Each time the trigger fires, the corresponding action is invoked, with the trigger event as its input. It is possible for a single trigger event to invoke multiple actions, or to have one rule that is invoked as a response to events from multiple triggers.

To create the rule:

wsk rule create firstRule sayHello hello

It can be disabled at any time:

wsk rule disable firstRule

After firing the trigger you can check and get the activation ID:

wsk activation list --limit 1 hello

And check the result:

wsk activation result <<activation id number>>

To see the payload:

{
"payload": "Hello World"
}

Use cases

Much like AWS Lambda, OpenWhisk is not going to fit every use case. However for bots and mobile back end solutions, it is perfect. Having the option to use Swift as well as Java means it is an easy transition for many iOS mobile developers. Being able to abstract away costly operations away from devices is perfect for filtering, and avoiding making too many API calls from the client.

Like AWS Lambda, the pain of setting up and maintaining infrastructure is nonexistent, and you don’t have to worry about scaling. Serverless architecture will undoubtably be a key player in the Internet of Things.

In terms of pricing, its not that straightforward. OpenWhisk is free to get started with, but its not that simple to find out how much it will cost you. It also depends entirely on the services you want to use.

Further reading

OpenWhisk tutorial

OpenWhisk bot example

OpenWhisk tech talk

GitHub

Serverless with OpenWhisk

About The Author
- Katharine is a Java developer by trade, turned Community & Content Manager for Voxxed. Helping developers learn and share knowledge. Contact me at kbe@voxxed.com with any news, articles or tutorials.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>