Building a URL Shortener with Redis
Introduction
In this tutorial, we’ll build a simple URL shortener using Redis and Python. The short URL service will generate a random short code for each URL, store it in Redis, and allow users to retrieve the original URL using the short code. We’ll also implement an expiration time for each shortened URL, making it expire after a specified period.
Environment Setup
First, install the necessary dependencies, including Upstash Redis and python-dotenv for environment variables:
pip install upstash-redisDatabase Setup
Create a Redis database using the Upstash Console or Upstash CLI, and export the UPSTASH_REDIS_REST_URL and UPSTASH_REST_TOKEN to your environment:
export UPSTASH_REDIS_REST_URL=<YOUR_URL>export UPSTASH_REDIS_REST_TOKEN=<YOUR_TOKEN>You can also use python-dotenv to load environment variables from a .env file:
UPSTASH_REDIS_REST_URL=<YOUR_URL>UPSTASH_REDIS_REST_TOKEN=<YOUR_TOKEN>Application Setup
In this example, we will build a URL shortener where each short URL will be stored in Redis with an expiration time.
Create url_shortener.py:
import stringimport randomfrom upstash_redis import Redisfrom dotenv import load_dotenvimport os# Load environment variablesload_dotenv()# Redis connectionredis = Redis.from_env()# Characters to generate the short URL fromCHARS = string.ascii_letters + string.digitsBASE_URL = "https://short.url/"# Function to generate a random string for the short URLdef generate_short_code(length=6): return ''.join(random.choices(CHARS, k=length))# Function to shorten the URL with an expiration timedef shorten_url(url, expiration=3600): # Generate a random short code short_code = generate_short_code() # Save the short code in Redis redis.set(short_code, url, ex=expiration) return BASE_URL + short_code# Function to get the original URL from the short URLdef get_original_url(short_code): return redis.get(short_code)# Example usageif __name__ == "__main__": original_url = "https://example.com/my-very-long-url" # Shorten the URL short_url = shorten_url(original_url, expiration=600) print(f"Shortened URL: {short_url}") # Get the original URL original_url = get_original_url(short_url.split("/")[-1]) if original_url: print(f"Original URL: {original_url}") else: print("Short URL expired or not found")Running the Application
Simply run the Python script:
python url_shortener.pyThis script will shorten a long URL, store the mapping in Redis, and print the shortened URL. It will then attempt to retrieve the original URL using the short code.
Here is an example output:
Shortened URL: https://short.url/0lSLFIOriginal URL: https://example.com/my-very-long-urlTo monitor your data in Redis, you can use the Upstash Console and check out the Data Browser tab.
How to Use the URL Shortener for Web Applications
-
Extract the short code from the shortened URL (e.g.,
0lSLFI). -
Look up the original URL in Redis using that code.
-
Redirect the user to the original URL.
Code Breakdown
-
Random Short Code Generation: The
generate_short_codefunction creates a random string of characters (letters and digits) that will serve as the short code for the URL. -
Storing in Redis: The
shorten_urlfunction takes the original URL and stores it in Redis using the randomly generated short code as the key. Theexparameter sets an expiration time (in seconds) for how long the shortened URL will be valid. -
Retrieving the Original URL: The
get_original_urlfunction takes the short code and looks it up in Redis to retrieve the original URL. If the short code doesn't exist (due to expiration or other reasons), it returnsNone. -
Expiration Handling: If the short code has expired, Redis automatically removes the entry, and the script will print a message indicating that the URL has expired or cannot be found.