OpenFaas | Serverless | Speed up business transformations made simple

Rajat Nigam
Engineered @ Publicis Sapient
5 min readSep 20, 2021

When application developers struggle to create a production-grade microservice, it is a key responsibility of the architect to bring in a platform that offloads the responsibility of managing non-functional requirements like traffic management, security, deployment/releases, observability, etc.

This blog aims to articulate the adoption of OpenFaas as a serverless framework, by showcasing the ease of bootstrapping development, testing and deployment efforts, also suggesting that the future of application development is in transition from microservice to serverless architectures on Kubernetes.

Introduction

Serverless is a concept where implementation goes a layer under where the only responsibility of developers is to focus on developing behavior using functions. And rest of the non-functional requirements are taken care of by the serverless framework.

OpenFaas makes it easy for developers to deploy event-driven functions and microservices to Kubernetes without repetitive, boiler-plate coding. Package your code or an existing binary in a Docker image to get a highly scalable endpoint with auto-scaling and metrics.

Scenarios

  • Event-driven data processing
    IoT, fraud detection, payment processing, website monitoring and real-time marketing.
  • Web applications
    A simple Serverless GraphQL API for MySQL, Postgres and Aurora.
  • Mobile applications
    If a user clicks the “Buy” button in a mobile application with a serverless backend, it can trigger a backend function or a series of functions that initiate, record transactions, and begin to deliver what the user purchases.
  • Application ecosystems
    FaaS is a highly abstract computing service in which the service provider fully controls the management of computing resources, including the runtime process of executing code.

    Serverless describes a specialized PaaS that combines FaaS to interact with other hosted Cloud services to create a cost-effective and scalable architecture.

    BaaS (Backend as a Service) provides back-end functions for browsers or mobile applications. It can be delivered on top of the stack without a server, or in a Container as a Service (CaaS) service, Infrastructure as a Service (IaaS), or other technology stacks.

    SaaS simply means providing any software-based system without running on your own infrastructure. A SaaS system can be built on any technology stack; what matters is how users consume it.
  • Event workflows
https://docs.openfaas.com/reference/triggers/

Architecture

The gateway can be accessed through its REST API, CLI, or UI. All services or functions expose default routes, but it is also possible to use custom domains for each endpoint. Prometheus collects metrics, which can be obtained through the gateway API and used for autoscaling. By changing the URL of the function from /function/name to /asyncfunction/name, the NATS flow can be used to execute calls in the queue. You can also pass an optional callback URL.

  • Simple, but usable functions (i.e. small, useable building blocks)
  • Scales with usage (servers are autoscaled on the user’s behalf)
  • Built-in availability and fault tolerance (i.e. NoOps)
PLONK Stack

The above can be achieved using the PLONK stack

Prometheus: Metrics and time-series
Linkerd: Service mesh
OpenFaaS: Management and auto-scaling of computes — PaaS/FaaS
NATS: Asynchronous message bus/queue
Kubernetes: Declarative, extensible, scale-out, self-healing clustering

Reference: https://www.openfaas.com/blog/plonk-stack/

System Requirements [along with arch]

Kind (Kubernetes In Docker): https://kind.sigs.k8s.io/
Binary Installation | faas-cli: https://docs.openfaas.com/cli/install/

Setup OpenFaas

Helm Chart
https://github.com/openfaas/faas-netes/tree/master/chart/openfaas

Edited values.yaml to make it run on Kind

Create a function

I’m writing this function in Golang for it’s the comfortable choice of secondary programming language for me after Java.

Bootstrap OpenFaas project
faas-cli new go-app --lang=golang-http

Implement controller

Unit Test

go test 

Deployment

Build an image into the local Docker library
faas-cli build -f go-app.yml

Push that image to a remote container registry
faas-cli push -f go-app.yml

Deploy your function into a cluster
faas-cli deploy -f go-app.yml

Gateway (OpenFaas UI)

Testing

Acceptance Testing

Setup
https://github.com/cucumber/godog
https://github.com/gkushang/cucumber-html-reporter

Script

  • cd godogs/
  • godog features/*
  • godog features/* — format=cucumber > cucumber.json
  • npm install cucumber-html-reporter — save-dev
  • node index.js
  • rm -rf node_modules/ package*

Output

Performance Testing

Setup
https://github.com/rakyll/hey

Script
hey -z 10s -m POST http://127.0.0.1:8001/function/go-app -d {“name”:”testUser”,”age”:18}

Output

Summary:
Total: 10.0556 secs
Slowest: 0.4814 secs
Fastest: 0.0147 secs
Average: 0.1410 secs
Requests/sec: 353.4352
Total data: 67526 bytes
Size/request: 19 bytes
Response time histogram:
0.015 [1] |
0.061 [233] |■■■■■■■■■
0.108 [1045] |■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
0.155 [1032] |■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
0.201 [665] |■■■■■■■■■■■■■■■■■■■■■■■■■
0.248 [313] |■■■■■■■■■■■■
0.295 [165] |■■■■■■
0.341 [52] |■■
0.388 [33] |■
0.435 [12] |
0.481 [3] |
Latency distribution:
10% in 0.0696 secs
25% in 0.0925 secs
50% in 0.1277 secs
75% in 0.1769 secs
90% in 0.2312 secs
95% in 0.2660 secs
99% in 0.3642 secs
Details (average, fastest, slowest):
DNS+dialup: 0.0000 secs, 0.0147 secs, 0.4814 secs
DNS-lookup: 0.0000 secs, 0.0000 secs, 0.0000 secs
req write: 0.0000 secs, 0.0000 secs, 0.0003 secs
resp wait: 0.1408 secs, 0.0146 secs, 0.4813 secs
resp read: 0.0000 secs, 0.0000 secs, 0.0004 secs
Status code distribution:
[200] 3554 responses

You can change from zero replicas or 0/0 expansion through the scale_from_zero environment variable of the OpenFaaS gateway. It is enabled by default in Kubernetes and faasd. The delay between accepting a request for an unavailable feature and a service request is sometimes called a “Cold Start.”

Cold Start in OpenFaaS is strictly optional, and it is recommended to avoid cold start for urgent operations. This can be achieved by not scaling key functions to zero replicas, or by calling them using asynchronous routing that separates the request time from the caller.

Alternatives

Conclusion

As compared to its alternatives, the adoption rate of OpenFaas is the highest with more than 20k GitHub stars. That results in huge community contributions. From the ease of debugging to the wide range of languages, OpenFaas speeds up functional deliveries. One of the impressive features is seamless integration with KEDA (Kubernetes-based Event-Driven Autoscaling) [https://github.com/kedacore/keda], which makes it easy to implement and test autoscaling applications from day one.

Reference

--

--