Skip to content

Instantly share code, notes, and snippets.

@StevenACoffman
Last active July 14, 2024 18:08
Show Gist options
  • Save StevenACoffman/8252d3bff913c1f946dd26453c030756 to your computer and use it in GitHub Desktop.
Save StevenACoffman/8252d3bff913c1f946dd26453c030756 to your computer and use it in GitHub Desktop.
GraphQL vs HTTP2

See:

General

What else is there?

  • WebSockets - Bidirectional communication over a single TCP connections. Messages are asynchronous and can be sent from client to server and server to client at the same time. This is natively supported by most browsers.
  • gRPC - High-performance bidirectional streaming. This not only supports simultaneous client/server and server/client messages like WebSockets, but it also offers multi-plexing, which means multiple messages can be sent in a single direction at the same time without blocking.
  • MQTT - Minimalistic publish/subscribe communication protocol. Designed for IoT use cases, MQTT offers multiple levels of delivery fidelity (Quality of Service) and defines what to do when a connection is dropped (Last Will and Testament).
  • Server-Sent Events (SSE) - Simple, one-way communication from a server to a client over HTTP. Mostly intended for browser-based interactions, this mechanism offers features like automatic reconnection and native browser support.

Use Cases

Now that we know what each of these are, we should talk about when to use them. As with any question in software, the answer of course is it depends, but as a general guideline, let’s take a look at certain situations where each one is the most appropriate.

  • WebSockets - Online multiplayer games, chat apps, collaborative tools, etc… Use cases are generally around syncing state between multiple users.
  • gRPC - Microservice communication, mobile apps, and high-performance systems. Typically you see use cases with requirements around low-latency and efficient bandwith usage.
  • MQTT - IoT devices. This is generally seen in environments where connections are intermittent and clients are devices that send frequent data.
  • Server-Sent Events (SSE) - Live news, sports scoring, monitoring dashboards. Use cases are around getting data from a server with no communication from the client.

Of course you can use these communication mechanisms for other use cases, but generally you will find them best suited for the tasks mentioned above.

Decision Matrix

API Decision making matrix Client & server (same team) client & server (different team) client and server (different company)
client / server both use typescript tRPC / GraphQL GraphQL Federation GraphQL / REST
Client / Server use different languages GraphQL GraphQL Federation GraphQL / REST
Client is Backend Service or CLI gRPC gRPC GraphQL / REST
Long-Running Operation Events AsyncAPI (PubSub / stream) AsyncAPI (PubSub / stream) Webhooks
  • Client & Server developed by the same team, using TypeScript only: tRPC or GraphQL
  • Client & Server developed by the same team, using different languages: GraphQL
  • Client is a Backend or CLI: gRPC
  • Long-Running Operations, within the same company: AsyncAPI (PubSub/Stream)
  • Long-Running Operations, across companies: WebHooks
  • Multiple Clients & Servers across teams: GraphQL Federation
  • Public APIs: REST / GraphQL

SSE:

gRPC:

HTTP3 stuff

chrome --origin-to-force-quic-on=www.example.org:443

the hard part is convincing the major browsers[2] to use H3 while connecting to server. I have added DNS HTTPS[3] record and also alt-svc[4] header in responses in both HTTP/1.1 and HTTP/2. Still no luck. By the way, the i tested the browsers with HTTP/3 test pages[5] and they have HTTP/3 support.

[1]: "docker run -it --rm ymuski/curl-http3 --http3 -svk "https://172.17.20.158/" -H "Host: www.example.com"

[2]: Chromium 120.0.6099, Firefox ESR 115.6, Opera 106.0.4998

[3]: example.com. 3600 IN HTTPS 1 www.example.com. alpn="h3,h2"

[4]: 'alt-svc: h3=":443"; ma=86400, h3-32=":443"; ma=86400, h3-29=":443"; ma=86400'

[5]: "quic.aiortc.org", "https://domsignal.com/http3-test", "https://www.http3check.net/"

alpn="h3,h2" ipv4hint="127.0.0.1" ipv6hint="::1"

Or

http3.dev 3600 IN HTTPS 1 . alpn="h3,h2" ipv4hint="127.0.0.1" ipv6hint="::1"

Auth stuff:

https://github.com/grpc-ecosystem/go-grpc-middleware/blob/main/examples/server/main.go https://eli.thegreenplace.net/2024/sign-in-with-google-in-go/ https://github.com/salrashid123/grpc_google_id_tokens https://github.com/juanfont/headscale/blob/eb1591df35624b6cbe85e5c671869a0806dedfba/hscontrol/app.go#L324 https://gist.github.com/mjudeikis/4773b47c2fcd11c6b21c92b74d809979

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment