apollo-server icon indicating copy to clipboard operation
apollo-server copied to clipboard

Serverless Subscriptions

Open schickling opened this issue 7 years ago • 47 comments

During last the AWS Reinvent event they announced WebSockets support for API Gateway in combination with AWS Lambda. (See example using the Serverless framework.)

This opens the door for Serverless Subscriptions™️ which means instead of running stateful long-running servers, you can use Lambda functions to handle connections and process messages. Making this possible will require us to rethink how subscriptions are being implemented and deployed.

I'd like to open up a discussion and brainstorm API design ideas. 🚀

schickling avatar Dec 23 '18 12:12 schickling

Great idea!

ErickWendel avatar Dec 26 '18 18:12 ErickWendel

I just want to publish here my solution which could provide you with some sort of a base to start implementing :) https://github.com/michalkvasnicak/aws-lambda-graphql

Also @schickling had good question about the need of storage https://spectrum.chat/graphql/general/graphql-subscriptions-over-websockets-with-aws-api-gateway-v2-and-aws-lambda~4cf780af-9891-4dcc-b4ed-1202216f59fa

We could minify the need of storage only to subscriptions, which we need to store somewhere with connection info. Events don't need to be processed in another lambda instance but can be fired from current lambda that is publishing them. So some sort of Memory PubSub could be implemented.

For example it could be a hybrid of PubSub and event processor:

https://github.com/michalkvasnicak/aws-lambda-graphql/blob/master/packages/aws-lambda-graphql/src/PubSub.ts#L47 there could be a code to fetch all registered subscriptions so basically you just need to move this code https://github.com/michalkvasnicak/aws-lambda-graphql/blob/master/packages/aws-lambda-graphql/src/createDynamoDBEventProcessor.ts#L74 to publish method.

So implementation can be rather simplified :)

michalkvasnicak avatar Jan 14 '19 20:01 michalkvasnicak

That's awesome to learn about WebSocket support on gateway/lambda, sounds like a great option.

In my case, I'm running apollo-server on Google Cloud Functions, and so I'm looking for a more general solution to handling subscriptions that isn't coupled to a specific provider.

I'd like to be able to continue running queries and mutations via the serverless function (whether that be GCF, AWS Lambda, etc.) and have a long-running server (i.e. AppEngine) handle the web socket connections and function as a proxy for the subscription operations, and then use split from apollo-link in my client to route subscriptions there and queries/mutations to the serverless function. Communication between the two would use some pubsub service (i.e Google CloudPubSub) where the subscription server would subscribe to relevant topics, and be notified via publish events triggered after mutations on the serverless function.

It sounds like this general approach is discussed in the spec:

In large scale subscription systems, the Subscribe() and ExecuteSubscriptionEvent() algorithms may be run on separate services to maintain predictable scaling properties. See the section below on Supporting Subscriptions at Scale.

...

Supporting subscriptions is a significant change for any GraphQL service. Query and mutation operations are stateless, allowing scaling via cloning of GraphQL server instances. Subscriptions, by contrast, are stateful and require maintaining the GraphQL document, variables, and other context over the lifetime of the subscription.

Consider the behavior of your system when state is lost due to the failure of a single machine in a service. Durability and availability may be improved by having separate dedicated services for managing subscription state and client connectivity.

I'm wondering how involved the subscription server needs to be, specifically, whether it needs full knowledge of the schema or if it could just handle the client connection state and proxy operations to the serverless function. If not, I'm assuming I'm essentially going to have to run the full GraphQL server on AppEngine as well and just ensure operation splitting happens in our clients.

If anyone has done anything along these lines, I'd appreciate any help/suggestions!

stephenhandley avatar Jan 16 '19 22:01 stephenhandley

@stephenhandley Yes it needs to be aware of schema because on each event you are running the operation that has been used to subscribe against schema.

So basically it works like you have subscription XYZ {} stored somewhere on a server and then on received event from PubSub you are running it against schema. https://github.com/michalkvasnicak/aws-lambda-graphql/blob/master/packages/aws-lambda-graphql/src/createMemoryEventProcessor.ts#L71

I have a few interfaces there that you could use to implement subscriptions for google cloud functions but you will need to somehow create a layer that will emit events for Websocket connection in similar manner as AWS API Gateway v2 is doing.

--

I'd like to try implement subscriptions in ApolloServer by implementing subscription support to apollo-server-lambda. Maybe if it wasn't coupled to subscriptions-transport-ws it could be easier to support different connection/subscription schemas.

michalkvasnicak avatar Jan 17 '19 08:01 michalkvasnicak

@michalkvasnicak awesome thank you, have been looking at your repo and its a great jumping off point. will let you know once i make some progress.

stephenhandley avatar Jan 17 '19 23:01 stephenhandley

