I am using the firebase Stripe API, and what is happening is my app doesn't have a lot of traffic yet, nor will it for a little while. Firebase decided, after 2-3 minutes of no invocations on the function, it goes into cold start mode. This is unfortunate because it means my wait time from when a new user hits register, and goes to the checkout page, it is like 8 seconds. How horrendous is that!
Anyways, does anyone know a way around this, maybe setting a script to run in the background at all times, or something I can do from inside firebase?
One way to help is to add a "cold-start" command to the Cloud Function (i.e. a "no-op" invocation/call), and invoke it when your user starts the checkout process (before collecting any information). If the User doesn't complete check-out, no-harm-no-foul; if they do, the cloud function has already been started.
Update 2020-01-01:
Firebase now allows you to designate for each function, in the console, a minimum (and/or maximum) number of invocations - i.e. keeping functions in memory. A single active function costs about $0.33 to $0.50 per month - a fairly low (but not zero) cost for keeping cold starts down...
Related
In my project i am using lambda function with the timeout configuration for 25 seconds.
If i didn't hit any api's for sometime and after that if i hit the api , the time exceeds 25 seconds and throwing me Time out error(Internal server error) in postman.And after that if i hit api's it is working.
To avoid cold start i am using lambda ping using cloudwatch event rule which is triggered for every one minute.
If i speak about logs in cloudwatch , i am getting logs upto certain program flow and it stops.But the point where the program flow stops differs for each time , from this i probably can confirm that there is no problem where the code goes wrong.
I am using javascript formy project.
Cloudwatch Events to keep a lambda warm are great, but only do so much.
If you make a deployment to your lambda, it starts a new Version, and that version hasn't started up yet so it may still encounter the Cold Start
If your lambda is getting hit hard and has lots of invocations, AWS will provision a second concurrent execution, and this will get hit with the Cold Start
And a few other less likely scenarios, but in effect you still hit the cold start. Somewhere, somehow, a cold start has to happen.
You have two options: 1) increase the timeout of your lambda. But likely, given that you are already up to 25 seconds, you should probably be considering breaking this into multiple lambdas, using a Step Function if you have a flow, or figuring out event driven activation of the various components
adjust your Provisioned Concurrency. This value, along with cloud watch events, is the most useful part of keeping lambdas warm - it is saying 'Hey, always have at least this many concurrent executions going at a given time'
I am trying to create an app that simulates opening a tab at a bar. I am running into one issue that I can't seem to figure out - it goes as follows:
When someone opens a bar tab, dynamically create a scheduled task that executes code to close the tab after 24 hours.
If the tab gets closed before the 24 hours, cancel the scheduled task.
If the tab doesn't get closed after 24 hours, execute the code described in step 1 to initiate a payment on the card used to open the tab.
I was initially looking into Firebase Functions, and was thinking about using a setTimeout() callable function, but after doing some research I found that Firebase Function's cannot be invoked for longer than 9 minutes.
NOTE: I would like this to be dynamic. Meaning, having it account for a variable amount of users. There could be 100 or 1000 users on the platform, each of them needs the ability to have a unique scheduled task for them (sometimes multiple per user).
Please see the comments for the full solution.
There are multiple approaches to circumvent the 10 minutes rule (which is prevalent in the serverless code) but here's something that can help you. I suggest separating the task into three:
A cloud function that close the tab when called.
A schedule function that calls it (https://firebase.google.com/docs/functions/schedule-functions)
A way to start and stop the schedule function.
I am not sure how firebase function work, but I worked with azure functions before and those can be controlled with command line (CLI) or with a sdk for your language of choice. To cancel using the command line, try something like this:
firebase functions:delete scheduledFunction
from How to cancel a scheduled firebase function?.
Now what's left is how to figure out how to start the function, and if it's possible to pass in a parameter to schedule it.
Good luck!
I would like the messenger bot to notify the user that their session ended if they did not write/give input to the bot for some time. In order to do so, I first thought about using setTimeout() for each user which will reset upon activity. But that means if there will be 100 active users, there will be 100 Timeouts at the same time.
I wanted to know, if having one Interval instead that checks through each user`s session end timestamp every 30-60 seconds a better approach? The active users are stored in memory.
setTimeout is more precise in your case: each session will be ended independently and closer to the time it was supposed to end. It will also spread the activity more evenly. Since JS is single-threaded, the timeouts will not fire in parallel, even though there will be hundreds of them at the same time.
setInterval will create spikes of activity every 30-60 seconds, and will sometimes let the sessions stay alive longer than they should.
As per the cost of multiple timeouts running at the same time, please see this answer.
Just to be clear my understanding of long polling is that you make request to a server on a time interval.
I am trying to implement a bitcoin purchasing system that checks the blockchain for change in my wallets balance. I know there are websockets that do this but I have to wait for 1 confirmation to receive an update and the REST API offers more flexibility, so I would just prefer to make a request to the server every 5 seconds or so and check each response for a change in my balance then go from there.
The issue is I can't seem to figure out how to do this in NodeJS. Functionially this is how I imagine my code.
Get current balance (make request)
Get current balance again (make request)
Check if there is a difference
**If not**
wait 5 seconds
Get current balance
Check for difference
repeat till different (or till timeout or something)
If different
do some functions and stop checking balance.
I've been trying to do each step but I've gotten stuck at figuring out how to create a loop of checking the balance, and stopping the loop if it changes.
My original thought was to use promises and some for loops but that doesn't materialize.
So now I am asking for your help, how should I go about this?
One way to do this would be to setup a setInterval timer to kickoff a request every x seconds. By setting some logic after the response you can then choose to de-reference the timer and trigger another function. Here's a snippet. You'll notice I set a variable to reference the timer, and then de-reference it by setting it to null, where then the GC is smart enough to release. You may also use the 'clearTimeout' function, which is perhaps the better way to go.
My app's framework is built around collapsing backbone models sending the data via websockets and updating models on other clients with the data. My question is how should I batch these updates for times when an action triggers 5 changes in a row.
The syncing method is set up to update on any change but if I set 5 items at the same time I don't want it to fire 5 times in a row.
I was thinking I could do a setTimeout on any sync that gets cleared if something else tries to sync within a second of it. Does this seem like the best route or is there a better way to do this?
Thanks!
i haven't done this with backbone specifically, but i've done this kind of batching of commands in other distributed (client / server) apps in the past.
the gist of it is that you should start with a timeout and add a batch size for further optimization, if you see the need.
say you have a batch size of 10. what happens when you get 9 items stuffed into the batch and then the user just sits there and doesn't do anything else? the server would never get notified of the things the user wanted to do.
timeout generally works well to get small batches. but if you have an action that generates a large number of related commands you may want to batch all of the commands and send them all across as soon as they are ready instead of waiting for a timer. the time may fire in the middle of creating the commands and split things apart in a manner that causes problems, etc.
hope that helps.
Underscore.js, the utility library that Backbone.js uses, has several functions for throttling callbacks:
throttle makes a version of a function that will execute at most once every X milliseconds.
debounce makes a version of a function that will only execute if X milliseconds elapse since the last time it was called
after makes a version of a function that will execute only after it has been called X times.
So if you know there are 5 items that will be changed, you could register a callback like this:
// only call callback after 5 change events
collection.on("change", _.after(5, callback));
But more likely you don't, and you'll want to go with a timeout approach:
// only call callback 30 milliseconds after the last change event
collection.on("change", _.debounce(30, callback));