0% found this document useful (0 votes)
130 views14 pages

Design and Deployment of TinyURL

The document discusses the design and deployment of a URL shortening service called TinyURL. It describes the system APIs for shortening, redirecting, and deleting URLs. It outlines the design components, including a database to store URL mappings, a short URL generator using a sequencer and base-58 encoder, and API servers to handle requests. It also includes a design diagram showing how short URLs are generated from long URLs and redirects are handled.

Uploaded by

Hyeo Woo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
130 views14 pages

Design and Deployment of TinyURL

The document discusses the design and deployment of a URL shortening service called TinyURL. It describes the system APIs for shortening, redirecting, and deleting URLs. It outlines the design components, including a database to store URL mappings, a short URL generator using a sequencer and base-58 encoder, and API servers to handle requests. It also includes a design diagram showing how short URLs are generated from long URLs and redirects are handled.

Uploaded by

Hyeo Woo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

10/04/2023, 09:36 Design and Deployment of TinyURL - Grokking Modern System Design Interview for Engineers & Managers

Ask a Question

Design and Deployment of TinyURL


Deep-diving into the design and deployment of the URL shortening service.

We'll cover the following

• System APIs
• Shortening a URL
• Redirecting a short URL
• Deleting a short URL
• Design
• Components
• Design diagram
• Workflow

System APIs
To expose the functionality of our service, we can use REST APIs for the following features:

Shortening a URL
https://www.educative.io/courses/grokking-modern-system-design-interview-for-engineers-managers/qV6WgYr4jJr 1/14
10/04/2023, 09:36 Design and Deployment of TinyURL - Grokking Modern System Design Interview for Engineers & Managers

Redirecting a short URL


Deleting a short URL

Create url
Redirect url
Delete url Shorten a URL
Redirect a short URL
Reply Delete a short URL

Client API servers Database

System API design overview

Shortening a URL

We can create new short URLs with the following definition:

shortURL(api_dev_key, original_url, custom_alias=None, expiry_date=None)

The API call above has the following parameters:

Parameter Description

api_dev_key A registered user account’s unique identifier. This is useful in tracking a user’s
activity and allows the system to control the associated services accordingly.

original_url The original long URL that is needed to be shortened.

custom_alias The optional key that the user defines as a customer short URL.

https://www.educative.io/courses/grokking-modern-system-design-interview-for-engineers-managers/qV6WgYr4jJr 2/14
10/04/2023, 09:36 Design and Deployment of TinyURL - Grokking Modern System Design Interview for Engineers & Managers

expiry_date The optional expiration date for the shortened URL.

A successful insertion returns the user the shortened URL. Otherwise, the system returns an appropriate
error code to the user.

Redirecting a short URL

To redirect a short URL, the REST API’s definition will be:

redirectURL(api_dev_key, url_key)

With the following parameters:

Parameter Description

api_dev_key The registered user account’s unique identifier.

url_key The shortened URL against which we need to fetch the long URL from the database.

A successful redirection lands the user to the original URL associated with the url_key.

Deleting a short URL


https://www.educative.io/courses/grokking-modern-system-design-interview-for-engineers-managers/qV6WgYr4jJr 3/14
10/04/2023, 09:36 Design and Deployment of TinyURL - Grokking Modern System Design Interview for Engineers & Managers

Similarly, to delete a short URL, the REST API’s definition will be:

deleteURL(api_dev_key, url_key)

and the associated parameters will be:

Parameter Description

api_dev_key The registered user account’s unique identifier.

url_key The shortened URL against which we need to fetch the long URL from the database.

A successful deletion returns a system message, URL Removed, conveying the successful URL removal from the
system.

Design
Let’s discuss the main design components required for our URL shortening service. Our design depends on
each part’s functionality and progressively combines them to achieve different workflows mentioned in the
functional requirements.

Components

https://www.educative.io/courses/grokking-modern-system-design-interview-for-engineers-managers/qV6WgYr4jJr 4/14
10/04/2023, 09:36 Design and Deployment of TinyURL - Grokking Modern System Design Interview for Engineers & Managers

We’ll explain the inner mechanism of different components within our system, as well as their usage as a part
of the whole system below. We’ll also highlight the design choices made for each component to achieve the
overall functionality.

Database: For services like URL shortening, there isn’t a lot of data to store. However, the storage has to be
horizontally scalable. The type of data we need to store includes:

