System design URL shortener: Spring Boot 4.1.1, base62 ids, Redis cache-aside, Redis Lua token bucket, 24 tests and live transcripts
This commit is contained in:
Executable
+49
@@ -0,0 +1,49 @@
|
||||
#!/usr/bin/env bash
|
||||
# Starts its own instances (ports 8082 and 8083) against the same PostgreSQL + Redis, so it needs the packaged jar:
|
||||
# mvn -DskipTests package
|
||||
# Shows (07) block allocation across a restart and (08) the snowflake profile's longer codes.
|
||||
set -uo pipefail
|
||||
cd "$(dirname "$0")/.."
|
||||
JAR=target/system-design-url-shortener-1.0.0.jar
|
||||
PSQL=${PSQL:-"psql -h /tmp -p 5433 -U pgrun -d urlshort -X -q"}
|
||||
mkdir -p output
|
||||
|
||||
start() { # start PORT [extra args] ; waits until it answers
|
||||
local port=$1; shift
|
||||
java -Duser.timezone=UTC -jar $JAR --server.port=$port "$@" > /tmp/urlshort-$port.log 2>&1 &
|
||||
echo $! > /tmp/urlshort-$port.pid
|
||||
for _ in $(seq 1 60); do curl -s -o /dev/null http://127.0.0.1:$port/api/v1/debug/cache-stats && return 0; sleep 0.5; done
|
||||
echo "instance on $port did not start" >&2; return 1
|
||||
}
|
||||
stop() { kill "$(cat /tmp/urlshort-$1.pid)" 2>/dev/null; wait "$(cat /tmp/urlshort-$1.pid)" 2>/dev/null; }
|
||||
create() { # create PORT URL -> prints the code
|
||||
redis-cli --raw DEL rl:127.0.0.1 > /dev/null
|
||||
curl -s -X POST -H 'Content-Type: application/json' -d "{\"url\":\"$2\"}" http://127.0.0.1:$1/api/v1/urls | sed -n 's/.*"code":"\([^"]*\)".*/\1/p'
|
||||
}
|
||||
|
||||
{
|
||||
$PSQL -c "TRUNCATE urls" -c "ALTER SEQUENCE url_id_seq RESTART WITH 1"
|
||||
start 8082
|
||||
echo "--- instance A: three links"
|
||||
for n in 1 2 3; do create 8082 "https://example.com/a$n" > /dev/null; done
|
||||
$PSQL -c "SELECT id, code, length(code) AS len FROM urls ORDER BY id"
|
||||
stop 8082
|
||||
echo "--- instance A stopped and restarted (ids 4..100 of its first block are never used)"
|
||||
start 8082
|
||||
create 8082 "https://example.com/after-restart" > /dev/null
|
||||
$PSQL -c "SELECT id, code, length(code) AS len FROM urls ORDER BY id"
|
||||
echo "--- two instances running at once never overlap: B starts while A is alive"
|
||||
start 8083
|
||||
create 8083 "https://example.com/from-b" > /dev/null
|
||||
create 8082 "https://example.com/from-a" > /dev/null
|
||||
$PSQL -c "SELECT id, code FROM urls WHERE long_url IN ('https://example.com/from-b','https://example.com/from-a') ORDER BY id"
|
||||
stop 8083; stop 8082
|
||||
} > output/07-id-generation.txt 2>&1
|
||||
|
||||
{
|
||||
start 8083 --spring.profiles.active=snowflake --app.node-id=7
|
||||
for n in 1 2 3; do c=$(create 8083 "https://example.com/snow$n"); echo "code=$c length=${#c}"; done
|
||||
$PSQL -c "SELECT id, code FROM urls WHERE long_url LIKE 'https://example.com/snow%' ORDER BY id"
|
||||
stop 8083
|
||||
} > output/08-snowflake.txt 2>&1
|
||||
echo "transcripts 07 and 08 written to output/"
|
||||
Reference in New Issue
Block a user