When to Use Serverless, and When to Use Kubernetes
If you are stuck at a crossroads and need some help deciding, here are some conditions which might help you make your selection.
Join the DZone community and get the full member experience.Join For Free
a question that i get asked very often is: should i use serverless or kubernetes to build cloud-native applications? both computing options have pros and cons and it depends on your needs which option you should choose.
ansgar schmidt and i will give a session next week about this topic. we are still finalizing our slides, but have finished the slide which summarizes when to use kubernetes and when to use serverless.
note that we don’t compare serverless with containers, but with kubernetes. with the open source serverless platform openwhisk you can build functions with docker containers as well. we also don’t compare one simple serverless function with a sophisticated microservices application on kubernetes, but applications with similar complexities.
use serverless if...
you have variable and irregular workloads
a good serverless scenario is a local website which doesn’t get much or any traffic during nights. since serverless platforms only charge for the duration your code runs, this can lead to significantly lower costs. the longer applications don’t do anything, the better are the chances that serverless is cheaper.
however serverless doesn’t automatically mean lower costs, like when your applications need to run 24/7. there can also be some hidden costs like extra costs for api management or the costs for the function invocations for tests.
you need developer productivity for first time experience
if you neither have experience in serverless nor in kubernetes, it’s easier to get a first hello world application running on serverless platforms. when you use kubernetes, you typically have to wait some time to create a cluster, configure kubernetes to get a public ip address, and then deploy your first container. with serverless platforms you can simply use web tooling provided by the cloud providers to get started in minutes.
however, serverless isn’t always easier than kubernetes. building and managing a serverless application with a bunch of functions is more difficult than a simple kubernetes application with only one container. in fact, it might be easier to use kubernetes for more complex applications because the platform is more mature (see below for more).
you need inherent auto-scalability
one of the great capabilities of serverless is the inherent auto-scalability of functions, since as a developer you don’t have to do anything to leverage this functionality. with kubernetes you can also use auto-scalability of your pods and even nodes, but it requires some configuration and it is a little slower since this process is only triggered when certain rules apply.
however, kubernetes might provide better scalability features than some serverless platforms, since kubernetes is more mature and provides even ha (high availability) between different zones which not all serverless platforms provide yet.
use kubernetes if...
you need mature deployment options
i’m not aware of any serverless platform that supports a/b testing, which i consider a key capability for building cloud native applications. additionally, the monitoring capabilities of kubernetes applications are much more mature. for example, with istio you can see the execution time of microservices, which service invoked which other services and whether there are bottlenecks. serverless platforms don’t really have this yet and started only rather recently to add features like amazon step functions and openwhisk composer to define flows between functions.
however, if your application is rather simple, with perhaps only one function to provide an api, serverless might be a better option for you, since the deployment would be easier and monitoring for single functions is provided by the various serverless platforms.
you require minimal response latency
when using serverless platforms the first invocation of a function takes some time since the code needs to be initialized. for example, in openwhisk you can use docker containers which can run java applications that take some time to start. if you need fast and reliable response times, you should use kubernetes.
serverless platforms like openwhisk have improved significantly recently by doing a lot of caching. after the very first cold start you shouldn’t see these longer response times anymore which might be sufficient for your applications.
you want high performance computing without resource restrictions
serverless platforms typically have certain resource restrictions, for example, functions cannot use more than 512 mb ram and cannot take longer than 5 minutes. if these restrictions are too strict for your applications, you need to use kubernetes.
however, sometimes it might be possible to break up your applications in smaller functions. in some cases this might even be desirable, for example when moving an existing monolithic application into the cloud.
i think it will be interesting to observe how these technologies will evolve this year. i’ve heard about several activities to make kubernetes easier as well as several activities to make serverless more mature.
if you want to try out serverless and kubernetes, you can get a free lite account (no credit card required) on the ibm cloud .
Opinions expressed by DZone contributors are their own.