User details.
Mappings of the URLs, that is, the long URLs that are mapped onto short URLs.

Our service doesn’t require user registration for the generation of a short URL, so we can skip adding certain
data to our database. Additionally, the stored records will have no relationships among themselves other than
linking the URL-creating user’s details, so we don’t need structured storage for record-keeping. Considering
the reasons above and the fact that our system will be read-heavy, NoSQL is a suitable choice for storing data.
In particular, MongoDB is a good choice for the following reasons:

1. It uses leader-follower protocol, making it possible to use replicas for heavy reading.
2. MongoDB ensures atomicity in concurrent write operations and avoids collisions by returning duplicate-
key errors for record-duplication issues.

Quiz

Question
https://www.educative.io/courses/grokking-modern-system-design-interview-for-engineers-managers/qV6WgYr4jJr 5/14
10/04/2023, 09:36 Design and Deployment of TinyURL - Grokking Modern System Design Interview for Engineers & Managers

Why are NoSQL databases like Cassandra or Riak not good choices instead of MongoDB?

Show Answer

Short URL generator: Our short URL generator will comprise a building block and an additional component:

A sequencer to generate unique IDs


A Base-58 encoder to enhance the readability of the short URL

We built a sequencer in our building blocks section to generate 64-bit unique numeric IDs. However, our
proposed design requires 64-bit alphanumeric short URLs in base-58. To convert the numeric (base-10) IDs to
alphanumeric (base-58), we’ll need a base-10 for the base-58 encoder. We’ll explore the rationale behind these
decisions alongside the internal working of the base-58 encoder in the next lesson.

Take a look at the diagram below to understand how the overall short URL generation unit will work.

https://www.educative.io/courses/grokking-modern-system-design-interview-for-engineers-managers/qV6WgYr4jJr 6/14
10/04/2023, 09:36 Design and Deployment of TinyURL - Grokking Modern System Design Interview for Engineers & Managers

http://www.educative.io/service/subservice/a-uselessly-long-link Long URL

5yms49nf = encoded Unique ID

Long URL Short URL


ID
https://tinyurl.com/5yms49nf
Short URL
Service host Sequencer Base-58 encoder

Short URL generator

Internal working of a short URL generator

Other building blocks: Beside the elements mentioned above, we’ll also incorporate other building blocks
like load balancers, cache, and rate limiters.

Load balancing: We can employ Global Server Load Balancing (GSLB) apart from local load balancing to
improve availability. Since we have plenty of time between a short URL being generated and
subsequently accessed, we can safely assume that our DB is geographically consistent and that
distributing requests globally won’t cause any issues.
Cache: For our specific read-intensive design problem, Memcached is the best choice for a cache
solution. We require a simple, horizontally scalable cache system with minimal data structure
requirements. Moreover, we’ll have a data-center-specific caching layer to handle native requests.
Having a global caching layer will result in higher latency.
Rate limiter: Limiting each user’s quota is preferable for adding a security layer to our system. We can
achieve this by uniquely identifying users through their unique api_dev_key and applying one of the

https://www.educative.io/courses/grokking-modern-system-design-interview-for-engineers-managers/qV6WgYr4jJr 7/14
10/04/2023, 09:36 Design and Deployment of TinyURL - Grokking Modern System Design Interview for Engineers & Managers

discussed rate-limiting algorithms (see Rate Limiter from Building Blocks). Keeping in view the simplicity
of our system and the requirements, the fixed window counter algorithm would serve the purpose, as we
can assign a set number of shortening and redirection operations per api_dev_key for a specific
timeframe.

Quiz

Question 1

How will we maintain a unique mapping if redirection requests can go to different data centers that
are geographically apart? Does our design assume that our DB is consistent geographically?

Show Answer

1 of 3

Design diagram

A simple design diagram of the URL shortening system is given below.

https://www.educative.io/courses/grokking-modern-system-design-interview-for-engineers-managers/qV6WgYr4jJr 8/14
10/04/2023, 09:36 Design and Deployment of TinyURL - Grokking Modern System Design Interview for Engineers & Managers

Cache service Datastore


Rate limiter

Application
User Load balancer Short URL generator
Web server service

A design diagram of the URL shortening service

Workflow

