Containers vs Serverless: Which One Should You Choose and When?

Containers vs Serverless: Which One Should You Choose and When?
July 9, 2021 Harpreet Janeja

Containers vs serverless has been doing the rounds for a while now.

All enterprises today want a modern, future-proof architecture. And both containers and serverless can help you achieve them.

When used correctly, they both help DevOps teams deploy applications faster and economically. 

Although when comparing the features of containers vs serverless architectures, overlapping occurs in some respects yet they are not interchangeable technologies. Containers work better for some use cases, while in others, serverless is just what you need. 

We want to highlight the similarities and differences between containers and serverless computing services and explain why a DevOps team might choose one technology or the other, depending on that team’s needs. 

containers and serverless

What are containers?

pexels tom fisk 3063470 scaled

Containers allow you to deploy individual applications (or, in some cases, complete operating systems) inside portable, isolated environments. 

Containers share system resources with the host server rather than emulating a virtual operating system, which makes them more efficient than virtual machines. 

By isolating an application from the external host environment, containers facilitate a frictionless application deployment.  

If your host server supports a container, you can deploy containerized applications on it without having to worry about tweaking the application configuration or fighting with environment variables to get things running.  

For the same reason, containerized applications can be easily moved between hosts. 

What is serverless?

MicrosoftTeams image 74

Serverless computing allows application code to run on demand.  

From a user’s perspective, the only work necessary to run a code is to upload it and trigger when it should perform. Hence, it is called serverless—there is no server to be maintained. 

A major advantage of serverless is that it allows users to execute code whenever they need, without having to maintain a host environment on an ongoing basis. This is an economical option, particularly in cases where you need to execute resource-intensive code.

Similarities between containers and serverless 

Containers and serverless are different. However, they do have certain overlapping functionalities. 

  • Both are more efficient than virtual machines. 
  • Both abstract applications away from the host environment. 
  • Both allow you to deploy application code. 
  • Both require orchestration tools to use effectively at scale.

Containers vs serverless

Functionalities Containers Serverless
Supported host environment Runs on any modern Linux server and certain versions of Windows Runs on specific host platforms, mostly public cloud (like AWS Lambda or Azure Functions)
Self-servicing ability Set up your own on-premises host environment or use a public cloud service like ECS. Allows you to use only a public cloud.
Cost You can set up a container environment yourself using open-source code for free. These environments are hosted in the cloud, you must pay to use them.
Supported languages You can containerize an application written in any type of language if the host server supports the language. Supports a limited number of languages and may vary from platform to platform.
Statefulness Containers present their own storage challenges yet creating stateful containerized apps is certainly possible. Most serverless platforms are designed to support stateless workloads.
Availability Containers can run for as long as you need. Serverless functions are designed to run for a brief period—usually, a few hundred seconds—before they shut down.
containers vs serverless

When to choose containers?

pexels anna nekrashevich 6801874 scaled

Containers are great if you want the flexibility to install and utilize software with specific requirements. With containers, you have the advantage of choosing the operating system and having full control of the installed programming language and runtime version. 

You can also operate containers with different software stacks throughout a large container fleet–especially if you need to migrate an old, legacy system into a containerized environment. As a bonus, many tools for managing large-scale container set-ups (like Kubernetes) come with all the best practices already baked in. 

This flexibility comes with an operational price tag. Containers require a lot of maintenance and set-up. 

For maximum benefit, you will need to divide your monolithic application into different microservices, which in turn need to be rolled out as individual groups of containers. This means you will require tooling that allows all those containers to function with each other. You will also need to do the work of keeping their operating systems current with regular security fixes and other updates. 

While you can tune the container orchestration platform to automatically handle traffic fluctuations for you (a.k.a, self-healing and auto-scaling), the task of detecting traffic pattern changes and spinning the containers up or down will not be instantaneous. A full shutdown where no container-related infrastructure is running at all will also not happen. There will always be runtime costs. 

When to choose serverless?

MicrosoftTeams image 77

Serverless is a great fit if you need traffic pattern changes to be automatically detected and handled instantly. The application can be completely shut down if there is no traffic at all.  

With serverless, you only pay for the resources you use, no usage, no costs. 

The serverless developer does not have to worry about administrating underlying infrastructure; they must only care about the code and the business value to end users.  

Iteration can be faster here, as code can be shipped faster without any set-up or. In fact, since the underlying infrastructure is removed, the developer may not even know what it looks like. Also, they do not have to. 

But currently, there are certain limitations in terms of vendor support and ecosystem lock-in. Programming languages and runtimes are limited to whichever the provider supports. Event sources are usually services that the specific cloud provider offers. 

Reasoning about all the individual pieces of the application stack becomes harder when the infrastructure and the code are so separate. Serverless is a bit newer, and its tools still have room to evolve. That is what we are actively working on here at Serverless.com, anyway. 

Final verdict of containers vs serverless

Choose containers and container orchestrators when you need flexibility, or when you need to migrate legacy services. Choose serverless when you need the speed of development, automatic scaling and significantly lowered runtime costs. 

serverless computing services

Image sources:

Cover picture: Kevin Ku from Pexels

Image 1: Tom Fisk from Pexels

Image 2: Field Engineer from Pexels

Image 3: Anna Nekrashevich from Pexels

Image 4: Christina Morillo from Pexels