Creating a simple tiny URL generator using Golang, PostgreSQL and Redis

“programming language illustration” by Christopher Robin Ebbinghaus on Unsplash
  1. PostgreSQL: I assume that PostgreSQL DB is already setup and configured to accept external connections. The steps to download and install it can be found here. Once setup, please create a database with a name of your choice and keep the hostname, port, username, password and the database name handy. I would be using tiny_scale_go as my database name in this article.
  2. Redis: We will be using Redis for caching our URLs generated for faster access. I assume Redis server is setup and running. The steps to install the same can be found here. Once setup, please keep the hostname, port and password handy.
  3. Text editor: Any popular text editor can be used. Most of them will have a plugin for Golang, so please install it as it will aid greatly in the process of writing code.
export GOBIN=$GOPATH/binexport PATH=$PATH:$(go env GOPATH)/bin
go get -u get -u
postgres", "host= port=5432 user=postgres dbname=tiny_scale_go password=<db password> sslmode=disable
Addr: "localhost:6379",
Password: "",
DB: 0,
  • We encode it into a base64 string to remove any special characters which may interfere with the operation of the URL. After encoding, as it might still contain + and / characters, we replace them with _
  • Once the string is generated, we extract first 6 characters of it to keep it short.
  • We initially check for the presence of the tinyURL in the DB for a different long URL. If its present, we recursively increase the start index for fetching the 6 characters until there is no conflict.
<domain>/tiny/?longUrl=<long URL>
<domain>/long/?tinyUrl=<tiny URL>



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Mahesh H P

Mahesh H P

Full stack developer | Occasional Blogger | Love ☕ | Opinions here are my own |