How to design a URL shortening service like Bitly

Both “URL shortening service” and “URL shortener” are commonly used terms to refer to the same concept. They both describe a service or tool that shortens long URLs into shorter, more manageable links, by using a redirect. URL shortener is widely understood to refer to a service that performs the task of shortening URLs.

There are several reasons why you might consider using a URL shortening service:

  • To avoid character limits, such as on Twitter or SMS messages.
  • To make URLs easier to read, type, remember, and share.
  • To track clicks and engagement with UTM parameters or analytics tools.
  • To create branded URLs with your own domain name or custom keywords.
  • To send different visitors different links based on criteria.

Most popular

An URL shortening service can help your website by making your links more appealing, trustworthy, and trackable. However, you should also be careful about using shortened links that may disguise the underlying address or lead to malicious sites. Some of the popular URL shortening services are:

  • Bitly: It is perhaps the most well-known of all URL shortening services, and it provides a wide variety of features for both paying and free customers. It also allows you to create branded URLs and track engagement with UTM parameters.
  • Rebrandly: It is an alternative to Bitly that lets you create custom branded links with your own domain name. It also offers advanced features such as link retargeting, QR codes, and analytics.
  • TinyURL: It is a free, fast, and anonymous service that lets you create shortened links without any registration or tracking. It is ideal for situations where you just need a simple and easy-to-use tool to shorten a single URL.
  • BL.INK: It is for business owners who want to create smart links that can adapt to different devices, locations, and languages. It also integrates with popular platforms such as WordPress, Shopify, and Mailchimp.
  • Short.io: It lets you send different visitors different links based on their location, device, language, or other criteria. You can also use it to create branded domains, QR codes, and deep links.

What to expect

The service should be able to generate a short and unique URL for a given long URL, redirect users to the original URL when they access the short URL, optionally allow users to specify custom aliases and expiration times for their short URLs, and provide some analytics on the usage of the short URLs.

The service should be highly available, reliable, fast, and secure. It should also be able to handle a large volume of requests and scale horizontally as the demand grows.

Based on some assumptions about the expected traffic and storage needs, we can estimate the required bandwidth, memory, disk space, and servers for the service. For example, if we assume that we have 500 million new URLs per month, with a 100:1 read/write ratio, we can expect 50 billion redirections per month. This translates to about 200 new URLs per second and 20,000 redirections per second. Assuming an average URL size of 500 bytes and a short URL size of 20 bytes, we would need about 250 GB of storage per month for the URLs. We can also estimate the number of servers we need based on the expected load and performance of each server.

How to design

Designing a URL shortening service is a common system design question that requires you to consider various aspects such as functional requirements, non-functional requirements, capacity estimation, database design, API design, and scalability.

There are many possible ways to design such a service, but here is a high-level overview of one possible approach:

  • Client-Facing Component: Develop a web-based user interface where users can enter their long URLs and receive shortened URLs as output. Implement an API that allows programmatic access to the service.

  • URL Shortening Service: Use a distributed system architecture for scalability and fault tolerance. Consider using multiple application servers behind a load balancer to handle incoming requests. Implement a URL shortening algorithm that generates unique and short URLs. Use a distributed key-value store (e.g., Redis, Cassandra) to store the mappings between short URLs and long URLs.

  • Redirection Component: Set up a redirection service that listens for incoming requests to the short URLs. Use a reverse proxy or load balancer to distribute the incoming traffic across multiple redirection servers. Maintain an in-memory cache (e.g., Redis, Memcached) to store frequently accessed short URL mappings for faster redirection. When a request comes in, look up the short URL in the cache. If not found, retrieve the mapping from the distributed key-value store.

  • Custom URLs and Analytics: Allow users to customize their short URLs with preferred aliases, ensuring uniqueness. Store additional metadata, such as click counts, timestamps, and referrer information, to provide analytics on URL usage. Use a separate analytics database or data pipeline for efficient data storage and processing.

  • Scaling and Performance: Employ horizontal scaling by adding more application servers, load balancers, and redirection servers as the traffic grows. Consider sharding the URL mappings across multiple distributed key-value store instances for improved performance. Implement caching mechanisms at various levels (client-side, server-side, and CDN) to reduce database load and improve response times. Use content delivery networks (CDNs) to distribute static content (e.g., CSS, JavaScript) and handle high traffic loads.

  • Security: Implement user authentication and access controls to prevent unauthorized usage. Validate and sanitize user inputs to mitigate security risks like cross-site scripting (XSS) and SQL injection attacks. Use encryption (e.g., HTTPS) to secure communication between clients and the service.

  • Monitoring and Logging: Set up monitoring tools (e.g., Prometheus, Grafana) to track system performance, resource utilization, and error rates. Implement centralized logging (e.g., ELK stack) to collect and analyze logs for debugging and troubleshooting purposes. Set up alerts and notifications to proactively identify and address issues.

The implementation details may vary based on specific requirements and technical constraints. During a system design interview, it’s crucial to engage in a discussion, ask clarifying questions, and consider trade-offs while designing the system.

API design

