YouTube2Text

Scale an App to Millions of Users - System Design — Transcript

by Caleb Curry · 7,730 words · 1,095 segments · language en · Watch on YouTube

Full transcript

  1. 0:00hey welcome in this video we're going to
  2. 0:02go through an exercise of talking about
  3. 0:04everything you need to know to scale a
  4. 0:06web app from a few users to millions of
  5. 0:09users this is likely a pretty common
  6. 0:12system design interview question but
  7. 0:14it's also a good thing to understand if
  8. 0:16you're building out your own apps so if
  9. 0:19you're going to end up being a backend
  10. 0:20engineer or if you are a software
  11. 0:23architect these are the kinds of things
  12. 0:25you're going to want to understand and
  13. 0:27we'll start with just the small little
  14. 0:30tiny app that might have a few users
  15. 0:33nothing too crazy I mean look at it it's
  16. 0:35so cute it's not going to be able to
  17. 0:37support millions of users though so
  18. 0:39we're going to continue to add new
  19. 0:41things to improve the overall capacity
  20. 0:44of our app so we can support more and
  21. 0:46more users before we get started a few
  22. 0:48things I wanted to mention first off the
  23. 0:50system design interview book pretty good
  24. 0:52book if you want some practical
  25. 0:53exercises on system design so I've been
  26. 0:56reading through this might be a good
  27. 0:58additional resource for those of you who
  28. 1:00are looking to do system design
  29. 1:02interviews the next thing is I have a
  30. 1:04backend Engineers mind map and this is
  31. 1:06going to categorize a substantial number
  32. 1:09of different softwares and Technologies
  33. 1:11so you have a good framework for
  34. 1:13learning as well as a road map at the
  35. 1:15bottom giving you a step-by-step guide
  36. 1:17on which Technologies to learn in which
  37. 1:19order now if you're learning the things
  38. 1:21to become a software engineer but you
  39. 1:22need some additional support I have a
  40. 1:24mentorship program where you can get
  41. 1:26one-on-one support and accountability to
  42. 1:29help you learn all of the different
  43. 1:31components of building backend services
  44. 1:33and websites perhaps you're already in
  45. 1:35Tech and you're looking to advance your
  46. 1:37career or you want to transition into
  47. 1:40your first software engineering role
  48. 1:42whatever it might be that mentorship
  49. 1:44program can help you get there so I'll
  50. 1:46have a link to that and the mindmap down
  51. 1:48below so we've talked about scalability
  52. 1:50a little bit in the previous system
  53. 1:52design videos but it's not really a
  54. 1:54prerequisite to watch this video so if
  55. 1:55you're just jumping in that's fine but
  56. 1:57we're going to review real quickly what
  57. 1:59scalability is in general scalability is
  58. 2:03the ability to support more users so if
  59. 2:05your app grows your app doesn't collapse
  60. 2:08because you've hit maximum capacity so
  61. 2:11pretend this is your web server and you
  62. 2:14might be utilizing say 50% of This
  63. 2:17Server maybe even a little bit more but
  64. 2:19as you start to consume all of the
  65. 2:22resources of This Server you might have
  66. 2:24some issues so you definitely don't want
  67. 2:26to get too close to using all of the
  68. 2:28computer's resource ources now that
  69. 2:30exact number whether you utilize 50% or
  70. 2:3370% that really depends on how much
  71. 2:37variation in the computation is needed
  72. 2:40so if you're at a constant 70% all the
  73. 2:44time that's probably fine but the
  74. 2:46reality is typically the usage will go
  75. 2:49up and down and that usage can vary on
  76. 2:53time of day or a certain season so you
  77. 2:56might typically say only be using 60% of
  78. 3:00the total computation power of the
  79. 3:03server but let's say your application is
  80. 3:05a little bit more popular on the
  81. 3:07weekends and now you start getting up
  82. 3:09here where you're at 90% well now you
  83. 3:12have to really start considering how can
  84. 3:14we scale so we can support more users so
  85. 3:17you'll need to concern yourself about
  86. 3:18scalability from ups and downs but also
  87. 3:21the growth of the app over time perhaps
  88. 3:25when you start out you might be right
  89. 3:27here in terms of the grow both of your
  90. 3:30application and your business so this
  91. 3:32might potentially work but next year you
  92. 3:35might have twice the number of users so
  93. 3:38not only do you have to think about the
  94. 3:40ups and downs either dayto day week by
  95. 3:43week season by season but also the
  96. 3:45growth over time so that's the idea of
  97. 3:49scalability we need to structure the
  98. 3:52backend to be able to support more users
  99. 3:55if you're dealing with just daytoday ups
  100. 3:58and downs then you need to think about
  101. 4:00having enough capacity to support the
  102. 4:03changes in traffic and this can often be
  103. 4:06as simple as getting a stronger server
  104. 4:09or adding more servers but when you
  105. 4:11start thinking about growth over time
  106. 4:13you might need to introduce some
  107. 4:15different systems or structures to your
  108. 4:17back end to make it more reasonable so
  109. 4:20we'll talk about both of those things in
  110. 4:22this video so let's make sure we are on
  111. 4:24the same page with vertical and
  112. 4:26horizontal scaling so I have a video
  113. 4:29that should be coming out soon which
  114. 4:31we'll talk about the ups and downs of
  115. 4:34these in much more detail but as a
  116. 4:37general idea vertical scaling is
  117. 4:39improving the power of a single system
  118. 4:42often called scaling up where horizontal
  119. 4:45scaling is just adding more servers so a
  120. 4:49visual difference would be going from
  121. 4:54this to this and now I have a much
  122. 4:58beefier system that we can support more
  123. 5:00users with comparing that to just going
  124. 5:03from this
  125. 5:05to now say three smaller notes so both
  126. 5:10of these can achieve similar results
  127. 5:13it's just a matter of which one you want
  128. 5:14to do and most likely it's going to be a
  129. 5:17combination of both but throughout this
  130. 5:20you'll need to understand that it's
  131. 5:21often the case we can be working with
  132. 5:24multiple servers that are working
  133. 5:27together as a single unit that's really
  134. 5:30important to understand because one of
  135. 5:31the easiest ways to increase the scale
  136. 5:33of our app is just to add more servers
  137. 5:36but you'll have to set up your back end
  138. 5:38in such a way that this can work such as
  139. 5:39using a load balancer and the ease of
  140. 5:43doing this really depends on the type of
  141. 5:46thing you're trying to
  142. 5:47scale so doing this is a lot easier with
  143. 5:51a stateless backend API for example but
  144. 5:54more complicated to do with a database
  145. 5:57now it's often the case you can also
  146. 5:58have some automation so we'll just say
  147. 6:02autoscaling and this addresses the first
  148. 6:05thing we were talking about which is the
  149. 6:07ups and downs in usage from day to day
  150. 6:11or week to week The Simple Solution for
  151. 6:14this is an autoscaling setup where if
  152. 6:17you have more users come in you just add
  153. 6:21another node and I didn't draw it good
  154. 6:24here with room but you can add more
  155. 6:26nodes to support those users as needed
  156. 6:30then when the UPS are over you no longer
  157. 6:34need that additional computing power you
  158. 6:36can remove those nodes and go back to
  159. 6:38your standard three while people often
  160. 6:41use three you don't have to use three so
  161. 6:44don't think there's any magical number
  162. 6:46here in terms of the number of nodes
  163. 6:48that you use when you're using
  164. 6:50horizontal scaling for our visuals here
  165. 6:53though I'll often draw as three nodes
  166. 6:56now a node is just another name for a
  167. 6:58server so you probably got that figured
  168. 7:00out by now but as we add nodes it
  169. 7:03basically just means adding a server to
  170. 7:06a larger cluster or unit so autoscaling
  171. 7:11is a term to describe elasticity and
  172. 7:15this is a little bit different than
  173. 7:19scalability which this deals with the
  174. 7:22growth of users being able to support
  175. 7:24them whereas elasticity refers to going
  176. 7:27up and down in computer hardware to be
  177. 7:31able to dynamically adjust all right so
  178. 7:34we understand the types of scaling now
  179. 7:36there's one additional thing I want to
  180. 7:38talk about and that is a typical app
  181. 7:41structure so we'll go through that
  182. 7:43architecture real quick and then we'll
  183. 7:44go through the exercise of starting with
  184. 7:46a few users and going to Millions so an
  185. 7:49app architecture could depend a lot on
  186. 7:51the type of app you're building for this
  187. 7:54exercise I'm really thinking of a web
  188. 7:56application or even like a mobile
  189. 7:58application with a backend API and a
  190. 8:02database so this is the typical three
  191. 8:04tier architecture which we did a
  192. 8:06dedicated video on but that's what we're
  193. 8:09going to be going through in this video
  194. 8:11as well so I won't get into all of the
  195. 8:13details of this because it's not really
  196. 8:15the point of the video but the general
  197. 8:17structure will look like this you'll
  198. 8:19have a database layer then you'll have a
  199. 8:21backend layer and then you will have a
  200. 8:24frontend layer this front end could be a
  201. 8:27website it could be a desktop
  202. 8:28application it could be a mobile
  203. 8:30application it could be a video game it
  204. 8:33doesn't really matter the main thing you
  205. 8:35need to understand is that it connects
  206. 8:37to some centralized backend which is our
  207. 8:39job to design and a database to store
  208. 8:43information this is the basic structure
  209. 8:46and then we'll kind of adapt this as we
  210. 8:48go I wanted to draw these out separately
  211. 8:51at first so we have the DB we have the
  212. 8:54back end and we have the front end
  213. 8:57because the way you SC scale these might
  214. 9:01differ so the database scaling I'm going
  215. 9:04to have a different approach than the
  216. 9:06backend scaling and same idea the front
  217. 9:09end might be a little bit different as
  218. 9:11well so we'll go through some of those
  219. 9:13differences throughout this video but
  220. 9:14we'll primarily be focusing first on
  221. 9:17this back end as there's a lot of things
  222. 9:19that we can do to make this back end
  223. 9:21better and scaling a database is more
  224. 9:24complex there's a lot of things we can
  225. 9:26do at this level to red U the
  226. 9:29computation at the database level so by
  227. 9:33working here first we can simplify this
  228. 9:37so a very simple example of what I mean
  229. 9:39there would be a caching layer we can
  230. 9:41cache data so we don't have to access
  231. 9:43the database as much so that's just an
  232. 9:45example we'll get into the details of
  233. 9:47what that means and now with all of that
  234. 9:49Foundation out of the way we should be
  235. 9:50able to get started by going back to our
  236. 9:53little tiny square box and scaling from
  237. 9:56a few users to Millions so let's start
  238. 9:58with a single server I'm going to make
  239. 10:00it just a little bit bigger than what we
  240. 10:02started with so I have some room to draw
  241. 10:05in here with this structure we're going
  242. 10:06to have the database the back end and
  243. 10:10the front end all served from a single
  244. 10:13server so this would be the most simple
  245. 10:16architecture but it's also going to be
  246. 10:18the most
  247. 10:19limited So within the server we have a
  248. 10:23database server so basically the
  249. 10:25database is in the same computer this
  250. 10:28would be kind kind of like if you're on
  251. 10:30your local computer and you download
  252. 10:33postgress or some database you can run
  253. 10:35your backend from that computer and you
  254. 10:37can connect to the database that's also
  255. 10:39on that same computer in this
  256. 10:41environment we would also be hosting the
  257. 10:42web server so we will have something
  258. 10:44like engine X or Apache which will allow
  259. 10:47us to take requests and process those
  260. 10:49requests on the back end getting the
  261. 10:52data from the database and ultimately
  262. 10:54giving HTML or css or whatever the front
  263. 10:56end is to the client so so it will look
  264. 11:00something like this let's say the client
  265. 11:01is over here and they're accessing from
  266. 11:03a computer let's just say we're building
  267. 11:05a
  268. 11:06website they make a request to our
  269. 11:09server we have a single application in
  270. 11:12code which is going to probably have
  271. 11:17some om or database models to work with
  272. 11:21the database it will probably have some
  273. 11:23API so we'll just
  274. 11:26say the back end and it probably has
  275. 11:30some way to generate the front end so
  276. 11:33with it being just a single monolithic
  277. 11:36application you might be using some
  278. 11:39template engine such as Ginger 2 razor
  279. 11:42pug there's a bunch of different
  280. 11:44template engines and basically what
  281. 11:46those will do is they will allow you to
  282. 11:49take some backend code let's say it's
  283. 11:51python give you an HTML structure and
  284. 11:56you can write that python in the HT HML
  285. 12:00it will process it on the back end
  286. 12:02ultimately giving you out just HTML
  287. 12:05which can then be sent to the client so
  288. 12:08we're kind of getting into the weeds
  289. 12:09here of how to actually design the code
  290. 12:12which I don't want to get too distracted
  291. 12:14from scaling apps but with this
  292. 12:17structure everything is being done
  293. 12:19within a single application we've talked
  294. 12:22about the MVC design pattern and that
  295. 12:24might be something you implement for
  296. 12:26this monolithic application where the
  297. 12:29request from the user hits a controller
  298. 12:32that model will do the database stuff
  299. 12:34getting data from the database we'll
  300. 12:36embed it in that view which is this
  301. 12:39templating stuff and then that is the
  302. 12:41response that ultimately makes it back
  303. 12:43to the end user so you can kind of
  304. 12:46ignore all of the details here the main
  305. 12:48thing you need to understand is that we
  306. 12:50are running from a single computer and
  307. 12:53that's very limiting for a couple of
  308. 12:54reasons so let's talk about the pros and
  309. 12:57cons of this setup the pros it's super
  310. 13:00simple for beginners if you're working
  311. 13:03on your first web app you can do this in
  312. 13:06no time additionally it is simple to
  313. 13:09scale now I don't really want to say
  314. 13:11that it's easy because there are some
  315. 13:15complexities but it's simple because all
  316. 13:19you have to do is add computer hardware
  317. 13:21the limitation is you can't be specific
  318. 13:24about what parts of the app you want to
  319. 13:26scale so it's simple to scale because
  320. 13:29all you would do is vertically scale if
  321. 13:31you need more RAM you just add more RAM
  322. 13:33to the server if you need more CPU you
  323. 13:36just add CPU to the server but this is
  324. 13:39directly related to the con which is you
  325. 13:41cannot scale sections independently
  326. 13:45second major con is there is no
  327. 13:49separation of
  328. 13:51concerns and this is actually focusing
  329. 13:54more at the code level everything is
  330. 13:57just in one app on one server meaning
  331. 14:01everything's tightly coupled it's going
  332. 14:03to be hard to work on that application
  333. 14:06with a larger team so if it's just you
  334. 14:09hey maybe this will work but as you
  335. 14:11start to add new members it's going to
  336. 14:13be more challenging to scale not just
  337. 14:16the actual computer hardware but
  338. 14:18actually build code that's easy to work
  339. 14:21with in a team environment so you may be
  340. 14:23limited to just the general scalability
  341. 14:26of the code and the maintenance of that
  342. 14:28code so that might not be a direct
  343. 14:30performance concern but it is a concern
  344. 14:34in terms of going from where you are now
  345. 14:36to millions and millions of users if
  346. 14:38you're doing that all within just a
  347. 14:40single project it might be difficult to
  348. 14:42achieve the end result of having
  349. 14:44millions of users if you have a
  350. 14:46limitation in the code maintainability
  351. 14:49and you cannot easily add code or keep
  352. 14:51it organized working in a team
  353. 14:53environment next major con is this is a
  354. 14:55single point of failure you have one
  355. 14:58server if something goes wrong with that
  356. 14:59server literally everything is broken so
  357. 15:02this is the biggest red flag in my book
  358. 15:05here you don't want to have a single
  359. 15:07point of failure and having everything
  360. 15:09on one server is asking for problems
  361. 15:11there's also concerns such as dsing so
  362. 15:15if somebody wanted to get your app to
  363. 15:18stop working they could just send a ton
  364. 15:20of traffic to this one server maxing out
  365. 15:23its resources and causing your app to
  366. 15:25crash the other thing is this is limited
  367. 15:27to Vertical scaling
  368. 15:30which likely has diminishing returns as
  369. 15:32you go to a larger and larger system so
  370. 15:34we want to think of ways we can
  371. 15:36introduce horizontal scaling to this
  372. 15:38setup so this would be the pros and cons
  373. 15:40of this scenario which is the most
  374. 15:42extreme example of a monolithic
  375. 15:44application probably not something
  376. 15:46you're going to be doing in the real
  377. 15:48world as a professional developer but if
  378. 15:51you just have a small project of your
  379. 15:52own that really doesn't have a lot of
  380. 15:55users this architecture definitely could
  381. 15:57work it's simplest you just need to set
  382. 15:59up one virtual machine on the cloud you
  383. 16:02can run everything from that VM and
  384. 16:04you're good to go but if you need any
  385. 16:05kinds of reliability failover or scale
  386. 16:08you're going to immediately hit all of
  387. 16:09these problems so we're going to talk
  388. 16:11about the next logical move which is to
  389. 16:13go to that three-tier architecture where
  390. 16:16we split out the database the back end
  391. 16:17and the front end so now with the three-
  392. 16:20tier architecture we're going to have a
  393. 16:22dedicated server for the database so you
  394. 16:25can think of our database inside of this
  395. 16:28this computer that looks really terrible
  396. 16:31I'm going to have to redraw that so now
  397. 16:33we have a dedicated database server we
  398. 16:36have a dedicated server for the back end
  399. 16:40and we can have if needed a dedicated
  400. 16:43server for the front end this split
  401. 16:47wouldn't necessarily be required but
  402. 16:49it's a very logical split and it allows
  403. 16:51us to independently scale so going from
  404. 16:54that previous all-in-one monolithic
  405. 16:56thing I would probably just jump to the
  406. 16:58three tier architecture and just split
  407. 17:00it out to have the back
  408. 17:02end the front
  409. 17:04end and the database I would also split
  410. 17:08it out this way just because of my
  411. 17:10experience with Cloud infrastructure
  412. 17:13very easy to do this so for example in
  413. 17:16AWS it might look like this we have
  414. 17:21RDS
  415. 17:24ec2 and then S3 for our frontend static
  416. 17:28file hosting the exact Technologies you
  417. 17:30use for cloud providers might vary if
  418. 17:32you're going to Azure or Google Cloud
  419. 17:34platform and if you're self-hosting it's
  420. 17:36going to be a little bit different but
  421. 17:38these are the things I'm familiar with
  422. 17:40and doing it in this structure is very
  423. 17:42easy where you have a dedicated
  424. 17:44front-end application using something
  425. 17:47like
  426. 17:48react or another frontend Library so
  427. 17:52we're no longer using the template
  428. 17:53engines embedded in the back end one way
  429. 17:56to achieve scale easily is to use
  430. 17:59serverless services so in this scenario
  431. 18:02you pay a cloud provider for the usage
  432. 18:05of your application instead of for a
  433. 18:07certain size of server so that's pretty
  434. 18:11common with the front end so going back
  435. 18:13to what I just had with S3 it is a
  436. 18:16serverless thing where you don't have to
  437. 18:18provision you know a medium server with
  438. 18:21this much RAM and this much CPU you just
  439. 18:23host stuff on S3 and get charged for
  440. 18:27doing so so the easiest Next Step would
  441. 18:29be to make this a serverless thing and
  442. 18:33we're just going to jump right to that
  443. 18:35so running the front end from some
  444. 18:36serverless service is going to be the
  445. 18:39ideal setup since you're just working
  446. 18:41with static files it's very cheap very
  447. 18:43simple and you don't have to be serving
  448. 18:45those from the back end they would just
  449. 18:48have
  450. 18:51HTML CSS and JavaScript and that
  451. 18:54JavaScript would then make a request to
  452. 18:57your backend API so that's going to be
  453. 19:00your typical architecture the exact
  454. 19:02details of how you host the front end is
  455. 19:04not a huge concern because this is most
  456. 19:07likely not going to be the bottleneck
  457. 19:09since you're just working with static
  458. 19:10files you're not doing any computation
  459. 19:12there is computation being done in
  460. 19:14JavaScript but that's all done client
  461. 19:16side so that's given to the user and the
  462. 19:19JavaScript will execute on their machine
  463. 19:22our biggest concern is probably going to
  464. 19:23be here where we have to do computation
  465. 19:27and doing computation for for each
  466. 19:29request and doing that for potentially
  467. 19:31millions of users can get pretty
  468. 19:32expensive so we're going to prioritize
  469. 19:35scaling this layer first we're not even
  470. 19:37going to worry about this layer and then
  471. 19:39we're going to look at that layer later
  472. 19:41so let's just kind of zoom in on this
  473. 19:44section and talk about things we can add
  474. 19:46or change to make it better before we go
  475. 19:48in and change a bunch of stuff with our
  476. 19:50backend architecture let's just do a
  477. 19:52quick check in so this would be the next
  478. 19:55logical step going from just a monolith
  479. 19:57to now having this three tier
  480. 20:00architecture with basically three
  481. 20:02sections and you can now scale each one
  482. 20:05of these independently so if your
  483. 20:07original bottleneck was just the entire
  484. 20:09server it might have been a little bit
  485. 20:11more challenging because you'd have to
  486. 20:12go in there and figure out what's using
  487. 20:14what resources but now if you have a
  488. 20:16dedicated database server you can just
  489. 20:18see how much resources is on that server
  490. 20:21and is it enough to run that database
  491. 20:24server so you can independently scale up
  492. 20:27or down same thing for the back end and
  493. 20:31the front end so that is the biggest
  494. 20:33upgrade here is independently scaling
  495. 20:36these things having these split out to
  496. 20:38independently scale can potentially help
  497. 20:40us find any bottlenecks and just improve
  498. 20:42the overall performance of our
  499. 20:44application so let's talk about the pros
  500. 20:46and cons of this setup the first one
  501. 20:48it's really
  502. 20:50common which means you're not doing some
  503. 20:53crazy setup nobody's done before if you
  504. 20:55run into issues you'll probably be able
  505. 20:56to find plenty of articles TOS or people
  506. 20:59to support you to get those issues
  507. 21:01figured out next up we can independently
  508. 21:04scale the different
  509. 21:05sections which is a major improvement
  510. 21:08over the previous architecture where
  511. 21:10everything was ran from a single server
  512. 21:12the
  513. 21:13cons from a code perspective you will
  514. 21:16now have to have an API whereas before
  515. 21:19you were possibly just using some kind
  516. 21:21of template engine you didn't have to
  517. 21:23design your API layer so I don't even
  518. 21:26know if that's a con it's just something
  519. 21:27to be aware of perhaps you now have to
  520. 21:31code more interfaces between different
  521. 21:34components it also adds complexity with
  522. 21:38cores so cross origin requests from the
  523. 21:42front end to the back end you'll have to
  524. 21:44figure that all out as well as
  525. 21:47networking between the backend and the
  526. 21:50database so you have to learn a little
  527. 21:53bit about permissions and how to
  528. 21:56actually get those to communicate now
  529. 21:57when it comes to scaling you can be more
  530. 21:59granular growing each individual thing
  531. 22:01up or down but you have to be able to
  532. 22:04know how to identify what parts aren't
  533. 22:07working well and you can't just grow one
  534. 22:10server which is really simple and we're
  535. 22:12still limited to Vertical scaling in
  536. 22:15this
  537. 22:17setup with the exception of the front
  538. 22:19end which is probably serverless but
  539. 22:22we're really thinking about the backend
  540. 22:24here we didn't talk about how to
  541. 22:25actually set up horizontal scaling and
  542. 22:27that's going to be our next logical
  543. 22:29progression so overall this is better
  544. 22:32than the first setup but still has some
  545. 22:34limitations all right so we've made it
  546. 22:36through the extreme monolith the three-
  547. 22:38tier architecture and now we're going to
  548. 22:40zoom into to the backend layer of that
  549. 22:42three- tier architecture and talk about
  550. 22:44scaling the back end most likely this is
  551. 22:47going to be the first bottleneck you
  552. 22:49encounter because the front end is
  553. 22:51extremely easy to scale and the database
  554. 22:54layer you'll most likely just vertically
  555. 22:55scale for a while so we'll zoom into
  556. 22:58that back end layer and the very first
  557. 23:00thing we're going to want to do is do
  558. 23:02horizontal
  559. 23:03scaling this is going to allow us to
  560. 23:06remove the single point of failure if
  561. 23:09you have just one backend server and all
  562. 23:13of the requests go to this same server
  563. 23:15and for some reason this server goes
  564. 23:17down your entire application is now
  565. 23:20broken so if we introduce horizontal
  566. 23:22scaling we'll now have multiple servers
  567. 23:25which means we have some redundancy if
  568. 23:27one server goes down we can redirect
  569. 23:29traffic to a server that is working so
  570. 23:32we can just introduce a couple of
  571. 23:34additional servers these don't have to
  572. 23:37be more powerful servers simply by
  573. 23:40adding more you can achieve more
  574. 23:42throughput so we've pretty much 3x the
  575. 23:46number of users we can support and if
  576. 23:49for some reason one of these goes
  577. 23:51down we can just redirect this traffic
  578. 23:55to another node instead of this one so
  579. 23:58all in all our system is still
  580. 24:01operational so far so good the challenge
  581. 24:05is how do you know which of these
  582. 24:07servers to connect to and how do you do
  583. 24:10that from a frontend application if you
  584. 24:13have multiple end points and what if one
  585. 24:15of them goes down well this is all going
  586. 24:18to be done for you by a load balancing
  587. 24:21layer so you can think of the load
  588. 24:25balancer as a service which will take
  589. 24:27requests and direct them to the correct
  590. 24:29location so it might look like this
  591. 24:31where blue is a request and then this
  592. 24:34will direct that to the appropriate
  593. 24:38server so we only have one end point and
  594. 24:41this would be where the front end
  595. 24:43connects to all of this splitting out
  596. 24:45the traffic to the individual servers is
  597. 24:49going to be abstracted away from the
  598. 24:50front end handled all by the load
  599. 24:53balancer so the load balancer would be
  600. 24:55the one responsible to adjust the
  601. 24:58traffic if one of these servers goes
  602. 25:00down so it might look something like
  603. 25:02this and the load balancer will also do
  604. 25:05its best to balance the traffic so
  605. 25:08instead of This Server just getting
  606. 25:10double the traffic it would now just
  607. 25:12split 50/50 across these two nodes so
  608. 25:17that's what a load balancer does and
  609. 25:18when I draw these out I'll often just
  610. 25:20draw it as like a circle or a square
  611. 25:22around the servers so it might look
  612. 25:25something like this where we have the
  613. 25:27three servers and they're all wrapped by
  614. 25:30a load balancer so now this is what our
  615. 25:33back end would look like where this
  616. 25:35would be the front end and that would be
  617. 25:36the database and you can imagine having
  618. 25:39a domain such as api. mysite.com and
  619. 25:43this is going to point to the IP address
  620. 25:46of that load balancer so far so good
  621. 25:49let's go through the pros and cons of
  622. 25:50this so for the pros we removed the
  623. 25:54single point of failure in our back end
  624. 25:57we're able to to achieve High
  625. 26:00availability because we have failover if
  626. 26:03one server goes down the traffic's
  627. 26:05redirected to another server so the
  628. 26:07overall functionality of our application
  629. 26:09is not disrupted we are able to
  630. 26:11horizontally scale which in theory is
  631. 26:15cheaper than vertically scaling due to
  632. 26:17diminished returns of vertical scaling
  633. 26:20plus apis are typically stateless they
  634. 26:22don't have to store any information like
  635. 26:25a database so there's no major downside
  636. 26:29to using horizontal scaling over
  637. 26:31vertical scaling another Pro is that
  638. 26:34this would support Auto scaling so we
  639. 26:36can increase and decrease Computing
  640. 26:38Hardware as needed for different spikes
  641. 26:41in our traffic cons is you have to add a
  642. 26:44load balancing service not a huge deal
  643. 26:47but you have to be aware of that and
  644. 26:48know how to do that and then I would say
  645. 26:50the other major con is no
  646. 26:54caching so every request is seen as a
  647. 26:57fresh fresh request coming from the
  648. 27:00front-end client hitting the API making
  649. 27:02a request to the database then returning
  650. 27:05that ultimately back to the client even
  651. 27:07if 80% of the requests are the same
  652. 27:09thing it's still seen as a completely
  653. 27:11new request every time so the next
  654. 27:13logical move will be to talk about
  655. 27:15things we can do to Cache information
  656. 27:18you can think of a cache as something
  657. 27:20that stores information temporarily so
  658. 27:22say we have a database
  659. 27:26here and this database has has some data
  660. 27:29let's just put some
  661. 27:32numbers and then we have a cach layer
  662. 27:36here and then we have a client make a
  663. 27:39request and the request is to retrieve
  664. 27:43the value five so five is going to be
  665. 27:46stored in the cash and then the next
  666. 27:48time a client makes a request to get
  667. 27:50five it just retrieves the cached five
  668. 27:53not having to make another request to
  669. 27:55the database so you can see there's only
  670. 27:57one one request to the database but two
  671. 28:00clients requesting five so this means we
  672. 28:02can drastically reduce the amount of
  673. 28:05requests to the database using that cach
  674. 28:07layer say a client requests something
  675. 28:10that's not in the cach say it wants s it
  676. 28:13would basically look in the cache
  677. 28:15realize it's not
  678. 28:17there retrieve it from the
  679. 28:21database and then that would be stored
  680. 28:23in the cache as well and then seven
  681. 28:26would be given back to this client so
  682. 28:29that first client might take a little
  683. 28:30bit extra time cuz it has to go to the
  684. 28:33database but then for future requests
  685. 28:35say this client wants seven as well it's
  686. 28:38going to be a lot faster because it can
  687. 28:40just grab the cash version so that's the
  688. 28:42idea of a Cash There's also another
  689. 28:44related thing which is a CDN and this is
  690. 28:47a Content delivery network but it
  691. 28:50functions pretty much like a cache so
  692. 28:52say you are over here and you want to
  693. 28:55make a request to my server
  694. 28:58down here the amount of distance you
  695. 29:00have to travel is quite far and when you
  696. 29:04make a request to some web server that
  697. 29:06request ultimately does have to make its
  698. 29:08way through the internet and travel some
  699. 29:12distance so that's a far distance to
  700. 29:14travel and that's going to make the
  701. 29:17application slower so what a CDN will do
  702. 29:20is it will basically put your
  703. 29:22application casted results in a bunch of
  704. 29:26different locations so so it might look
  705. 29:28something like
  706. 29:31this and now when you make a request you
  707. 29:33get some information from the server the
  708. 29:35request will actually go to the closest
  709. 29:39node to you drastically reducing the
  710. 29:42time it takes to get the response
  711. 29:45because you don't have to go all the way
  712. 29:47back to the main server so a CDN is
  713. 29:49going to introduce two major benefits
  714. 29:51the first is it's going to be an
  715. 29:53increase in speed for the end user or
  716. 29:56you can think of it as reducing lat laty
  717. 29:58and it's going to reduce computational
  718. 30:01effort on the end server because that
  719. 30:05request does not have to make it to the
  720. 30:07server so introducing a CDN to our
  721. 30:11structure is going to be the next
  722. 30:13logical step now what people typically
  723. 30:16use cdns for is for caching static
  724. 30:19content CSS HTML JavaScript pictures
  725. 30:23videos and this is ultimately to
  726. 30:25increase the speed of the application so
  727. 30:29the end user's experience is much better
  728. 30:31while you can potentially cache certain
  729. 30:34API responses that don't change very
  730. 30:36frequently you're most likely going to
  731. 30:39use cdns for this purpose and use a
  732. 30:42separate caching layer such as redus to
  733. 30:45improve the compute aspect of the back
  734. 30:48end that being said if you are trying to
  735. 30:51build an app that millions of people are
  736. 30:52going to use you will definitely want it
  737. 30:55to be fast and performant and one of the
  738. 30:58best ways to do that is to get the files
  739. 31:00as close to the user as possible so one
  740. 31:03of the most popular CDN is cloud flare
  741. 31:06and you could add that to your overall
  742. 31:09architecture so let's go through the
  743. 31:11pros and cons of a CDN the biggest Pro
  744. 31:15is going to be a reduction in latency so
  745. 31:18from the time the user requests your web
  746. 31:21page to when it receives a response it's
  747. 31:24going to be much faster with the CDN the
  748. 31:27biggest con is it increases the
  749. 31:31complexity because now you have to know
  750. 31:33how a CDN works you have to worry about
  751. 31:37the cach time so for example if data
  752. 31:40does change how long until that cash
  753. 31:43expires and the new static files are
  754. 31:46pulled for the CDN or you'll have to go
  755. 31:49in there and manually expire stuff so
  756. 31:52there's a lot of complexities introduced
  757. 31:54with caching layers so under
  758. 31:56complexities we have the time to live
  759. 32:00which is the cash time we were just
  760. 32:02referring to you also have to worry
  761. 32:04about things like cash hit
  762. 32:06ratios freaking
  763. 32:08fly he terrorizing me this one goes back
  764. 32:12to the TTL but you have a risk of
  765. 32:14serving outdated data this is also the
  766. 32:16easiest way to bring an app global
  767. 32:18pretty similar to latency but if you
  768. 32:20zoom in on that idea a little bit you
  769. 32:22could have servers in different
  770. 32:23continents or you can cheat and just use
  771. 32:25a CDN to kind of make your app Global
  772. 32:28not the perfect solution especially if
  773. 32:30you have to make requests to the backend
  774. 32:32and the back end is in a different
  775. 32:33continent but this is a great first step
  776. 32:36because it's relatively simple to set up
  777. 32:38it's affordable and it will reduce
  778. 32:39latency for users significantly it can
  779. 32:42improve security so often cdns have
  780. 32:46things like dos protection and other
  781. 32:48services to protect your app and if set
  782. 32:50up correctly cdns could be good for SEO
  783. 32:53so you can read more about that if
  784. 32:55you're into SEO but just some that might
  785. 32:58be worth knowing about the other one is
  786. 33:01it's great for images videos downloads
  787. 33:04if your app utilizes a lot of images
  788. 33:06videos and downloads you may benefit
  789. 33:08from a CDN not only for performance but
  790. 33:11also for cost savings so you may need to
  791. 33:14look into that in more detail but worth
  792. 33:16knowing that this exists if you are
  793. 33:18building something like this you can do
  794. 33:20some additional Research into cdns so
  795. 33:23here is the flow of a request from an
  796. 33:26end user's computer they're going to
  797. 33:28make a request to our front end now when
  798. 33:32they do this they'll just put in a URL
  799. 33:34into their browser this is going to
  800. 33:37utilize a DNS record so this is kind of
  801. 33:40a separate thing but just kind of wanted
  802. 33:42to mention it here and that DNS record
  803. 33:44is going to associate that URL to some
  804. 33:48IP address typically the IP address of
  805. 33:51the front end application that they're
  806. 33:52trying to access when you're utilizing a
  807. 33:54CDN it might be a little bit more
  808. 33:56complicated because it might direct them
  809. 33:58to the IP address of an edge node
  810. 34:00instead of directly to our frontend
  811. 34:07server so I'll just write the CDN here
  812. 34:11so they'll either get the front end from
  813. 34:14one of the edge nodes or directly from
  814. 34:16the server and that'll make a request to
  815. 34:19the load balancer which is going to
  816. 34:21manage our back end and in here we might
  817. 34:24have say three
  818. 34:25servers and it will manage the traffic
  819. 34:28between those and then I'll just say
  820. 34:30back end here so it might look something
  821. 34:33like
  822. 34:34that you can visualize it however you
  823. 34:36need but basically the load balancer
  824. 34:39manages the three backend servers in
  825. 34:41this scenario and we just have a single
  826. 34:43end point for the front end to connect
  827. 34:46to the back end and
  828. 34:48then each of these will connect to the
  829. 34:51same database server so it looks
  830. 34:55something like that so far so good now
  831. 34:58the next thing that we can introduce is
  832. 35:00a caching layer which instead of caching
  833. 35:04here we'll cash here and this can reduce
  834. 35:08the number of requests to the database
  835. 35:10because we might be retrieving the same
  836. 35:11stuff over and over and over again so we
  837. 35:13get to store it in this cach layer in
  838. 35:15memory which will increase the overall
  839. 35:18speed but also reduce the computation
  840. 35:21needed at the database level so as
  841. 35:23mentioned the next thing you can do to
  842. 35:24improve both the performance and the
  843. 35:26scale of your system is to add in
  844. 35:29caching between the API backend server
  845. 35:32and the database so it might look
  846. 35:34something like this where you have the
  847. 35:38database and then you have a cash
  848. 35:42layer and this will act as an
  849. 35:44intermediary between the server and the
  850. 35:46database so a request to the database
  851. 35:49will first check the cash for that value
  852. 35:52so the biggest cache that you're
  853. 35:53probably going to come across is reddis
  854. 35:56there's also M cach and probably some
  855. 35:58others but this is an inmemory thing so
  856. 36:01this will be in
  857. 36:05Ram but you don't need a lot of ram to
  858. 36:09cach the majority of requests it's often
  859. 36:13the case that there is some natural data
  860. 36:16that gets retrieved a lot more
  861. 36:18frequently than other data so it might
  862. 36:21be the case that 80% of the requests are
  863. 36:25retrieving 20% of the dat
  864. 36:28so in that situation we could put the
  865. 36:3120% in Ram which is substantially
  866. 36:35faster and reduce the total of request
  867. 36:39counts to the database by 80% so not
  868. 36:43only do you get a lot more speed but you
  869. 36:45massively reduce the amount of computing
  870. 36:47power that the database layer needs now
  871. 36:50we talked about how caches work we said
  872. 36:53that when some value is retrieved from
  873. 36:55the database the first time it will pass
  874. 36:58through the cache grab the data and then
  875. 37:01on the way back store that data in the
  876. 37:03cache the other thing you need to be
  877. 37:05aware of is if you update this data
  878. 37:08let's say we updated it to a six we
  879. 37:10would need to write our application code
  880. 37:12so that when we make that update we also
  881. 37:15update the cach or clear the cach so
  882. 37:19that the cach doesn't have outdated data
  883. 37:23so again an extra complexity you need to
  884. 37:25be concerned with that's an easy way of
  885. 37:27doing it is just whenever you update the
  886. 37:28data in the database also update the
  887. 37:30cache or clear the
  888. 37:32cach otherwise there could be out of
  889. 37:35sync data here which in some
  890. 37:36applications that's not a huge deal
  891. 37:38especially if the time that the data
  892. 37:40stays in the cache is not forever right
  893. 37:43if it expires every so often then this
  894. 37:46will basically just lag behind the
  895. 37:48database a little bit but just updating
  896. 37:51both at once is a really good way to
  897. 37:53keep those in sync now an in-memory
  898. 37:55cache does store data but it's all
  899. 37:57volatile because it's in Ram meaning if
  900. 38:00the system shuts down and comes back
  901. 38:03online the data is going to be gone it's
  902. 38:06not stored on disk that's usually fine
  903. 38:08because we can just get the data from
  904. 38:10the database again it would just be a
  905. 38:12temporary performance hit because nobody
  906. 38:15would be hitting the cached data they
  907. 38:17would have to make the request to the
  908. 38:19database you can scale the cash
  909. 38:21horizontally and have less of concerns
  910. 38:24with data issues but you can look into
  911. 38:27into all of the details on reddis
  912. 38:29clustering and the different
  913. 38:30configurations you can do but I would
  914. 38:32horizontally scale the cache before I
  915. 38:35horizontally scaled the database layer
  916. 38:37unless you're working with a database
  917. 38:39that is very good at horizontal scaling
  918. 38:42which we'll talk about here in just a
  919. 38:44moment after we get through the cach
  920. 38:46Section but you can scale this cach as
  921. 38:49needed to support the number of requests
  922. 38:51from the API so this does add an
  923. 38:54additional layer and you might think oh
  924. 38:56that would cost more but based on the
  925. 38:58way the cash works it's probably going
  926. 39:00to end up being cheaper and
  927. 39:02substantially faster to use a cash
  928. 39:05versus just making a request straight to
  929. 39:07the database every time and you can also
  930. 39:09really consider situations where perhaps
  931. 39:12there are some complex joins or some
  932. 39:14things going on behind the scenes for a
  933. 39:16particular query and caching the result
  934. 39:19of that is going to save a lot of
  935. 39:21computation because you're not going to
  936. 39:23have to make those complex joints over
  937. 39:25and over again so let's talk about the
  938. 39:27pros and cons the pros it is going to
  939. 39:31reduce the amount of compute needed for
  940. 39:34the
  941. 39:35database it's going to improve the
  942. 39:38overall speed of your application or
  943. 39:41reduce latency it's probably going to
  944. 39:43end up being cheaper the major cons is
  945. 39:46an increase in complexity so all of the
  946. 39:49issues that we were talking about with
  947. 39:50the CDN is going to apply here but it's
  948. 39:53worse because it actually requires us to
  949. 39:56do different stuff in our code such as
  950. 39:58write to the cach or read from the cache
  951. 40:01if we update something in the database
  952. 40:03we might need to update the cach or
  953. 40:04clear the cache all of this stuff has to
  954. 40:06be done in code where the CDN is a bit
  955. 40:08more automatic so it's going to increase
  956. 40:11the development
  957. 40:13efforts which could actually cost money
  958. 40:17because if you're paying for development
  959. 40:20time and they have to write more code or
  960. 40:22the system is more confusing or complex
  961. 40:25that's going to take more time that's a
  962. 40:26pretty good summary of the pros and cons
  963. 40:28of a caching layer and now let's talk
  964. 40:31about the database layer up until this
  965. 40:33point you've done a lot of things to
  966. 40:35scale your app and you're probably in a
  967. 40:37good
  968. 40:38scenario you usually don't have to do
  969. 40:41too much with the database Beyond
  970. 40:42vertical scaling basically just creating
  971. 40:45a really big database server but you
  972. 40:47might be concerned about the high
  973. 40:49availability aspect and for that you can
  974. 40:52have a standin
  975. 40:54database so if this is your database
  976. 40:56server you can have one that is just
  977. 40:59there in the background as a backup if
  978. 41:01something goes wrong with your main
  979. 41:03database that is one option as an
  980. 41:05alternative to having multimaster
  981. 41:08clustering which can be fairly complex
  982. 41:11you can also clone to read nodes and I'm
  983. 41:14kind of glossing over a lot of the
  984. 41:16details here because I'm going to have
  985. 41:18multiple videos on database systems
  986. 41:21coming up so I would just encourage you
  987. 41:24to check those videos out for more
  988. 41:26details but I just want wanted to
  989. 41:27include the basics here to
  990. 41:29comprehensively cover scaling and app
  991. 41:32these could be read
  992. 41:34nodes and it's often the case that an
  993. 41:37application is going to do primarily
  994. 41:39reads so you might
  995. 41:41do just one big write or Master node
  996. 41:45server and then have a bunch of
  997. 41:46independent databases that are for
  998. 41:49reading so you can scale reads and wrs
  999. 41:51independently and probably scale reads
  1000. 41:54higher than the rights because because
  1001. 41:57you probably have more reads than writes
  1002. 42:00this may be the setup you use if you're
  1003. 42:01using a relational database hosted in
  1004. 42:03RDS for example but you can also use
  1005. 42:06larger cluster services for relational
  1006. 42:08databases and you can also look into
  1007. 42:11multimaster databases especially if you
  1008. 42:14go into the nosql world that's pretty
  1009. 42:17common but typically that previous setup
  1010. 42:19is adequate however if you do get into
  1011. 42:21multimaster it might be something like
  1012. 42:24this where you have multiple nodes
  1013. 42:29and the data is either split up across
  1014. 42:31these nodes or replicated to each
  1015. 42:36other so there's various setups for
  1016. 42:39cluster databases again we're going to
  1017. 42:41get into that in more detail in upcoming
  1018. 42:43videos so to summarize the database
  1019. 42:45layer I would pretty much just scale
  1020. 42:47vertically as much as humanly possible
  1021. 42:50before having to scale horizontally
  1022. 42:53unless you're working with a database
  1023. 42:54service that does scale horizontally
  1024. 42:57very nicely and then as for high
  1025. 42:58availability you could have a
  1026. 43:00synchronized standin node which could
  1027. 43:02fill in if the main server goes down
  1028. 43:04that's just one approach there are
  1029. 43:06probably many different approaches to
  1030. 43:08the data layer but most of the time the
  1031. 43:11issues are going to be at the API level
  1032. 43:14the caching level or even the CDN static
  1033. 43:18content and front-end level another
  1034. 43:20thing you can do with the database layer
  1035. 43:21is instead of storing everything in one
  1036. 43:23massive database you could split that
  1037. 43:25out into smaller more Focus fed
  1038. 43:27databases so for example you might have
  1039. 43:29a database just for user authentication
  1040. 43:31same idea with the backend you can split
  1041. 43:33out your services into smaller more
  1042. 43:35focused services and start moving
  1043. 43:38towards a microservice approach which
  1044. 43:40you can then more independently scale it
  1045. 43:43does always introduce complexities
  1046. 43:44because you'll have to worry about
  1047. 43:45communication between the microservices
  1048. 43:48but as a general rule of thumb if a
  1049. 43:51service starts to get too many users you
  1050. 43:54can split it out horizontally or grow
  1051. 43:56vertically or split the service into
  1052. 43:58smaller microservices and scale those
  1053. 44:02independently sort of how we did that
  1054. 44:04from a monolith to a three tier you
  1055. 44:07could do the same idea just more
  1056. 44:08granular so let me just do a quick
  1057. 44:10overview of what our full architecture
  1058. 44:12will be kind of something like this so
  1059. 44:14we have the end user making a request
  1060. 44:18and this will get the front end or some
  1061. 44:22cached version from the
  1062. 44:25CDN this will make request to the load
  1063. 44:30balancer which will hit the API servers
  1064. 44:34and then you don't really go through a
  1065. 44:37cache to get to the database it's more
  1066. 44:39kind of like
  1067. 44:42this where we have the cach and then we
  1068. 44:46have the
  1069. 44:49database because you can think of the
  1070. 44:50cach is a data store kind of like a
  1071. 44:52database it's just that it uses memory
  1072. 44:55instead of the disc
  1073. 44:58so in code you would check the
  1074. 45:00cache and then you would go to the
  1075. 45:03database if it wasn't found so there's a
  1076. 45:06million different ways of visualizing it
  1077. 45:07so apologies if I'm changing it but I
  1078. 45:10don't want to give you the impression
  1079. 45:11that there's only one visual correct way
  1080. 45:15of representing this information I also
  1081. 45:17forgot an e so there we go so that is an
  1082. 45:20overview of scaling and app we're going
  1083. 45:22to cover a lot more system design topics
  1084. 45:24and get into some of the information we
  1085. 45:26talked about here here in much more
  1086. 45:27detail so stay tuned for those upcoming
  1087. 45:30lessons again as a reminder if you want
  1088. 45:32the backend mind map or you're looking
  1089. 45:35for more involved mentorship check out
  1090. 45:37the links down below that will get you
  1091. 45:39started to get into a software
  1092. 45:41engineering role and get some more
  1093. 45:43practical guidance on things like this
  1094. 45:46so stay tuned for upcoming content I'll
  1095. 45:47see you in the next one

About this transcript

This page contains the full transcript of Scale an App to Millions of Users - System Design by Caleb Curry, generated from the public captions YouTube serves with the video. The transcript has 7,730 words across 1,095 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 choose a caption track available on the video.

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, caption language selection, AI prompts for ChatGPT, Claude, and Gemini, and exports to TXT, SRT, VTT, or Markdown.