Proof of concept: support immutable trpc servers using lambdas to ensure client/server compatibility

Overview

auto-versioned-trpc-aws-lambda

Proof of concept to support an automatically versioned AWS Lambda running tRPC to ensure a somewhat graceful and automated handling of client/server compatibility. Note that the solution here is not really specific to tRPC.

The problem

The concept of removing the boundary between the frontend and backend environment as seen in libraries like tRPC, Remix, Next.js is a really pleasant and effective way of building web applications. But the abstraction is also leaky as it usually hides the fact that the frontend and backend environment cannot practically be updated at the same time – a stale web browser will eventually make calls to a newer version of the server.

Here we investigate a solution for tRPC. The main selling point of tRPC is "End-to-end typesafe APIs made easy", but this doesn't hold if you are not mindful about deployment or keeping your client/server versions in sync – something that isn't trivially solved.

Options for solving this

We have several options to try to solve the client/server version compatibility:

  • force clients to be in sync with the latest server. We cannot guarantee this, but we can nudge the user to reload their browser, update when the user navigates, and add a reload CTA in case of API failures for forms/actions.
  • versioning (manually or automated) the endpoints and gracefully keep old versions around until clients have migrated to the new endpoints. Manual versioning is standard practice but doesn't fit well with tRPC and Remix, where the actual endpoints are abstracted away.
  • backward compatible endpoints for a grace period until we expect the clients to be updated. This can likely be enforced by a type checker build time by checking old endpoints are still present and the input and output DTOs are a superset of previous versions.

How does framework X solve this?

Some observations when looking at how this is handled in different frameworks and tools:

  • GraphQL: ”avoid breaking changes by making it backward compatible”.
  • Next.js: automatically load the latest version in the background when routing. But API Routes (which are usually used for actions/forms) are not automatically versioned. This means you can easily break actions if you are not manually versioning the API Routes.
  • Remix: Any action on the page (e.g. forms) will break if the client is outdated and the loader is not compatible...
  • tRPC: the community suggests keeping the client up to date

Proposal: Multiple immutable auto versioned tRPC servers

In this proof of concept, we look into automatic versioning of endpoints. The idea: deploy multiple immutable tRPC servers – each prefixed with a hash of the content of the server.

Here we are using the node.js AWS SDK to deploy the AWS Lambda.

What goes on behind the scenes?

For every deploy:

  1. Check if any lambda matches the hash of the server (skip the rest if that is the case)
  2. Create a new lambda function + add a Function URL

Local development

Setup:

yarn install

Local development:

yarn start

AWS Provisioning

The deployment requires a bit of provisioning.

export AWS_REGION=eu-west-1
export SERVICE_LAMBDA_ROLE="versioned-trpc-lambda-role"

# create an IAM role for the lambda
aws iam create-role --role-name "$SERVICE_LAMBDA_ROLE" --assume-role-policy-document file://trust-policy-lambda.json

AWS Deployment

Run the deployment of the service.

AWS_REGION=eu-west-1 yarn esno scripts/deploy.ts

AWS Clean up

This should clean up most of the mess this proof of concept created in your AWS account...

# TODO: script to delete functions
aws iam delete-role --role-name versioned-trpc-lambda-role
You might also like...

SafeCycle—a tool that keeps cyclists safe. Gone are days of weaving through busy city streets, SafeCycle finds all the bike routes for you to ensure a smooth ride wherever you want to go.

Inspiration Biking—an everyday form of travel for students and professionals across the globe. On-campus, back home, and with the people that we know

May 2, 2022

🧩 TypeScript utility type in order to ensure to return only properties (not methods) containing values in primitive types such as number or boolean (not Value Objects)

🧩 TypeScript utility type in order to ensure to return only properties (not methods) containing values in primitive types such as number or boolean (not Value Objects)

🧩 TypeScript Primitives type TypeScript utility type in order to ensure to return only properties (not methods) containing values in primitive types

Dec 7, 2022

We are creating a Library that would ensure developers do not reinvent the wheel anymore as far as Authentication is concerned. Developers can easily register and download authentication codes that suits their need at any point.

We are creating a Library that would ensure developers do not reinvent the wheel anymore as far as Authentication is concerned. Developers can easily register and download authentication codes that suits their need at any point.

#AuthWiki Resource Product Documentation Figma Database Schema First Presentation Live Link API Documentation Individual Contributions User Activity U

Dec 2, 2022

Shield is a development framework for circom developers. The core reason is to provide libraries, plugins, and testing tools to ensure code quality and security.

Shield is a development framework for circom developers. The core reason is to provide libraries, plugins, and testing tools to ensure code quality and security.

