目录

Short URL Service

A high-performance short URL service built with Go, Gin, and gokv2.

Features

  • Create, read, update, delete short URLs
  • Click tracking and statistics
  • Expiration management
  • High-performance caching:
    • L1 Local Cache (in-memory)
    • Bloom Filter for fast existence checks
    • Block Cache for data blocks
    • Async write queue for write optimization

Tech Stack

Backend

  • Go 1.25+
  • Gin (HTTP framework)
  • gokv2 (KV store)

Frontend

  • React 19
  • Next.js 16 (via vinext)
  • Bootstrap 5

Project Structure

shorturl/
├── main.go                 # Entry point
├── go.mod                  # Go module
├── go.sum                  # Go dependencies
├── internal/
│   ├── handler/            # HTTP handlers
│   │   └── handler.go
│   └── store/              # Data layer
│       └── store.go        # L1 Cache, Bloom Filter, Async Writer
└── frontend/               # Frontend app
    ├── app/
    │   ├── layout.tsx
    │   └── page.tsx
    ├── vite.config.ts
    ├── package.json
    └── tsconfig.json

Getting Started

Prerequisites

  • Go 1.25+
  • Node.js 18+
  • pnpm (recommended) or npm

Backend

cd shorturl
go mod tidy
go run main.go

The server will start on http://localhost:8080

Frontend

cd shorturl/frontend
pnpm install
pnpm dev

The frontend will start on http://localhost:5173

API Endpoints

Method Endpoint Description
POST /api/shorten Create short URL
GET /api/shorten List all URLs
GET /api/shorten/:code Get URL details
PUT /api/shorten/:id Update URL
DELETE /api/shorten/:code Delete URL
GET /:code Redirect to original URL

Create Short URL

curl -X POST http://localhost:8080/api/shorten \
  -H "Content-Type: application/json" \
  -d '{"original": "https://example.com/very/long/url", "expire_days": 30}'

Redirect

Access http://localhost:8080/{code} to be redirected to the original URL.

Performance Optimization

Read Path

  1. Bloom Filter check (fast path for non-existent keys)
  2. L1 Local Cache (in-memory, 10k items)
  3. gokv2 (KV store)

Write Path

  1. Write to async channel queue
  2. Worker goroutines batch write to gokv2
  3. Update Bloom Filter and Cache

Bloom Filter

  • Size: 1M bits
  • Hash functions: 3
  • False negatives: 0%
  • False positives: <1%

Async Writer

  • Queue size: 10k
  • Worker count: 3
  • Provides write buffering and peak clipping

License

MIT

关于
58.0 KB
邀请码
    Gitlink(确实开源)
  • 加入我们
  • 官网邮箱:gitlink@ccf.org.cn
  • QQ群
  • QQ群
  • 公众号
  • 公众号

版权所有:中国计算机学会技术支持:开源发展技术委员会
京ICP备13000930号-9 京公网安备 11010802032778号