├── LICENSE ├── README.md ├── SUMMARY.md ├── basics ├── caching.md ├── cap-theorem.md ├── client-server-communication.md ├── consistent-hashing.md ├── indexes.md ├── key-characteristics.md ├── load-balancing.md ├── proxies.md ├── queues.md ├── redundancy.md ├── sharding.md └── sql-vs-nosql.md ├── bin └── publish-gh-pages.sh ├── book.json ├── designs ├── dropbox.md ├── facebook-messenger.md ├── facebook-newsfeed.md ├── instagram.md ├── pastebin.md ├── short-url.md ├── ticketmaster.md ├── twitter-search.md ├── twitter.md ├── uber-backend.md ├── web-crawler.md ├── yelp.md └── youtube.md └── img ├── dropbox-detail.png ├── dropbox-overview.png ├── facebook-messenger-detail.png ├── facebook-messenger-overview.png ├── facebook-newsfeed-overview.png ├── instagram-detail.png ├── instagram-overview.png ├── pastebin-detail.png ├── pastebin-overview.png ├── short-url-detail.png ├── short-url-overview.png ├── ticketmaster-overview.png ├── twitter-detail.png ├── twitter-overview.png ├── twitter-search-detail.png ├── twitter-search-overview.png ├── uber-backend-overview.png ├── web-crawler-detail.png ├── web-crawler-overview.png ├── yelp-overview.png ├── youtube-detail.png └── youtube-overview.png /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | 635 | Copyright (C) 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | Copyright (C) 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [Grokking System Design Interview](https://www.educative.io/collection/5668639101419520/5649050225344512) 2 | ==== 3 | Source: [educative](https://www.educative.io) 4 | 5 | ## Interview Process 6 | - Scope the problem 7 | - Don’t make assumptions. 8 | - Ask clarifying questions to understand the constraints and use cases. 9 | - Steps 10 | - Requirements clarifications 11 | - System interface definition 12 | - Sketch up an abstract design 13 | - Building blocks of the system 14 | - Relationships between them 15 | - Steps 16 | - Back-of-the-envelope estimation 17 | - Defining data model 18 | - High-level design 19 | - Identify and address the bottlenecks 20 | - Use the fundamental principles of scalable system design 21 | - Steps 22 | - Detailed design 23 | - Identifying and resolving bottlenecks 24 | 25 | ## Distributed System Design Basics 26 | - [Key Characterics](basics/key-characteristics.md) 27 | - [Load balancing](basics/load-balancing.md) 28 | - [Caching](basics/caching.md) 29 | - [Sharding](basics/sharding.md) 30 | - [Indexes](basics/indexes.md) 31 | - [Proxies](basics/proxies.md) 32 | - [Queues](basics/queues.md) 33 | - [Redundancy](basics/redundancy.md) 34 | - [SQL vs. NoSQL](basics/sql-vs-nosql.md) 35 | - [CAP Theorem](basics/cap-theorem.md) 36 | - [Consistent Hashing](basics/consistent-hashing.md) 37 | - [Client Server Communication](basics/client-server-communication.md) 38 | 39 | ## System Designs 40 | - [Short URL Service](designs/short-url.md) 41 | - [Pastebin](designs/pastebin.md) 42 | - [Instagram](designs/instagram.md) 43 | - [Dropbox](designs/dropbox.md) 44 | - [Twitter](designs/twitter.md) 45 | - [Youtube](designs/youtube.md) 46 | - [Twitter Search](designs/twitter-search.md) 47 | - [Web Crawler](designs/web-crawler.md) 48 | - [Facebook Newsfeed](designs/facebook-newsfeed.md) 49 | - [Yelp](designs/yelp.md) 50 | - [Uber Backend](designs/uber-backend.md) 51 | - [Ticketmaster](designs/ticketmaster.md) 52 | 53 | 54 | ## System Design Interviews: A step by step guide 55 | ************************************************************************************************************************** 56 | ************************************************************************************************************************** 57 | 58 | # SYSTEM DESIGN PREPARATION 59 | * How to prepare for and answer system design questions 60 | 61 | ## Objective 62 | *Learning about and implementing large-scale distributed system is not easy. I do not want to give the impression that it's something that can be learnt in a month.* 63 | What this repository aims to achieve, is for software engineers and students to get a rough idea of how the thought process of designing a large scale works and how big companies have managed to solve really hard problems. Along with that, there is a recent trend for companies to have an open-ended interview with system design questions, which is at times hard for engineers of all levels if they haven't gotten the opportunity to work on such systems themselves. 64 | 65 | This is a collection of links/documents for the following use cases: 66 | a) Prepare for a system design or open-ended rounds. 67 | b) Learn more about how large-scale systems work and thought process of designing a new system. 68 | 69 | ## Index 70 | - [ ] [Starting point](#start) 71 | - [ ] [basics](#basics) 72 | - [ ] [How to answer in interviews](#howtoans) 73 | - [ ] [Steps how I approach the system design questions in interviews](#myapproach) 74 | - [ ] [Common Design questions](#designques) 75 | - [ ] [architecture](#architecture) 76 | - [ ] [company engineering blog links](#blog) 77 | - [ ] [Low on time ?](#tldr) 78 | 79 | ## Starting point 80 | 81 | For a very broad overview please go through these lectures, really useful: 82 | * [Gaurav Sen's system design series](https://www.youtube.com/playlist?list=PLMCXHnjXnTnvo6alSjVkgxV-VH6EPyvoX) 83 | Starts from simple stuff like load balancing and message queues, then moves to building full systems like Whatsapp and Tinder. 84 | 85 | * [david malans cs75 scalability talk](https://www.youtube.com/watch?v=-W9F__D3oY4&list=PLmhRNZyYVpDmLpaVQm3mK5PY5KB_4hLjE&index=10) 86 | Feel free to go through other lectures if needed. 87 | 88 | * [david huffman's talk , scaling up talk](https://www.udacity.com/course/web-development--cs253) ([Youtube link](https://www.youtube.com/watch?v=pjNTgULVVf4&list=PLVi1LmRuKQ0NINQfjKLVen7J2lZFL35wP&index=1)) 89 | 90 | * [scalability for dummies](http://www.lecloud.net/tagged/scalability) 91 | 92 | * [Designing data intensive appliations]() This is by far one of the best books about large-scale systems and the practical challenges encountered during building them. It's focussed more on data-oriented applications though. 93 | 94 | These talks should give you a starting point on how to think about such problems. 95 | 96 | ## Basics 97 | 98 | But before you begin, here are some topics(in no particular order) which in my opinion you should have a decent idea of before proceeding. 99 | 100 | 1. Operating system basics: how a file system, virtual memory, paging, instruction execution cycle etc work 101 | (For starters silbershatz should be enough. If you already have decent knowledge try stallings book on OS) 102 | 2. Networking basics: 103 | Should know the TCP/IP stack, basics of how Internet, HTTP, TCP/IP work at the minimum. cs75 on youtube (1st lecture) should give a broad overview. I personally love [networking-a top down approach](http://www.amazon.com/Computer-Networking-Top-Down-Approach-Edition/dp/0132856204). 104 | 3. Concurrency basics: threads, processes, threading in the language you know. Locks , mutex etc. 105 | 4. DB basics: types of DB's (SQL vs noSQL etc ), hashing and indexing, EAV based databases, Sharding, caching for databases, master-slave etc 106 | 5. A basic idea of how a basic web architecture is: say load balancers, proxy, servers, Database servers, caching servers, precompute, logging big data etc. Just know broadly what is each layer for. 107 | 6. very basic summary of what the [CAP theorem](http://robertgreiner.com/2014/08/cap-theorem-revisited/) is (Have never been asked about the theorem itself, but knowing it will help you in designing large-scale systems. 108 | 109 | ## How to answer in interviews 110 | 111 | * I found [hiredintech](http://www.hiredintech.com/system-design) videos an excellent place to start with. The way how to approach a design question as given in the link is really useful. It goes into how we start with clearing the use-cases of the system, then thinking in the abstract manner of the various component and the interactions. Think about the bottlenecks of the system and what is more critical for your system (eg latency vs reliability vs uptime etc) Address those giving the tradeoff of your approach. 112 | 113 | * [system design in crack the coding interview](http://www.flipkart.com/cracking-coding-interview-150-programming-questions-solutions-english-5th/p/itmdz4pvzbhcv6uv): good approach on how to begin attacking a problem by first solving for a small usecase then expanding the system. 114 | 115 | * The best way to prepare for such questions is do mock interviews, pick any topic (given below) try to come up with a design and then go and see how and why it is designed in that manner. There is absolutely no alternative to practice!! Whiteboarding a system design question is similar to actually writing code and testing it! Just reading will only take you so far. 116 | 117 | ## Steps how I approach the system design questions in interviews 118 | 119 | These are the steps I go through mentally in the interviews, followed by actual interview experiences: 120 | 121 | * a) **Be absolutely sure you understand the problem being asked**, clarify on the onset rather than assuming anything 122 | * b) **Use-cases**. This is critical, you MUST know what is the system going to be used for, what is the scale it is going to be used for. Also, constraints like requests per second, requests types, data written per second, data read per second. 123 | * c) Solve the problem for a **very small set**, say, 100 users. This will broadly help you figure out the data structures, components, abstract design of the overall model. 124 | * d) Write down the various components figured out so far and how will they interact with each other. 125 | * e) As a rule of thumb remember at least these : 126 | * 1. processing and servers 127 | * 2. storage 128 | * 3. caching 129 | * 4. concurrency and communication 130 | * 5. security 131 | * 6. load balancing and proxy 132 | * 7. CDN 133 | * 8. Monetization: if relevant, how will you monetize? 134 | eg. What kind of DB (Is Postgres enough, if not why?), do you need caching and how much, is security a prime concern? 135 | * f) **Special cases** for the question asked. Say designing a system for storing thumbnails, will a file system be enough? What if you have to scale for facebook or google? Will a nosql based database work? 136 | * g) After I have my components in place, what I generally try to do is look for minor optimization in various places according to the use-cases, various tradeoffs that will help in better scaling in 99% cases. 137 | * h) [Scaling out or up] (http://highscalability.com/blog/2014/5/12/4-architecture-issues-when-scaling-web-applications-bottlene.html) 138 | * i) Check with the interviewer is there any other special case he is looking to solve? Also, it really helps if you know about the company you are interviewing with, what its architecture is, what will the interviewer have more interest in based on the company and what he works on? 139 | 140 | ## Common Design questions 141 | It generally depends what you are and you will be working on. Also what your level is but these are some of the more frequent interview questions. 142 | 143 | * Design amazon's frequently viewed product page (eg. which shows the last 5 items you saw) 144 | * Design an online poker game for multiplayer. Solve for persistence, concurrency, scale. Draw the ER diagram for this 145 | * Design a [url compression system] (http://www.hiredintech.com/system-design/the-system-design-process/) 146 | * [Search engine](http://infolab.stanford.edu/~backrub/google.html) (generally asked with people who have some domain knowledge): basic crawling, collection, hashing etc. Depends on your expertise on this topic 147 | * Design dropbox's architecture. [good talk on this](https://www.youtube.com/watch?v=PE4gwstWhmc) 148 | * Design a [picture sharing website](http://highscalability.com/blog/2011/12/6/instagram-architecture-14-million-users-terabytes-of-photos.html). How will you store thumbnails, photos? Usage of CDNS? caching at various layers etc. 149 | * * Design a news feed (eg. Facebook , Twitter): [news feed](http://www.quora.com/Software-Engineering-Best-Practices/What-are-best-practices-for-building-something-like-a-News-Feed) 150 | * Design a product based on maps, eg hotel / ATM finder given a location. 151 | * Design malloc, free and [garbage collection system](http://courses.cs.washington.edu/courses/csep521/07wi/prj/rick.pdf). What data structures to use? decorator pattern over malloc etc. 152 | * Design a site like [junglee.com](http://www.junglee.com/) i.e price comparision, availability on e-commerce websites. When and will you cache, how much to query, how to crawl efficiently over e-commerce sites, sharding of databases, basic database design 153 | * A web application for instant messaging, eg [whatsapp](http://highscalability.com/blog/2014/2/26/the-whatsapp-architecture-facebook-bought-for-19-billion.html), facebook chat. Issues of each, scaling problems, status and availability notification etc. 154 | * Design a system for collaborating over a document simultaneously (eg [google docs](https://neil.fraser.name/writing/sync/)) 155 | * (very common:) top 'n' or most frequent items of a running stream of data 156 | * Design election commission architecture : 157 | Let's say we work with the Election Commission. On Counting day, we want to collate the votes received at the lakhs of voting booths all over the country. Each booth has a voting machine, which, when connected to the network, returns an array of the form {[party_id, num_votes],[party_id_2, num_votes_2],...}. We want to collect these and get the current scores in real time. The report we need continuously is how many seats is each party leading in. Please design a system for this. 158 | * Design a logging system 159 | (For web applications, it is common to have a large number of servers running the same application, with a load balancer in front to distribute the incoming requests. In this scenario, we want to check and alarm in case an exception is thrown in any of the servers. We want a system that checks for the appearance of specific words, "Exception", "Disk Full" etc. in the logs of any of the servers. How would you design this system?) 160 | 161 | ## Architectures : 162 | 163 | Personally I looked into the following architectures: 164 | 165 | * [Basics of google search](http://infolab.stanford.edu/~backrub/google.html) 166 | * Basics of messaging frameworks like Kafka , queuing architectures like rabbitmq. 167 | * Broad overview and advantages of Redis , mongodb , cassandra. 168 | * [Google file system](http://static.googleusercontent.com/media/research.google.com/en//archive/gfs-sosp2003.pdf) 169 | * [Google architecture] (http://highscalability.com/google-architecture) 170 | * [Instagram](http://instagram-engineering.tumblr.com/post/13649370142/what-powers-instagram-hundreds-of-instances) and other image based social networks 171 | * [Memcache scaling by facebook](https://cs.uwaterloo.ca/~brecht/courses/854-Emerging-2014/readings/key-value/fb-memcached-nsdi-2013.pdf) 172 | * [Twitter scaling](https://www.youtube.com/watch?v=z8LU0Cj6BOU) and facebook feeds 173 | * [facebook graph api](https://cs.uwaterloo.ca/~brecht/courses/854-Emerging-2014/readings/data-store/tao-facebook-distributed-datastore-atc-2013.pdf) 174 | * [facebook haystack needle architecture](https://www.usenix.org/legacy/event/osdi10/tech/full_papers/Beaver.pdf) 175 | * [youtube architecture and optimizations for video](https://www.youtube.com/watch?v=ZW5_eEKEC28) 176 | 177 | ## Company engineering blog links 178 | 179 | courtesy [checkcheckzz](https://github.com/checkcheckzz/system-design-interview#toc) 180 | 181 | Depending on where you are interviewing, go through the company blog. VERY USEFUL IN INTERVIEWS! It really helps if you have an idea of the architecture, as the questions asked will generally be of that domain and your prior knowledge will help out here. 182 | 183 | * [Airbnb Engineering](http://nerds.airbnb.com/) 184 | * [Amazon](https://developer.amazon.com/blogs) 185 | * [Amazon AWS](https://aws.amazon.com/blogs/) 186 | * [Bandcamp Tech](http://bandcamptech.wordpress.com/) 187 | * [BankSimple Simple Blog](https://www.simple.com/engineering/) 188 | * [Bitly Engineering Blog](http://word.bitly.com/) 189 | * [Cloudera Developer Blog](http://blog.cloudera.com/blog/) 190 | * [Dropbox Tech Blog](https://tech.dropbox.com/) 191 | * [Engineering at Quora](http://engineering.quora.com/) 192 | * [Etsy Code as Craft](http://codeascraft.com/) 193 | * [Facebook Engineering](https://www.facebook.com/Engineering) 194 | * [Flickr Code](http://code.flickr.net/) 195 | * [Foursquare Engineering Blog](http://engineering.foursquare.com/) 196 | * [Google Research Blog](http://googleresearch.blogspot.com/) 197 | * [Groupn Engineering Blog](https://engineering.groupon.com/) 198 | * [High Scalability](http://highscalability.com/) 199 | * [Instagram Engineering](http://instagram-engineering.tumblr.com/) 200 | * [LinkedIn Engineering](http://engineering.linkedin.com/blog) 201 | * [Oyster Tech Blog](http://tech.oyster.com/) 202 | * [Pinterest Engineering Blog](http://engineering.pinterest.com/) 203 | * [Songkick Technology Blog](http://devblog.songkick.com/) 204 | * [SoundCloud Backstage Blog](https://developers.soundcloud.com/blog/) 205 | * [Square The Corner](http://corner.squareup.com/) 206 | * [THE REDDIT BLOG](http://www.redditblog.com/) 207 | * [The GitHub Blog](https://github.com/blog/category/engineering) 208 | * [The Netflix Tech Blog](http://techblog.netflix.com/) 209 | * [Twilio Engineering Blog](http://www.twilio.com/engineering) 210 | * [Twitter Engineering](https://engineering.twitter.com/) 211 | * [Uber Engineering](https://eng.uber.com/) 212 | * [Walmart Labs Tech Blog](https://medium.com/walmartlabs) 213 | * [WebEngage Engineering Blog](http://engineering.webengage.com/) 214 | * [Yammer Engineering](http://eng.yammer.com/blog/) 215 | * [Yelp Engineering Blog](http://engineeringblog.yelp.com/) 216 | * [Smarkets Blog](https://smarketshq.com/) 217 | 218 | ## Low on time ? 219 | 220 | **I would HIGHLY recommend you do not take a shortcut unless you have a week or so for an interview. System design is best learnt by practising, shortcuts might help you in the short term, but would recommend coming back to this link for an in-depth understanding after the interview** 221 | 222 | * a) Go through cs76 and Udacity's links given above for scaling systems. 223 | * b) Go through the engineering blog of the company you are interviewing in (or if its a startup go through the link of the company closest to yours) 224 | * c) See this talk: http://www.hiredintech.com/system-design/the-system-design-process/ and develop a process for how to answer such questions. 225 | * d) Remember these terms, just roll over them in your interview in your mind, and if relevant mention it in the interview 226 | 1. processing and servers 227 | 2. storage 228 | 3. caching 229 | 4. concurrency and communication 230 | 5. security 231 | 6. load balancing and proxy 232 | 7. CDN 233 | 8. Monetization 234 | 235 | Best of luck :+1:, feel free to send pull requests to add more content to this git! 236 | -------------------------------------------------------------------------------- /SUMMARY.md: -------------------------------------------------------------------------------- 1 | * [Contents](README.md) 2 | 3 | * Basics 4 | - [Key Characterics](basics/key-characteristics.md) 5 | - [Loading balancing](basics/load-balancing.md) 6 | - [Caching](basics/caching.md) 7 | - [Sharding](basics/sharding.md) 8 | - [Indexes](basics/indexes.md) 9 | - [Proxies](basics/proxies.md) 10 | - [Queues](basics/queues.md) 11 | - [Redundancy](basics/redundancy.md) 12 | - [SQL vs. NoSQL](basics/sql-vs-nosql.md) 13 | - [CAP Theorem](basics/cap-theorem.md) 14 | - [Consistent Hashing](basics/consistent-hashing.md) 15 | - [Client Server Communication](basics/client-server-communication.md) 16 | 17 | * Designs 18 | - [Short URL Service](designs/short-url.md) 19 | - [Pastebin](designs/pastebin.md) 20 | - [Instagram](designs/instagram.md) 21 | - [Dropbox](designs/dropbox.md) 22 | - [Twitter](designs/twitter.md) 23 | - [Youtube](designs/youtube.md) 24 | - [Twitter Search](designs/twitter-search.md) 25 | - [Web Crawler](designs/web-crawler.md) 26 | - [Facebook Newsfeed](designs/facebook-newsfeed.md) 27 | - [Yelp](designs/yelp.md) 28 | - [Uber Backend](designs/uber-backend.md) 29 | - [Ticketmaster](designs/ticketmaster.md) 30 | -------------------------------------------------------------------------------- /basics/caching.md: -------------------------------------------------------------------------------- 1 | Caching 2 | ==== 3 | 4 | - Take advantage of the locality of reference principle: recently requested data is likely to be requested again. 5 | - Exist at all levels in architecture, but often found at the level nearest to the front end. 6 | 7 | ## Application server cache 8 | - Cache placed on a request layer node. 9 | - When a request layer node is expanded to many nodes 10 | - Load balancer randomly distributes requests across the nodes. 11 | - The same request can go to different nodes. 12 | - Increase cache misses. 13 | - Solutions: 14 | - Global caches 15 | - Distributed caches 16 | 17 | ## Distributed cache 18 | - Each request layer node owns part of the cached data. 19 | - Entire cache is divided up using a consistent hashing function. 20 | - Pro 21 | - Cache space can be increased easily by adding more nodes to the request pool. 22 | - Con 23 | - A missing node leads to cache lost. 24 | 25 | ## Global cache 26 | - A server or file store that is faster than original store, and accessible by all request layer nodes. 27 | - Two common forms 28 | - Cache server handles cache miss. 29 | - Used by most applications. 30 | - Request nodes handle cache miss. 31 | - Have a large percentage of the hot data set in the cache. 32 | - An architecture where the files stored in the cache are static and shouldn’t be evicted. 33 | - The application logic understands the eviction strategy or hot spots better than the cache 34 | 35 | ## Content distributed network (CDN) 36 | - For sites serving large amounts of static media. 37 | - Process 38 | - A request first asks the CDN for a piece of static media. 39 | - CDN serves that content if it has it locally available. 40 | - If content isn’t available, CDN will query back-end servers for the file, cache it locally and serve it to the requesting user. 41 | - If the system is not large enough for CDN, it can be built like this: 42 | - Serving static media off a separate subdomain using lightweight HTTP server (e.g. Nginx). 43 | - Cutover the DNS from this subdomain to a CDN later. 44 | 45 | ## Cache invalidation 46 | - Keep cache coherent with the source of truth. Invalidate cache when source of truth has changed. 47 | - Write-through cache 48 | - Data is written into the cache and permanent storage at the same time. 49 | - Pro 50 | - Fast retrieval, complete data consistency, robust to system disruptions. 51 | - Con 52 | - Higher latency for write operations. 53 | - Write-around cache 54 | - Data is written to permanent storage, not cache. 55 | - Pro 56 | - Reduce the cache that is no used. 57 | - Con 58 | - Query for recently written data creates a cache miss and higher latency. 59 | - Write-back cache 60 | - Data is only written to cache. 61 | - Write to the permanent storage is done later on. 62 | - Pro 63 | - Low latency, high throughput for write-intensive applications. 64 | - Con 65 | - Risk of data loss in case of system disruptions. 66 | 67 | ## Cache eviction policies 68 | - FIFO: first in first out 69 | - LIFO: last in first out 70 | - LRU: least recently used 71 | - MRU: most recently used 72 | - LFU: least frequently used 73 | - RR: random replacement 74 | -------------------------------------------------------------------------------- /basics/cap-theorem.md: -------------------------------------------------------------------------------- 1 | [CAP Theorem](https://en.wikipedia.org/wiki/CAP_theorem) 2 | ==== 3 | 4 | - Consistency: every read receives the most recent write or an error. 5 | - Availability: every request receives a response that is not an error. 6 | - Partition tolerance: the system continues to operate despite an arbitrary number of messages being dropped (or delayed) by the network between nodes 7 | - CAP theorem implies that in the presence of a network partition, one has to choose between consistency and availability 8 | - CAP is frequently misunderstood as if one has to choose to abandon one of the three guarantees at all times. In fact, the choice is really between consistency and availability only when a network partition or failure happens; at all other times, no trade-off has to be made. 9 | - [ACID](https://en.wikipedia.org/wiki/ACID) databases choose consistency over availability. 10 | - [BASE](https://en.wikipedia.org/wiki/Eventual_consistency) systems choose availability over consistency. 11 | -------------------------------------------------------------------------------- /basics/client-server-communication.md: -------------------------------------------------------------------------------- 1 | Client-Server Communication 2 | ==== 3 | 4 | ## Standard HTTP Web Request 5 | 1. Client opens a connection and requests data from server. 6 | 2. Server calculates the response. 7 | 3. Server sends the response back to the client on the opened request. 8 | 9 | ## Ajax Polling 10 | The client repeatedly polls (or requests) a server for data, and waits for the server to respond with data. If no data is available, an empty response is returned. 11 | 12 | 1. Client opens a connection and requests data from the server using regular HTTP. 13 | 2. The requested webpage sends requests to the server at regular intervals (e.g., 0.5 seconds). 14 | 3. The server calculates the response and sends it back, like regular HTTP traffic. 15 | 4. Client repeats the above three steps periodically to get updates from the server. 16 | 17 | Problems 18 | - Client has to keep asking the server for any new data. 19 | - A lot of responses are empty, creating HTTP overhead. 20 | 21 | ## HTTP Long-Polling 22 | The client requests information from the server exactly as in normal polling, but with the expectation that the server may not respond immediately. 23 | 24 | 1. The client makes an initial request using regular HTTP and then waits for a response. 25 | 2. The server delays its response until an update is available, or until a timeout has occurred. 26 | 3. When an update is available, the server sends a full response to the client. 27 | 4. The client typically sends a new long-poll request, either immediately upon receiving a response or after a pause to allow an acceptable latency period. 28 | 29 | Each Long-Poll request has a timeout. The client has to reconnect periodically after the connection is closed, due to timeouts. 30 | 31 | ## WebSockets 32 | - A persistent full duplex communication channels over a single TCP connection. Both server and client can send data at any time. 33 | - A connection is established through WebSocket handshake. 34 | - Low communication overhead. 35 | - Real-time data transfer. 36 | 37 | ## Server-Sent Event (SSE) 38 | 1. Client requests data from a server using regular HTTP. 39 | 2. The requested webpage opens a connection to the server. 40 | 3. Server sends the data to the client whenever there’s new information available. 41 | 42 | - Use case: 43 | - When real-time traffic from server to client is needed. 44 | - When server generates data in a loop and sends multiple events to client. 45 | -------------------------------------------------------------------------------- /basics/consistent-hashing.md: -------------------------------------------------------------------------------- 1 | Consistent Hashing 2 | ==== 3 | 4 | ## Simple hashing 5 | Problems of simple hashing function `key % n` (`n` is the number of servers): 6 | - It is not horizontally scalable. Whenever a new cache host is added to the system, all existing mappings are broken. 7 | - It may not be load balanced, especially for non-uniformly distributed data. Some servers will become hot spots. 8 | 9 | ## Consistent Hashing 10 | - Consistent hashing maps a key to an integer. 11 | - Imagine that the integers in the range are placed on a ring such that the values are wrapped around. 12 | - Given a list of servers, hash them to integers in the range. 13 | - To map a key to a server: 14 | - Hash it to a single integer. 15 | - Move clockwise on the ring until finding the first cache it encounters. 16 | - When the hash table is resized (a server is added or deleted), only `k/n` keys need to be remapped (`k` is the total number of keys, and `n` is the total number of servers). 17 | - To handle hot spots, add “virtual replicas” for caches. 18 | - Instead of mapping each cache to a single point on the ring, map it to multiple points on the ring (replicas). This way, each cache is associated with multiple portions of the ring. 19 | - If the hash function is “mixes well,” as the number of replicas increases, the keys will be more balanced. 20 | -------------------------------------------------------------------------------- /basics/indexes.md: -------------------------------------------------------------------------------- 1 | Indexes 2 | ==== 3 | 4 | - Improve the performance of search queries. 5 | - Decrease the write performance. This performance degradation applies to all insert, update, and delete operations. 6 | -------------------------------------------------------------------------------- /basics/key-characteristics.md: -------------------------------------------------------------------------------- 1 | # Key Characteristics of Distributed Systems 2 | 3 | ## Scalability 4 | - The capability of a system to grow and manage increased demand. 5 | - A system that can continuously evolve to support growing amount of work is scalable. 6 | - Horizontal scaling: by adding more servers into the pool of resources. 7 | - Vertical scaling: by adding more resource (CPU, RAM, storage, etc) to an existing server. This approach comes with downtime and an upper limit. 8 | 9 | ## Reliability 10 | - Reliability is the probability that a system will fail in a given period. 11 | - A distributed system is reliable if it keeps delivering its service even when one or multiple components fail. 12 | - Reliability is achieved through redundancy of components and data (remove every single point of failure). 13 | 14 | ## Availability 15 | - Availability is the time a system remains operational to perform its required function in a specific period. 16 | - Measured by the percentage of time that a system remains operational under normal conditions. 17 | - A reliable system is available. 18 | - An available system is not necessarily reliable. 19 | - A system with a security hole is available when there is no security attack. 20 | 21 | ## Efficiency 22 | - Latency: response time, the delay to obtain the first piece of data. 23 | - Bandwidth: throughput, amount of data delivered in a given time. 24 | 25 | ## Serviceability / Manageability 26 | - Easiness to operate and maintain the system. 27 | - Simplicity and spend with which a system can be repaired or maintained. 28 | -------------------------------------------------------------------------------- /basics/load-balancing.md: -------------------------------------------------------------------------------- 1 | Load Balancing (LB) 2 | ==== 3 | 4 | Help scale horizontally across an ever-increasing number of servers. 5 | 6 | ## LB locations 7 | - Between user and web server 8 | - Between web servers and an internal platform layer (application servers, cache servers) 9 | - Between internal platform layer and database 10 | 11 | ## Algorithms 12 | - Least connection 13 | - Least response time 14 | - Least bandwidth 15 | - Round robin 16 | - Weighted round robin 17 | - IP hash 18 | 19 | ## Implementation 20 | - Smart clients 21 | - Hardware load balancers 22 | - Software load balancers 23 | -------------------------------------------------------------------------------- /basics/proxies.md: -------------------------------------------------------------------------------- 1 | Proxies 2 | ==== 3 | 4 | - A proxy server is an intermediary piece of hardware / software sitting between client and backend server. 5 | - Filter requests 6 | - Log requests 7 | - Transform requests (encryption, compression, etc) 8 | - [Cache](caching.md) 9 | - Batch requests 10 | - Collapsed forwarding: enable multiple client requests for the same URI to be processed as one request to the backend server 11 | - Collapse requests for data that is spatially close together in the storage to minimize the reads 12 | -------------------------------------------------------------------------------- /basics/queues.md: -------------------------------------------------------------------------------- 1 | Queues 2 | ==== 3 | 4 | - Queues are used to effectively manage requests in a large-scale distributed system, in which different components of the system may need to work in an asynchronous way. 5 | - It is an abstraction between the client’s request and the actual work performed to service it. 6 | - Queues are implemente on the asynchronious communication protocol. When a client submits a task to a queue they are no longer required to wait for the results 7 | - Queue can provide protection from service outages and failures. 8 | -------------------------------------------------------------------------------- /basics/redundancy.md: -------------------------------------------------------------------------------- 1 | Redundancy 2 | ==== 3 | 4 | - Redundancy: **duplication of critical data or services** with the intention of increased reliability of the system. 5 | - Server failover 6 | - Remove single points of failure and provide backups (e.g. server failover). 7 | - Shared-nothing architecture 8 | - Each node can operate independently of one another. 9 | - No central service managing state or orchestrating activities. 10 | - New servers can be added without special conditions or knowledge. 11 | - No single point of failure. 12 | -------------------------------------------------------------------------------- /basics/sharding.md: -------------------------------------------------------------------------------- 1 | Sharding / Data Partitioning 2 | ==== 3 | 4 | ## Partitioning methods 5 | - Horizontal partitioning 6 | - Range based sharding. 7 | - Put different rows into different tables. 8 | - Con 9 | - If the value whose range is used for sharding isn’t chosen carefully, the partitioning scheme will lead to unbalanced servers. 10 | - Vertical partitioning 11 | - Divide data for a specific feature to their own server. 12 | - Pro 13 | - Straightforward to implement. 14 | - Low impact on the application. 15 | - Con 16 | - To support growth of the application, a database may need further partitioning. 17 | - Directory-based partitioning 18 | - A lookup service that knows the partitioning scheme and abstracts it away from the database access code. 19 | - Allow addition of db servers or change of partitioning schema without impacting application. 20 | - Con 21 | - Can be a single point of failure. 22 | 23 | ## Partitioning criteria 24 | - Key or hash-based partitioning 25 | - Apply a hash function to some key attribute of the entry to get the partition number. 26 | - Problem 27 | - Adding new servers may require changing the hash function, which would need redistribution of data and downtime for the service. 28 | - Workaround: [consistent hashing](https://en.wikipedia.org/wiki/Consistent_hashing). 29 | - List partitioning 30 | - Each partition is assigned a list of values. 31 | - Round-robin partitioning 32 | - With `n` partitions, the `i` tuple is assigned to partition `i % n`. 33 | - Composite partitioning 34 | - Combine any of above partitioning schemes to devise a new scheme. 35 | - Consistent hashing is a composite of hash and list partitioning. 36 | - Key -> reduced key space through hash -> list -> partition. 37 | 38 | ## Common problems of sharding 39 | Most of the constraints are due to the fact that operations across multiple tables or multiple rows in the same table will no longer run on the same server. 40 | 41 | - Joins and denormalization 42 | - Joins will not be performance efficient since data has to be compiled from multiple servers. 43 | - Workaround: denormalize the database so that queries can be performed from a single table. But this can lead to data inconsistency. 44 | - Referential integrity 45 | - Difficult to enforce data integrity constraints (e.g. foreign keys). 46 | - Workaround 47 | - Referential integrity is enforced by application code. 48 | - Applications can run SQL jobs to clean up dangling references. 49 | - Rebalancing 50 | - Necessity of rebalancing 51 | - Data distribution is not uniform. 52 | - A lot of load on one shard. 53 | - Create more db shards or rebalance existing shards changes partitioning scheme and requires data movement. 54 | -------------------------------------------------------------------------------- /basics/sql-vs-nosql.md: -------------------------------------------------------------------------------- 1 | SQL vs. NoSQL 2 | ==== 3 | 4 | ## Common types of NoSQL 5 | ### Key-value stores 6 | - Array of key-value pairs. The "key" is an attribute name. 7 | - Redis, Vodemort, Dynamo. 8 | 9 | ### Document databases 10 | - Data is stored in documents. 11 | - Documents are grouped in collections. 12 | - Each document can have an entirely different structure. 13 | - CouchDB, MongoDB. 14 | 15 | ### Wide-column / columnar databases 16 | - Column families - containers for rows. 17 | - No need to know all the columns up front. 18 | - Each row can have different number of columns. 19 | - Cassandra, HBase. 20 | 21 | ### Graph database 22 | - Data is stored in graph structures 23 | - Nodes: entities 24 | - Properties: information about the entities 25 | - Lines: connections between the entities 26 | - Neo4J, InfiniteGraph 27 | 28 | ## Differences between SQL and NoSQL 29 | ### Storage 30 | - SQL: store data in tables. 31 | - NoSQL: have different data storage models. 32 | 33 | ### Schema 34 | - SQL 35 | - Each record conforms to a fixed schema. 36 | - Schema can be altered, but it requires modifying the whole database. 37 | - NoSQL: 38 | - Schemas are dynamic. 39 | 40 | ### Querying 41 | - SQL 42 | - Use SQL (structured query language) for defining and manipulating the data. 43 | - NoSQL 44 | - Queries are focused on a collection of documents. 45 | - UnQL (unstructured query language). 46 | - Different databases have different syntax. 47 | 48 | ### Scalability 49 | - SQL 50 | - Vertically scalable (by increasing the horsepower: memory, CPU, etc) and expensive. 51 | - Horizontally scalable (across multiple servers); but it can be challenging and time-consuming. 52 | - NoSQL 53 | - Horizontablly scalable (by adding more servers) and cheap. 54 | 55 | ### ACID 56 | - Atomicity, consistency, isolation, durability 57 | - SQL 58 | - ACID compliant 59 | - Data reliability 60 | - Gurantee of transactions 61 | - NoSQL 62 | - Most sacrifice ACID compliance for performance and scalability. 63 | 64 | ## Which one to use? 65 | ### SQL 66 | - Ensure ACID compliance. 67 | - Reduce anomalies. 68 | - Protect database integrity. 69 | - Data is structured and unchanging. 70 | 71 | ### NoSQL 72 | - Data has little or no structure. 73 | - Make the most of cloud computing and storage. 74 | - Cloud-based storage requires data to be easily spread across multiple servers to scale up. 75 | - Rapid development. 76 | - Frequent updates to the data structure. 77 | -------------------------------------------------------------------------------- /bin/publish-gh-pages.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # install the plugins and build the static site 4 | gitbook install && gitbook build 5 | 6 | # checkout to the gh-pages branch 7 | git checkout gh-pages || git checkout -b gh-pages 8 | 9 | # pull the latest updates 10 | git pull origin gh-pages --rebase 11 | 12 | # copy the static site files into the current directory. 13 | cp -R _book/* . 14 | 15 | # remove 'node_modules' and '_book' directory 16 | git clean -fx node_modules 17 | git clean -fx _book 18 | 19 | # add all files 20 | git add . 21 | 22 | # commit 23 | git commit -a -m "Update docs" 24 | 25 | # push to the origin 26 | git push -u origin gh-pages 27 | 28 | # checkout to the master branch 29 | git checkout master 30 | -------------------------------------------------------------------------------- /book.json: -------------------------------------------------------------------------------- 1 | { 2 | "plugins": [ 3 | "-sharing", 4 | "github", 5 | "anchors", 6 | "splitter" 7 | ], 8 | 9 | "pluginsConfig": { 10 | "github": { 11 | "url": "https://github.com/tuliren/grokking-system-design" 12 | } 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /designs/dropbox.md: -------------------------------------------------------------------------------- 1 | # Dropbox 2 | 3 | ## Summary 4 | ![overview](../img/dropbox-overview.png) 5 | ![summary](../img/dropbox-detail.png) 6 | -------------------------------------------------------------------------------- /designs/facebook-messenger.md: -------------------------------------------------------------------------------- 1 | # Facebook Messenger 2 | 3 | ## Summary 4 | ![overview](../img/facebook-messenger-overview.png) 5 | ![summary](../img/facebook-messenger-detail.png) 6 | -------------------------------------------------------------------------------- /designs/facebook-newsfeed.md: -------------------------------------------------------------------------------- 1 | # Facebook Newsfeed 2 | 3 | ## Summary 4 | ![overview](../img/facebook-newsfeed-overview.png) 5 | -------------------------------------------------------------------------------- /designs/instagram.md: -------------------------------------------------------------------------------- 1 | # Instagram 2 | 3 | ## Summary 4 | ![overview](../img/instagram-overview.png) 5 | ![summary](../img/instagram-detail.png) 6 | -------------------------------------------------------------------------------- /designs/pastebin.md: -------------------------------------------------------------------------------- 1 | # Pastebin 2 | 3 | ## Summary 4 | ![overview](../img/pastebin-overview.png) 5 | ![summary](../img/pastebin-detail.png) -------------------------------------------------------------------------------- /designs/short-url.md: -------------------------------------------------------------------------------- 1 | # URL Shortening Service 2 | 3 | ## Summary 4 | ![overview](../img/short-url-overview.png) 5 | ![summary](../img/short-url-detail.png) 6 | 7 | ## Requirements 8 | - Functional Requirements 9 | - Given a URL, generate a shorter and unique alias (short link). 10 | - When users access a short link, redirect to the original link. 11 | - Users should optionally be able to pick a custom short link for their URL. 12 | - Links will expire after a standard default timespan. Users should also be able to specify the expiration time. 13 | 14 | - Non-Functional Requirements 15 | - The system should be highly available. This is required because, if our service is down, all the URL redirections will start failing. 16 | - URL redirection should happen in real-time with minimal latency. 17 | - Shortened links should not be guessable (not predictable). 18 | 19 | - Extended Requirements 20 | - Analytics; e.g., how many times a redirection happened? 21 | - Be accessible through REST APIs by other services. 22 | 23 | ## Capacity Estimation and Constraints 24 | - Assumption 25 | - Read-heavy. More redirection requests compared to new URL shortenings. 26 | - Assume **100:1** ratio between read and write. 27 | 28 | - Traffic estimates 29 | - **500M** new URL shortenings per month, 100 * 500M => 50B redirections per month. 30 | - New URL shortenings per second 31 | - 500 million / (30 days * 24 hours * 3600 seconds) = **~200 URLs/s** 32 | - URLs redirections per second 33 | - 50 billion / (30 days * 24 hours * 3600 sec) = **~19K/s** 34 | 35 | - Storage estimates 36 | - Assume storing every URL shortening request for 5 years, each object takes **500 bytes** 37 | - Total objects: 500 million * 5 years * 12 months = **30 billion** 38 | - Total storage: 30 billion * 500 bytes = **15 TB** 39 | 40 | - Bandwidth estimates 41 | - Write: 200 URL/s * 500 bytes/URL = **100 KB/s** 42 | - Read: 19K URL/s * 500 bytes/URL = **~9 MB/s** 43 | 44 | - Cache memory estimates 45 | - Follow the 80-20 rule, assuming 20% of URLs generate 80% of traffic, cache 20% hot URLs 46 | - Requests per day: 19K * 3600 seconds * 24 hours = **~1.7 billion/day** 47 | - Cache 20%: 0.2 * 1.7 billion * 500 bytes = **~170GB** 48 | 49 | - Summary 50 | - Assuming 500 million new URLs per month and 100:1 read:write ratio 51 | 52 | Category | Calculation | Estimate 53 | ---- | ---- | ---- 54 | New URLs | 500 million / (30 days * 24 hours * 3600 seconds) | 200 /s 55 | URL redirections | 500 million * 100 / (30 days * 24 hours * 3600 seconds) | 19 K/s 56 | Incoming data | 500 bytes/URL * 200 URL/s | 100 KB/s 57 | Outgoing data | 500 bytes/URL * 19K URL/s | 9 MB/s 58 | Storage for 5 years | 500 bytes/URL * 500 million * 60 months | 15 TB 59 | Memory for cache | 19K URL * 3600 seconds * 24 hours * 500 bytes * 20% | 170 GB 60 | 61 | ## System APIs 62 | ### `createUrl` 63 | - Parameters 64 | Name | Type | Note 65 | ---- | ---- | ---- 66 | `api_dev_key` | `string` | The API developer key of a registered account. This will be used to, among other things, throttle users based on their allocated quota. 67 | `original_url` | `string` | Original URL to be shortened. 68 | `custom_alias` | `string` | Optional custom key for the URL. 69 | `user_name` | `string` | Optional user name to be used in encoding. 70 | `expire_date` | `string` | Optional expiration date for the shortened URL. 71 | - Return 72 | - `string` 73 | - A successful insertion returns the shortened URL; otherwise, it returns an error code. 74 | 75 | ### `deleteUrl` 76 | - Parameters 77 | Name | Type | Note 78 | ---- | ---- | ---- 79 | `api_dev_key` | `string` | The API developer key of a registered account. This will be used to, among other things, throttle users based on their allocated quota. 80 | `url_key` | `string` | Short URL. 81 | - Return 82 | - `string` 83 | - A successful deletion returns ‘URL Removed’. 84 | 85 | ## Database design 86 | - Observations 87 | - Need to store billions of records. 88 | - Each object is small (less than 1K). 89 | - No relationships between records—other than storing which user created a URL. 90 | - Read-heavy. 91 | - A [NoSQL](../basics/sql-vs-nosql.md) choice would also be easier to scale. 92 | - Comment: SQL with sharding should also work 93 | 94 | - Schema 95 | - URL 96 | Column | Type 97 | ---- | ---- 98 | `hash` | varchar(16) 99 | `original_url` | varchar(512) 100 | `creation_date` | datetime 101 | `expiration_date` | datetime 102 | `user_id` | int 103 | - User 104 | Column | Type 105 | ---- | ---- 106 | `name` | varchar(20) 107 | `email` | varchar(32) 108 | `creation_date` | datetime 109 | `last_login` | datetime 110 | 111 | ## Basic System Design and Algorithm 112 | ### Encoding actual URL 113 | - Compute unique hash 114 | - `base64`: A-Z, a-z, 0-9, `-`, `.` 115 | - 6 letters: 64 ^ 6 = ~68.7 billion 116 | - 8 letters: 64 ^ 8 = ~281 trillion 117 | - Use 6 letters 118 | - `MD5` generates 128 bit hash value 119 | - Each `base64` character encodes 6 bits 120 | - `base64` encoding generates 22 characters 121 | - Select 8 characters 122 | - Issues with this approach 123 | - Same URL from multiple users 124 | - URL-encoded 125 | - Workaround 126 | - Append an increasing sequence number to each input URL, and generate a hash for it 127 | - Append user id to input URL 128 | 129 | ### Generating keys offline 130 | - Standalone Key Generation Service (KGS) 131 | - Generate random 6 letter strings and store them in a database (key DB) 132 | - When a short URL is needed, take one from the key DB 133 | 134 | - Key DB size 135 | - 6 characters/key * 68.7B unique keys = 412 GB 136 | 137 | - Concurrency issue 138 | - If there are multiple servers reading keys concurrently, two or more servers try to read the same key from the database. 139 | 140 | - Workaround 141 | - Servers can use KGS to read/mark keys in the database. 142 | - KGS can use two tables to store keys: one for keys that are not used yet, and one for all the used keys. 143 | - KGS can always keep some keys in memory so that it can quickly provide them whenever a server needs them. 144 | - KGS needs to make sure not to give the same key to multiple servers. 145 | - Comment: keys are sharded. Each KGS server only serves one application server. 146 | 147 | - Key lookup 148 | - When a key is found, issue an "HTTP 302 Redirect" status and passing the stored URL. 149 | - When a key is not found, issue an "HTTP 404 Not Found", or redirect to homepage. 150 | 151 | ### UUID 152 | Replace KGS with UUID. 153 | 154 | ## Data Partitioning and Replication 155 | - Range Based Partitioning 156 | - Store URLs in separate partitions based on the first letter of the URL or the hash key. 157 | - Combine certain less frequently occurring letters into one database partition. 158 | - Problem with this approach 159 | - Unbalanced servers. 160 | 161 | - Hash-Based Partitioning 162 | - Take a hash of the short URL we are storing, and calculate which partition to use based upon the hash. 163 | - Use [consistent hashing](../basics/consistent-hashing.md) 164 | 165 | ## Cache 166 | - Eviction policy 167 | - LRU: discard the least recently used URL first 168 | - Cache update 169 | - Cache miss: hit backend database and pass new entry to all cache replicas 170 | 171 | ## Load Balancer (LB) 172 | - LB locations 173 | - Between Clients and Application servers 174 | - Between Application Servers and database servers 175 | - Between Application Servers and Cache servers 176 | 177 | ## DB Sweeping 178 | A separate Cleanup service can run periodically to remove expired links from our storage and cache. 179 | 180 | ## Telemetry 181 | Statistics about the system: how many times a short URL has been used 182 | 183 | ## Security and Permissions 184 | - Store permission level (public/private) with each URL in the database 185 | - Send an error (HTTP 401) for unauthorized access 186 | -------------------------------------------------------------------------------- /designs/ticketmaster.md: -------------------------------------------------------------------------------- 1 | # Ticketmaster 2 | 3 | ## Summary 4 | ![overview](../img/ticketmaster-overview.png) 5 | -------------------------------------------------------------------------------- /designs/twitter-search.md: -------------------------------------------------------------------------------- 1 | # Twitter Search 2 | 3 | ## Summary 4 | ![overview](../img/twitter-search-overview.png) 5 | ![detail](../img/twitter-search-detail.png) 6 | -------------------------------------------------------------------------------- /designs/twitter.md: -------------------------------------------------------------------------------- 1 | # Twitter 2 | 3 | ## Summary 4 | ![overview](../img/twitter-overview.png) 5 | ![summary](../img/twitter-detail.png) 6 | -------------------------------------------------------------------------------- /designs/uber-backend.md: -------------------------------------------------------------------------------- 1 | # Uber Backend 2 | 3 | ## Summary 4 | ![overview](../img/uber-backend-overview.png) 5 | -------------------------------------------------------------------------------- /designs/web-crawler.md: -------------------------------------------------------------------------------- 1 | # Web Crawler 2 | 3 | ## Summary 4 | ![overview](../img/web-crawler-overview.png) 5 | ![detail](../img/web-crawler-detail.png) 6 | -------------------------------------------------------------------------------- /designs/yelp.md: -------------------------------------------------------------------------------- 1 | # Yelp 2 | 3 | ## Summary 4 | ![overview](../img/yelp-overview.png) 5 | -------------------------------------------------------------------------------- /designs/youtube.md: -------------------------------------------------------------------------------- 1 | # Youtube 2 | 3 | ## Summary 4 | ![overview](../img/youtube-overview.png) 5 | ![detail](../img/youtube-detail.png) 6 | -------------------------------------------------------------------------------- /img/dropbox-detail.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Jeevan-kumar-Raj/Grokking-System-Design/dcafc27dc6ea68b563bf14ac920b8471ba863cde/img/dropbox-detail.png -------------------------------------------------------------------------------- /img/dropbox-overview.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Jeevan-kumar-Raj/Grokking-System-Design/dcafc27dc6ea68b563bf14ac920b8471ba863cde/img/dropbox-overview.png -------------------------------------------------------------------------------- /img/facebook-messenger-detail.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Jeevan-kumar-Raj/Grokking-System-Design/dcafc27dc6ea68b563bf14ac920b8471ba863cde/img/facebook-messenger-detail.png -------------------------------------------------------------------------------- /img/facebook-messenger-overview.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Jeevan-kumar-Raj/Grokking-System-Design/dcafc27dc6ea68b563bf14ac920b8471ba863cde/img/facebook-messenger-overview.png -------------------------------------------------------------------------------- /img/facebook-newsfeed-overview.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Jeevan-kumar-Raj/Grokking-System-Design/dcafc27dc6ea68b563bf14ac920b8471ba863cde/img/facebook-newsfeed-overview.png -------------------------------------------------------------------------------- /img/instagram-detail.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Jeevan-kumar-Raj/Grokking-System-Design/dcafc27dc6ea68b563bf14ac920b8471ba863cde/img/instagram-detail.png -------------------------------------------------------------------------------- /img/instagram-overview.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Jeevan-kumar-Raj/Grokking-System-Design/dcafc27dc6ea68b563bf14ac920b8471ba863cde/img/instagram-overview.png -------------------------------------------------------------------------------- /img/pastebin-detail.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Jeevan-kumar-Raj/Grokking-System-Design/dcafc27dc6ea68b563bf14ac920b8471ba863cde/img/pastebin-detail.png -------------------------------------------------------------------------------- /img/pastebin-overview.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Jeevan-kumar-Raj/Grokking-System-Design/dcafc27dc6ea68b563bf14ac920b8471ba863cde/img/pastebin-overview.png -------------------------------------------------------------------------------- /img/short-url-detail.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Jeevan-kumar-Raj/Grokking-System-Design/dcafc27dc6ea68b563bf14ac920b8471ba863cde/img/short-url-detail.png -------------------------------------------------------------------------------- /img/short-url-overview.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Jeevan-kumar-Raj/Grokking-System-Design/dcafc27dc6ea68b563bf14ac920b8471ba863cde/img/short-url-overview.png -------------------------------------------------------------------------------- /img/ticketmaster-overview.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Jeevan-kumar-Raj/Grokking-System-Design/dcafc27dc6ea68b563bf14ac920b8471ba863cde/img/ticketmaster-overview.png -------------------------------------------------------------------------------- /img/twitter-detail.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Jeevan-kumar-Raj/Grokking-System-Design/dcafc27dc6ea68b563bf14ac920b8471ba863cde/img/twitter-detail.png -------------------------------------------------------------------------------- /img/twitter-overview.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Jeevan-kumar-Raj/Grokking-System-Design/dcafc27dc6ea68b563bf14ac920b8471ba863cde/img/twitter-overview.png -------------------------------------------------------------------------------- /img/twitter-search-detail.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Jeevan-kumar-Raj/Grokking-System-Design/dcafc27dc6ea68b563bf14ac920b8471ba863cde/img/twitter-search-detail.png -------------------------------------------------------------------------------- /img/twitter-search-overview.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Jeevan-kumar-Raj/Grokking-System-Design/dcafc27dc6ea68b563bf14ac920b8471ba863cde/img/twitter-search-overview.png -------------------------------------------------------------------------------- /img/uber-backend-overview.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Jeevan-kumar-Raj/Grokking-System-Design/dcafc27dc6ea68b563bf14ac920b8471ba863cde/img/uber-backend-overview.png -------------------------------------------------------------------------------- /img/web-crawler-detail.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Jeevan-kumar-Raj/Grokking-System-Design/dcafc27dc6ea68b563bf14ac920b8471ba863cde/img/web-crawler-detail.png -------------------------------------------------------------------------------- /img/web-crawler-overview.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Jeevan-kumar-Raj/Grokking-System-Design/dcafc27dc6ea68b563bf14ac920b8471ba863cde/img/web-crawler-overview.png -------------------------------------------------------------------------------- /img/yelp-overview.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Jeevan-kumar-Raj/Grokking-System-Design/dcafc27dc6ea68b563bf14ac920b8471ba863cde/img/yelp-overview.png -------------------------------------------------------------------------------- /img/youtube-detail.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Jeevan-kumar-Raj/Grokking-System-Design/dcafc27dc6ea68b563bf14ac920b8471ba863cde/img/youtube-detail.png -------------------------------------------------------------------------------- /img/youtube-overview.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Jeevan-kumar-Raj/Grokking-System-Design/dcafc27dc6ea68b563bf14ac920b8471ba863cde/img/youtube-overview.png --------------------------------------------------------------------------------