It’s important to note that the specific APIs and their functionality may vary between different URL shortening services. The main APIs typically include:

  • URL Shortening: This API allows you to programmatically shorten a long URL by sending a request to the service with the original URL as a parameter. The API will return a shortened version of the URL that you can use in your applications or websites.

  • Link Retrieval: This API allows you to retrieve information about a shortened link, such as the original long URL, creation date, click statistics, and other metadata associated with the link. This can be useful for tracking link performance or fetching details about specific shortened links.

  • Link Analytics: Many URL shortening services provide APIs to access detailed analytics data for your shortened links. This includes information about the number of clicks, geographical data, referrer sources, device types, and other metrics. These APIs allow you to programmatically retrieve and analyze link engagement data.

  • Customization and Branding: Some URL shortening services offer APIs to enable the customization and branding of shortened links. With these APIs, you can create custom short links using your own domain or branding, providing a consistent and recognizable experience for your users.

  • Bulk Operations: Some URL shortening services offer APIs that allow you to perform bulk operations, such as creating multiple shortened links in a single request or retrieving analytics data for multiple links at once. These APIs are useful when you need to handle large volumes of links efficiently.

URL shortening algorithms

For generating a short URL that is unique from an existing URL, different URL shortening services may use different hashing techniques. One common technique is to use a base conversion algorithm to encode the auto-incremented ID of the URL in the database into a short string with a limited set of characters. Some may also use other methods such as random string generation, key-value mapping, or custom aliasing to create short URLs.

  • Hashing Algorithm: You can use a hashing algorithm such as MD5, SHA-256, or CRC32 to generate a hash value from the long URL. The hash value can then be encoded using base62 or base64 encoding to create a short URL. This approach provides a good balance between uniqueness and simplicity.

  • Base Conversion Algorithm: Convert the unique ID of the long URL into a shorter representation using base conversion techniques. For example, you can convert the decimal representation of the ID into a base58 or base62 encoding, excluding easily confused characters like ‘0’, ‘O’, ‘1’, ‘I’, etc.

  • Bijective Function Algorithm: Utilize a bijective function that maps a unique identifier to a short URL and vice versa. For example, you can use a function like the Bijective Conversion Function (BCF) algorithm, which converts a decimal ID into a sequence of characters using a predefined set of characters.

  • Randomized Approach: Generate a random sequence of characters of a fixed length (e.g., 6 or 8 characters) to create the short URL. Although this approach may not guarantee uniqueness, you can perform a lookup in the database to ensure uniqueness and regenerate if there’s a collision.

When choosing an algorithm, consider factors such as uniqueness, collision probability, ease of implementation, and the desired length of the short URL. Additionally, take into account the scalability of the algorithm, as it should be able to handle a potentially large number of URLs and avoid collisions.

Ultimately, the choice of algorithm depends on your specific requirements and preferences. It may be beneficial to experiment and evaluate different algorithms based on factors like uniqueness, simplicity, and performance to find the best fit for your URL shortening service.

Databases

We need to store two types of data: the mapping between the long URLs and the short URLs, and the analytics data for each short URL. We can use two different databases for these purposes: a NoSQL database for the URL mapping and a relational database for the analytics data

Different services may use different databases to store and retrieve the long and short URLs. Some of the common databases are:

  • MySQL: A popular relational database management system that can handle large amounts of data and queries efficiently. Bitly, one of the most widely used URL shorteners, uses MySQL as its primary database.

  • MongoDB: A document-oriented database that stores data in JSON-like format and allows flexible schema design. Rebrandly, a URL shortener that allows custom branding and analytics, uses MongoDB as its main database.

  • Redis: An in-memory data structure store that can serve as a database, cache, or message broker. It offers fast performance and supports various data types and operations. BL.INK, a URL shortener for business owners, uses Redis as its caching layer to speed up the redirection process.

  • DynamoDB: A fully managed NoSQL database service offered by Amazon Web Services. It provides high availability, scalability, and low latency. TinyURL, one of the oldest and simplest URL shorteners, uses DynamoDB as its backend database.

Scalability

To ensure that our service can handle high traffic and scale horizontally, we can use some techniques such as load balancing, caching, sharding, replication, message queues, etc. For example:

  • Load balancing: We can use a load balancer like Nginx or HAProxy to distribute the incoming requests among multiple servers based on some criteria such as round robin or least connections. This way we can avoid overloading any single server and improve availability and performance.

  • Caching: We can use a cache like Redis or Memcached to store frequently accessed or hot data such as popular short URLs or recent analytics data. This way we can reduce the latency and database load for these requests.

  • Sharding: We can partition our data into smaller chunks based on some criteria such as hash or range of the short URL or the user ID. This way we can distribute our data across multiple servers or clusters and improve scalability and performance.

  • Replication: We can create copies of our data on different servers or clusters to improve availability and fault tolerance. We can use either synchronous or asynchronous replication depending on the consistency and latency requirements. For example, we can use synchronous replication for the URL mapping database to ensure strong consistency and asynchronous replication for the analytics database to reduce write latency.

  • Message queues: We can use a message queue like Kafka or RabbitMQ to decouple the write and read operations for the analytics data. For example, we can have a producer that writes the analytics data to a message queue and a consumer that reads the data from the queue and writes it to the database. This way we can handle spikes in traffic and avoid blocking the redirection requests.