{"id":4838,"date":"2024-06-14T17:24:00","date_gmt":"2024-06-14T11:54:00","guid":{"rendered":"https:\/\/namastedev.com\/blog\/?p=4838"},"modified":"2024-06-14T17:24:00","modified_gmt":"2024-06-14T11:54:00","slug":"url-shortener-implementation-without-external-api","status":"publish","type":"post","link":"https:\/\/namastedev.com\/blog\/url-shortener-implementation-without-external-api\/","title":{"rendered":"URL Shortener Implementation without external API"},"content":{"rendered":"<p><span class=\"ql-size-large ql-font-monospace\">This is an implementation of a URL Shortener Implementation without use of any external API using SHA1 encryption.<\/span><\/p>\n<p><span class=\"ql-size-large ql-font-monospace\">The long URL which will be given to us will be in the format of <\/span><\/p>\n<p><a class=\"ql-size-large ql-font-monospace\" href=\"https:\/\/localhost:5050\/track-link\/A-23-Depot-Z-M-01?id=%24%7Buuid\" target=\"_blank\" rel=\"noopener noreferrer\">https:\/\/localhost:5050\/track-link\/A-23-Depot-Z-M-01?id=${uuid<\/a><span class=\"ql-size-large ql-font-monospace\">}&#8221;<\/span><\/p>\n<p><span class=\"ql-size-large ql-font-monospace\">uuid is unique identifier of 36 characters.<\/span><\/p>\n<h2><strong class=\"ql-size-large ql-font-monospace\">Problem Statement<\/strong><\/h2>\n<p><span class=\"ql-size-large ql-font-monospace\">The problem statement is to convert this long url into a shortened url. Whenever we click on this shortened url, it should redirect us to the long url and eventually to the webpage that the long url is directing to.<\/span><\/p>\n<h2><strong class=\"ql-size-large ql-font-monospace\">Back of the Envelope Estimation<\/strong><\/h2>\n<p><span class=\"ql-size-large ql-font-monospace\">We want our shortened url to be of length \u2264 7. <\/span><\/p>\n<p><span class=\"ql-size-large ql-font-monospace\"> Character set of our shortened url includes [A-Z], [a-z], [0\u20139] which is 26 + 26 + 10 = 62 characters.<\/span><\/p>\n<p><span class=\"ql-size-large ql-font-monospace\">So with this we can have 62 ^ 7 = <\/span><em class=\"ql-size-large ql-font-monospace\">35 billion unique urls<\/em><span class=\"ql-size-large ql-font-monospace\">. And this is the reason why we will be using base-62 encryption as well in our implementation.<\/span><\/p>\n<h2><strong class=\"ql-size-large ql-font-monospace\">Implementation<\/strong><\/h2>\n<p><span class=\"ql-size-large ql-font-monospace\">The original url is encrypted using SHA-1 to a 40 hex encrypted hash.<\/span><\/p>\n<p><span class=\"ql-size-large ql-font-monospace\">We slice this 40 hex hash into 5 equal parts of 8 hex each, which is converted into decimal which again is base62 encoded.<\/span><\/p>\n<p><span class=\"ql-size-large ql-font-monospace\">This splitting into 5 equal parts is done to make sure that our final base-62 encrypted string will have a length of \u2264 7, since the main target is to produce a shortened url.<\/span><\/p>\n<p><span class=\"ql-size-large ql-font-monospace\">This shortened url is then stored in database along with the original long url as a kind of a mapper, which will be used while redirection of the shortened url to the original url.<\/span><\/p>\n<p><span class=\"ql-size-large ql-font-monospace\">Now while storing the shortened url in database, we will first check if this shortened url exists or not. If it exists, then we will put some degree of randomness and will generate the shortened url again.<\/span><\/p>\n<p><span class=\"ql-size-large ql-font-monospace\">This retry count has been limited to 10 in our implementation, so as to not affect latency of the system a lot. Also as per our functional requirement, the expiry of the shortened url in database is 3 months.<\/span><\/p>\n<h2><strong class=\"ql-size-large ql-font-monospace\">Test Results<\/strong><\/h2>\n<p><span class=\"ql-size-large ql-font-monospace\">Repeats in a run of 1000000 are 99 with repeat percentage of 0.009899999999999999.<\/span><\/p>\n<p><span class=\"ql-size-large ql-font-monospace\">Repeats in a run of 8500000 are 8416 with repeat percentage of 0.09901176470588235.<\/span><\/p>\n<p><span class=\"ql-size-large ql-font-monospace\">Repeats in a run of 10000000 are 11725 with repeat percentage of 0.11725.<\/span><\/p>\n<h2><strong class=\"ql-size-large ql-font-monospace\">Github Link<\/strong><\/h2>\n<p><a class=\"ql-size-large ql-font-monospace\" href=\"https:\/\/github.com\/swapnil-pando\/url-shortener\" target=\"_blank\" rel=\"noopener noreferrer\"><em>https:\/\/github.com\/swapnil-pando\/url-shortener<\/em><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is an implementation of a URL Shortener Implementation without use of any external API using SHA1 encryption. The long URL which will be given to us will be in the format of https:\/\/localhost:5050\/track-link\/A-23-Depot-Z-M-01?id=${uuid}&#8221; uuid is unique identifier of 36 characters. Problem Statement The problem statement is to convert this long url into a shortened<\/p>\n","protected":false},"author":29,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[231,334,351],"tags":[335,352,353],"class_list":{"0":"post-4838","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-article","7":"category-best-practices","8":"category-general","9":"tag-best-practices","10":"tag-general","11":"tag-good-pratice"},"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/4838","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/users\/29"}],"replies":[{"embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/comments?post=4838"}],"version-history":[{"count":1,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/4838\/revisions"}],"predecessor-version":[{"id":4841,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/4838\/revisions\/4841"}],"wp:attachment":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/media?parent=4838"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/categories?post=4838"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/tags?post=4838"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}