Serverless architecture is a software design pattern in which long-lived server processes are replaced with a combination of third-party cloud services and event-driven ephemeral functions.
As technology evolves and the market demand for applications soar, software development patterns are changing to keep up with the pace. Over the last few years, several innovations such as virtual machines and containers have allowed developers to migrate more of their operations to the cloud. The latest innovation is serverless computing where cloud vendors are, at least in principle, in charge of all operations.
Serverless architecture is a software design model that offloads the cost and complexity of server management to cloud vendors. It encourages the use of third-party services and serverless functions to save on operational costs and increase the productivity of developers.
Serverless is an abstraction in the same manner as “the cloud,” meaning that it hides operational complexity. Under the hood, vendors are definitely using servers. But from the developer’s point of view there are no servers that need managed.
Imagine that you’re building a text analysis application. The non-serverless approach is to make a single package with everything needed to do the job. The serverless approach, on the other hand, is to break it down in multiple serverless functions.
For example, take the following function to count words:
addEventListener("fetch", event => {
event.respondWith(handleRequest(event.request));
});
function countWords(str) {
return str.trim().split(/\s+/).length;
}
async function handleRequest(request) {
try {
const url = await request.text();
const response = await fetch(url);
const text = await response.text();
const count = countWords(text);
return new Response(count.toString());
} catch (e) {
return new Response(e.stack || e, {
status: 500
});
}
}
Note: You can try this function in StackPath’s Serverless Sandbox. Copy the code, switch the right panel to Raw mode, and change the method to POST. In the Body, type the URL to analyze. (Text websites work best.)
Once the function is deployed, it’s just a matter of calling it from the browser or mobile device:
fetch('http://"your_app_url.com/api/word/count', {
method: 'POST',
headers: { "Content-Type": "text/plain" },
body: "http://$URL_TO_ANALIZE"
})
.then((res) => {
do_something(res);
});
Adding more functionality is easy. Simply create another function. For example, you could change the count function to do some ranking and get word usage analysis:
function getFrequency(str) {
let frequency = {};
const words = str.trim().split(/\s+/);
for (i = 0; i < words.length; i++) {
if( words[i] in frequency ) {
frequency[words[i]]++;
}
else {
frequency[words[i]] = 1;
}
}
return frequency;
}
function sortByCount(words) {
let sorted = [];
for (let word in words) {
sorted.push([word, words[word]]);
};
sorted.sort(function(a, b) { return b[1] - a[1];});
return sorted;
}
Since pricing is based on usage, you can cut down costs by caching previous word counts. You could, for instance, provision a key-value service to act as a cache. With it, clients can directly try the cache before calling the functions.
As you can see, the serverless approach makes it possible to go from an idea to a prototype and from a prototype to a production-grade application with little effort. New features can be experimented with quickly and applications can grow more organically.