Let’s analyze the system in-depth and how the individual pieces fit together to provide the overall
functionality.

Keeping in view the functional requirements, the workflow of the abstract design above would be as follows.

1. Shortening: Each new request for short link computation gets forwarded to the short URL generator
(SUG) by the application server. Upon successful generation of the short link, the system sends one copy
back to the user and stores the record in the database for future use.

https://www.educative.io/courses/grokking-modern-system-design-interview-for-engineers-managers/qV6WgYr4jJr 9/14
10/04/2023, 09:36 Design and Deployment of TinyURL - Grokking Modern System Design Interview for Engineers & Managers

Quiz

Question 1

How does our system avoid duplicate short URL generation?

Hide Answer

Computing a short URL for an already existing long URL is redundant, and the system sends the
long URL to the database server to check its existence in the system. The system will check the
respective entry in the cache first and then query the database.
If the short URL for the corresponding long URL is already present, the database returns the
saved short URL to the application server which reroutes the requested short URL to the user.
If the requested short URL is unavailable in the system, the application server requests the SUG to
compute the short URL for the requested long URL. Once computed, the SUG sends back a copy of
the requested short URL to the application server and another copy to the database server.

1 of 2

https://www.educative.io/courses/grokking-modern-system-design-interview-for-engineers-managers/qV6WgYr4jJr 10/14
10/04/2023, 09:36 Design and Deployment of TinyURL - Grokking Modern System Design Interview for Engineers & Managers

2. Redirection: Application servers, upon receiving the redirection requests, check the storage units
(caching system and database) for the required record. If found, the application server redirects the user
to the associated long URL.

Quiz

Question

How does our system ensure that our data store will not be a bottleneck?

Hide Answer

We can ensure that our data store doesn’t become a bottleneck, using the following two approaches:

1. We use a range-based sequencer in our design, which ensures basic level mapping between the
servers and the short URLs. We can redirect the request to the respective database for a quick
search.
2. As discussed above, we can also have unique IDs for various data stores and integrate them into
short URLs. We can subsequently redirect requests to the respective datastore for efficient
request handling.

https://www.educative.io/courses/grokking-modern-system-design-interview-for-engineers-managers/qV6WgYr4jJr 11/14
10/04/2023, 09:36 Design and Deployment of TinyURL - Grokking Modern System Design Interview for Engineers & Managers

Both of these approaches ensure smooth traffic handling and mitigate the risk of the datastore
becoming a bottleneck.

3. Deletion: A logged-in user can delete a record by requesting the application server which forwards the
user details and the associated URL’s information to the database server for deletion. A system-initiated
deletion can also be triggered upon an expiry time, as we’ll see ahead.
4. Custom short links: This task begins with checking the eligibility of the requested short URL. The
maximum length allowed is 11 alphanumeric digits. We can find the details on the allowed format and
the specific digits in the next lesson. Once verified, the system checks its availability in the database. If
the requested URL is available, the user receives a successful short URL generation message, or an error
message in the opposite case.

The illustration below depicts how URL shortening, redirection, and deletion work.

https://www.educative.io/courses/grokking-modern-system-design-interview-for-engineers-managers/qV6WgYr4jJr 12/14
10/04/2023, 09:36 Design and Deployment of TinyURL - Grokking Modern System Design Interview for Engineers & Managers

API Gateway

Return a not found error message

Application
server

Not found in the


storage

Cache server Database server

Storage

URL deletion - unsuccessful deletion (Case 2)

21 of 21

https://www.educative.io/courses/grokking-modern-system-design-interview-for-engineers-managers/qV6WgYr4jJr 13/14
10/04/2023, 09:36 Design and Deployment of TinyURL - Grokking Modern System Design Interview for Engineers & Managers

Question

Upon successful allocation of a custom short URL, how does the system modify its records?

Hide Answer

Since the custom short URL is the base-58 encoding of an available base-10 unique ID, marking that
unique ID as unavailable for future use is necessary for the system’s integrity.

On the backend, the system accesses the server with the base-10 equivalent unique ID of that specific
base-58 short URL. It marks the ID as unavailable in the range, eliminating any chance of reallocating
the same ID to any other request.

123

abc 123 123

B 58 B 10

https://www.educative.io/courses/grokking-modern-system-design-interview-for-engineers-managers/qV6WgYr4jJr 14/14

You might also like