Ballerina – dance with the cloud

What’s all the fuzz?
Working in application/data integration for the last 15 years was a wonderful journey. However, sometimes it was painful.
My first contact with application integration was using heavy, enterprise class IBM WebSphere Application Server and ESB/BPM. We done a lot of things, like integrating heterogeneous environments (Java/.NET).
Everything changed with the introduction of cloud. Oh, and yes, microservices. Since we are mostly working on cloud and developing microservices-based apps these days, I’ve always questioned the concept of integration. To be honest, old-fashioned ESB’s are not worthwhile checking there.
First, you have to walk through hell of installing ESB/application servers. And yes, to configure it afterwards. Then we have to set-up complete environment. Working on the integration project is interesting, but it requires using XPath to access and transform complex types. Which can be challenging. Again, what was the main problem here is working in team – how we can establish common concepts such as source control? What about CI/CD?
Sometimes we tend to use (classical) programming languages but then we miss constructs from ESB tools.
Finally, with serverless and microservices, where you have a bunch of loosely-coupled services which communicate, in the fine-grained and lightweight manner, we need something different.
So I was pleasantly surprised when I’ve heard of Ballerina. Actually, it was developed by WSO2, company I’m following for many years (and developers of very nice WSO ESB&BPM platform).
Let’s take a look at the story behind this concept. BTW, it is not usual that someone develops new programming language in order to solve problem. Why did they do it? As stated on Ballerina Philosophy page:
Cloud native and microservices architectures enable enterprises to have trillions of programmable network endpoints, making integration the glue that brings APIs, microservices, and data together as a whole. Centralized and configuration-driven approaches to integration, with ESBs, are not agile or cloud native.
https://v0-991.ballerina.io/philosophy/
Altough, from technical point of view, we can use traditional ESB’s for microservices integration, it just doesn’t goes along with cloud paradigms. And with Ballerina we can have:
Ballerina makes it easy to write cloud native applications while maintaining reliability, scalability, observability, and security.
https://v0-991.ballerina.io/philosophy/
Oh yes, but how? Here it is also:
Ballerina incorporates fundamental concepts of distributed system integration into the language and offers a type safe, concurrent environment to implement microservices with distributed transactions, reliable messaging, stream processing, and workflows.
https://v0-991.ballerina.io/philosophy/
Let’s see some basic concepts behind Ballerina.
Concepts
What amazes me with Ballerina is that every program can be visualized as sequence diagram! Remembering the old days, when I’ve started working with UML (long time ago, actually), sequence diagrams was something that I felt pretty powerful. It’s like you can enter into business process and visualize how your program will work. To refresh, sequence diagrams are defined as:
The most common kind of Interaction Diagram is the Sequence Diagram, which focuses on the Message interchange between a number of Lifelines.A sequence diagram describes an Interaction by focusing on the sequence of Messages that are exchanged, along with their corresponding OccurrenceSpecifications on the Lifelines.
https://www.omg.org/spec/UML/2.5.1/PDF, page 637
Ballerina is event-driven and parallel programming language. What is great, is it’s parallel execution paradigm. You can have one or more workers in each function. Those are independent execution blocks (called “strands”).
With Ballerina, you have tight integration with JSON, XML, HTTP, MIME and other data types.
Enough with writing, let’s see some code. We should start with “Hello World”. But first, let’s see how to install it!
Installation
Simple as it gets, just go to Ballerina Download page and select appropriate package. You can also download VSIX template installer for Visual Studio.
What amazes me is great integration with VSCode (which I personally use often). You can get Ballerina extension from VSCode Marketplace:

Hello World
So let’s take a look at our first Ballerina program. You will see it’s very close to other programming languages like Java/C# or JavaScript:
import ballerina/io;
public function main() {
io:println("Hello, World!");
}
Save the file as hello.bal and start it with ballerina run hello.bal. Or you can use VSCode and go directly with running and debug:

Simple Service
It is very easy to create service in Ballerina. This is not unexpected due to is’t network-orientation:
import ballerina/http;
import ballerina/log;
service archybility on new http:Listener(8081) {
resource function simpleService(http:Caller caller, http:Request req) {
var result = caller->respond("Hello, World!");
if (result is error) {
log:printError("Error sending response", result);
}
}
}
Save the file as SimpleService.bal and start it with ballerina run SimpleService.bal. You will get following output:
Compiling source
SimpleService.bal
Generating executables
Running executables
[ballerina/http] started HTTP/WS listener 0.0.0.0:8081
Then you can browse to http://localhost:8081/archybility/simpleService and you will get expected response.
API Designer
Ease of use can be shown also regarding your service design. Activate VSCode command pallete and type: Ballerina – Show API Designer.

Conclussion
In the next posts, we will tackle some interesting points. One I liked very much is data transformation, so you can check how easy it is done.