Sarıkaya Dev Logo

Build Serverless Edge AI Functions with Deno Deploy and Supabase Edge Runtime

Mahmut Sarıkaya 5 min read 5 Views 0
Build Serverless Edge AI Functions with Deno Deploy and Supabase Edge Runtime

Ever wondered how a single line of JavaScript can run an image‑recognition model at the edge, milliseconds before the user even sees the page?

Understanding Serverless Edge Functions

Edge functions execute code in geographic proximity to the end‑user, reducing round‑trip latency to under 20 ms for many global regions. Unlike traditional cloud VMs, they spin up in milliseconds, charge per request, and scale automatically. Deno Deploy and Supabase Edge Runtime are built on V8 isolates, meaning you write pure JavaScript (or TypeScript) without worrying about container orchestration.

Getting Started with Deno Deploy

Deno Deploy offers a free tier that includes 1 GB of bandwidth and 1 M requests per month—enough for a prototype AI endpoint. First, create a project on the Deno Deploy dashboard, then link a GitHub repository. The runtime ships with the latest stable Deno version, so you can use top‑level await and the standard library out of the box.

Below is a minimal HTTP handler that accepts a JSON payload, runs a placeholder AI model, and returns the inference result. Save this file as main.ts in your repo.

import { serve } from \"https://deno.land/std@0.203.0/http/server.ts\";\n\nserve(async (req) => {\n  const { prompt } = await req.json();\n  const result = await runModel(prompt); // runModel uses TensorFlow.js or ONNX\n  return new Response(JSON.stringify({ result }), { status: 200, headers: { \"content-type\": \"application/json\" } });\n});\n\nasync function runModel(input) {\n  // Placeholder: replace with actual model loading\n  return \"Echo: \" + input;\n}\n

Deploy the branch; Deno Deploy will provide a URL like https://your‑project.deno.dev. You can test it with curl -X POST -H "Content-Type: application/json" -d '{"prompt":"hello"}' https://your-project.deno.dev. The response should contain {"result":"Echo: hello"}.

Integrating Supabase Edge Runtime for Data Access

Most AI services need to log requests, fetch model versions, or retrieve user‑specific thresholds. Supabase Edge Runtime lets you run the same JavaScript code inside Supabase’s edge network, sharing the same V8 isolate technology. Create a new Supabase project, enable the Edge Functions beta, and add the SUPABASE_URL and SUPABASE_ANON_KEY as environment variables in the dashboard.

The snippet below demonstrates how to insert an inference log into a Supabase table called inference_logs. Place the file in functions/log_inference.ts and publish it via supabase functions deploy log_inference.

import { createClient } from \"https://esm.sh/@supabase/supabase-js@2.39.0\";\n\nconst supabase = createClient(\n  Deno.env.get(\"SUPABASE_URL\")!,\n  Deno.env.get(\"SUPABASE_ANON_KEY\")!\n);\n\nexport default async function handler(req) {\n  const { prompt, result } = await req.json();\n  const { data, error } = await supabase\n    .from(\"inference_logs\")\n    .insert({ prompt, result, created_at: new Date().toISOString() });\n  if (error) {\n    return new Response(JSON.stringify({ error: error.message }), { status: 500 });\n  }\n  return new Response(JSON.stringify(data), { status: 201, headers: { \"content-type\": \"application/json\" } });\n}\n

Now your Deno Deploy endpoint can call this edge function after each inference, keeping latency low while persisting data securely.

Running AI Inference at the Edge

TensorFlow.js and ONNX Runtime Web are both compatible with Deno’s V8 engine. For a lightweight image classifier, you can load a pre‑converted TensorFlow.js graph (.json + binary weights) from a CDN. The following example loads a MobileNet model and classifies a base64‑encoded image sent in the request body.

import * as tf from \"https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@4.12.0/dist/tf.min.js\";\n\nlet modelPromise = null;\nasync function loadModel() {\n  if (!modelPromise) {\n    modelPromise = tf.loadGraphModel(\"https://cdn.jsdelivr.net/npm/@tensorflow-models/mobilenet@2.1.0/model.json\");\n  }\n  return modelPromise;\n}\n\nexport async function runModel(base64Image) {\n  const img = tf.node.decodeImage(Buffer.from(base64Image, \"base64\"), 3);\n  const resized = tf.image.resizeBilinear(img, [224, 224]).div(255).expandDims();\n  const model = await loadModel();\n  const predictions = model.predict(resized);\n  const top = predictions.argMax(-1).dataSync()[0];\n  return top; // numeric class ID\n}\n

Because the model is cached after the first request, subsequent inferences cost only the compute time (typically 30‑50 ms on Deno’s edge VMs). Pair this with the Supabase logging function to build a complete serverless AI pipeline.

Performance Tips and Cost Considerations

1. Cold‑start mitigation: Warm the function by scheduling a ping every 5 minutes using a GitHub Action or a third‑party cron service. This keeps the V8 isolate alive and cuts latency by 40 % on average.
2. Payload size: Keep request bodies under 100 KB. Large images should be resized client‑side before upload; otherwise, edge bandwidth charges can exceed the free tier quickly.
3. Model size: Models larger than 5 MB increase cold‑start time dramatically. Split the model into layers and lazy‑load optional branches if you need higher accuracy.
4. Monitoring: Use Deno Deploy’s built‑in metrics and Supabase’s pg_stat_statements view to track average latency and request count. Set alerts when average latency exceeds 100 ms.

Conclusion

By combining Deno Deploy’s ultra‑fast edge runtime with Supabase’s seamless data layer, you can ship AI inference endpoints that respond in under 100 ms worldwide, all with pure JavaScript and a serverless pricing model. The key steps are: spin up a Deno function, load a lightweight TensorFlow.js or ONNX model, and log results via a Supabase Edge Function. With the free tiers of both platforms, developers can prototype production‑grade edge AI without upfront infrastructure costs.

Sources

• Deno Deploy Documentation – deno.com/deploy/docs
• Supabase Edge Functions Guide – supabase.com/docs/guides/functions/edge-functions
• TensorFlow.js Model Repository – tfjs.dev/model/mobilenet

Author: Mahmut Sarıkaya — sarikayadev.com

Tags: #Deno Deploy #edge functions #serverless JavaScript #Supabase Edge Runtime #AI inference
Share:
M

Written by

Mahmut Sarıkaya

Software Developer

Comments

No comments yet. Be the first to share your thoughts!

Leave a Comment

8 + 7 =