I heard that Cloud Functions 2nd gen is built on top of Cloud run which supports Websockets.
Does this mean that Cloud Functions 2nd gen can also handle Websockets?
https://cloud.google.com/blog/products/serverless/cloud-run-gets-websockets-http-2-and-grpc-bidirectional-streams
https://cloud.google.com/blog/products/serverless/cloud-functions-2nd-generation-now-generally-available
I don't think so.
Cloud Functions are a FAAS, fully on-demand serverless. The purpose of a function is to come to life when an event occurs (pub/sub trigger, http request, etc), perform some compute logic and than die.
Web sockets do require a connection alive, so Cloud Functions don't fit well for that.
Go for Cloud Run (low learning curve coming from functions - no docker knowledge required) or investigate the option to use Firebase Datastore to have client/server read/write and stay in synch (even easier then web socket).
Related
Firebase function v2 is here.
Now as per the docs:
Function instances can now execute more than one request at a time. This feature is available for functions with one dedicated CPU or higher. Concurrency levels can be set on a per-function basis or across all functions with the setGlobalOptions method. If unspecified, new functions with 1 dedicated CPU or higher will default to 80 concurrent requests. See Allow concurrent requests.
Now my doubt is if a Function instance spun up for handling functionX will also receive traffic for functionY? OR, this means functionX instance will receive concurrent traffic only for just functionX?
Firebaser here. An instance of functionX will only receive concurrent traffic for functionX. In Cloud Functions for Firebase, an instance always only consists of a single function (functionX or functionY, never both).
Implementing it works, but I have read that due to how Cloud Functions are designed they are not the best way to use socket.io. Why?
Actually, socket.io does not work with Cloud Functions. Cloud Functions have the following properties that make them incompatible with long-lived socket connections:
The maximum duration of a Cloud Function can only be 9 minutes. The socket will be forced closed after that time. This is counter to the normal expectation of socket.io to keep a socket connection alive indefinitely.
Cloud Functions will read the entire contents of the request, and only then will write the entire contents of the response. There is only one full round trip - a client can not "chat back and forth" over the connection with the function.
See also
Google Cloud Functions with socket.io
Run a web socket on Cloud Functions for Firebase?
Is it possible to host a express and socket.io app on Firebase Hosting?
Cloud Functions are made for simple requests, they are not made for long-running processes. If you want to stick with serverless architecture, try Cloud Run. They released an update this year (January 2021) and the platform is now able to support WebSockets including Socket.io.
Here is the link to the Cloud Run documentation
Here is the link to the blog post (their announcement)
I have a situation where I host a high RPS highly available service that receives requests aka commands. These commands have to be sent to N downstream clients, who actually execute them. Each downstream client is separate microsevice and has different constraints like mode (sync,async), execution cadence etc.
Should a slow downstream client build the logic to receive all requests and execute them in batches as they want ? Or my service should build logic to talk to slow and fast clients by maintaining state for commands across downstream clients. Share your opinions
Not enough info to give any prescriptive advice, but I'd start with dividing the tasks into async and sync first. Those are 2 completely different workloads that, most likely, would require different implementation stacks. I'll give you an idea of what you can start with in the world of AWS...
Not knowing what you mean by async, I'd default to a message-bus setup. In that case you can use something like Amazon Kinesis or Kafka for ingestion purposes, and kicking off Lambda or EC2 instance. If the clients need to be notified of a finished job they can either long-poll an SQS queue, subscribe to an SNS topic, or use MQTT with websockets for a long-running connection.
The sync tasks are easier, since it's all about processing power. Just make sure you have your EC2 instances in an auto-scaling group behind an ALB or API Gateway to scale out, and in, appropriately.
This is a very simple answer since I don't have any details needed to be more precise, but this should give you an idea of where to get started.
Suppose I have multiple AWS Lambdas making a request to some HTTP API, is there any documentation how these requests will be seen on the API side in terms of throttling. Will they be treated as requests coming from different hosts or sometimes (or always) the same one?
You should assume no 2 runs of the same Lambda function will even run on the same machine. AWS might reuse the same instance to run your function, but that's to save the time spent downloading the code on every function call.
You can here from the AWS Lambda FAQ page found here, the following:
Q: Will AWS Lambda reuse function instances?
To improve performance, AWS Lambda may choose to retain an instance of
your function and reuse it to serve a subsequent request, rather than
creating a new copy. To learn more about how Lambda reuses function
instances, visit our documentation. Your code should not assume that
this will always happen.
Q: Why must AWS Lambda functions be stateless?
Keeping functions stateless enables AWS Lambda to rapidly launch as
many copies of the function as needed to scale to the rate of incoming
events. While AWS Lambda’s programming model is stateless, your code
can access stateful data by calling other web services, such as Amazon
S3 or Amazon DynamoDB.
Also, to understand more take a look at this interesting blog post (back from 2014)
If you place your Lambda functions outside of a VPC, your API will see requests coming from different hosts or sometimes the same one (Which is unpredictable since AWS reuses already provisioned Hot Lambda functions to handle requests for the same configuration)
However if you place your Lambda functions inside a VPC in a private subnet while configuring a NAT gateway for egress traffic, your API will see them coming from the NAT gateway IP address.
Depending on your rquirement you can use either the approaches where mostly the second option is needed for security purposes to whitelist IPs for ingress traffic to the API.
I'm currently building a Koa app with the Firebase Node library. Is there a speed difference between using that compared to REST?
This is something that would best be determined by some profiling or jsperf-style testing.
The simplest answer is that there would naturally be a difference, particularly at higher frequencies of transcations. The node.js SDK works over a socket connection, whereas the REST client would have the overhead of establishing and tearing down connections with each payload.
One helpful tool that can help narrow the gap in performance is to utilize HTTP 1.1's keep-alive feature. However, it's certainly not going to be comparable to web sockets.
Firebase is a web-socket real-time database provider, so it's much more faster than HTTP REST calls, that has a big overhead of creating a new connection for each call, so you can use this link below to have an idea:
http://blog.arungupta.me/2014/02/rest-vs-websocket-comparison-benchmarks/