Module 3 · API design

Lesson 16 — gRPC, GraphQL, WebSockets and SSE

When REST isn't enough and what each alternative costs you.

Published
In this lesson
  1. Exercise 1 — The decision matrix
  2. Exercise 2 — Minimum viable SSE
  3. Exercise 3 — From reservation event to push
  4. Exercise 4 — Connection load
  5. Exercise 5 — The written decision (mini-ADR)
  6. Submit

ASGI (Uvicorn) + Redis pubsub. Do not look at solutions.md before submitting.

Exercise 1 — The decision matrix

  1. For each case, pick the protocol and justify in 2 lines: (a) public API for integrators; (b) internal billing-service talking to reservation-service; (c) a mobile app showing a configurable dashboard; (d) live support chat; (e) a "your ticket is ready" notification.
  2. Which of the five cases does NOT deserve anything new on TicketFlow today, and why?

Exercise 2 — Minimum viable SSE

  1. Start the lesson's SSE endpoint (or a simulated one publishing the free-seat count every 2s) with python manage.py runserver (ASGI) and consume it with curl -N http://.../events/{uuid}/availability/stream/. Do you see events arrive?
  2. Publish from another process (redis-cli PUBLISH avail:42 '{"free": 41}') and verify reception on the stream.
  3. Add keepalive: a : ping event every 15s. Why do Nginx/proxies cut the connection without it?

Exercise 3 — From reservation event to push

  1. Connect reserve() (10): after creating the reservation, PUBLISH avail:{event_id} {"free": N}. Verify your exercise 2 curl receives the update.
  2. Design question: do you publish inside transaction.atomic() or after the commit? What does the client receive if you pick the wrong side? (hint: rollback).

Exercise 4 — Connection load

  1. Open 200 SSE streams (a script with parallel curl -N or async httpx). Watch the process's memory (05) and ss -tan (05): how many ESTAB connections?
  2. Change keepalive to 1s and repeat: does anything measurable change? What happens with 2,000 connections and Uvicorn defaults (hint: open-file limits — ulimit -n)?

Exercise 5 — The written decision (mini-ADR)

Write TicketFlow's ADR-0007: "Live availability via SSE" — context (the real pain), decision, rejected alternatives (polling with numbers, WS), operational consequences (connections, proxies, Last-Event-ID reconnection).


Submit

Paste the stream curl, the ADR and the load numbers. Next: Lesson 17 — Webhooks and third-party APIs.