Skip to content

Commit

Permalink
Update formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
madhuramendis committed Jun 6, 2018
1 parent 6474c85 commit 04a3014
Showing 1 changed file with 49 additions and 50 deletions.
99 changes: 49 additions & 50 deletions docs/posts/ballerina-a-cloud-native-programming-language.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,28 @@ title: Ballerina - A cloud native programming language for integration
author: Kasun Indrasiri
date: 5 June 2018
status: Published
abstract: Ballerina is a cloud-native programming language which aims to fill the gap between integration products and general purpose programming languages by making it easy to write programs that integrate and orchestrate across distributed microservice and endpoints in type-safe and resilient manner.
abstract: Ballerina is a cloud-native programming language which aims to fill the gap between integration products and general purpose programming languages by making it easy to write programs that integrate and orchestrate across distributed microservice and endpoints in type-safe and resilient manner.
socialmediaimage: drone-innovation-technology-57544.jpg
---

# Ballerina - A cloud native programming language for integration
Ballerina is a cloud-native programming language which aims to fill the gap between integration products and general purpose programming languages by making it easy to write programs that integrate and orchestrate across distributed microservice and endpoints in type-safe and resilient manner.
Ballerina is a cloud-native programming language which aims to fill the gap between integration products and general purpose programming languages by making it easy to write programs that integrate and orchestrate across distributed microservice and endpoints in type-safe and resilient manner.

In this post, we will cover what Ballerina is and some of the key highlights of it.
In this post, we will cover what Ballerina is and some of the key highlights of it.


## A fully fledged programming language
Ballerina is a fully fledged programming language which is compiled, type-safe, Turing-complete, concurrent and secure by design. Ballerina incorporates fundamental concepts of distributed system integration into the language and offers a type-safe, concurrent environment to implement and integrate microservices with distributed transactions, reliable messaging, stream processing, and workflow.

## Integration with code over config using text or graphic syntaxes
Both Ballerina code and the graphical syntax are inspired from how the independent parties communicate via interactions in a sequence diagram.
Both Ballerina code and the graphical syntax are inspired from how the independent parties communicate via interactions in a sequence diagram.

In the graphical syntax, Ballerina represents clients, workers and remote systems as different actors in the sequence diagram. For example, as shown in figure 1, the interaction between the client/caller, service, worker and external endpoint can be represented using a sequence diagram. Each endpoint is represented as an actor in the sequence diagram and actions are represented as interactions between them.
In the graphical syntax, Ballerina represents clients, workers and remote systems as different actors in the sequence diagram. For example, as shown in figure 1, the interaction between the client/caller, service, worker and external endpoint can be represented using a sequence diagram. Each endpoint is represented as an actor in the sequence diagram and actions are represented as interactions between them.

![Sequence Diagram](images/figure_2.1.png)
![Sequence Diagram](images/figure_2.1.png)
*Figure 1*

In code, remote endpoints are interfaced via endpoints, which offer type safe actions and worker’s logic is written as sequential code inside either a resource or a function. You can define a service and bind it to a server endpoint (for example, HTTP server endpoint can listen on a given HTTP port). Each service contains one or more resource which has the sequential code related to a worker and runs by a dedicated worker thread.
In code, remote endpoints are interfaced via endpoints, which offer type safe actions and worker’s logic is written as sequential code inside either a resource or a function. You can define a service and bind it to a server endpoint (for example, HTTP server endpoint can listen on a given HTTP port). Each service contains one or more resource which has the sequential code related to a worker and runs by a dedicated worker thread.

```ballerina
import ballerina/http;
Expand Down Expand Up @@ -78,11 +78,11 @@ service<http:Service> timeInfo bind listener {
```

Inside the resource code, you can write the interaction with external endpoint by using client endpoints of given protocol (e.g HTTP client) as well as the generic programming logic.
Inside the resource code, you can write the interaction with external endpoint by using client endpoints of given protocol (e.g HTTP client) as well as the generic programming logic.

## Network Aware

