Menu

Why Is Redis INCR a Bad Fit for a Public URL Shortener?
πŸ“°
0

Why Is Redis INCR a Bad Fit for a Public URL Shortener?

DEV Community: redisΒ·LeetDezineΒ·about 1 month ago
#39tTK1y4
#dev#code#highlight#redis#class#incr
Reading 0:00
15s threshold

LeetDezine Redis INCR is one of those solutions that looks perfect the first time you see it. Atomic counter increments. Every call returns a unique integer. Base62-encode it and you have a short code β€” zero collision checks, zero retries, no background service. It's cleaner than anything else on the board. So why does every serious URL shortener reject it? The answer has nothing to do with code generation. How Redis INCR Works (And Why It's Technically Correct) The mechanics are clean: Creation request arrives β†’ Redis: INCR url_counter β†’ returns 1000000 β†’ Base62 encode 1000000: Divide repeatedly, collect remainders, stop when quotient = 0: 1000000 Γ· 62 = 16129 remainder 22 β†’ 'M' (quotient != 0, keep going) 16129 Γ· 62 = 260 remainder 9 β†’ '9' (quotient != 0, keep going) 260 Γ· 62 = 4 remainder 12 β†’ 'C' (quotient != 0, keep going) 4 Γ· 62 = 0 remainder 4 β†’ '4' (quotient = 0, stop) Read remainders bottom to top: "4C9M" β†’ pad to 6 chars β†’ "004C9M" β†’ INSERT short_code = "004C9M" β†’ Done. Redis is single-threaded.…

Continue reading β€” create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.

Read More