System Design Interview Question: Design URL Shortener

Поделиться
HTML-код
  • Опубликовано: 21 ноя 2024

Комментарии • 51

  • @vijayansivaraman7923
    @vijayansivaraman7923 8 месяцев назад +5

    @Hayk Simonyan, In my 13 years of experience in .Net as as full stack developer, So far i didn't see any other channel explaining this much crisp and clear with animation images to understand better. Kudos to your effort and hardwork. Very much Appreciated. I wish you will get more subscribers and will reach great heights. Can you post videos on Angular, Javascript and .Net 7, EFCore , Azure full tutorials with real time examples ?

    • @hayk.simonyan
      @hayk.simonyan  8 месяцев назад

      @vijayansivaraman7923 It's great to hear that you're interested in these topics! Expect more tutorials like this here. I'm not an expert in .Net, but I'm planning to post about JS + frontend frameworks (Angular, React) and cloud providers (Azure, etc.) on this channel

  • @Coding101-nb5ej
    @Coding101-nb5ej 8 месяцев назад +12

    I feel like I should have paid for this level of insights. thank you so much for sharing

    • @hayk.simonyan
      @hayk.simonyan  8 месяцев назад

      Glad to hear that

    • @mattf4089
      @mattf4089 3 месяца назад

      @@hayk.simonyan Agreed. Thanks for the great content.

    • @hayk.simonyan
      @hayk.simonyan  3 месяца назад

      @@mattf4089 You're welcome!

  • @kzrfaisal
    @kzrfaisal 8 месяцев назад +4

    Wooahh....I have watched so many videos on this topic but always loose it in the middle of the video, but you made me stick to the end, the amount of simplicity you brought in system design explanation is commendable. We really need more of these 🙌. THANKS A LOT.

    • @hayk.simonyan
      @hayk.simonyan  8 месяцев назад

      Great to hear that!! Expect many more

  • @GermainHirwa
    @GermainHirwa 23 дня назад

    Let me take this time to thank you for this great video Sir. I have my first ever LinkedIn system design interview for my university internship and I feel a little bit confident after watching your video a couple times. Will watch it like four to five times and will be good for the interview.
    Thanks again for this lengthy free resource. I appreciate

    • @hayk.simonyan
      @hayk.simonyan  21 день назад

      You're welcome! If you're preparing for system design interview, I suggest you watch the other 3 videos as well from this playlist ruclips.net/p/PLdNCznBZ77NqqZMrLPRb1RLm7LObOCb21 to be prepared for different scenarios. And wish you good luck in your interviews 💪

  • @RandomGuy34-j1u
    @RandomGuy34-j1u 8 месяцев назад +2

    Damn this is some gold content !! Absolutely loved it !!

  • @jorgegallego9672
    @jorgegallego9672 Месяц назад

    Hayk you are my favorite system design channel. Congrats, no other channel taught me as much as you did!

  • @karvinus
    @karvinus 3 месяца назад

    I tell you why your videos are better than others. They are short and to the point. At the same time they are covering everything in depth without feeling being rushed. This helps me understand to pace at which i should deliver mine. mine will be slightly longer because of drawing and asnwering questions.

    • @hayk.simonyan
      @hayk.simonyan  3 месяца назад

      Thanks! I always aim to condense essential topics into brief videos, cutting out unnecessary details to save your time 👍

  • @janakiramankirthivasan5955
    @janakiramankirthivasan5955 4 месяца назад

    after watching five different videos , i finally understood the concept from yours . Thank you very much!

  • @JoseSanchez-vv1zd
    @JoseSanchez-vv1zd 2 месяца назад +1

    Excellent video. Thank you for creating it!

  • @sankalpswami3335
    @sankalpswami3335 Месяц назад

    simply the best!!!!

  • @vinoths7140
    @vinoths7140 4 месяца назад

    I am grateful for your help in making me understand.

    • @hayk.simonyan
      @hayk.simonyan  4 месяца назад

      You're welcome! Glad it was helpful.

  • @passionforsciencel5180
    @passionforsciencel5180 6 месяцев назад +2

    Hey, I stumbled upon a more efficient approach for the initial step of our URL shortener project. Instead of the traditional method involving database creation and random ID insertion to ensure unpredictability, I devised a single script. This script generates IDs synchronously without relying on a database. It's incredibly memory-efficient (no heap allocation) and adept at handling high traffic seamlessly. The secret? Just a simple mathematical concept. Intrigued? Let me know if you want to dive in! 😊

    • @hayk.simonyan
      @hayk.simonyan  6 месяцев назад

      hey, yes creating all keys upfront will be inefficient. A better approach is to start with a database auto-incrementing integer as your unique identifier for each shortened URL. Then encode this integer into a short alphanumeric string (using base62 or similar techniques) for a compact and user-friendly representation

    • @passionforsciencel5180
      @passionforsciencel5180 6 месяцев назад +1

      @hayk.simonyan,
      I have come across a mathematical concept known as the "Modular Inverse" that could revolutionize the approach to URL shortening, eliminating the need for a database entirely:
      First, select a large prime number, denoted as 'm', ideally the nearest prime number to the anticipated number of links to be generated over the lifetime of the application.
      Within your application, initialize a counter, 'i', starting from 2 (1 always return 1) and incrementing up to 'm'.
      For each incoming request, return the modular inverse of 'i' with respect to 'm'. This operation guarantees unpredictability and non-repetitiveness due to the prime nature of 'm'. Increment 'i' by one after each request.
      Here's an example with 'm' set to 37:
      i = 2, result = 19
      i = 3, result = 25
      i = 4, result = 28
      i = 5, result = 37
      i = 6, result = 31
      i = 7, result = 16
      i = 8, result = 14
      i = 9, result = 33
      i = 10, result = 26
      i = 11, result = 27
      i = 12, result = 34
      i = 13, result = 20
      i = 14, result = 8
      i = 15, result = 5
      i = 16, result = 7
      i = 17, result = 34
      i = 18, result = 35
      i = 19, result = 2
      i = 20, result = 13
      i also searched about the time complexity of Modular_Inverse function and i get O(log(min(i,m))) .
      So what you think ?
      Best regards, Bouzid Kobchi

    • @hayk.simonyan
      @hayk.simonyan  6 месяцев назад

      @@passionforsciencel5180 it's a clever approach 👍 For URL shortening systems where the anticipated scale is known, this inverse method might be a great fit. However, for large scale systems where flexibility, unpredictability, and advanced features such as custom short URLs are expected, a traditional database backed method is likely more suitable

    • @passionforsciencel5180
      @passionforsciencel5180 6 месяцев назад +1

      @@hayk.simonyan
      Anyway, i like to mention it , sometimes mathematicians can replace us 😅
      Thanks for your feedback

  • @2sourcerer
    @2sourcerer 2 месяца назад +2

    10:12 you said the system with Postgres doesn't handle generating 1000 URLs / sec so cache layer is introduced. Yet 10:22 you talked about "popular URL" which implies the URL is meant for a redirection service. So is the caching layer needed for both creation and redirection? I don't understand how the creation could be "locked" with cache?

    • @hayk.simonyan
      @hayk.simonyan  2 месяца назад

      The caching layer in this system is mainly for accessing the URLs from the NoSQL database, not when generating them via URL shortener. That's because this is a read heavy system, meaning it will get many more reads compared to writes

  • @girjashankar7925
    @girjashankar7925 3 месяца назад +1

    why do we need url shortening and lengthing techniques?

    • @hayk.simonyan
      @hayk.simonyan  3 месяца назад

      Example use case is for posting on Twitter (X) which limits your characters and you need to shorten some URLs to fit content. Another example is to also add tracking through this URL.

  • @AizazShahid-ck8cn
    @AizazShahid-ck8cn 4 месяца назад

    Why not use a database with B-trees instead of LSM + SST in Cassandra considering we want to optimise for reads? And if we use that then we can use the same database for storing short URLs as well rather than doing another network call for the short URL from a separate databse

    • @hayk.simonyan
      @hayk.simonyan  4 месяца назад

      Good question! While B tree databases could optimize reads for this case, the choice of LSM-based systems (like cassandra) often comes from their great scalability and distributed nature. It also excels at handling high write throughput and horizontal scaling and these features are important for handling the massive traffic and global distribution typical of URL shorteners

    • @AizazShahid-ck8cn
      @AizazShahid-ck8cn 4 месяца назад

      @@hayk.simonyan What makes Cassandra better for horizontal scaling compared to something like mysql or postgres? In this scenario, complex multi-shard joins is not really a use case then what makes LSM-based systems scale better?

    • @hayk.simonyan
      @hayk.simonyan  4 месяца назад

      @@AizazShahid-ck8cn cassandra distributes data evenly and ensures high availability unlike MySQL/postgres, which require complex sharding logic. And in a URL shortener we typically prioritize write performance and scalability over complex multi-shard joins

  • @user-gj1ps4kq3e
    @user-gj1ps4kq3e Месяц назад

    Don't we also have to scale Postgres ?

    • @hayk.simonyan
      @hayk.simonyan  27 дней назад

      Initially postgres will have less load compared to nosql database because it stores simple structured data, but along with user growth yes, postgres might also need to scale

  • @ntnmnk2009
    @ntnmnk2009 8 месяцев назад

    How do you use the two databases, one SQL and another NoSQL? Like what is each database's purpose?

    • @hayk.simonyan
      @hayk.simonyan  8 месяцев назад

      Sql database keeps track of the available keys, and Nosql is the primary database that stores the shortened URLs and their metadata

  • @LironTal-q4x
    @LironTal-q4x 3 месяца назад

    I don't understand, you mentioned that:
    URL shortener service contains logic for
    * generating short URLs
    * Storing URL mappings
    * Retrieving original URLs for redirection
    Then why there's no connection between service and DB in the diagram? Why don't we replicate the service itself? How would the service know the short URL doesn't exist already?
    Thanks

    • @hayk.simonyan
      @hayk.simonyan  3 месяца назад

      The URL shortener doesn't read directly from the NoSQL database, but the information that is needed from NoSQL can be passed through the web servers to this URL shortener. That's why you don't see a connection in the diagram. Hope this helps!

    • @LironTal-q4x
      @LironTal-q4x 3 месяца назад +1

      @@hayk.simonyan Still unclear, and I'm extremely curious :)
      1. Storing URL mappings - Meaning it passes the mappings through the web servers and to the database? Same for reading?
      2. Why don't we replicate the service itself?
      3. How would the service know the short URL doesn't exist already?
      4. Is this a design pattern going through the web servers?
      thanks a lot

    • @hayk.simonyan
      @hayk.simonyan  3 месяца назад

      ​@@LironTal-q4x 1. Yes, in the most basic form of this desig, web server checks the availability of a url from URL shortener & if it's available it stores the mapping in the database (same for retreival)
      2. You could replicate the URL shortener if needed, but in this case one service should be enough for storing 3.5T keys
      3. If the short URL already exists, our web server will notify the URL shortener and that short URL key will be marked as used (the value will be set to false)
      4. Yes, the requests go through web servers

    • @LironTal-q4x
      @LironTal-q4x 3 месяца назад

      @@hayk.simonyan Ok last question if I may
      generating short URLs and storing them, I understand it's the shortener service job (because it can access the SQL DB of keys, and tell the web servers what to store in the NoSQL)
      But I don't understand the redirection part, if I wish to redirect a user through the GET /api/urls/{shortUrlId}, why can't I let the web servers look directly at the No SQL for the tiny url? why do I have to go through the shortener to do redirection?
      thanks!!!

    • @hayk.simonyan
      @hayk.simonyan  3 месяца назад +1

      @@LironTal-q4x you only go through the shortener the first time when you create the shortened URL (to check the availability of the URL) and when a user removes their shortened URL, you mark that URL as unused via shortener service. But the redirection is happening through web servers, not through URL shortener.

  • @piotrkulinski922
    @piotrkulinski922 4 месяца назад

    Low latency as functional?

    • @hayk.simonyan
      @hayk.simonyan  4 месяца назад

      no, low latency was mentioned in non-functional requirements section

  • @chessmaster856
    @chessmaster856 2 месяца назад

    How is az different from AZ in case of url.. also this calculation does not have much values
    Is your code and database scalable? That's all

  • @moonlight-kh6uz
    @moonlight-kh6uz 2 месяца назад +1

    Even with Canva, it is a time-consuming to create these diagrams, and then add text, narrative. Does it pay off?

    • @hayk.simonyan
      @hayk.simonyan  2 месяца назад +1

      You're right, creating these presentations is time-consuming. If you mean in terms of YT monetization, then no, it hardly even covers the Canva pro subscription ))