Its specialization is integration; it brings fundamental concepts, ideas, and tools of distributed system integration into the language and offers a type-safe, concurrent environment to implement such applications. These include distributed transactions, resiliency, concurrency, security, and container-management platforms.

Ballerina has been inspired by Java, Go, C, C++, Rust, Haskell, Kotlin, Dart, TypeScript, JavaScript, Swift, and other languages. It is an open source project, distributed under the Apache 2.0 license, and you can find its source code in the project's GitHub repository.

Textual and graphical syntaxes

Ballerina's programming language semantics are created to be natural for developers to express the structure and the logic of a program. To describe complex interactions between multiple parties, we typically use a sequence diagram. This approach enables visualization of endpoints and actions, such as asynchronous and synchronous message passing and parallel executions, in an intuitive manner.

Built-in resiliency

Resilient and type-safe integration is built into the language. When you try to invoke an external endpoint that might be unreliable, you can circumvent that interaction with resilience capabilities, such as circuit breakers, failover, and retry, for your specific protocol.

Circuit breaker

Adding a circuit breaker is as trivial as passing a few additional parameters to your client endpoint code.

endpoint http:Client backendClientEP {
url: "http://localhost:8080",
// Circuit breaker configuration options
circuitBreaker: {
// Failure calculation window.
rollingWindow: {
// Time period in milliseconds for which the failure threshold
// is calculated.
timeWindowMillis: 10000,
// The granularity at which the time window slides.
// This is measured in milliseconds.
bucketSizeMillis: 2000
},
// The threshold for request failures.
// When this threshold exceeds, the circuit trips.
// This is the ratio between failures and total requests.
failureThreshold: 0.2,
// The time period(in milliseconds) to wait before
// attempting to make another request to the upstream service.
resetTimeMillis: 10000,
// HTTP response status codes which are considered as failures
statusCodes: [400, 404, 500]
},
timeoutMillis: 2000
};

Failover

You can define client endpoints that need to failover with timeout intervals and failover codes.

Asynchronous and parallel execution

Ballerina's execution model is composed of parallel execution units known as workers. A worker represents Ballerina's basic execution construct. In Ballerina, each function consists of one or more workers, which are independent parallel execution code blocks. If explicit workers are not mentioned with worker blocks, the function code will belong to a single, implicit default worker.

Ballerina also offers native support for fork-join, which is a special case of worker interaction. With fork-join, you can fork the logic and offload the execution to multiple workers and conditionally join the result of all workers inside the join clause.

Ballerina also supports asynchronous invocation of functions or endpoints. Although most of the synchronous invocations' external endpoints are implemented in the fully non-blocking manner in Ballerina, there are certain situations where you have to invoke an endpoint or function asynchronously and later check for the result.

Transaction handling

Ballerina has language-level constructs in handling transactions, where you can do local transactions with connectors, and distributed transactions with X/A-compatible connectors, or even service-level transactions with the built-in coordination support available in the language runtime.

In Ballerina, doing a set of actions transactionally is just a matter of wrapping all the operations in a "transaction" block.

Secure by design

Ballerina is designed to ensure that programs written with Ballerina are inherently secure. Ballerina programs are resilient to major security vulnerabilities, including SQL injection, path manipulation, file manipulation, unauthorized file access, and unvalidated redirect (open redirect). This is achieved with a taint analysis mechanism, in which the Ballerina compiler identifies untrusted (tainted) data by observing how tainted data propagates through the program. If untrusted data is passed to a security-sensitive parameter, a compiler error is generated.

The @sensitive annotation can be used with parameters of user-defined functions. This allows users to restrict passing tainted data into a security-sensitive parameter.

For example, Ballerina's taint-checking mechanism completely prevents SQL injection vulnerabilities by disallowing tainted data in the SQL query. The following results in a compiler error because the query is appended with a user-provided argument.

The following results in a compiler error because a user-provided argument is passed to a sensitive parameter.

userDefinedSecureOperation(args[0]);

After performing necessary validations and/or escaping, the untaint unary expression can be used to mark the proceeding value as trusted and pass it to a sensitive parameter.

userDefinedSecureOperation(untaint args[0]);

Native support for Docker and Kubernetes

Ballerina understands the architecture around it; the compiler is environment-aware with microservices directly deployable into infrastructure like Docker and Kubernetes by autogenerating Docker images and YAMLs. To explain, let's look at sample hello_world.bal code.

Ballerina Central

Ballerina fosters reuse and sharing of its packages via its global central repository, Ballerina Central. There you can share endpoint connectors, custom annotations, and code functions as shareable packages by using push-and-pull versioned packages.

Learn more

With the emergence of microservice architectures, the software industry is moving towards cloud-native application development. Cloud-native programming languages, such as Ballerina, will be an essential element of fast innovation.

Resources for learning more about Ballerina are available on the Learn Ballerina section of the project's website. Also, consider attending Ballerinacon, July 18, 2018, in San Francisco and streamed globally. This full-day event will offer intense training on the best practices of microservice development, resiliency, integration, Docker and Kubernetes deployment, service meshes, serverless, test-driven microservice development, lifecycle management, observability, and security. OpenSource.com readers can attend for free by using coupon code BalCon-OpenSource when ordering tickets.

Topics

About the author

About the author

Lakmal Warusawithana - Lakmal Warusawithana is the Senior Director - Cloud Architecture of WSO2. Lakmal has a long history of working in open source, cloud, and DevOps technologies and has been Vice President of Apache Stratos PaaS Project. Lakmal is a key contributor for containerization and deployment orchestration of Ballerina. Lakmal has also presented at numerous events, including ApacheCon, CloudOpen, QCon, JaxLondon, Cloud Expo, Cloudstack Collaboration Conference, WSO2Con, and many tech meetups.

Footer

The opinions expressed on this website are those of each author, not of the author's employer or of Red Hat.

Opensource.com aspires to publish all content under a Creative Commons license but may not be able to do so in all cases. You are responsible for ensuring that you have the necessary permission to reuse any work on this site. Red Hat and the Shadowman logo are trademarks of Red Hat, Inc., registered in the United States and other countries.