YouTube2Text

Websocket, SSE e Polling: Atualizações em tempo real para entrevistas (System Design #4) — Transcript

by Pedro Camaforte · 4,226 words · 639 segments · language pt-BR · Watch on YouTube

Full transcript

  1. 0:00Imagine if, when you're chatting with
  2. 0:02someone on WhatsApp, you had to keep
  3. 0:04reloading the screen every time to see
  4. 0:06if a new message arrived. That would be
  5. 0:08awful, wouldn't it? And it's not
  6. 0:10supposed to be like that. Messages are
  7. 0:12meant to be in real-time. They should
  8. 0:13happen in real-time. If I send a
  9. 0:15message, I expect the other person to
  10. 0:16receive it right away. And if they send
  11. 0:18one, I receive it right away. And that
  12. 0:20is exactly what we're going to talk
  13. 0:22about in today's video. Real-time
  14. 0:24updates. When we stop to analyze the
  15. 0:28big companies we have in the market
  16. 0:30today, it's expected that this concept
  17. 0:31would come up so often in interviews.
  18. 0:34Because, for example, Google Docs, when
  19. 0:36we have multiple people editing the
  20. 0:38same document in real-time, or Figma,
  21. 0:39for example, when multiple people are
  22. 0:41dragging elements around, we can see
  23. 0:43the cursor exactly where the person is
  24. 0:45in real-time. Uber, so we can see the
  25. 0:48driver's exact location in real-time as
  26. 0:50well. Or, for example, iFood, when
  27. 0:52we're watching the status of our order,
  28. 0:54when the driver left to deliver our
  29. 0:56order—all of this happens in
  30. 0:57real-time, and all these companies use
  31. 0:59this type of architecture. And that is
  32. 1:01exactly what we're going to talk about
  33. 1:03today. We're going to cover
  34. 1:05architectures and concepts like polling
  35. 1:07, server-sent events, and web sockets.
  36. 1:10But while most people stop there and
  37. 1:12just talk about this in the interview,
  38. 1:14we're going to go a little deeper and
  39. 1:15understand some details that will
  40. 1:17surely earn you some extra points in
  41. 1:18the interview. Unfortunately, there are
  42. 1:21many silly mistakes people make because
  43. 1:23they're nervous or because they forget
  44. 1:24things during the interview. And that
  45. 1:26is exactly what I want to avoid. I want
  46. 1:29to leave you 100%prepared to answer
  47. 1:30this kind of deeper questioning during
  48. 1:32interviews, and by the end of this
  49. 1:34video, you'll understand which mistakes
  50. 1:36people make most often and how to avoid
  51. 1:38them. Nice to meet you, my name is
  52. 1:41Pedro Camaforte, I'm a senior developer
  53. 1:42, and I've been working for companies
  54. 1:44abroad for almost two years now. And
  55. 1:47this is our fourth video in our
  56. 1:49seven-video series on the main system
  57. 1:51design concepts that come up in
  58. 1:53interviews for big companies, for
  59. 1:54top-tier companies—that is, companies
  60. 1:57that will pay you 20, 30, or 40,000
  61. 1:59reais and up. If you missed the other
  62. 2:02videos in the series, the playlist is
  63. 2:03here in the description. So let's get
  64. 2:05started. Our first strategy that we are
  65. 2:07going to cover here is the polling
  66. 2:09strategy. Super simple, super basic.
  67. 2:11I'm sure most people have seen it or
  68. 2:13already implemented it, and know how it
  69. 2:15works. But polling is nothing more than
  70. 2:18firing an HTTP request to the server
  71. 2:20every x seconds to ask if there are any
  72. 2:22updates, if there is anything new. So,
  73. 2:25for example, we will ask if there are
  74. 2:27new notifications to show to our user.
  75. 2:30So, we can, for example, set an
  76. 2:32interval in our code and every 5
  77. 2:34seconds make a call to check if there
  78. 2:36is a new notification. If a new
  79. 2:38notification exists, we go ahead and
  80. 2:40update our screen. Then 1, 2, 3 new
  81. 2:42notifications appear there in our UI.
  82. 2:45So, for non-critical flows, for
  83. 2:47notification systems, for reporting
  84. 2:49systems, this is a super valid strategy
  85. 2:52. When we don't have that many users
  86. 2:55logged in at the same time, polling is
  87. 2:57super viable because it's super simple,
  88. 2:59it's not complex at all, and it meets
  89. 3:01the momentary need we have right there.
  90. 3:05We don't need a critical flow, we don't
  91. 3:07need to know the exact moment a
  92. 3:09notification arrived for our user or
  93. 3:10that a report is ready for us to
  94. 3:12download. It's okay if we have a little
  95. 3:16delay of 3, 5, 8 seconds; it's not a
  96. 3:18problem. Polling already meets this
  97. 3:20type of need. I remember back at the
  98. 3:22beginning of my career, when I was a
  99. 3:24junior, I had to implement a polling
  100. 3:26system for a report screen, so the user
  101. 3:27wouldn't need to reload the page to see
  102. 3:29the report available to download. I
  103. 3:32looked at it and said: "Wow, what a
  104. 3:33horrible architecture, what a horrible
  105. 3:35system, it's going to keep wasting
  106. 3:37requests and nothing will happen on the
  107. 3:39screen." Most of the time the user
  108. 3:41isn't even downloading the report. Wow,
  109. 3:43horrible. Until I saw a senior
  110. 3:46developer there with over 8, 10 years
  111. 3:48of experience saying that polling is
  112. 3:50exactly made for this type of situation
  113. 3:52. And seniority is about that. It's
  114. 3:54about understanding that simple
  115. 3:56problems require simple solutions. If
  116. 3:58we need something more advanced, there
  117. 4:00will be a more appropriate solution.
  118. 4:02But for systems like this, systems at
  119. 4:04this level, polling is totally
  120. 4:06reasonable for us to use. Because if
  121. 4:09our demand is very low, there's no
  122. 4:11point in setting up a web socket to
  123. 4:12update in real time the moment the
  124. 4:14report is ready. There are 10 users
  125. 4:17using our system. So polling is 100%
  126. 4:19effective for this type of case. Don't
  127. 4:21be afraid to mention this during an
  128. 4:23interview. If the interviewer tells you
  129. 4:26there are 1,000 users connected to your
  130. 4:27system and it’s just a simple system
  131. 4:29to show them a notification, polling is
  132. 4:31perfect for that. There’s no reason
  133. 4:34to make things up or talk about
  134. 4:35server-sent events. Just say that
  135. 4:38polling solves it. That’s what they
  136. 4:39expect from you. But, of course, most
  137. 4:42things have a tradeoff. And what is the
  138. 4:44tradeoff of polling? As we already
  139. 4:47mentioned, high latency and low
  140. 4:48precision. So, imagine if we were to
  141. 4:50use polling in an Uber system, for
  142. 4:52example. Imagine if we updated the
  143. 4:54driver's location every 10 seconds; the
  144. 4:56car would be jumping, skipping around,
  145. 4:58instead of us seeing exactly which
  146. 5:00street it’s turning onto in a smooth
  147. 5:02motion, exactly in real-time. So,
  148. 5:05polling is not suitable for types of
  149. 5:07systems that require that perfect
  150. 5:09real-time coordination. And also
  151. 5:12because it ends up wasting resources
  152. 5:14when there are many active users at the
  153. 5:15moment. Polling also won't scale, okay?
  154. 5:21It works for notifications, but if you
  155. 5:22have 2 million users connected to your
  156. 5:24system at the same time, polling will
  157. 5:26end up putting a heavy load on your
  158. 5:28server with ghost requests that aren't
  159. 5:29doing anything, but are there accessing
  160. 5:31your database and consuming resources
  161. 5:33from your database and your server. So,
  162. 5:37for this type of situation, it's also
  163. 5:39not highly recommended. It would be for
  164. 5:42a medium or small-sized application,
  165. 5:43for simpler things that aren't as
  166. 5:45sensitive to latency. It’s also worth
  167. 5:48mentioning another really cool strategy
  168. 5:50, similar to polling, which is long
  169. 5:52polling. Long polling is nothing more
  170. 5:54than the same thing that polling does,
  171. 5:56an HTTP call to the server. But the
  172. 5:59server, instead of responding instantly
  173. 6:01with a positive or negative result,
  174. 6:03will hold that connection for 20 or 30
  175. 6:05seconds. When it has something to
  176. 6:08return to the client, it sends it, and
  177. 6:09then the client updates in near
  178. 6:11real-time. It’s a bit more precise
  179. 6:13than polling because it keeps that
  180. 6:15connection open and receives data
  181. 6:16exactly when the server has something
  182. 6:18new. But it’s also something that
  183. 6:21doesn’t scale; it’s something that
  184. 6:22wasn’t built to scale infinitely. So
  185. 6:24much so that most companies, when they
  186. 6:26want this type of strategy, turn to the
  187. 6:28next concept we’re going to talk
  188. 6:30about, the next strategy, which is
  189. 6:32server-sent events. When we want a more
  190. 6:36precise update, so that when something
  191. 6:38changes on the server it automatically
  192. 6:40and instantly changes on our client, we
  193. 6:42use the SSE strategy, Server-Sent
  194. 6:44Events. It is perfect for when we want
  195. 6:49to wait for something to happen on the
  196. 6:51server, some change, and we want to
  197. 6:52update our client in real time
  198. 6:54instantaneously. There's no more delay,
  199. 6:57no more waiting. How do we do this? Our
  200. 7:02browser's API has this EventSource here
  201. 7:04, this special class, which we use to
  202. 7:06make an HTTP request to one of our
  203. 7:08server endpoints, and we keep listening
  204. 7:10for changes at that endpoint. So,
  205. 7:15whenever it sends us word that
  206. 7:16something has changed, in this case
  207. 7:18that a new notification has arrived, we
  208. 7:20parse that data in our example and show
  209. 7:22the notification to our user, all
  210. 7:24happening instantaneously. So,
  211. 7:27Server-Sent Events are great for, for
  212. 7:29example, financial market applications,
  213. 7:31where we want to keep track of stock
  214. 7:33prices in real time. We cannot have a
  215. 7:35delay. We need to know exactly when a
  216. 7:38stock price is going up or down, or
  217. 7:39charts that we want to keep monitoring:
  218. 7:42the status of our application, our
  219. 7:43server, or our database resource usage.
  220. 7:46We want something happening in real
  221. 7:47time there. Server-Sent Events are
  222. 7:50perfect for this because they are
  223. 7:52simple and scale very easily, much
  224. 7:54better than polling. If you're
  225. 7:57wondering how the server will send the
  226. 7:59events to our frontend, we'll get to
  227. 8:01that; we'll talk about it later in our
  228. 8:02next concept. But first, let's talk
  229. 8:05about the tradeoff. The tradeoff of
  230. 8:07Server-Sent Events is that it only
  231. 8:09carries text, so it doesn't carry
  232. 8:11binary, for example, which we will
  233. 8:13comment on in our next concept, as it
  234. 8:15has more capacity than Server-Sent
  235. 8:16Events for data transfer. And most
  236. 8:20importantly, it is a unidirectional
  237. 8:22communication, so it is always from the
  238. 8:24server to our client, never the other
  239. 8:26way around. Our client can never talk
  240. 8:29to our server and send things there. It
  241. 8:32is always just us listening to the data
  242. 8:34the server sends to us. So, for certain
  243. 8:36types of applications, such as chat
  244. 8:38apps, where we have one user sending
  245. 8:40and another receiving in real time, and
  246. 8:42then they reply and the other receives,
  247. 8:44it doesn't make sense anymore. We can't
  248. 8:46use it, because it's always the server
  249. 8:48sending to us and not us to the server.
  250. 8:50For this, we'll need a different
  251. 8:51strategy. And that other strategy is
  252. 8:54our great and beloved Web Socket. Our
  253. 8:57Web Socket will allow us to have
  254. 8:59bidirectional connections. So, both the
  255. 9:02client and the server can exchange data
  256. 9:04with each other. I, as a client, can
  257. 9:07send data to the server and the server
  258. 9:09can send data to me. And how does it
  259. 9:12work? Just like Server-Sent Events,
  260. 9:14we'll have a specific Web API to work
  261. 9:16with Web Sockets. Notice that the
  262. 9:18protocol is different; it's no longer
  263. 9:20HTTP. So, we can create this chat
  264. 9:23object here with 'new WebSocket'
  265. 9:25pointing to our host. And then we can
  266. 9:28both listen for events that the server
  267. 9:29sends to us. So, whenever it sends one,
  268. 9:32I'll convert the message here. And I
  269. 9:34noticed here, look, it's a message from
  270. 9:36Ana, she said: "Hey, how are you?" I go
  271. 9:38there and put it inside my chat. Can I
  272. 9:40send it back to the server? So, my chat
  273. 9:42. Send, I'm here sending a reply
  274. 9:45message to Ana. I'll say the type here
  275. 9:47is message and say here, hey, all good.
  276. 9:51So we can have this exchange, we can
  277. 9:53reach another range of applications,
  278. 9:54such as chats and real-time
  279. 9:56collaboration. So if you were wondering
  280. 10:00what Google Docs or Figma use under the
  281. 10:02hood to have this, to have this type of
  282. 10:04application, this type of strategy, it
  283. 10:06is exactly Web Socket. So as an
  284. 10:08advantage here, we have the
  285. 10:10bidirectional connection, which is
  286. 10:11totally distinct from the other
  287. 10:13strategies. Latency, just like
  288. 10:16Server-Sent Events, is practically
  289. 10:18instantaneous, under 500 milliseconds.
  290. 10:21And besides text, it handles binary
  291. 10:23frames if that's a need for our
  292. 10:25application. And how does this Web
  293. 10:27Socket communication work? Because I
  294. 10:29mentioned here that the protocol is
  295. 10:30different. Actually, the websocket
  296. 10:33starts with an HTTP request, but in the
  297. 10:35request header, it sends this: an '
  298. 10:37upgrade websocket' and a 'connection
  299. 10:39upgrade'. So what is it going to do? It
  300. 10:42will open this initial HTTP connection,
  301. 10:44but then it will change, update to a
  302. 10:46TCP connection. And then it will keep
  303. 10:50this connection open with our server in
  304. 10:52a tunnel format, a tunneling, and then
  305. 10:53both can freely exchange data with each
  306. 10:55other. This last header here is just a
  307. 10:58header that is normally sent to verify
  308. 11:00if the server is capable of handling
  309. 11:02WebSocket connections. It sends a key
  310. 11:05to the server to validate, and then it
  311. 11:07revalidates that key to see if the
  312. 11:09server converted or processed it
  313. 11:10correctly. So, with this, we can keep
  314. 11:13this connection open with the Socket
  315. 11:15and its specific protocol, which makes
  316. 11:18it very, very, very efficient and fast.
  317. 11:21But since not everything is perfect, we
  318. 11:23also have difficulties and tradeoffs
  319. 11:25when working with WebSockets. And the
  320. 11:28main one is that it is more complex and
  321. 11:29requires specific infrastructure. So,
  322. 11:33we will have certain difficulties
  323. 11:34working with it, unlike Server-Sent
  324. 11:36Events, it's a bit different. Let's
  325. 11:38understand what these complexities are,
  326. 11:40what these extra difficulties are that
  327. 11:42we will face. Let's imagine the
  328. 11:43following scenario. Imagine we have a
  329. 11:46server with 16 cores, 32 GB of RAM, and
  330. 11:49our messages have a size of about 1 KB,
  331. 11:52so roughly 1,000 characters. And in
  332. 11:55total, all the people connected to this
  333. 11:57server, all the connected WebSockets,
  334. 11:59will be generating about 100,000
  335. 12:01messages per second. Based on these
  336. 12:04calculations and data, we can reach
  337. 12:07about 300,000 to 500,000 simultaneous
  338. 12:09connections on this server, which is a
  339. 12:11very cool value. But how many
  340. 12:14simultaneous connections do you think
  341. 12:16WhatsApp keeps open per day? Much more
  342. 12:19than that. And for that, we will need
  343. 12:22to expand our servers and perform
  344. 12:24replication so we can meet all our user
  345. 12:26demand. So, just like any other
  346. 12:30application, we will replicate our
  347. 12:32servers to have more redundancy and
  348. 12:34allow more users to connect to our
  349. 12:35application. To route our users, we
  350. 12:40will need a load balancer. But this
  351. 12:43load balancer has a characteristic; it
  352. 12:45won't be a conventional load balancer.
  353. 12:48Normally, when we have servers serving
  354. 12:51standard HTTP requests, or the typical
  355. 12:53API calls we see in other systems, they
  356. 12:55are layer 7 load balancers, meaning
  357. 12:57they act at the application layer. For
  358. 13:01WebSockets, it's different; we will
  359. 13:03have to use layer 4 load balancers.
  360. 13:05This makes all the difference when
  361. 13:06mentioned in an interview. The goal of
  362. 13:10this video isn't to explain exactly how
  363. 13:12load balancers work under the hood, but
  364. 13:14it's important for you to know that
  365. 13:15load balancers typically have seven
  366. 13:17layers, and that HTTP calls happen at
  367. 13:19the seventh layer, while WebSocket
  368. 13:21calls happen at the fourth layer. And
  369. 13:24why can't WebSockets happen at layer
  370. 13:26seven? Because when a client makes a
  371. 13:29request to our server and the load
  372. 13:31balancer is in the middle, it takes
  373. 13:32that request, opens it, reads the
  374. 13:34headers, repackages it, and then makes
  375. 13:36another call to the server, essentially
  376. 13:38performing a redirection. For
  377. 13:40WebSockets, this doesn't work because
  378. 13:42it ends up breaking the WebSocket flow,
  379. 13:44which needs to be constant and
  380. 13:45continuous. So, layer 4 allows us to do
  381. 13:48this. It won't open anything, it won't
  382. 13:50do anything; it will just redirect to
  383. 13:52the server with the fewest connections.
  384. 13:54For example, this is already a load
  385. 13:56balancing strategy. It checks which
  386. 13:58server has the fewest open connections
  387. 14:00at the moment. It goes there and
  388. 14:02redirects, performing this routing. And
  389. 14:04that's why we use layer 4 and not layer
  390. 14:067. Cool, great. So, we have our layer 4
  391. 14:09load balancer here, redirecting to our
  392. 14:11servers. And now, instead of handling
  393. 14:14just 500,000 connections, we can handle
  394. 14:17500k, 1 million, 2 million, 2.5 million
  395. 14:19simultaneous connections on our servers
  396. 14:22. Wonderful. That's a whole lot. But
  397. 14:26there is something that happens here,
  398. 14:28which is exactly where the interviewer
  399. 14:30will want to know if you'll pass the
  400. 14:31interview or not, if you have the
  401. 14:33knowledge beyond just saying that
  402. 14:34WebSockets solve the real-time problem.
  403. 14:40And it's the following case: our user
  404. 14:42one is talking to our user two, but
  405. 14:44when routing, our load balancer decided
  406. 14:46to send them to server one and our user
  407. 14:48two to our server number four. So, how
  408. 14:52are they going to talk to each other?
  409. 14:56How will user two, when they send a
  410. 14:58message, know that server four needs to
  411. 15:00send a message over to server one to
  412. 15:01respond? If they were both connected to
  413. 15:05the same server, great, wonderful,
  414. 15:06solved. But what about when we have
  415. 15:08these multiple servers; how do servers
  416. 15:10communicate with each other? How do we
  417. 15:12solve this problem? So, to solve this
  418. 15:15problem, we are going to choose a
  419. 15:16strategy that many companies use. It's
  420. 15:19kind of a standard, but of course, it's
  421. 15:21not the only solution. We could use
  422. 15:24Rabbit, we could use Kafka, but many
  423. 15:26companies choose this strategy for its
  424. 15:28simplicity, as it solves our exact
  425. 15:30problem: the pub/sub strategy with
  426. 15:32Redis. Redis tackles this exact problem
  427. 15:37of communication between servers, and
  428. 15:39here is how it works. Basically, when
  429. 15:43our first user connects to the server,
  430. 15:45they subscribe to a Redis topic. That
  431. 15:48topic will be called user 2.1, for
  432. 15:50example, with the one being the user's
  433. 15:53ID. This Redis topic acts like a
  434. 15:55listener; the person is listening to
  435. 15:57that topic, and whenever someone
  436. 15:59publishes something to it, the topic
  437. 16:01receives that publication, that event,
  438. 16:03that message. And we will use this
  439. 16:06exact strategy to address this server
  440. 16:08communication problem we have. So, in
  441. 16:11the same way, user two goes to server
  442. 16:13four and subscribes to a topic with
  443. 16:16their ID. When our first user went
  444. 16:19there and listed all the conversations
  445. 16:20they had on the frontend, they clicked
  446. 16:22on the conversation with user two.
  447. 16:24Since they listed it beforehand, they
  448. 16:26already have access to the ID of user
  449. 16:28two they want to communicate with. So,
  450. 16:29they will load that ID into the
  451. 16:31conversation because they will need it
  452. 16:33on the backend. When they hit enter,
  453. 16:36they send the message from user one to
  454. 16:37our server. It will publish this
  455. 16:41message to the topic user 2.2, which is
  456. 16:43the user they want to communicate with.
  457. 16:47Since user two is subscribed to that
  458. 16:49topic with their ID, they will go there
  459. 16:51and receive the message from user one.
  460. 16:54Likewise, when they publish to user
  461. 16:56one's topic, because user one is
  462. 16:58subscribed, they will receive the
  463. 17:00message. That is how we maintain this
  464. 17:04connection between both users, how we
  465. 17:06facilitate this conversation between
  466. 17:08them, regardless of which server they
  467. 17:10land on; Redis acts as our hub, our
  468. 17:11broker for messages and events
  469. 17:13occurring based on topic subscriptions
  470. 17:15within it. And this doesn't just work
  471. 17:19for users; it works for groups too. For
  472. 17:21example, if we have a group with 10
  473. 17:23people and all 10 are subscribed to
  474. 17:25topic group 2.1.23, for instance,
  475. 17:27whenever someone sends a message to
  476. 17:29that topic, everyone subscribed will
  477. 17:31receive it. The servers will all update
  478. 17:34at the same time and send it to the
  479. 17:36frontend. So this works for a user, a
  480. 17:40group, or anything that is a topic in
  481. 17:42Redis, and if someone publishes to that
  482. 17:44topic, all subscribers will receive
  483. 17:46that message. Cente already solves this
  484. 17:49server issue, allowing for 10, 15, or
  485. 17:51even 20 servers. Redis will be our
  486. 17:54controller; it's what will route the
  487. 17:55messages to the servers that are
  488. 17:57subscribed to it. This is phenomenal
  489. 18:00because in most interviews, people stop
  490. 18:02here; they know how to use WebSockets,
  491. 18:04they know Server-Sent Events exist,
  492. 18:06they know the concepts, but they don't
  493. 18:08know what comes next. So, knowing that
  494. 18:11the Load Balancer needs to be layer 4
  495. 18:13for WebSocket connections, and
  496. 18:15understanding how it works on the
  497. 18:17server side—the second stage of
  498. 18:18communication, how servers talk to each
  499. 18:21other—is phenomenal. And there's a
  500. 18:25little question that always comes up in
  501. 18:26interviews when the subject is
  502. 18:28real-time chat updates, for example,
  503. 18:30which is: "What if the user is offline?
  504. 18:32How will they receive the messages?""
  505. 18:35Because if Redis publishes there, and
  506. 18:37someone publishes a message to Redis
  507. 18:39saying: 'Hi, how are you?'" And that
  508. 18:41person is not subscribed. That message
  509. 18:44is lost; there's no way to recover it.
  510. 18:46So, how do we solve the case for people
  511. 18:48who have their phones turned off? "For
  512. 18:50example?" The answer to that is
  513. 18:51actually very simple. There are other
  514. 18:54ways to do this, but I’m going to
  515. 18:56share with you one way, a strategy that
  516. 18:58is quite reasonable and works perfectly
  517. 19:00for our cases. So, we will have, for
  518. 19:03example, a table in our database for
  519. 19:05pending messages. Whenever we publish a
  520. 19:08message to Redis, we will also save
  521. 19:10that message in this table. So, if our
  522. 19:14user isn't online, they won't receive
  523. 19:16that message in real-time via the Redis
  524. 19:18subscription, but they will have this
  525. 19:20backup in that table. As soon as they
  526. 19:23reconnect to our application, they make
  527. 19:25a call to this table and check if there
  528. 19:26is any data in it. If there is, we grab
  529. 19:29everything in a package, send it all
  530. 19:31back to them, and then they can see all
  531. 19:33the messages that were pending for them
  532. 19:35. And we can also approach this type of
  533. 19:37strategy in two ways. Some people keep
  534. 19:41all these messages stored as a history
  535. 19:43in the database, but others prefer to
  536. 19:45keep it temporarily. For example, if
  537. 19:49I'm not mistaken, WhatsApp does this.
  538. 19:51He doesn't keep those messages saved in
  539. 19:53his database due to legal reasons. He
  540. 19:56doesn't want to store sensitive data on
  541. 19:57his end in case of a leak or anything
  542. 19:59like that. So, he leaves these messages
  543. 20:02temporarily, and as soon as you
  544. 20:04retrieve all messages after being
  545. 20:06offline, he deletes everything, ensures
  546. 20:08you have it all on your phone, performs
  547. 20:10the monthly backup, and so on; this way
  548. 20:12, he keeps his side clean, without
  549. 20:14messages containing sensitive data, and
  550. 20:16ensures you already have everything you
  551. 20:18need on your cell phone, for example.
  552. 20:21Or we could add a timestamp to these
  553. 20:23messages, for example, and say: "Look,
  554. 20:25I only received up to this last message
  555. 20:27. Give me everything that came after it
  556. 20:30while I was offline." We go there,
  557. 20:32perform this calculation, fetch all the
  558. 20:34messages, and return them to our user.
  559. 20:36We can do it in either of these two
  560. 20:38ways. And with that, we finish all the
  561. 20:40concepts and strategies for dealing
  562. 20:42with real-time updates. We talked about
  563. 20:45WebSockets, polling, and server-sent
  564. 20:47events, and we understood how the L4
  565. 20:49Load Balancer works and why it needs to
  566. 20:51be at that layer, specifically because
  567. 20:53of the TCP connection that WebSockets
  568. 20:55have, which requires constant tunneling
  569. 20:57. We understood the second part, how
  570. 21:00servers communicate with each other,
  571. 21:02and how we handle offline users. And
  572. 21:05here, just to be clear, server-sent
  573. 21:07events also work the same way. We can
  574. 21:10also use Redis Pub/Sub to facilitate
  575. 21:12this conversation when we have many
  576. 21:14servers communicating, when something
  577. 21:16happens on one server, but the user is
  578. 21:18connected to a different server to
  579. 21:20receive the event via SSE. So we also
  580. 21:23use Redis Pub/Sub, which will also work
  581. 21:26perfectly for these cases. And this way
  582. 21:29, we manage to address all the points
  583. 21:31and all the complexities we had in mind
  584. 21:32. And I am sure you will be much better
  585. 21:36prepared to answer interview questions
  586. 21:38about real-time updates. The main
  587. 21:41mistakes people make during interviews
  588. 21:43are exactly the ones we discussed. They
  589. 21:46don't know how to handle horizontal
  590. 21:48server scaling. They don't talk about
  591. 21:51or mention how this second part works,
  592. 21:53how servers communicate with each other
  593. 21:54, or what mechanism they will use to
  594. 21:56make that happen. They don't know how
  595. 21:59to handle it when a user is offline, or
  596. 22:01they simply freeze when the interviewer
  597. 22:03asks. It is also worth remembering that
  598. 22:06polling is not a bad strategy; it is a
  599. 22:08simple strategy for a simple problem.
  600. 22:10And sometimes that is exactly what the
  601. 22:11interviewer wants from you. You're not
  602. 22:13going to go around putting a bunch of
  603. 22:15web socket infrastructure into a system
  604. 22:17that has 100 active users. It doesn't
  605. 22:19even make sense. So the interviewer
  606. 22:22often wants to know if you understand
  607. 22:24the problem at hand, what is being
  608. 22:25required of you, which solution to
  609. 22:27implement for that case, and they
  610. 22:29evolve it gradually. So, okay, that's
  611. 22:31fine for 100 active users, but what if
  612. 22:34we now have 100,000 active users
  613. 22:36operating there daily? So it is
  614. 22:38progressive. Don't fall into the trap
  615. 22:41of using a super heavy architecture for
  616. 22:43a system that is super simple. And it's
  617. 22:46also super important to remember to
  618. 22:48mention: "Whenever we are working with
  619. 22:50Web Sockets, we use layer 4 load
  620. 22:52balancers, not layer 7." Extremely
  621. 22:54important, it will guarantee you extra
  622. 22:56points in the interview. I truly hope
  623. 23:00you learned something new in this video
  624. 23:02or that some concept was reinforced for
  625. 23:04you. So, please, leave a like here,
  626. 23:06leave a comment with any suggestions or
  627. 23:08any possible questions, and subscribe
  628. 23:10to the channel so you don't miss the
  629. 23:12next videos in the series. In the next
  630. 23:15one, we'll talk about how to deal with
  631. 23:17gigantic files, how we manage to upload
  632. 23:19a 200 GB file to YouTube or Google
  633. 23:21Drive, and how they handle such a
  634. 23:23colossal file size. That is exactly
  635. 23:26what we will discuss in the next video,
  636. 23:28tying up a concept we left open from
  637. 23:30the last video regarding pre-signed
  638. 23:32URLs. So subscribe, don't miss it, and
  639. 23:34I'll see you in a bit. M.

About this transcript

This page contains the full transcript of Websocket, SSE e Polling: Atualizações em tempo real para entrevistas (System Design #4) by Pedro Camaforte, generated from the public captions YouTube serves with the video. The transcript has 4,226 words across 639 segments, with the original timestamps preserved so you can click any line to jump to that moment in the embedded player.

What you can do with it

Use the transcript to take notes, quote the speaker, build a study guide, generate a summary with ChatGPT or Claude via the YouTube Summary tool, or export it as a timed subtitle file with YouTube to SRT. You can also re-open it in the transcriber to translate the transcript into 100+ languages.

Free YouTube transcript tool

YouTube2Text is a free YouTube transcript generator — no signup, no daily limit. Paste any YouTube link and get the full transcript instantly, with timestamps, click-to-jump, translation to 100+ languages, AI prompts for ChatGPT, Claude, and Gemini, and exports to TXT, SRT, VTT, or Markdown.