@stephenhandley how did it go? I'm looking to do the same thing with a repo I've been working on, and running into the same issue. It's Apollo Server deployed on GCF. Was thinking along the same lines as you, that lambdas are a perfect fit for traditional request-response APIs, but it seems like they don't make much sense for subscriptions over websockets because the connections are stateful and potentially long-lived. How do you prevent the function (lambda) from shutting down prematurely, cutting off the connection? You would have to make it self-aware in that regard, only shutting down when it's sure there are no subscriptions. Right? Does that make sense? If so, how would I even start to go about that? Is this a problem that's already been figured out and a solution exists somewhere?

Otherwise, would be great to split the two services up as you suggest, with the traditional API served over the lambda and subscriptions on a long-lived server, with the two communicating somehow. Was wondering if you or anyone else had made any progress there.

nateq314 avatar Mar 08 '19 13:03 nateq314

@nateq314 sorry for the delay responding. I actually ended up punting on this because we were only needing subscriptions for a small section of our app, and so we just used an off the shelf push service to handle that outside graphql as a stopgap solution.

That said, I'm still interested in figuring this out though, just haven't had the bandwidth. I don't think what you've outlined in the first paragraph would be viable from a cost perspective. at that point, it might make more sense to just run the whole thing on appengine or some other long lived runtime.

I'll check back in once I've had a chance to properly dig into this. Another thing I've been meaning to do which you might be interested in looking into how AWS App Sync appears to handle a similar process using subscriptions along with serverless deployment of GraphQL.
https://docs.aws.amazon.com/appsync/latest/devguide/system-overview-and-architecture.html

stephenhandley avatar Mar 28 '19 21:03 stephenhandley

Ta-da https://github.com/fanout/apollo-serverless-demo

jkarneges avatar Aug 01 '19 03:08 jkarneges

@nateq314 connections are not managed by Lambda but by API Gateway.

superandrew213 avatar Aug 01 '19 05:08 superandrew213

@jkarneges Why not use API Gateway Websockets (tutorial) instead of Fanout?

hakimio avatar Aug 01 '19 06:08 hakimio

Hi @hakimio, the main benefit of Fanout is that it's pub/sub-based, so you can send data to multiple WebSocket clients with a single publish API call. For example, if you wanted to broadcast a sports score to thousands of people, Fanout is more optimized for that.

Fanout could also be useful for anyone not using AWS, for example people deploying to something like Zeit instead of Lambda. Our implementation currently relies on DynamoDB for state but that could be substituted.

Finally, it's worth noting that Fanout's proxy is open source (https://github.com/fanout/pushpin). So you can run it locally, or with OpenFaaS, etc.

jkarneges avatar Aug 01 '19 15:08 jkarneges

@michalkvasnicak made a similar example using AWS API Gateway v2 Websockets, lambda and DynamoDB -> aws-lambda-graphql. If you are already using so many Amazon services, why not go all the way.

hakimio avatar Aug 01 '19 17:08 hakimio

Yup if you're already on AWS then using API Gateway WebSockets is certainly a fine way to go. Just mentioning another option.

jkarneges avatar Aug 01 '19 17:08 jkarneges

@hakimio aws-lambda-graphql requires a not-standart apollo client, that is why aws-lambda-graphql match only for javascript clients

bublig737 avatar Sep 10 '19 12:09 bublig737

Here is my example implementation of AWS API Gateway Websockets subsriptions https://github.com/AlpacaGoesCrazy/serverless-graphql-subscriptions It uses regular apollo-client

AlpacaGoesCrazy avatar Sep 18 '19 12:09 AlpacaGoesCrazy

From what I see I can remove my own websocket implementation because back in the day when I was implementing it, the apollo subscription link could not communicate with AWS API Gateway because the websocket protocol was not supported. Now it can be changed, so problably I should investigate that :)

michalkvasnicak avatar Sep 18 '19 16:09 michalkvasnicak

@michalkvasnicak Hey, I tried updating WS protocol in your library so it can work with apollo-link-ws and subscriptions-transport-ws. Seems to work just fine. https://github.com/AlpacaGoesCrazy/aws-lambda-graphql/tree/change-to-apollo-client Maybe you can tell what else should be done with this solution to be integrated in your library?

AlpacaGoesCrazy avatar Sep 24 '19 11:09 AlpacaGoesCrazy

does it have to be tied to API Gateway? I guess some of us use an Application Load Balancer

Maxwell2022 avatar Sep 25 '19 05:09 Maxwell2022

@Maxwell2022 API Gateway V2 in this case is only needed to provide WebSocket server, not to handle http requests. You can use whatever you want for http.

hakimio avatar Sep 25 '19 07:09 hakimio

What I was trying to say is that ALB is also supporting websocket connections, so do we need to add a dependency on the API Gateway to support websockets? I'm guessing there is a cost involved with websocket connection on API Gateway?

Maxwell2022 avatar Sep 25 '19 07:09 Maxwell2022