URL Shortener: Trade-Offs and Wrap-Up
Trade-offs are the gains and losses that come with each design choice. This lesson weighs the trade-offs and wraps up the URL shortener, the last step of the method.
Links that end
Link expiry gives each link an end day in its record. The printout shows a link past its end day refusing to redirect, while later end days still work.
records = {
"qi": ("https://example.com/a-very-long-article", 12),
"qj": ("https://example.com/another-long-page", 5),
"qk": ("https://example.com/a-third-long-page", 20),
}
today = 10
print("today is day", today)
for code in ["qi", "qj", "qk"]:
long_url, end_day = records[code]
if today < end_day:
print(code, "still works, ends on day", end_day)
else:
print(code, "has ended, ended on day", end_day)Counting visits under each status
A browser may keep a 301 answer and reuse it without asking. A 302 answer is asked for again, so the service sees and counts every visit.
def counted_visits(status, visits):
service_calls = 0
browser_has_copy = False
for i in range(visits):
if status == 301 and browser_has_copy:
continue
service_calls = service_calls + 1
if status == 301:
browser_has_copy = True
return service_calls
print("visits made: 4")
print("status 301, visits the service sees:", counted_visits(301, 4))
print("status 302, visits the service sees:", counted_visits(302, 4))Blocking bad codes
A block list stops known bad codes before any redirect is sent. The printout shows a blocked code refused while a normal code still redirects.
store = {"qi": "https://example.com/a-very-long-article", "qj": "https://example.com/another-long-page"}
blocked = {"bad1"}
def redirect(code):
if code in blocked:
return "blocked"
if code in store:
return "redirect to " + store[code]
return "not found"
for code in ["qi", "bad1", "qj"]:
print(code, "->", redirect(code))Example
decisions = [
("code length", "7 characters"),
("code source", "a counter written in base62"),
("redirect status", "302, so visits stay countable"),
("reads", "a cache in front of the database"),
("link ends", "an end day stored in each record"),
("bad links", "a block list checked before redirecting"),
]
for area, choice in decisions:
print(area, ":", choice)
print("decisions listed:", len(decisions))Key points
- Link expiry: a rule that ends a short link after a set time or on a set date.
- Expired link: a link past its end, which no longer redirects.
- Abuse: short links can hide a harmful destination, so known bad codes go on a block list.
- Analytics: a count of visits for each code, which link owners use to see how a link performs.
- Status 301 trade-off: browsers may reuse a permanent redirect, so repeat visits skip the service and stay uncounted.
- Status 302 trade-off: browsers ask again each time, so visits stay countable and the service carries every visit.
- Counter trade-off: counter codes never collide, but the next code is easy to guess.
In this example, the wrap-up lists the main decisions of the whole shortener design. The design stays simple, and each choice names what it gains and what it gives up.
Note
stopping one client from calling too often is a different problem, rate limiting, and it is the next problem in this course.
Try it Yourself
Edit the code, then press Run.
Running is simulated
Exercise
A link owner needs an exact count of every visit. Which redirect status fits?