Serverless computing is becoming increasingly popular in cloud native development. It provides advantages to an organization in terms of cost, scalability, and agility. Ballerina was created to make integrations simple. It also adds agility to the development process. What will happen when Ballerina meets serverless? A couple of serverless platforms have already started supporting Ballerina as a runtime.
This presentation will discuss the advantages of adopting a serverless strategy, the work done in running Ballerina in OpenWhisk and Kubeless, and other efforts going on to support Ballerina in the serverless world.
4. Server-full Computing
○ API or REST endpoint, e.g., “/hello”
○ Request/Response handler or function...
… to accept input, return output
5. The Function
public function msg(json data) returns json {
json? name = data.name;
if (name == null) {
return { msg: "hello world!" };
} else {
return { msg: "hello " + name.toString() + "!" };
}
}
input output
{ } → { msg: “hello world!” }
{ name: “san francisco” } → { msg: “hello san francisco!” }
hello-function.bal
6. The Server hello-server.bal
import ballerina/http;
endpoint http:Listener listener { port: 8080 };
service default bind listener {
hello(endpoint caller, http:Request req) {
var params = req.getQueryParams();
json body = params.hasKey("name") ? { name: params.name } : {};
http:Response res = new;
res.setPayload(msg(body));
_ = caller->respond(res);
}
}
7. Run the Server
> ballerina run hello-server.bal
ballerina: initiating service(s) in 'hello.bal'
ballerina: started HTTP/WS endpoint 0.0.0.0:8080
localhost:8080/default/hello
localhost:8080/default/hello?name=san francisco
9. Deploy the Server
VM
rent bare metal
rent virtual machine
rent a container
Manage servers.
Operate servers.
Secure servers.
Scale servers.
Pay for servers...
… Even when idle.
10. So what is Serverless Computing?
VM
Function
with serverless, there is the function,
and only the function, for you to
create, deploy and operate.
12. 3337 stars, 631 forks
120 contributors
Serverless Functions with
Partners: Adobe, IBM, Red Hat
Many on-prem deployments
Use as hosted service from IBM, Adobe
And now available from WSO2
Deploys to Kube, Mesos, OpenShift, VMs
Run full stack on your laptop
Topped Hacker News in February
912 on Slack
https://openwhisk.apache.org
13. Reminder: The Function
public function msg(json data) returns json {
json? name = data.name;
if (name == null) {
return { msg: "hello world!" };
} else {
return { msg: "hello " + name.toString() + "!" };
}
}
hello-function.bal
14. Deploy the Function
> wsk action create hello hello-function.bal --web true
ok: created action hello
guest.localhost/default/hello.json
guest.localhost/default/hello.json?name=san francisco
15. Under the hood
“Deploy my code hello-function.bal as a function named hello”
hello = hello-function.bal
Function is deployed.
Nothing is running.
Your cost is $0.
Controller
Code Store
Executor
16. Under the hood
“Run my function hello and return the result”
Controller
Code Store
Executor
Function starts to execute within milliseconds.
You pay for the duration of the function.
Functions auto-scale with load.
GET /hello
23. Serverless use cases
Periodic and scheduled events
run function when ...
Integrated with event sources
crop images, analyze content, transform data, conversation bots
Batch processing
integrate with data and object stores
24. What about this Server part?
import ballerina/http;
endpoint http:Listener listener { port: 8080 };
@http:ServiceConfig { basePath: "/" }
service default bind listener {
@http:ResourceConfig { methods:["GET"], path: "/hello" }
hello(endpoint caller, http:Request req) {
var params = req.getQueryParams();
json body = params.hasKey("name") ? { name: params.name } : {};
http:Response res = new;
res.setPayload(msg(body));
_ = caller->respond(res);
}
}
25. Ballerina’s cloud-native edge
● Integrated application architecture
○ The “server” maps routes to functions
○ Generate Swagger → Configure API Gateway
26. Ballerina’s cloud-native edge
● Integrated application architecture
○ The “server” maps routes to functions
○ Generate Swagger → Configure API Gateway
● Functions as APIs and APIs as Functions
○ Single semantic-domain
● Integrate Functions in any language
27. Ballerina’s cloud-native edge
● Integrated application architecture
○ The “server” maps routes to functions
○ Generate Swagger → Configure API Gateway
● Functions as APIs and APIs as Functions
○ Single semantic-domain
● Integrate Functions in any language
● Leverage Ballerina’s built-in network type-system, error handling
30. Why Kubernetes?
○ Production-grade orchestration system
○ Infrastructure as code
○ Suited for microservice architectures
○ Easily extendable to build abstractions/APIs on to
31. Deploy a function
$ kubeless function deploy hello
--runtime ballerina0.980.0
--handler hello.foo
--from-file helloget.bal
import kubeless/kubeless;
public function foo(
kubeless:Event event,
kubeless:Context context)
returns (string|error)
{
return "Hello World Ballerina";
}