<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Dinithi Pramodya</title>
    <description>The latest articles on DEV Community by Dinithi Pramodya (@dinithi_pramodya_ad93a533).</description>
    <link>https://dev.to/dinithi_pramodya_ad93a533</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3602702%2F037929a1-e8cd-4790-93eb-4464f8970509.jpg</url>
      <title>DEV Community: Dinithi Pramodya</title>
      <link>https://dev.to/dinithi_pramodya_ad93a533</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/dinithi_pramodya_ad93a533"/>
    <language>en</language>
    <item>
      <title>Building a Simple URL Shortener with Go and Redis</title>
      <dc:creator>Dinithi Pramodya</dc:creator>
      <pubDate>Tue, 18 Nov 2025 14:11:11 +0000</pubDate>
      <link>https://dev.to/dinithi_pramodya_ad93a533/building-a-simple-url-shortener-with-go-and-redis-1i62</link>
      <guid>https://dev.to/dinithi_pramodya_ad93a533/building-a-simple-url-shortener-with-go-and-redis-1i62</guid>
      <description>&lt;p&gt;URL shorteners are everywhere, from social media to analytics platforms. They look simple, but under the hood, they require careful thought: idempotency, collision management, fast lookups, and clean API design.&lt;/p&gt;

&lt;p&gt;I recently completed the &lt;strong&gt;URL Shortener&lt;/strong&gt; challenge from &lt;a href="https://codingchallenges.fyi/challenges/challenge-url-shortener" rel="noopener noreferrer"&gt;Coding Challenges&lt;/a&gt;, and although it seems like a tiny project on the surface, it turned into a rich learning experience.&lt;/p&gt;

&lt;p&gt;In this article, I walk you through how I built a lightweight, fast, and reliable URL Shortener using:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Go &lt;/li&gt;
&lt;li&gt;Redis(Memurai on Windows)&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Why I Chose Go + Redis
&lt;/h2&gt;

&lt;p&gt;I built the project using:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Go for its simplicity, speed, and clean standard library&lt;/li&gt;
&lt;li&gt;Redis as an in-memory key–value store for O(1) lookups&lt;/li&gt;
&lt;li&gt;A very small project structure with separate handlers and utilities&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What the Service Does
&lt;/h2&gt;

&lt;p&gt;The service exposes two endpoints:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;POST /shorten&lt;/code&gt;&lt;br&gt;
Accepts a long URL and returns a short one.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;GET /{shortCode}&lt;/code&gt;&lt;br&gt;
Redirects the user to the original URL.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The project structure looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;url-shortner/
├─ config/
│  └─ redis.go
├─ handlers/
│  ├─ shorten.go
│  └─ redirect.go
├─ utils/
│  └─ hash.go
├─ main.go
├─ go.mod
└─ go.sum
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  How It Works
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. Shortening URLs
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;ShortenHandler&lt;/code&gt;in &lt;code&gt;handlers/shorten.go&lt;/code&gt; handles POST requests to &lt;code&gt;/shorten&lt;/code&gt;. The workflow is as follows:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Receive request&lt;/strong&gt; – The API expects a JSON payload with the &lt;code&gt;URL&lt;/code&gt; field.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Check for existing mapping&lt;/strong&gt; – If the long URL already exists in Redis, the same short URL is returned (ensuring idempotency).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Generate a shortcode&lt;/strong&gt; – A random alphanumeric code of 6 characters is generated. If a collision occurs, a salted SHA1 hash ensures uniqueness.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Store mappings&lt;/strong&gt; – The short code maps to the long URL, and a reverse mapping is also stored for idempotency.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Example request:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;POST /shorten
{
  "url": "https://example.com/very-long-url"
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Response:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "short_url": "http://localhost:8080/aB9xYz"
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  2. Redirecting Short URLs
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;RedirectHandler&lt;/code&gt;in &lt;code&gt;handlers/redirect.go&lt;/code&gt; handles GET requests for short URLs:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Extract the short code from the request path.&lt;/li&gt;
&lt;li&gt;Look up the original URL in Redis.&lt;/li&gt;
&lt;li&gt;Redirect the client to the long URL using HTTP status &lt;code&gt;302 Found&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  3. Generating Short Codes
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;utils/hash.go&lt;/code&gt; provides two functions:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;GenerateShortCode(n int) – Generates a random alphanumeric code.&lt;/li&gt;
&lt;li&gt;GenerateShortCodeWithSalt(url string) – Uses SHA1 hashing and the current timestamp as a salt to avoid collisions in case of duplicates.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Redis Integration
&lt;/h4&gt;

&lt;p&gt;&lt;code&gt;config/redis.go&lt;/code&gt; sets up the Redis client:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Connects to Redis at &lt;code&gt;localhost:6379.&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Pings the server to ensure connectivity.&lt;/li&gt;
&lt;li&gt;Provides a global &lt;code&gt;RedisClient&lt;/code&gt;and &lt;code&gt;Ctx&lt;/code&gt;for use across the project.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Redis stores&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;shortCode&lt;/strong&gt; → &lt;strong&gt;longURL&lt;/strong&gt;(for redirecting)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;longURL&lt;/strong&gt; → &lt;strong&gt;shortCode&lt;/strong&gt; (for idempotent shortening)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here’s the &lt;a href="https://github.com/DinithiPramodya/url-shortner" rel="noopener noreferrer"&gt;full implementation on GitHub&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Key Features
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Idempotent URL shortening – Multiple requests for the same long URL return the same short URL.&lt;/li&gt;
&lt;li&gt;Collision handling – Ensures unique short codes.&lt;/li&gt;
&lt;li&gt;Fast in-memory storage – Using Redis for near-instant lookups.&lt;/li&gt;
&lt;li&gt;Simple API – Minimalist design for ease of use.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Future Enhancements
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Add analytics to track URL clicks.&lt;/li&gt;
&lt;li&gt;Implement custom short codes for user-defined aliases.&lt;/li&gt;
&lt;li&gt;Add expiration time for temporary links.&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>go</category>
      <category>redis</category>
    </item>
  </channel>
</rss>
