{"id":26910209,"url":"https://github.com/zhima-mochi/linkzapurl","last_synced_at":"2026-04-12T09:39:06.980Z","repository":{"id":218882177,"uuid":"745438955","full_name":"Zhima-Mochi/linkZapURL","owner":"Zhima-Mochi","description":"System design for URL shortener service.","archived":false,"fork":false,"pushed_at":"2024-01-26T13:26:45.000Z","size":675,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-06-18T08:51:47.119Z","etag":null,"topics":["docker","docker-compose","golang","mongodb-sharding","nginx","redis-cluster","system-design"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Zhima-Mochi.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-01-19T10:37:07.000Z","updated_at":"2024-04-23T14:19:42.000Z","dependencies_parsed_at":"2024-01-26T14:30:24.357Z","dependency_job_id":"5e5ac18a-fcba-4c2b-b299-d5f5b06db2c4","html_url":"https://github.com/Zhima-Mochi/linkZapURL","commit_stats":null,"previous_names":["zhima-mochi/linkzapurl"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/Zhima-Mochi/linkZapURL","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Zhima-Mochi%2FlinkZapURL","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Zhima-Mochi%2FlinkZapURL/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Zhima-Mochi%2FlinkZapURL/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Zhima-Mochi%2FlinkZapURL/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Zhima-Mochi","download_url":"https://codeload.github.com/Zhima-Mochi/linkZapURL/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Zhima-Mochi%2FlinkZapURL/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":267316373,"owners_count":24068222,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-07-27T02:00:11.917Z","response_time":82,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["docker","docker-compose","golang","mongodb-sharding","nginx","redis-cluster","system-design"],"created_at":"2025-04-01T13:31:45.084Z","updated_at":"2026-04-12T09:39:01.959Z","avatar_url":"https://github.com/Zhima-Mochi.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# linkZapURL - System Design for URL Shortener Service\n\n## Table of Contents\n- [Introduction](#introduction)\n- [Features](#features)\n- [Architecture](#architecture)\n  - [High Level Design](#high-level-design)\n  - [Final Architecture](#final-architecture)\n- [API Sequence Diagram](#api-sequence-diagram)\n    - [Shortening Service](#shortening-service)\n    - [Redirection Service](#redirection-service)\n- [Tech Stack](#tech-stack)\n- [Run](#run)\n- [Endpoints](#endpoints)\n  - [Swagger](#swagger)\n  - [linkZapURL Service](#linkzapurl-service)\n\n## Introduction\nlinkZapURL is a URL shortening service. It is a simple service that takes a long URL and returns a short URL. When a user visits the short URL, it will redirect to the long URL.\n\n## Features\n- Horizontal scalability (sharding, unique ID generator)\n- Snowflake ID (timestamp + machineID + sequence) as unique ID\n- Cache (including non-exist codes' requests)\n- base58 encoding\n\n## Architecture\n\n### High Level Design\n![High Level Design](./docs/design/High_Level_Design.jpeg)\n\n### MongoDB Schema Design for URL Collection\n\nEach document in the `url` collection represents a tiny URL entity, structured as follows:\n\n```bash\n{\n  \"ID\": int64,          // Unique ID generated by Snowflake algorithm\n  \"shardID\": int64,     // Derived from `ID`, used for sharding\n  \"url\": string,        // Actual URL\n  \"expireAt\": int64     // Expiration timestamp\n}\n```\nUnique index: (`shardID`, `ID`).\n\n### Redis key-value Design for Cache\n\nRedis is used to cache the tiny URL entity. The key is the encoded `ID` of the entity, and the value is the JSON string of the entity (URL and expiration timestamp).\n\n### Base58 Encoding Functions\n\n#### `Encode(num int64) string`\n- Converts a 64-bit integer to a base58 encoded string.\n- Ensures output is up to 7 characters long.\n\n#### `Decode(code string) int64`\n- Converts a base58 encoded string (7 characters) back to a 64-bit integer.\n\n\n### Final Architecture\nAll of the services are deployed in local machine for demonstration purpose. In production, the services should be deployed in different physical or virtual machines.\n\n![Final Design](./docs/diagrams/linkzapurl_architecture.png)\n\n## API Sequence Diagram\n### Shortening Service\n\n![Shortening Service Sequence Diagram](./docs/design/Shortening%20Service/sequentialDiagram.jpeg)\n\n### Redirection Service\n\n![Redirection Service Sequence Diagram](./docs/design/Redirection%20Service/sequentialDiagram.jpeg)\n\n## Tech Stack\n- [MongoDB](https://www.mongodb.com/): database, shard\n- [Redis](https://redis.io/): cache, cluster\n- [Docker](https://www.docker.com/): container\n- [Docker Compose](https://docs.docker.com/compose/): container orchestration\n- [gin](https://github.com/gin-gonic/gin)\n- [nginx](https://www.nginx.com/): reverse proxy\n\n## Run\nMy environment: WSL2\n```bash\n# Start MongoDB\ncd ./docker/mongodb \u0026\u0026 ./init.sh \u0026\u0026 cd ../..\n# Start Redis\ncd ./docker/redis \u0026\u0026 ./init.sh \u0026\u0026 cd ../..\n# Start Nginx\ncd ./docker/nginx \u0026\u0026 ./init.sh \u0026\u0026 cd ../..\n# Start Service\n./init.sh\n# Host: http://localhost\n```\n\n### Configurations\nSee [config.yaml](./docker/config.yaml).\n\n## Endpoints\n\n### [Swagger](./docs/swagger.json)\n- `GET /swagger/index.html`\n\n### linkZapURL Service\n- `POST /api/v1/shorten`\n\n    Request Body:\n    ```bash\n    {\n        \"url\": \"\u003coriginal_url\u003e\",\n        \"expireAt\": \"\u003cexpired_time\u003e\" // format: RFC3339\n    }\n    ```\n    \n    Response Body:\n    ```bash\n    {\n        \"id\": \"\u003ccode\u003e\",\n        \"shortUrl\": \"http://localhost/\u003ccode\u003e\"\n    }\n    ```\n- `GET /:code`\n\n   Redirect to the original URL\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzhima-mochi%2Flinkzapurl","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fzhima-mochi%2Flinkzapurl","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzhima-mochi%2Flinkzapurl/lists"}