SHIELD Shield is a development framework for circom developers but we plan it to other languages such as CAIRO, SNARKYJS etc. The core reason is to pr

Dec 22, 2022

🏗️ Figma Plugin for speeding up and ensure consistency in the structure of your Figma projects

🏗️ Figma Plugin for speeding up and ensure consistency in the structure of your Figma projects

🏗️ Codely Structurer Figma Plugin Figma Plugin for speeding up and ensure consistency in the structure of your Figma projects Stars are welcome 😊 👤

Dec 14, 2022

A fast Protocol 6 Agar.io client for private servers

A fast Protocol 6 Agar.io client for private servers

Cigar3 A fast Protocol 6 Agar.io client for private servers Getting started. After installing nodejs, run npm install and npm run cigar3 in a terminal

Mar 6, 2022

A minimal, mobile-ready, album-centered music client for subsonic compatible music servers.

A minimal, mobile-ready, album-centered music client for subsonic compatible music servers.

Soniclair An album-centered subsonic client for PWA, Android, Android TV and Desktop SonicLair is a minimal, mobile-ready, album-centered music client

Dec 13, 2022

Query for CSS brower support data, combined from caniuse and MDN, including version support started and global support percentages.

css-browser-support Query for CSS browser support data, combined from caniuse and MDN, including version support started and global support percentage

Nov 2, 2022

Interplanetary Database: A Database built on top of IPFS and made immutable using Ethereum blockchain.

IPDB IPDB (Interplanetary Database) is a key/value store database built on top of IPFS (Interplanetary File System). Project is intended to be an MVP

Oct 6, 2022
Comments
  • Showcase a working Datadog setup

    Showcase a working Datadog setup

    Just a hacky prototype demonstrating a working Datadog setup.

    Deploy using:

    EXTERNAL_DEPENDENCIES=datadog-lambda-js,dd-trace DD_API_KEY="INSERT_YOUR_KEY" yarn esno scripts/deploy.ts
    
    opened by skovhus 0
Owner
Kenneth Skovhus
Software engineer based in Copenhagen, Denmark
Kenneth Skovhus
A Web UI toolkit for creating rapid prototypes, experiments and proof of concept projects.

MinimalComps2 A Web UI tookkit for creating rapid prototypes, experiments and proof of concept projects. The site: https://www.minimalcomps2.com/ Full

Keith Peters 32 Apr 18, 2022
NFT vending machine proof of concept built on Solana Pay, Metaplex, Phantom Mobile and Next.js.

Solana NFT Vending Machine This is a proof of concept of an NFT vending machine built using Solana Pay, Metaplex, Phantom Mobile, and Next.js. This wa

Matt Rosenzweig 55 Dec 15, 2022
Simple Jai to WASM Proof-of-Concept

Simple Jai to WASM Proof-of-Concept Jai does not officially support WebAssembly compilation target. BUT! It allows you to dump LLVM IR via the llvm_op

Tsoding 52 Dec 14, 2022
A proof-of-concept malicious Chrome extension

Crux: Demonstration Malicious Chrome Extension This repository is for educational purposes only. If you use this outside of security research or autho

Michael Taggart 18 Nov 9, 2022
CVE-2022-22629 Proof of Concept

CVE-2022-22629 Proof of concept This post is about the poc for the WebGL bug that was patched in Safari 15.4 security updates. If you want to learn in

PARS Defense 45 Jan 9, 2023
A boilerplate project to build proof of concept paywalls with lightning service authentication tokens (LSATs)

lsat-workshop-boilerplate A boilerplate project to build proof of concept paywalls with lightning service authentication tokens (LSATs) Install Setup

Buck Perley 2 Oct 13, 2022
serverless plugin to setup custom domain for lambdas deployed with function url.

serverless-aws-function-url-custom-domain Automatically creates AWS CloudFront distribution and Route 53 records to AWS Lambda with Function URL (no a

Sha 8 Nov 27, 2022
Yet another linter rule to detect compatibility of CSS features.

stylelint-browser-compat Yet another linter rule to detect compatibility of CSS features. This plugin checks if the CSS you're using is supported by t

Masahiro Miyashiro (3846masa) 16 Dec 15, 2022
OpenAPI support for tRPC 🧩

trpc-openapi OpenAPI support for tRPC ?? Easy REST endpoints for your tRPC procedures. Perfect for incremental adoption. OpenAPI version 3.0.3. Usage

James Berry 841 Jan 9, 2023
next-graphql-server is a library for building production-grade GraphQL servers using Next.js with API Routes

next-graphql-server next-graphql-server is an easy to use Next.js library for creating performant GraphQL endpoints on top of Next.js API Routes. Star

Jakub Neander 82 Nov 21, 2022