Analysis / Technology / Top Stories

Ballerina: An API-First Programming Language

12 Jun 2018 1:20pm, by

Open source enterprise application integration software provider WSO2 has launched a programming language built around supporting application programming interfaces (APIs) as a first-class entity. The company debuted the language, called Ballerina, at the KubeCon + CloudNativeCon EU conference last month in Copenhagen.

“Ballerina is our approach to addressing integration issues in the marketplace,” said Tyler Jewell, CEO at WSO2, noting that the network programming language is ideal for large-scale integration work, cases where  “you want to write a service that resiliently talks to other services,” he said.

Most of the programming used today were created before the idea of containerizing microservices and running them in the cloud. As a result, the support for connecting a program to external APIs has been added on with many languages. In many cases, developers have to write drivers or plug-ins from scratch. Or they must struggle to understand nuanced programming constructs, such as callbacks in JavaScript/Node.js.

Ballerina is a compiled, typesafe, concurrent programming language. The code is compiled into bytecode, which is then run in a virtual machine, though eventually, the runtime will move over to LLVM compiler, which can generate native code. The artifacts are packaged into containers.

Ballerina was born as an internal project within WSo2, a company that has done a lot of enterprise integration work. The WSO2 engineers, as many others in the cloud-native space, were growing increasingly frustrated by the fact that most enterprise service buses (ESBs) require XML as a standard format for configuring message flows, support for which breaks the routine of the developer. The developers sought an easier way to build drivers and to encapsulate the logic to handle inbound traffic.

Ballerina’s language semantics model how independent parties communicate via structured interactions. In fact, Ballerina programs can be displayed as a sequence diagram, a common way of visualizing concurrent programs.  Developers can even author Ballerina through sequence diagrams, with the Ballerina Composer.

WSo2 CEO Tyler Jewell (left) and WSo2 co-founder Paul Fremantle, at KubeCon + CloudNativeCon EU.

This language “bakes in all the best practices” of today’s programming methodologies, Jewell said. For instance, he pointed to the  Golang’s dependency management model, for instance, can bring about transitive dependency issues, where two applications can import the same library but act differently. In contrast, Ballerina can offer people “a very clean project structure, a very clean code structure, and people will never have any transitive versioning issues.” And unlike Node.js, Ballerina offers type safety.

Still built around the idea of the “main” code block, Most languages don’t have a good model for concurrency, which is an absolute must for software that interacts in with the outside world, such as web server software. Some of the newer languages, such as Go, have built-in concurrency models, though they still require a fair amount of work in understanding the semantics.

In Ballerina, an HTTP client is a first-class construct, included in the standard library. Under the covers, Ballerina deploys a concurrent asynchronous non-blocking model, widely used in the manager/worker model of parallel programming, in which different workers are assigned the outbound work, while others handle inbound operations, eliminating any thread blocking or performance lags. The VM has a custom scheduler that manages the pool of workers against the pool of available threads offered by the operating system.

Tests have shown that this approach can offer 20,000-25,000 transactions per second on basic hardware, around 20 percent higher than Spring Boot, and 50x faster than equivalent Python, Jewell said.

Ballerina is not an object-oriented nor a functional language, though it supports objects and lambdas. It is a traditional declarative, language, like C. If the history of network programming has taught us anything through CORBA and JavaBeans, it is that most things that pass over the network are not objects.

Ballerina is open source under the Apache License, and the trademark is retained by the company.

Less Talk, More Code

Here is the code for a simple Ballerina program, <hello-service/hello_service.bal>,
that responds to API calls:

// Packages contain functions, annotations and connectors.
// This package is referenced by ‘http’ namespace in the code
// body.
import ballerina/http;
import ballerina/io;

// A service is a network-accessible API. This service
// is accessible at '/hello', and bound to a the listener on
// port 9090. `http:Service`is a connector in the `http`
// package.
service<http:Service> hello bind { port: 9090 } {

// A resource is an invokable API method.
// Accessible at '/hello/sayHello’.
// 'caller' is the client invoking this resource.
     sayHello (endpoint caller, http:Request request) {

       // Create object to carry data back to caller.
       http:Response response = new;

       // Objects have function calls.
       response.setTextPayload("Hello Ballerina!\n");

       // Send a response back to caller.
       // Errors are ignored with '_'.
       // ‘->’ is a synchronous network-bound call.
       _ = caller -> respond(response);
     }
   }

For live code examples, check out the Ballerina site.

WSO2 sponsored this post.

Feature image via Pixabay.


A digest of the week’s most important stories & analyses.

View / Add Comments

Please stay on topic and be respectful of others. Review our Terms of Use.