DEV Community

Thái Lê Trí
Thái Lê Trí

Posted on

How to build TinyURL yourself

I create an side project to implement TinyURL on my own with freedom in my technology choice.

From draft version to final version, performance proved to able to handle
21k read request per sec and
14k create request per sec with single instance.

Functional Requirement (* - Mandatory)

  1. Create unique shorten-url from an alias creation request
  2. Redirect the access from shortenned-url to target URL

Non-Functional Requirement (Not Mandatory, freedom part)
Note: In this project, I try to test some technology to make it fun, fast, stable, durable, and scalable

  1. Able to handle thousands create request per second for single instance
  2. Able to handle thousands access request per second for single instance
  3. Able to fast, and effortless detect invalid access request
  4. Create a performance test for prove the system durable, realiable, and scalable

First version:

  • Simple spring boot with mysql, support two api:
  • - POST /api/alias/create
  • - GET /{alias}
  • which are satisfy the functional requirement stated above.
  • Implement NanoId for create random alias
  • Implement Snowflake ID generation and Base58 convertion from SnowflakeID to character for newly created alias

Second version:
Add primary cache

  • using LRUCache (capacity 1000) implements from HashMap internally an instance, for fastest accessible
  • Add secondary cache using Redis with timetolive 1 day for durable and scalable cache support

Third version:

  • Implement bloomfilter base on Redis version 8.x for fast no-exist alias check.
  • Implement write-back every 2 sec for caching created request, empower the capacity for fast creation alias.
  • Implement hibernate batch create, update, sequence allocation for fast saving/persisting

Final version:
Add Grafana, Prometheous, Mysql-Exporter, Redis-Exporter for monitoring
Add K6 script for complex performance test scenario
Add WRK script for extreme performance test scenario

Result:

Create 14k per sec

Get 21k per sec

GIT: https://github.com/thailt/shorten-url

Top comments (0)