System Design Notebook / 07

Real-Time Backends

What it costs to let the server speak first

Polling, long polling, SSE and WebSockets, priced honestly. The proxies that eat your stream, the six-connections-per-origin limit, and what any of it does to a server you have to keep running.

pages
90
sections
49
editions
Reading, Print

Sign in to read it

The whole notebook is free for 7 days. No card, no payment. We ask for an account so the download link belongs to someone.

Sign in with GitHub or Google

What is inside

The full table of contents. Nothing here is hidden: if a section you need is not in this list, the notebook is not the right one and you should not spend a week on it.

  1. The whole picture on one page
  2. 1. The requirement: two people, one board
  3. 2. The shape of every request you have written so far
  4. 3. Polling, and why it is not stupid
  5. 4. The arithmetic of polling
  6. 5. Long polling, and the gap
  7. 6. The two inversions
  8. 7. A 200 that does not close
  9. 8. The wire format: four fields and a blank line
  10. 9. Reconnection you did not write
  11. 10. Why nobody streams an LLM with EventSource
  12. 11. The proxy that eats your stream
  13. 12. Six connections per origin
  14. 13. What SSE cannot do
  15. 14. The handshake, byte by byte
  16. 15. What 101 actually changed
  17. 16. The frame
  18. 17. Masking, and the proxy it was designed to protect
  19. 18. Ping, pong, and the connection that is already dead
  20. 19. Close codes, and the one you cannot send
  21. 20. Choosing: polling, long polling, SSE, WebSocket
  22. 21. Ceiling 1: file descriptors
  23. 22. The thousand-descriptor limit is language-dependent
  24. 23. Ceiling 2: the client's ports, not the server's
  25. 24. Ceiling 3: memory, and where 10 KB goes
  26. 25. The ceiling the benchmark did not measure
  27. 26. Compression, and the budget it destroys
  28. 27. What you just broke
  29. 28. Sticky sessions are not the answer, and not for the reason you think
  30. 29. Pub/sub: the instances talk to each other
  31. 30. Redis pub/sub is at-most-once, and deploys are when it bites
  32. 31. Ordering, and why sequence numbers come first
  33. 32. The catch-up protocol
  34. 33. Fan-out: events times subscribers
  35. 34. Coalescing: the other lever
  36. 35. The reconnect storm
  37. 36. Deploys: draining a connection that never ends
  38. 37. Authenticating a socket
  39. 38. The token that never expires
  40. 39. Cross-site WebSocket hijacking
  41. 40. Presence: who is online
  42. 41. Collaborative editing, in one page
  43. 42. Where this goes next: WebTransport
  44. 43. The delivery script
  45. 44. Whiteboard drawing order
  46. 45. Follow-up question bank
  47. Appendix A. Numbers to memorise
  48. Appendix B. Glossary
  49. Appendix C. Self-test

How to read it

Read with a pen. Every notebook opens with a question to answer before you start and asks you to redo the answer at the end, and the margin in the Print edition exists so you have somewhere to be wrong first. The Tablet edition is 16:9 with vector text, so note apps draw on it rather than treating it as a photograph.

more notebooks