![]() ![]() We then break this 32 byte value up into 3x ten bytes, and xor these together, to get an 8 byte value. Firstly it creates and MD5 hash of the URL, to give it a unique 32 byte value. Url_hash = hashlib.md5(url.encode()).hexdigest() It then goes through and adds those to the Redis cache so Redis is up to date and ready to answer the queries (instead of the DynamoDB). It creates a DynamoDB connection and reads the entries in the Table (table_name – our tinyURL key/pair table). STARTUP Functionītup_default_session(profile_name=’james’)ĭynamo_db = boto3.resource(‘dynamodb’, region_name=myRegion)Īwait redis_db.set(entries, entries) If its not present – it will return false (ie a cache miss). This function connects to Redis and then reads in the tinyURL from the cache. Val = await redis_db.get(url, encoding=’utf-8′) I will go over the key commands in the code, but the full code can be downloaded from HERE. I also use the aioredis module for python, allowing us to do async operations with Redis.DynamoDB is used as the backend to provide a persistent store of the URLs (which could also hold more data for each entry like user and time/date requested).I use Redis as the READ database, since its an in memory cache system that is super fast for NoSQL lookups (like this one – simply needing a key/value pair for the tinyURL and Original URL).Each character in the tinyURL will be from a base62 choice, which gives us 8^62 different combinations of characters (which is a BIG space), also meaning that the chance of a collision (same hash/tinyURL) is very small. The 8 char string will be built from an MD5 hash of the URL.Reads all existing key/value pairs from DynamoDB and inserts into Redis cacheĪ few notes on the steps and some reasoning behind why we use each technology: if not in redis, adds to DynamoDB and redis checks if already used (redis) – if so return shortened string takes in the URL and shrinks to a 8 char string The logic of the python app is as follows: We will build this as a CLI based python app – but in the real world you would have this done via a webpage (for the creation) and using DNS to do the lookup of tinyURL to match it to the full domain. DynamoDB (with a table called tinyUrl set up to store the entries with a sort key called miniUrl).You can run Redis from the source code on any instance method you like, but I wanted to work with Docker just to add to the technologies being used in the design Redis via docker instance (download HERE) (in my case, running on default port 6379).Python with BOTO3 module installed (see my previous post HERE).Its setup on a Windows 10 PC, with Python 3.8.5 via VisualStudio Code, and Docker for windows installed. I built this based on a a single instance of the app and database(s) – and review at the end some ideas on scaling. One of the well know questions is “How would you design a URL shortening service like tinyURL / bit.ly ?” I did some reading on this and it looked like a good product to actually build, so lets go through the steps and code that can make this work. This is where you do a high level design of a scalable product, and talk through the components and how you would build it (further on that in a coming post). For decoding, we just need to use the hash code value from the short URL to get the original long URL.One of the interview rounds that is a key part of major organisations (like AWS, Microsoft and Google) is the System Design interview. String ans = obj.decode(tiny) // returns the original url after deconding it.įor encoding, we can convert the long URL to hash code value and store it into a map. String tiny = obj.encode(url) // returns the encoded tiny url. It is guaranteed that the given shortUrl was encoded by the same object. String decode(String shortUrl) Returns the original long URL for the given shortUrl.String encode(String longUrl) Returns a tiny URL for the given longUrl.Solution() Initializes the object of the system.You just need to ensure that a URL can be encoded to a tiny URL and the tiny URL can be decoded to the original URL. There is no restriction on how your encode/decode algorithm should work. Design a class to encode a URL and decode a tiny URL. TinyURL is a URL shortening service where you enter a URL such as and it returns a short URL such as. Note: This is a companion problem to the System Design problem: Design TinyURL.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |