Chapter 20 of 27 · Part 9: Deployment and scaling

Serverless deployment

Deploy services as functions: what serverless gives, what it limits, and how to map functions to services.

key idea / coordinatorservice / good pathin progress / watch outproblem / failureneutral

Serverless

Upload functions and let the cloud run and scale them.

Serverless with AWS LambdaInvoke a Lambda byHTTP via AWS API GatewayAWS events (S3, DynamoDB, Kinesis)A schedule (cron-like)A direct API callTrade-offsBenefitsno sysadmin, elastic, pay per useDrawbackscold-start latency, short tasks only
Restaurant Service as Lambda functionsHTTP clientGET /restaurant/1API Gatewayroutes requestLambda handlerFindRestaurantRestaurant svcsame business codeOne function per endpoint. Order to evaluate: serverless, containers, VMs, then language packages.

Function as a Service

Serverless means servers still exist but you stop managing them. FaaS is the most talked-about kind (AWS Lambda started the excitement in 2014), but serverless also covers managed databases, queues and storage. With FaaS you deploy a function, it sleeps until a trigger fires, runs, then stops.

Trigger, run, stopFile arrivesQueue messageHTTP requestFunctionyour codeResultwrite, reply, emitruns, then stopsThe platform starts and stops instances and runs many copies in parallel.
BenefitWhy
Pay for useIdle code costs nothing; good for low or unpredictable load
Less operationsThe platform handles scaling up and down
Implicit resilienceSome high availability without you building it

From Building Microservices, ch. 8, "Function as a Service (FaaS)".

Limits and challenges

FaaS runs on containers you cannot see, which gives you less control. Check each limit against your workload.

LimitWhat it means
Runtime supportThe provider must support your language (Azure widest at the time; custom runtimes on AWS add upkeep)
ResourcesYou mostly choose memory only; CPU and I/O follow. You may buy memory just to get CPU
Execution timeCapped: about 9 minutes on Google, 15 on Lambda at the time; long jobs are a poor fit
StatelessnessA call cannot see the last one's memory; store state elsewhere. Chaining functions is hard
Cold startNew instance startup is slow on JVM and .NET, though platforms keep runtimes warm; Go, Python, Node and Ruby start fast
Concurrency capsHard limits on parallel runs; scaling functions can overwhelm things that do not scale
Scaling mismatchSpike1000 eventsFunctionsscale out fastRedis / DBfixed capacityOverloadOne part scales dynamically, another does not: the mismatch hurts (a team at Bustle hit this).
Cold starts and statelessnessCold startStateless callsFirst callstart runtimeNext callswarm, fastCall 1sets x = 5Call 2cannot see xput state in a databaseor object storeworst on JVM and .NETPlatforms keep runtimes warm, so the effect is often smaller than feared.

Tooling for chains: Azure Durable Functions suspends and resumes a function so it can orchestrate others. Newman finds it friendlier than AWS Step Functions, which wire functions together with JSON configuration.

WebAssembly (Wasm) is a sandboxed, lightweight code format that may one day challenge containers on the server, but it lacks mature server platforms. Treat it as something to watch.

From Building Microservices, ch. 8, "Function as a Service (FaaS)": limitations and challenges.

Mapping functions to microservices

With FaaS the link between a microservice and a deployable unit gets fuzzy. Start with the simple mapping; split only for a reason.

Two mappingsFunction per microserviceExpenses function: one entry point/receipt/claim/reportFunction per aggregateReceipt fnClaim fnReport fnShared or separate databaseBoth keep one coarse "Expenses" API for callers.

From Building Microservices, ch. 8, "Mapping to microservices".

PaaS, FaaS or containers?

A PaaS (Heroku is the benchmark) runs your technology-specific artifact and often hides scaling and databases. 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 by offering managed pieces (brokers, databases, storage) you assemble yourself.

The control-versus-effort spectrumMore control, more workLess control, less workPhysical / VMyou run everythingContainersyou orchestratePaaSpush an artifactFaaSpush a functionStay as far right as your workload and comfort with limits allow.
PaaSFaaSContainers (Kubernetes)
You supplyLanguage artifactA functionContainer image
ScalingPlatform, sometimes tunableAutomatic, with concurrency capsYou configure it
Watch outLittle control when it misbehavesTime, state and cold-start limitsReal operational complexity
Good fitStandard web appsSpiky or low load; event reactionsBroad workloads needing control

Newman's rules of thumb for where to deploy:

Mix, do not choose one. The BBC uses Lambda for part of its News site and EC2 where per-invocation cost would be too high. For Kubernetes users, Knative aims to bring a FaaS workflow to Kubernetes but needs a service mesh (Istio, in practice) and was still maturing; Newman points more cautious teams to OpenFaaS. For a deeper look at containers and Kubernetes, see lesson 19.

From Building Microservices, ch. 8, "Platform as a Service", "Which Deployment Option Is Right for You?" and "And Knative".

Recap

Quick check

Autoscaled functions slow down your product database under a spike. Which FaaS challenge is this, and what do you do?
Show answer

A concurrency mismatch: functions scale out faster than the database can. Cap concurrency, buffer with a queue, or scale the downstream system too.

A team wants to split an Expenses microservice into one function per state change. Why is Newman wary?
Show answer

It multiplies functions and weakens the aggregate as the unit of integrity. A function per aggregate is the finer limit he is comfortable with.