I’m building a web app with Astro using SSR and Deno for the backend. I have an API that connects to MongoDB, but I’ve noticed that the cold start time (the first request after restarting the server) is pretty long.
It’s killing my users’ experience, they’re waiting way too long. Like, I’m seeing load times around 10 seconds, which is just not acceptable.
Anyone else dealt with this kind of cold start optimization with Deno and MongoDB? I’d love to know:
- How can I speed up the MongoDB connection process in Deno?
- Is there any way to cache the results of queries to MongoDB so I don’t have to hit the database again on the first run?
- Maybe there are some Deno or Astro settings that could help make the API faster?
Here’s a snippet of my API code:
// api/users.ts
import { serve } from "https://deno.land/[email protected]/http/server.ts";
import { MongoClient } from "https://deno.land/x/[email protected]/mod.ts";
const handler = async (req: Request) => {
const client = new MongoClient("mongodb://localhost:27017");
await client.connect();
const db = client.database("mydatabase");
const collection = db.collection("users");
// ... code to handle the request and get data from the collection
return new Response(JSON.stringify(data), { headers: { 'Content-Type': 'application/json' } });
};
serve(handler, { port: 8080 });
I’m using MongoDB on my local machine, but I’m planning to move to a cloud database in the future.
I want to avoid using Node.js if possible.