djbalin
djbalin4w ago

Bandwidth usage multiplied by piping arguments?

Hellooo! I noticed that one of my daily cron jobs is incurring rater high bandwidth usage. We have a video streaming app, and this cron job collects all watch events within the past 24 hours (using the _creationTime index) and populates three aggregates with these documents. My specific question is this: Let's say I query 1 MB worth of data and call four functions with this as an argument:
const obj = await ctx.db.query("watchSessions").collect() // 1 MB of data
fn1(obj)
fn2(obj)
fn3(obj)
fn4(obj)
const obj = await ctx.db.query("watchSessions").collect() // 1 MB of data
fn1(obj)
fn2(obj)
fn3(obj)
fn4(obj)
Would this already incur +4 MB of bandwidth usage? As far as I can tell, objects are passed by reference as arguments to functions in JS, so I suppose not? What if instead of regular JS functions, I schedule/run four Convex functions:
const obj = await ctx.db.query("watchSessions").collect() // 1 MB of data
await ctx.runMutation(api.mut1,{obj})
await ctx.runMutation(api.mut2,{obj})
await ctx.runMutation(api.mut3,{obj})
await ctx.runMutation(api.mut4,{obj})
const obj = await ctx.db.query("watchSessions").collect() // 1 MB of data
await ctx.runMutation(api.mut1,{obj})
await ctx.runMutation(api.mut2,{obj})
await ctx.runMutation(api.mut3,{obj})
await ctx.runMutation(api.mut4,{obj})
2 Replies
Convex Bot
Convex Bot4w ago
Thanks for posting in <#1088161997662724167>. Reminder: If you have a Convex Pro account, use the Convex Dashboard to file support tickets. - Provide context: What are you trying to achieve, what is the end-user interaction, what are you seeing? (full error message, command output, etc.) - Use search.convex.dev to search Docs, Stack, and Discord all at once. - Additionally, you can post your questions in the Convex Community's <#1228095053885476985> channel to receive a response from AI. - Avoid tagging staff unless specifically instructed. Thank you!
ballingt
ballingt4w ago
Yep you've got it, calling functions javascript has no bandwidth cost, just like assigning to variables has no bandwidth cost. Running or scheduling 4 Convex functions charges you for 4 function calls, but there's no additional cost for bandwidth of the arguments or return values. In general, use helper functions! Convex functions are just TypeScript, make the outermost function that's wrapped in a mutation or action or query short and call other functions in it.