Ballerina is a language that is designed for integration of disparate service, systems, and data. Hence Ballerina provides native network aware constructs that provide the abstraction for interaction with endpoints via disparate network protocols. Ballerina offers out of the box support for most of the standard network communication protocols.
Ballerina is a language that is designed for integration of disparate service, systems, and data. Hence Ballerina provides native network aware constructs that provide the abstraction for interaction with endpoints via disparate network protocols. Ballerina offers out of the box support for most of the standard network communication protocols.

For example, you can use client connectors such as HTTP, MySQL, gRPC Kafka to produce the message to those systems.

Expand All @@ -92,7 +92,7 @@ endpoint http:Client timeServiceEP {
url:"http://localhost:9095"
};
// ...
// ...
http:Response response = check
timeServiceEP -> get("/localtime");
Expand All @@ -105,8 +105,8 @@ endpoint mysql:Client testDB {
poolOptions: { maximumPoolSize: 5 },
dbOptions: { useSSL: false }
};
// ...
var selectRet = testDB->select("SELECT * FROM student", ());
// ...
var selectRet = testDB->select("SELECT * FROM student", ());
// Kafka producer endpoint
endpoint kafka:SimpleProducer kafkaProducer {
Expand All @@ -116,12 +116,12 @@ endpoint kafka:SimpleProducer kafkaProducer {
noRetries:3
};
// ...
// ...
// Produce the message and publish it to the Kafka topic
kafkaProducer->send(serializedMsg, "product-price", partition = 0);
```
Similarly, you can leverage server connectors to receive the message via those protocols, and bind them to the service that intends to consume those messages. So, most implementation details of consuming messages via a given protocol are transparent to the developer.
```
Similarly, you can leverage server connectors to receive the message via those protocols, and bind them to the service that intends to consume those messages. So, most implementation details of consuming messages via a given protocol are transparent to the developer.

```ballerina
// Server endpoint configuration.
Expand All @@ -140,7 +140,7 @@ service SamplegRPCService bind ep {
string msg = serializedMsg.toString("UTF-8");
log:printInfo("New message received from the product admin");
...
...
endpoint jms:SimpleQueueReceiver consumer {
initialContextFactory:"bmbInitialContextFactory",
providerUrl:"amqp://admin:admin@carbon/carbon"
Expand All @@ -159,7 +159,7 @@ service<jms:Consumer> jmsListener bind consumer {
In addition to primitive network protocols, connectors are available for various web APIs such as Twitter, Github etc.

## Resilient by design
The resilient and type-safe integration is built into the language. So when you to invoke an external endpoint which might be unreliable, you can circumvent such interaction with resilience capabilities such as circuit breaker for the specific protocol that you are using. This is as trivial as passing a few additional parameters to your client endpoint code.
The resilient and type-safe integration is built into the language. So when you to invoke an external endpoint which might be unreliable, you can circumvent such interaction with resilience capabilities such as circuit breaker for the specific protocol that you are using. This is as trivial as passing a few additional parameters to your client endpoint code.


```ballerina
Expand All @@ -180,7 +180,7 @@ endpoint http:Client backendClientEP {
```

By design Ballerina code you write will not require specific tools for checking for vulnerabilities or best practices. For example, a common issue in building distributed systems is that data coming over the wire cannot be trusted not to include injection attacks. Ballerina assumes that all data coming over the wire is tainted. And compilation time checks prevent code that requires untainted data from accessing tainted data.
By design Ballerina code you write will not require specific tools for checking for vulnerabilities or best practices. For example, a common issue in building distributed systems is that data coming over the wire cannot be trusted not to include injection attacks. Ballerina assumes that all data coming over the wire is tainted. And compilation time checks prevent code that requires untainted data from accessing tainted data.


```ballerina
Expand Down Expand Up @@ -219,14 +219,14 @@ function sanitizeAndReturnUntainted (string input) returns @untainted string {
```

Most of the out of the box capabilities that Ballerina offer for resilient integration, drastically reduce the number of boilerplate code and external dependencies that you need to have to achieve resilient interactions and safe code.
Most of the out of the box capabilities that Ballerina offer for resilient integration, drastically reduce the number of boilerplate code and external dependencies that you need to have to achieve resilient interactions and safe code.

## Type Safe
Ballerina has a structural type system with primitive, record, object, tuple and union types. This typesafe model incorporates type inference at the assignment and provides numerous compile time integrity checks for connectors, logic and network-bound payloads.
## Type Safe
Ballerina has a structural type system with primitive, record, object, tuple and union types. This typesafe model incorporates type inference at the assignment and provides numerous compile time integrity checks for connectors, logic and network-bound payloads.

The code that integrates services and systems often has to deal with sophisticated distributed errors. Ballerina has error handling capabilities based on union types. Union types explicitly capture these semantics without requiring developers to create unnecessary “wrapper” types. When you decide to pass the errors back to the caller, you can use the ‘check’ operator.
The code that integrates services and systems often has to deal with sophisticated distributed errors. Ballerina has error handling capabilities based on union types. Union types explicitly capture these semantics without requiring developers to create unnecessary “wrapper” types. When you decide to pass the errors back to the caller, you can use the ‘check’ operator.

For example, when you have a JSON data received over a message, you can cast it to a type that you have defined as part of your logic. Then you can safely cast the two type by handling the possible error as part of a match clause written against that union type.
For example, when you have a JSON data received over a message, you can cast it to a type that you have defined as part of your logic. Then you can safely cast the two type by handling the possible error as part of a match clause written against that union type.



Expand All @@ -241,19 +241,19 @@ type Payment {
int cvc;
};
// ...
// ...
json payload = check request.getJsonPayload();
// The next line shows typesafe parsing of JSON into an object
Payment|error p = <Payment>payload;
match p {
Payment x => {
io:println(x);
res.statusCode = 200;
io:println(x);
res.statusCode = 200;
// return the JSON that has been created
res.setJsonPayload(check <json>x);
res.setJsonPayload(check <json>x);
}
error e => {
res.statusCode = 400 ;
error e => {
res.statusCode = 400 ;
// return the error message if the JSON failed to parse
res.setStringPayload(e.message);
}
Expand All @@ -262,15 +262,15 @@ match p {
```

Also, all the endpoints that we use for network interactions are type safe. Which means they have strongly typed parameters and returns, which are enforced with compile-time checking validates the types.
Also, all the endpoints that we use for network interactions are type safe. Which means they have strongly typed parameters and returns, which are enforced with compile-time checking validates the types.


## Asynchronous and Parallel
## Asynchronous and Parallel

Ballerina’s execution model is composed of parallel execution units known as workers. As we discussed earlier worker represents an execution of a given actor in an interaction between multiple endpoints. By default, a given resource or function comes with an implicit worker and you can add multiple workers as required and define the worker interactions too.
Ballerina’s execution model is composed of parallel execution units known as workers. As we discussed earlier worker represents an execution of a given actor in an interaction between multiple endpoints. By default, a given resource or function comes with an implicit worker and you can add multiple workers as required and define the worker interactions too.

For example, you can define multiple workers in your code and workers can interact with each other by sending and receiving messages. The message sending(e.g. (i, k) -> w2;
) happens fully asynchronous and even if you expect to receive a message, you need to explicitly do that in your code (e.g. j <- w2;)
) happens fully asynchronous and even if you expect to receive a message, you need to explicitly do that in your code (e.g. j <- w2;)


```ballerina
Expand Down Expand Up @@ -322,7 +322,7 @@ Ballerina also offers native support for fork-join, which is a special case of w
} join (all) (map results) {
int iW1;
}
}
```

Expand All @@ -342,24 +342,24 @@ future<http:Response | error> f1
var response = await f1;
```

For such requirements you can invoke a function or action with ‘start’ and later, once you require the result of the invocation, you can you ‘await’.
For such requirements you can invoke a function or action with ‘start’ and later, once you require the result of the invocation, you can you ‘await’.

## Comprehensive development and deployment lifecycle
Ballerina is not just a mere programming language, but it also defines a comprehensive development and deployment lifecycle that enables the developer productivity.
## Comprehensive development and deployment lifecycle
Ballerina is not just a mere programming language, but it also defines a comprehensive development and deployment lifecycle that enables the developer productivity.

### Tools and IDEs
Ballerina empowers the developers with IDE support with VS Code and IntelliJ. Ballerina composer, the web-based graphical and textual editor, enable the developers to code as well as model the high-level interactions graphically.
Ballerina empowers the developers with IDE support with VS Code and IntelliJ. Ballerina composer, the web-based graphical and textual editor, enable the developers to code as well as model the high-level interactions graphically.

### Build
The code you write using the development tools can go through the build process. The build process can have multiple phases including, building from source, unit and integration testing with Testerina; the test framework of Ballerina, generate deployment artifacts for the deployment platform (Docker or Kubernetes), generate executables (.balx) or libraries (.balo).
The code you write using the development tools can go through the build process. The build process can have multiple phases including, building from source, unit and integration testing with Testerina; the test framework of Ballerina, generate deployment artifacts for the deployment platform (Docker or Kubernetes), generate executables (.balx) or libraries (.balo).

![Development Lifecycle](images/figure_2.2.png)
![Development Lifecycle](images/figure_2.2.png)
*Figure 2*

Therefore, Ballerina natively supports every aspect of the development lifecycle. The Docker and Kubernetes annotations make the deployment artifacts and docker image generation a brisk.
Therefore, Ballerina natively supports every aspect of the development lifecycle. The Docker and Kubernetes annotations make the deployment artifacts and docker image generation a brisk.

### Deploy
The developers can generate the deployment artifacts for the Ballerina code, just by annotating code with the suitable annotations of a deployment method of their choice. For example in the following code snippet, we have annotated code with Kubernetes annotations to generate deployment artifacts.
The developers can generate the deployment artifacts for the Ballerina code, just by annotating code with the suitable annotations of a deployment method of their choice. For example in the following code snippet, we have annotated code with Kubernetes annotations to generate deployment artifacts.


```ballerina
Expand Down Expand Up @@ -392,20 +392,19 @@ service<http:Service> helloWorld bind helloEP {
```

So, during the build process the docker image and Kubernetes artifacts will be generated.
So, during the build process the docker image and Kubernetes artifacts will be generated.

![Deployment](./images/figure_2.3.png)
![Deployment](./images/figure_2.3.png)
*Figure 3*

Ballerina deployment choices are so diverse so that you can deploy it in a conventional VM or bare metal servers, Docker, Kubernetes and on a service mesh, f you are using a Service Mesh such as Istio.
Ballerina deployment choices are so diverse so that you can deploy it in a conventional VM or bare metal servers, Docker, Kubernetes and on a service mesh, f you are using a Service Mesh such as Istio.


## Ballerina Central - The central registry
## Ballerina Central - The central registry
Ballerina fosters reuse and sharing of its packages via the global central repository, Ballerina central. You can share, endpoint connectors, custom annotations, and code functions as shareable packages, by using push and pull versioned packages with Ballerina Central.

## Observable
Monitoring, logging, and distributed tracing are key methods that reveal the internal state of the Ballerina code to provide the observability. Ballerina provides out of the box capabilities to work with observability tools such as Prometheus, Grafana, Jaeger and Elastic Stack, with minimal configuration.
## Observable
Monitoring, logging, and distributed tracing are key methods that reveal the internal state of the Ballerina code to provide the observability. Ballerina provides out of the box capabilities to work with observability tools such as Prometheus, Grafana, Jaeger and Elastic Stack, with minimal configuration.

## Conclusion
In this post, we have covered some of the key highlights of Ballerina, the cloud-native programming language for integrating microservices and endpoints. You can learn more about Ballerina at https://ballerina.io/learn/ and stay tuned for more post on Ballerina.

In this post, we have covered some of the key highlights of Ballerina, the cloud-native programming language for integrating microservices and endpoints. You can learn more about Ballerina at https://ballerina.io/learn/ and stay tuned for more post on Ballerina.

0 comments on commit 04a3014

Please sign in to comment.