ai-assisted-todolist

Serverless: Lambda, Go and DynamoDB#

Status: ported (#547), and serving qa and prod. Gunnar chose this direction on 2026-11-09 (#535), and the spike that had to measure it first passed. The Quarkus backend it replaced went in

559, and its infrastructure, Quarkus on Fargate behind a load balancer with PostgreSQL on RDS, in#

557. The git tag pre-serverless marks the last main before the move, for a rollback in the#

worst case; the rollback-tags ruleset keeps anyone, administrators included, from deleting or moving it.

Why#

An environment cost about $1.86 a day while it was up on the containers (cost.md), almost all of it billed by the hour whether anyone used it or not: the load balancer, the Fargate task, the database instance and the task's public IPv4 address. So environments were taken down when idle, and env.sh up and down were a daily chore that was easy to forget: qa once ran four days unnoticed.

Serverless bills per request instead. At TaskFest's traffic, an environment that is always up costs about $1.80 a month at list price (cost.md):

Price in eu-central-1 At demo traffic
Lambda, arm64 $0.20 per million requests, $0.0000133334 per GB-second inside the free tier
API Gateway HTTP API $1.20 per million requests cents
DynamoDB on demand $0.7625 per million writes, $0.1525 per million reads; 25 GB free cents
S3 for attachments unchanged cents
CloudFront, Route 53 unchanged the zone's $0.50 a month
CloudWatch Logs $0.63 per GB after 5 GB free cents

Lambda's free tier is 1 million requests and 400,000 GB-seconds a month, account-wide, and does not expire.

No load balancer, no NAT gateway, no VPC for the backend at all, and so no public IPv4 address. Nothing to take down, and prod can simply stay up.

The shape#

  taskfest.cloud.hilling.de
            │
            ▼
       CloudFront ──── /*      ──> S3                  (the built SPA, unchanged)
            │
            └───────── /api/*  ──> API Gateway ──> Lambda (Go, arm64) ──> DynamoDB
                                    HTTP API              │
                                                          └──> S3 (attachments)

The code#

serverless/ (opens in a new tab) is one Go module; its conventions are in serverless/CLAUDE.md.

Locally, against the end-to-end stack (end-to-end-stack.md), whose DynamoDB Local is on port 8000, SeaweedFS on 8333 and Keycloak on 8082, with the stack's own backend stopped (docker compose -f deployment/docker/docker-compose.e2e.serverless.yml stop backend frontend):

cd serverless
TABLE_NAME=taskfest-local DYNAMODB_ENDPOINT=http://localhost:8000 AWS_REGION=eu-central-1 \
  AWS_ACCESS_KEY_ID=taskfest AWS_SECRET_ACCESS_KEY=taskfest-secret \
  TASKFEST_ATTACHMENTS_BUCKET=taskfest-attachments S3_ENDPOINT=http://s3.localhost:8333 \
  TASKFEST_AUTH_ENABLED=true TASKFEST_AUTH_PROVIDERS=keycloak TASKFEST_COOKIE_SECURE=false \
  TASKFEST_OIDC_KEYCLOAK_ISSUER=http://localhost:8082/realms/taskfest \
  TASKFEST_OIDC_KEYCLOAK_CLIENT_ID=taskfest-backend \
  TASKFEST_OIDC_KEYCLOAK_CLIENT_SECRET=taskfest-secret \
  TASKFEST_OIDC_KEYCLOAK_AUDIENCES=taskfest-app go run ./cmd/api

Tasks#

The board and creating, replacing and deleting a task (#550) keep every rule of the Quarkus backend's TaskResource, and its tests are ported case for case: internal/board holds the rules, internal/api the requests, internal/store the items.

Drafts#

Starting, adding and discarding a draft (#551) keep the Quarkus backend's DraftResource, and its tests are ported case for case; its files are attachments like a task's.

Attachments#

A task's files and links (#552) keep the Quarkus backend's AttachmentResource, and its tests, AttachmentThumbnailTest, LinkAttachmentTest and DraftTaskTest are ported case for case: internal/board holds the rules, internal/api the requests against an in-memory store and bucket, internal/store the items against DynamoDB Local, internal/files the signed links against LocalStack with signature checking on. Files still travel straight between the browser and S3, through presigned links signed for exactly the announced size and type, and the bucket is the same one.

The Go backend keeps to serverless/ in it (#578). Both backends ran side by side until the cut-over, each with its own database, and shared each environment's bucket, where the Quarkus backend's files are still at the top level:

The containers' backend The Go backend
Files <key>, <key>.thumbnail serverless/attachments/<key>, and .thumbnail
Deletion records deleted-accounts/<hash> serverless/deleted-accounts/<hash>

Each orphan clean-up took only its own files, and each replay only its own records, which it kept for as long as its own restores needed them: the RDS snapshots for the one, DynamoDB's restore points for the other. The Quarkus clean-up listed the whole bucket and left out every key with a slash, so its code needed no change. IAM keeps them apart as well: the Go functions' roles reach objects under serverless/ only. Callers in the Go code name a file by its bare key, and internal/files adds the prefix.

Sign-in#

The Go backend signs in as the Quarkus backend does (authentication.md), with the same paths, callbacks included, so the redirect URIs registered at Google and Cognito stay valid. The website signs in through the backend and gets a cookie; the apps send a bearer ID token, or the token of an app session they trade one for (#281, authentication.md). Every provider must vouch for the email, a bearer ID token must have been issued to one of the provider's named clients and be an ID token, and the provider list and the Gravatar fallback are unchanged. The function checks bearer tokens itself, not API Gateway's JWT authorizer: the cookie needs the function anyway, and one place decides for both.

Two things differ, by Gunnar's decision on #549:

Both kinds of item carry expiresAt, the table's TTL attribute (#554), and the backend checks the times itself as well, since TTL deletes late. A busy session records its latest request at most every five minutes. A request with no or an ended session gets a 401: Quarkus answered the website's scripts a 499 instead, and the frontend treats both alike. A sign-in that fails at the callback (a second tab's, a callback used twice, one cancelled at the provider) goes back to the app, signed out, rather than to Quarkus's blank 401. Signing in or out also expires any q_session and q_auth cookie left from the Quarkus backend.

Devices#

A device (#277) is a phone an app registered for push notifications with PUT /api/devices/{installationId}, by the installation id the app generated once. Gunnar's decision: a device lasts as long as the app session that registered it.

Push#

After a change to a task is written, the API hands it to the push function, which sends it to the owner's devices but the one that made it (#277, the decision).

Deleting an account#

DELETE /api/account (#553) keeps the decision and the Quarkus backend's tests: the record serverless/deleted-accounts/<SHA-256 of the email> is written to the attachment bucket first, where a restore of the table cannot take it back, and without a bucket an account is deleted without one. Two things differ, by Gunnar's decisions on

553:#

One gap is accepted, as in the Quarkus backend (Gunnar's decision on #572): two deletions of the same account at the same moment share one record, and if one of them cannot be marked, taking its record back also takes the other's. It needs both at once, a DynamoDB failure on exactly one, and a later restore; closing it would take a conditional delete in S3 or a lock on the account.

The replay#

cmd/replay deletes again any account a restore brought back, and prunes the records no restore can need any more; internal/replay keeps every rule of Quarkus's DeletionReplay, whose tests are ported. EventBridge Scheduler runs it daily, at 03:30 Berlin time. Lambda has no start-up to run it in, so the restore procedure below runs it once against the restored table before the API is switched to it, and nobody can sign in to a restored account first (Gunnar's decision on

553).#

Restoring the table#

A DynamoDB restore always creates a new table; the old one stays until it is deleted. What was written between the restore point and the switch is lost, as with the database's snapshot restores (database.md), and files uploaded since are left to the orphan clean-up. A restore is billed by the size restored, cents for TaskFest. Every step runs as the administrator, with its profile.

  1. Restore into a new table, from point-in-time recovery (prod) or a backup:
    aws dynamodb restore-table-to-point-in-time --source-table-name <table> \
      --target-table-name <table>-restored-<date> --restore-date-time <time>
    aws dynamodb restore-table-from-backup --backup-arn <arn> \
      --target-table-name <table>-restored-<date>
    aws dynamodb wait table-exists --table-name <table>-restored-<date>
  2. Run the replay against it, before anything else can reach it. It asks for the MFA code itself when the profile needs one, each time it runs, since it keeps no session the way the AWS CLI does:
    (cd serverless && AWS_PROFILE=<admin profile> AWS_REGION=eu-central-1 \
      TABLE_NAME=<table>-restored-<date> TASKFEST_ATTACHMENTS_BUCKET=<bucket> go run ./cmd/replay)
    It logs one JSON line, deletion-replay, with how many accounts it deleted again; it exits non-zero when one could not be, and is then run again.
  3. Switch the functions to the new table, as the administrator, in the environment's root. OpenTofu forgets the old table, which stays, protected from deletion, until it is deleted by hand; table_name in terraform.tfvars names the restored one; OpenTofu takes it over; deletion protection, which a restore does not carry over either, is set by hand on its own first; and the apply sets the rest a restore leaves out, TTL, the stream and point-in-time recovery, and points the functions, their roles and the stream's mapping at it:
    tofu state rm module.environment.aws_dynamodb_table.data
    # table_name = "<table>-restored-<date>" in terraform.tfvars
    tofu import module.environment.aws_dynamodb_table.data <table>-restored-<date>
    # Deletion protection first, alone. The apply would send it together with the stream, and
    # DynamoDB refuses that: "DeletionProtection modification must be the only operation in the
    # request" -- the apply then fails half done, as in the trial on #553.
    aws dynamodb update-table --profile <admin profile> --region eu-central-1 \
      --table-name <table>-restored-<date> --deletion-protection-enabled
    aws dynamodb wait table-exists --profile <admin profile> --region eu-central-1 \
      --table-name <table>-restored-<date>
    # Must print true before the apply; the waiter alone can return before the change shows.
    aws dynamodb describe-table --profile <admin profile> --region eu-central-1 \
      --table-name <table>-restored-<date> --query Table.DeletionProtectionEnabled
    and then env.sh up <env>, naming the administrator's profile for it, as TASKFEST_<ENV>_AWS_PROFILE=<admin profile> (infrastructure.md). The change of table_name reaches main just before, in a pull request; until this step has run, an apply of main would try to replace the old table and fail on its deletion protection.
  4. Redeploy the release that runs, with deploy-serverless.yml and its tag: the apply changed the functions' settings, which reach what serves only as a newly published version (#555, Deploying a release). Until then the API serves server errors for whatever needs the table: the apply has given its role the restored table, while the version that serves still names the old one. So this follows the apply at once; should it fail, the API stays down until it is run again, or until table_name is set back and applied.
  5. Run the replay again, now against the switched table, at least five minutes after the switch. The run in step 2 passed over the deletions of its last five minutes, which may still have been under way, and deletions kept being recorded until the switch; this run takes both. Until it has run, an account deleted in those minutes is back. Deletions are not stopped during a restore, which would take the API down for its length; the window is minutes, and only for accounts whose owners have just deleted them (Claude's decision on #554, which Gunnar confirmed on 2026-11-12).

Tried in qa on 2026-11-13 (#553), from an on-demand backup of taskfest-qa-data into taskfest-qa-data-restored-2026-11-13, which qa has used since. An account was deleted after the backup: it came back with the restore, the replay in step 2 deleted it again (reapplied: 1), the one in step 5 found nothing more (reapplied: 0), and signing in afterwards began a new, empty account. The restore took about five minutes. Two steps were found missing and are written in above: exporting the CLI's credentials for the replay in step 2, and the deletion protection set on its own before the apply in step 3, which had failed half done without it; set by hand, the apply went through. The old table and its backup stay until deleted by hand.

Why Go#

Measured cold starts of a real request after a pause, from published benchmarks:

Cold start
Quarkus on the JVM 3.3 s
Quarkus with SnapStart and priming 0.6 to 1.0 s
Quarkus native 0.56 to 0.72 s
Go about 0.46 s for a real app (2021, at 128 MB); a bare start 45 ms today
Rust about 0.12 s; a bare start 17 ms

Warm requests take about 5 ms in all of them. A demo is idle most of the time, so most visits start cold, and the cold start is what users notice. Go starts fast, compiles to one static binary for arm64, and its AWS SDK is first-class. Rust starts faster still, but not by an amount anyone would notice, at a far higher cost in writing it.

What speaks against it#

The spike first#

Before anything is ported, one endpoint, GET /api/tasks, is built in Go and run in qa beside the current backend, to measure the cold start in Frankfurt rather than trust benchmarks:

The spike's function and table, and serverless/spike/measure.sh, which uploaded the code, wrote twenty tasks for the test account taskfest-test-one@example.com and printed Lambda's own initialisation, cold and warm times, were removed with #554, which replaced them with the infrastructure. They are in the git history before it.

What the spike measured#

Run on 2026-11-10 in qa: five forced cold starts per memory size, each followed by three warm calls, all reading the board of twenty tasks. Times in milliseconds, from Lambda's REPORT lines.

Memory Initialisation First call Cold, together Warm Memory used
128 MB 58–70 1015–1159 about 1100 3–5, two at 50 34 MB
256 MB 57–70 432–545 490–615 3–5 35 MB
512 MB 58–72 180–265 240–340 3–5 38 MB

That is ten times faster than the JVM on Lambda without SnapStart, and faster than Quarkus with it, so the port goes ahead in Go, at 512 MB.

With the connection opened during initialisation#

Run again on 2026-11-10 with the foundation (#548): cmd/api, which queries DynamoDB once while it initialises (Store.Prime).

Memory Initialisation First call Cold, together Warm Memory used
256 MB 115–148 4–45 120–190 3–5, one at 16 34 MB
512 MB 120–142 4–5 125–146 3–8 39 MB

The infrastructure#

serverless.tf and serverless-jobs.tf in deployment/aws-tofu/modules/environment/ (#554), in both environments alike. The functions, the API and the table bill per request; what is fixed is in cost.md, the alarms most of it.

The background functions are in serverless-jobs.tf, each with a role for exactly what it does:

The alarms, each $0.10 a month, mail through the existing topic on entering ALARM and on leaving it: the API function's errors and throttles, the HTTP API's server errors, at least five in one five-minute period (#558, Gunnar's threshold), the HTTP API's request count, at least 20,000 in an hour (#607), the stream function more than ten minutes behind (it reports a failed record rather than failing, so its errors would never show one), the scheduled functions' errors, push's errors (#277), and the table's throttled reads and writes. That is ten per environment, and the orphan alarm the eleventh.

The dashboard taskfest-<env>-go-backend (#558, serverless-dashboard.tf) shows the requests and their answers, the time they take, the API function's time, errors and throttles, the jobs' runs and errors, how far the stream function is behind, and the table's reads, writes and throttles. It is free: CloudWatch includes three dashboards per account, and these are the second and third.

The functions are created from serverless/dist/<command>.zip, which serverless/build.sh makes, with Go where it is installed and the official Go image through podman or docker where it is not. OpenTofu uploads the code only when it creates a function; a release deploys its own (#555).

Applying it needs the administrator (infrastructure.md). From the repository root on main:

serverless/build.sh
(cd deployment/aws-tofu && TASKFEST_QA_AWS_PROFILE=<admin profile> ./env.sh up qa)

The plan it shows says what will change before anything happens.

Deploying a release#

A release deploys the code (#555), as it deploys the site:

A traffic-weighted canary was decided against (#555, the decision).

A function created by OpenTofu starts with what build.sh made, and its alias on that unpublished code. The first deploy pins the alias on a published version of it before uploading anything, so the upload does not serve the new code before its check. Code that changes what the functions may touch goes out with the release after the apply that changes their roles, or by hand before it, with the administrator, as #578's did:

for env in qa prod; do
  for c in api streams orphans replay logship push; do
    AWS_PROFILE=<admin profile> aws lambda update-function-code --region eu-central-1 \
      --function-name "taskfest-$env-$c" --zip-file "fileb://serverless/dist/$c.zip" >/dev/null
  done
done

Once a release's deploy has pinned the aliases on published versions, code uploaded so changes the unpublished code only, and the aliases keep their version until a deploy moves them. Before the first deploy, the aliases are still on the unpublished code, as OpenTofu creates them, and an upload serves at once, unchecked.