{"componentChunkName":"component---src-templates-blog-post-js","path":"/designing-a-url-shortening-service-like-tiny-url/","result":{"data":{"site":{"siteMetadata":{"title":"Undefined"}},"mdx":{"id":"a5e6c7d4-658a-50bc-8326-de9690e0a7ee","excerpt":"Step 1: System Requirements generate a short alias for url. when users access the short alias, redirect them to original url. customized alias set expire time…","body":"function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }\n\nfunction _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }\n\nfunction _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }\n\n/* @jsxRuntime classic */\n\n/* @jsx mdx */\nvar _frontmatter = {\n  \"title\": \"Designing a url shortening service like tiny url\",\n  \"date\": \"2021-04-03T23:56:03.284Z\",\n  \"description\": \"\",\n  \"tags\": [\"System Design\"]\n};\nvar layoutProps = {\n  _frontmatter: _frontmatter\n};\nvar MDXLayout = \"wrapper\";\nreturn function MDXContent(_ref) {\n  var components = _ref.components,\n      props = _objectWithoutProperties(_ref, [\"components\"]);\n\n  return mdx(MDXLayout, _extends({}, layoutProps, props, {\n    components: components,\n    mdxType: \"MDXLayout\"\n  }), mdx(\"h2\", null, \"Step 1: System Requirements\"), mdx(\"ul\", null, mdx(\"li\", {\n    parentName: \"ul\"\n  }, \"generate a short alias for url.\"), mdx(\"li\", {\n    parentName: \"ul\"\n  }, \"when users access the short alias, redirect them to original url.\"), mdx(\"li\", {\n    parentName: \"ul\"\n  }, \"customized alias\"), mdx(\"li\", {\n    parentName: \"ul\"\n  }, \"set expire time for url.\")), mdx(\"p\", null, \"technical requirements:\"), mdx(\"ul\", null, mdx(\"li\", {\n    parentName: \"ul\"\n  }, \"highly available. - if the service is down, all URL refirection will fail.\"), mdx(\"li\", {\n    parentName: \"ul\"\n  }, \"Read > Write - read-heavy.\"), mdx(\"li\", {\n    parentName: \"ul\"\n  }, \"Read need be very fast - low latency\"), mdx(\"li\", {\n    parentName: \"ul\"\n  }, \"data consistency ? < 1 minute - new created url need available for all users quickly. - Shortened links should not be guessable (not predictable).\")), mdx(\"p\", null, \"Expanded requirements:\"), mdx(\"ul\", null, mdx(\"li\", {\n    parentName: \"ul\"\n  }, \"Analytics - Do we need count the visit number of each url?\"), mdx(\"li\", {\n    parentName: \"ul\"\n  }, \"can we delete/update the created short url? - Let\\u2019s don\\u2019t support these functions.\"), mdx(\"li\", {\n    parentName: \"ul\"\n  }, \"the service need be accessible by REST APIs.\")), mdx(\"h2\", null, \"Step 2: System Interfaces\"), mdx(\"ul\", null, mdx(\"li\", {\n    parentName: \"ul\"\n  }, \"create_short_url(long_url, user_id, user_token)\"), mdx(\"li\", {\n    parentName: \"ul\"\n  }, \"visit_url(short_url)\"), mdx(\"li\", {\n    parentName: \"ul\"\n  }, \"delete_short_url(user_id, user_token, short_url)\\n\\u2026\")), mdx(\"h2\", null, \"Step 3: estimate metrics\"), mdx(\"p\", null, \"Assume QPS: 20000(read), 200(write) - assume 100:1 ratio between read and write.\\nStorage: 100 \", mdx(\"em\", {\n    parentName: \"p\"\n  }, \" 800 bytes (1 url = 100 chars) \"), \" 60 \", mdx(\"em\", {\n    parentName: \"p\"\n  }, \" 60 \"), \" 24 =  consider expire time here.\\nHow to estimate size of string?\\nbyte to GB?\"), mdx(\"p\", null, \"netword bandwidth: how to estimate? 1KB = 1000 bytes\"), mdx(\"p\", null, \"Memory estimate for cache:\\n80-20 rule = 20% of Urls generate 80% of traffic.\"), mdx(\"h2\", null, \"Step 4: Data model\"), mdx(\"p\", null, \"Thins to consider here before make decision:\"), mdx(\"ul\", null, mdx(\"li\", {\n    parentName: \"ul\"\n  }, \"read-heavy\"), mdx(\"li\", {\n    parentName: \"ul\"\n  }, \"billions of records, but each one is small < 1000 bytes\"), mdx(\"li\", {\n    parentName: \"ul\"\n  }, \"no strong relationships\")), mdx(\"p\", null, \"no strong relationships => NoSQL\"), mdx(\"p\", null, \"short_url_to_long_url table\\nid\\nshort_url index unique\\nlong_url index unique\\nuser_id fk\"), mdx(\"h2\", null, \"Step 5: high-level graph\"), mdx(\"p\", null, \"short_url service - db (1 master - multi slaves) - time\\n|\\ncache (cache strategy) what need to think for cache?\"), mdx(\"p\", null, \"algorithm of short url: hash function - less or no collision - no special character ? MD5 or SHA256, encoding for displaying - base36\", \"[a-z,0-9]\", \" base62\", \"[A-Z, a-z, 0-9]\", \" base64(- .)\"), mdx(\"p\", null, \"the length of short key? if base64 and len = 6, 64^6\"), mdx(\"h2\", null, \"Step 6: bottle neck\"), mdx(\"p\", null, \"where is bottle neck? read for db? - cache\"), mdx(\"p\", null, \"offline key generation service - concurrency problems?\"), mdx(\"h2\", null, \"Step 7: open questions\"), mdx(\"p\", null, \"How do we detect and prevent abuse? - authentication \"));\n}\n;\nMDXContent.isMDXComponent = true;","frontmatter":{"title":"Designing a url shortening service like tiny url","date":"April 03, 2021","description":"","tags":["System Design"]},"fields":{"langKey":"en","directoryName":"designing-a-url-shortening-service-like-tiny-url","slug":"designing-a-url-shortening-service-like-tiny-url"}}},"pageContext":{"slug":"designing-a-url-shortening-service-like-tiny-url","langKey":"en","previous":{"fields":{"langKey":"en","directoryName":"the-steps-of-system-design","slug":"the-steps-of-system-design"},"frontmatter":{"title":"The steps of system design"}},"next":{"fields":{"langKey":"en","directoryName":"minimum-spanning-tree","slug":"minimum-spanning-tree"},"frontmatter":{"title":"Find the Minimum Spanning Tree (MST)"}},"translations":["en"]}},"staticQueryHashes":["2841359383"]}