spencerkinney / go-url-shortener

simple URL shortener in Go for the purpose of learning Go
3 stars 2 forks source link

go-url-shortener

A simple URL shortener written in Go for the purpose of learning Go.

Purpose

This project is just for learning Go and an attempt at creating an ultra-fast URL shortener. The goal is to programmatically generate 10k+ shortened URLs per second (probably more), exploring a bit with Go's concurrency model and standard lib. Feel free to steal any code here or contribute whatever you think would be cool. I will be hosting this project on the domain 301.li.

Use Cases?

Q&A

Q: Why?

A: Why tf not?

Production Goals

I want to create a production-ready service that is free for anyone to use, incorporating:

Installation

# Clone the repository
git clone https://github.com/spencerkinney/go-url-shortener.git
# Navigate to project directory
cd url-shortener
# Build the project
go build
# Run the server
./go-url-shortener


Example Usage

Shorten a URL

curl -X POST -H "Content-Type: application/json" -d "{\"url\": \"https://www.example.com\"}" http://localhost:8080/shorten

Example Response:

{
  "shortUrl": "http://localhost:8080/NHMJWL"
}

Access a Shortened URL

Visit http://localhost:8080/NHMJWL in your web browser, or use curl to be redirected to the original URL.


URL Key Space

The short URL key is a 6-char string of uppercase letters and numbers, there are 36 possible characters for each position. With 6 characters in the key, this allows for 366 (2,176,782,336) unique combinations, making the probability of collisions very low with a moderate number of URLs. I'll prob later add proper collisions handling but for now let's just say its unlikely. This is what I'll currently use but might change to 8-char strings later.

Initial Benchmarks

Just got the initial code running and here are the initial performance numbers. It's a start, but honestly...

Total time for 10000 requests with 100 concurrency: 1.2382327s
Average request time: 12.311669ms
Median request time: 12.0107ms
Requests per second: 8076.026421
Min request time: < 1ns Max request time: 27.7081ms

...zzzz, this is SLOW. I'll make some changes later to make some easy perf enhancements. this was just the original code to get the thing working. 26ms max is EMBARRASSING. i'll fix later. I also need to check over my loadtest code to make sure it is properly and accurately measuring durations. as some point, I'll set up a proper benchmarking tool. btw this was ran on a AMD Ryzen 7 1700.