Serverless deployment
Even on Kubernetes, FTGO still sizes, patches and pays for servers that sit idle much of the day. This chapter shows how to hand that work to the cloud by deploying code as functions, and how to decide which services belong there.
key idea / coordinator service / good path in progress / watch out problem / failure neutral
In chapter 19 we packaged FTGO's services as container images, ran them on Kubernetes, described the infrastructure as code and released new versions safely. That left one problem open: the team still owns a cluster. Someone must size the nodes, patch them and pay for them, busy or not.
Can FTGO run some services without managing servers at all? By the end you can explain Function as a Service, check a workload against its limits, decide how many functions make up one service, and choose between PaaS, FaaS and containers for each service.
Stop running servers: Function as a Service
Restaurant Service answers menu lookups. It is busy at lunch and dinner and almost idle at 4 a.m., but its pods and the nodes under them run all night. When a node needs an operating system patch, an FTGO engineer drains it and reschedules pods. None of that work makes a customer's order arrive faster.
Serverless does not mean there are no servers. It means you stop managing them: the cloud provider runs, patches and scales them for you. Managed databases, queues and object storage are all serverless in this sense. The best-known kind is Function as a Service (FaaS); AWS Lambda started the excitement in 2014.
Deploy a function, not a server. The function sleeps until a trigger fires, runs your code, and stops. The platform starts as many copies as the load needs and none when there is no load.
On AWS Lambda, four kinds of trigger can start a function:
For Restaurant Service, each HTTP endpoint becomes a function. A request for GET /restaurant/1 goes through the AWS API Gateway to a FindRestaurant handler, which calls the same business code the service used before. Only the entry point changes.
| Benefit | Why |
|---|---|
| Pay for use | Idle code costs nothing, which suits low or unpredictable load like Restaurant Service at 4 a.m. |
| Less operations | The platform scales up and down and patches the machines |
| Implicit resilience | You get some high availability without building it |
FaaS runs your code on containers you cannot see or tune. You trade control for less work, and the platform sets hard rules about what a function may do.
What the platform will not let you do
Restaurant Service fits FaaS well. The next service the team tries does not.
The team moves Accounting's nightly settlement job to a function. On a busy Friday it runs past the platform's time limit and is killed halfway. The same week, the JVM-based Kitchen Service function delays the first ticket after a quiet spell, and a counter it kept in memory resets between calls.
Each of these is a documented limit. Check every one against the workload first.
| Limit | What it means |
|---|---|
| Runtime support | The provider must support your language. Azure had the widest support at the time; custom runtimes on AWS add upkeep. |
| Resources | You mostly choose memory only; CPU and I/O follow. You may buy memory just to get more CPU. |
| Execution time | Capped: about 9 minutes on Google and 15 on Lambda at the time. Long jobs, like the settlement run, are a poor fit. |
| Statelessness | A call cannot see the previous call's memory. State lives elsewhere, and chaining functions is hard. |
| Cold start | Starting a new instance is slow on the JVM and .NET; Go, Python, Node and Ruby start fast. Platforms keep runtimes warm, which helps. |
| Concurrency caps | There are hard limits on parallel runs, and fast scaling can overwhelm systems that do not scale (next section). |
Move short, event-driven work that keeps its state in a database or object store. Leave long jobs on containers, or split them into short steps. Pick a fast-starting runtime where the first call must be quick.
Splitting a long job into steps means one function must coordinate others, which plain FaaS does badly. Workflow tools fill that gap. Azure Durable Functions suspends a function and resumes it later, so it can orchestrate other functions in code. AWS Step Functions wires functions together with JSON configuration, which many teams find less friendly.
Each limit pushes work out of the function: state into a database, long jobs into containers, chains into a workflow tool. And automatic scaling creates a new problem for everything the functions call.
Functions scale faster than what they call
Automatic scaling removes capacity planning for the functions. It does not remove it for anything behind them.
At 12:00 on Saturday a thousand menu requests arrive within seconds. The platform starts hundreds of Restaurant Service function instances, and each opens its own connection to the restaurant database. The database has a fixed size and a connection limit. It slows down, then rejects connections, and the menu page fails for everyone.
A team at the media company Bustle hit exactly this. Whenever one part scales on its own and the next part does not, the slower part becomes the bottleneck.
Cap the function's concurrency to what the downstream system can take, buffer bursts in a queue so functions drain it at a steady rate, or scale the downstream system too, for example with a serverless database.
A concurrency cap or a queue gives back some of the elasticity you moved to FaaS for, and requests may wait. Scaling the database costs money. And once each endpoint is its own function, a new question appears: how many functions should one service become?
How many functions make one service?
With containers, one service was one deployable unit. With FaaS that link gets fuzzy.
The Restaurant Service team wants one function for every endpoint, then one for every state change of a restaurant. Soon they manage dozens of functions, each deployed on its own, and the rules that keep a restaurant consistent are spread across all of them.
A side example makes the options clear. Take an Expenses service with three endpoints: upload a receipt, submit a claim, and produce a report.
Start with one function per microservice: a single entry point that dispatches by path. Split into one function per aggregate only when you have a reason, and never go finer than the aggregate.
- Function per microservice. A good start. Callers see one service, and you deploy one unit.
- Function per aggregate. Keeps each aggregate's whole life cycle (chapter 3) inside one function. Keep the external interface coarse, so callers still see one Expenses API and you can regroup the functions later.
- Shared database. Fine while one team owns all the functions. Once their data needs diverge, give each its own data; at that point they are really separate microservices.
- Finer than an aggregate. Be wary. You get many more functions to manage, and the aggregate's integrity gets harder to protect.
A coarse function gives up some per-endpoint tuning; a split one adds units to deploy and watch. And FTGO still has to decide which of its services should use FaaS at all.
Pick a platform per service: PaaS, FaaS or containers
FaaS is the far end of a range of platforms. Each step along the range takes away work and control together.
One engineer proposes moving every FTGO service to FaaS, though Accounting's settlement job breaks the time limit and the rest run fine on Kubernetes. Another wants to avoid FaaS entirely to prevent lock-in.
A Platform as a Service (PaaS), with Heroku as the benchmark, sits between containers and FaaS. You push a technology-specific artifact and the platform runs it, often with scaling and databases included. It works well until you need to get under the hood, and the smarter its autoscaling tries to be, the more it can go wrong for unusual apps. Public cloud serverless products now fill much of the same need with managed pieces (brokers, databases, storage) that you assemble yourself.
| PaaS | FaaS | Containers (Kubernetes) | |
|---|---|---|---|
| You supply | Language artifact | A function | Container image |
| Scaling | Platform, sometimes tunable | Automatic, with concurrency caps | You configure it |
| Watch out | Little control when it misbehaves | Time, state and cold-start limits | Real operational complexity |
| Good fit | Standard web apps | Spiky or low load; event reactions | Broad workloads needing control |
Choose per service, not for the whole system. Evaluate in this order: serverless first, then containers, then VMs, then plain language packages, and stop at the first one the workload fits.
Rules of thumb:
- If it is not broken, do not fix it. Do not move a working service to follow fashion.
- Give up as much control as you are happy with, then a bit more. Prefer a good PaaS or FaaS if the service fits.
- Containers are a good compromise on isolation and cost, and if you need them, Kubernetes is probably in your future.
- On a public cloud, if the problem fits FaaS, try it before Kubernetes. Do not let fear of lock-in trap you in a mess of your own making.
For FTGO, that might mean Restaurant Service's menu lookups and image uploads on FaaS, and Order, Kitchen and Accounting, with their long jobs and steady load, staying on the Kubernetes cluster from chapter 19.
A mix means two or three platforms to learn, secure and observe. And whichever platform a service runs on, its code still needs a reliable, repeatable path from a developer's commit to production.
Patterns in this chapter
| Pattern | Problem it solves | What it costs |
|---|---|---|
| Function as a Service | Servers to size, patch and pay for while idle | Less control; limits on runtime, resources, time, state and concurrency |
| Fit the workload to the function | Long jobs killed, slow cold starts, lost in-memory state | State and long jobs move elsewhere; chains need a workflow tool |
| Concurrency caps and queue buffering | Functions scale out and overload a fixed-size downstream system | Some elasticity lost; requests may wait |
| Function per microservice, then per aggregate | Too many tiny functions and scattered aggregate rules | Less per-endpoint tuning; splits add units to manage |
| Highest-level platform that fits, per service | One platform forced on every service, or fashion-driven moves | Several platforms to run, secure and observe |
Recap
- Serverless means the provider runs the servers; FaaS runs a function only when a trigger such as an HTTP request, an event, a schedule or an API call fires it.
- FaaS brings pay-per-use, less operations work and some built-in resilience, in exchange for less control over the containers your code runs on.
- Check every workload against the limits on language, resources, execution time, state, cold starts and concurrency before moving it.
- Functions scale out faster than fixed-size databases and caches, so cap concurrency, buffer with a queue or scale the downstream system too.
- Start with one function per microservice, split to one per aggregate only for a reason, and do not go finer.
- Pick the highest-level platform that fits each service, and expect to mix FaaS, PaaS and containers in one system.
Quick check
Show answer
A scaling mismatch: the functions scaled out much faster than the fixed-size database, and each instance opened its own connections. Cap the function's concurrency to what the database can take, put a queue in front to smooth the burst, or move to a database that scales with the load.
Show answer
Not as it is. Execution time is capped (about 15 minutes on Lambda at the time), so the job would be killed. Keep it on containers, or split it into short steps coordinated by a workflow tool such as Durable Functions or Step Functions.
FTGO now knows where each service should run: functions, a PaaS or the Kubernetes cluster. But on any platform, code still has to travel from a developer's commit to production through builds and tests that everyone trusts.