Sample tokens generated for salt = ''this is my salt" and character set = "abcdefghijklmnopqrstuvwxyz1234567890". The code footprint is very minimal, and there are no external library dependencies. The Ids generated are mangled and are not easy to guess. Hashids can create short, unique Ids from numbers (positive numbers and zero) and allows custom alphabets as well as salt, so Ids are unique only to you. Third-Party LibraryĪ very reliable library that generates unique, random tokens (ids) is Hashids (which has support in more than 30 languages!). We'll cover each of these cases individually. Tokens can be generated by multiple strategies by using 3rd party libraries or using conventional methods like Base36 encoding. Otherwise, the system may incorrectly redirect the short URL to another location (long URL). The URL Shortening service must ensure that the token should not be assigned to two (or more) different long URLs. The logic to generate unique, random tokens can be tricky. URL Redirection workflow Token Generation The short domain in the short URL has to be mapped to the service like below: When a user clicks on the short URL, the request should be redirected to the URL Shortening Service, which would then redirect it to the actual long URL location. URL Generation Workflow HLD - Short URL Redirection If the existing short URL has expired, then a new one has to be created and sent as a response. If the incoming long URL already exists in the system, then the previous generated short URL has to be returned. The URL Shortening service accepts a long URL as input, generates a short URL, and returns it. Short URL Redirection - covers how to handle a short URL click request by a user so that the user can be redirected to the actual location i.e.Short URL Generation - covers how to handle a request by the system to generate a short URL for an incoming long URL.The High-Level Design (HLD) consists of two parts. The token generation is covered below, after the high-level design section.īuilding Blocks of a Scalable Architecture. If we require a larger range, then setting the token length to 8 characters gives us a larger set, which comes to around 2 trillion (we can build a service possibly with almost all URLs in the world! ). Token Lengthįrom the above table, we can choose the length of our token to be 7 characters long, which can accommodate more than 78 billion unique tokens. The number of unique tokens that can be generated using various token lengths are shown below. To be URL safe, consider smaller case characters a-z and numbers from 0-9, we have a total character set of 36 character set. While designing the URL shortening service, we pay close attention to the length of the token and the characters it contains. The length of a token typically varies from 6-9 characters. Tokens are alphanumeric strings and act as identifiers to the actual long URL. Short URLsĪ typical short URL could be something like this, where ts.dz is a short domain for a particular application, service, or company, and the last part in the URL qp7h3zi is a unique, random identifier. The identifiers should be short enough to fit in the URL with an acceptable character set. The interesting part of the problem is generating unique, random identifiers for these long URLs. Please pause here and think about how you could design a system to generate an infinite number of short URLs for possibly all long URLs in the world. The need for shortened URLs arises when long URLs have to be shared for various purposes, like sharing links via SMSs, Emails, media types, like Twitter or WhatsApp, etc.Ī URL shortening service might sound like a simple service at first, but it poses a very interesting and challenging engineering problem. Typically, a long URL is a system-generated URL, usually containing multiple query parameters that may be encrypted/encoded to mask those parameters.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |