#Reduce Cost with Caching Approach Check

9 messages · Page 1 of 1 (latest)

surreal tundra
#

Hey guys, title could have been better, but I am basically trying to setup an XML product feed for my store, which has 20k+ products including variants, and it constantly grows and shrinks throughout each day. I want the feed to rerun every 15 minutes, and I want to host this on vercel. I am worried about serverles costs since this is a function, below is my code with my approach I am doing caching, is this the most efficient way to handle this to reduce costs in Vercel? Is serverless (vercel) hosting bad for this use case?

// app/api/product-feed/route.ts

import { NextResponse } from 'next/server';
import { kv } from '@vercel/kv';
import { BigCommerceClient } from './bigcommerce-client';
import { generateXMLFeed } from './xml-generator';

const CACHE_KEY = 'product-feed-cache';
const CACHE_TTL = 15 * 60; // 15 minutes in seconds

export async function GET() {
  try {
    // Check cache first
    const cachedFeed = await kv.get(CACHE_KEY);
    if (cachedFeed) {
      return new NextResponse(cachedFeed, {
        headers: { 'Content-Type': 'application/xml' },
      });
    }

    // If not in cache, generate new feed
    const client = new BigCommerceClient();
    const products = await client.getAllProducts();
    const xmlFeed = generateXMLFeed(products);

    // Cache the new feed
    await kv.set(CACHE_KEY, xmlFeed, { ex: CACHE_TTL });

    return new NextResponse(xmlFeed, {
      headers: { 'Content-Type': 'application/xml' },
    });
  } catch (error) {
    console.error('Error generating product feed:', error);
    return new NextResponse('Error generating product feed', { status: 500 });
  }
}

// Scheduled job to update cache (run every 15 minutes)
export async function POST() {
  try {
    const client = new BigCommerceClient();
    const products = await client.getAllProducts();
    const xmlFeed = generateXMLFeed(products);
    await kv.set(CACHE_KEY, xmlFeed, { ex: CACHE_TTL });
    return new NextResponse('Cache updated successfully', { status: 200 });
  } catch (error) {
    console.error('Error updating cache:', error);
    return new NextResponse('Error updating cache', { status: 500 });
  }
}

Then I would setup a cron job in vercel

still baneBOT
#

🔎 This post has been indexed in our web forum and will be seen by search engines so other users can find it outside Discord

🕵️ Your user profile is private by default and won't be visible to users outside Discord, if you want to be visible in the web forum you can add the "Public Profile" role in id:customize

✅ You can mark a message as the answer for your post with Right click -> Apps -> Mark Solution
(if you don't see the option, try refreshing Discord with Ctrl + R)

wind spruce
#

dedicated server will be cheaper than vercel 🙂

surreal tundra
#

Thats what I was thinking

#

But is my approach still the best way or?

torpid swallow
surreal tundra
#

Thanks!

surreal tundra
torpid swallow
#

@vercel/kv and @vercel/blob are more expensive than their "backend" counterparts (https://service-markup.vercel.app). but honestly, if you are worried about costs, self hosting is cheapest at scale