All articles / System design
Cache Expiry vs Link Expiry: Getting a Go URL Shortener Right
A cache can save a database read. The application still has to decide whether a link is valid.
Originally published on Medium.
A short link expires at noon. At 12:00:01, should the service redirect because Redis still contains its destination? The answer should follow the link’s deadline. Whether the destination came from SQLite or a cache is an implementation detail.
That is the distinction I want to make explicit: cache retention controls how long I keep a copy; link expiry controls whether I may use it. They are related, but they answer different questions.
This Go URL shortener now keeps the absolute deadline in its cached record. Local regression tests reproduce two defects in the original handler and pass with the change. The clock-race timeline below remains illustrative; it is not a measured reproduction.
Two deadlines, two jobs
Imagine a link valid for three days. A five-minute cache TTL sends a later request back to SQLite; it does not expire the link. If the link has two seconds left, cache retention should be capped by those two seconds. Every read still needs to validate the absolute business deadline.
Redis checks timed-out keys on access. The issue here is not its background cleanup returning expired keys. It is what the application stores, what command it sends, and what it trusts on a hit. [1]
Original handler
The original grpc/server.go cached only the destination under short:<code>. A successful read returned before any business-expiry check. Original excerpt:
longURL, err := model.RDB.Get(ctx, key).Result()
if err == nil {
return &api.RedirectResponse{LongUrl: longURL}, nil
}The database path read the optional expiry, checked time.Now().After(*expireAt), and later calculated time.Until(*expireAt). Go defines Until using another clock read. [2] A normal positive TTL often worked. The boundary case was a deadline crossed between those reads.
In go-redis v9.10.0, Set adds expiry for positive durations. Zero and negative values omit it, except KeepTTL (−1 nanosecond), which emits KEEPTTL. A duration of −2 milliseconds therefore emits plain SET. [3] The sentinel and duration encoder define those special cases. Plain SET discards any previous TTL. [4] A successful write could leave a persistent raw URL with no deadline to check.
The fact that EXPIRE deletes a key for a nonpositive timeout does not change this call: the client can emit SET without an expiry option. [1]
The implemented boundary keeps both facts
model/cache.go now encodes long_url and optional expire_at as JSON. An omitted deadline means a permanent link. GetCachedURL decodes the record and checks validity after retrieval. A link is expired at its exact deadline. The predicate also rejects an empty URL:
func (r cacheRecord) validAt(now time.Time) bool {
return r.LongURL != "" && (r.ExpireAt == nil || now.Before(*r.ExpireAt))
}An expired cache record becomes a miss. The handler reads SQLite and applies the same inclusive deadline rule there. It returns gRPC NotFound if the database record is expired. This preserves the database as the fallback authority.
Implemented request path
Decode the cached URL and absolute deadline.
Return the URL only when it is nonempty and the deadline is still in the future, or absent.
Missing, failed, malformed, empty, or expired cache records all take this path.
Return NotFound. At the exact deadline, the link is expired.
Attempt a bounded cache write, then return the URL. A failed cache write does not fail the valid redirect.
CacheURL uses a five-minute retention cap, limited by remaining link lifetime. Permanent links keep that retention policy without acquiring a business expiry. The actual TTL calculation is:
func cacheTTL(expireAt *time.Time, now time.Time) time.Duration {
if expireAt == nil {
return cacheRetention
}
// Round down so Redis's millisecond precision cannot extend the deadline.
return min(cacheRetention, expireAt.Sub(now)).Truncate(time.Millisecond)
}A focused test caught why the truncation matters: go-redis turns a positive duration below one millisecond into PX 1. [5] The code now rounds down to milliseconds, then skips SET when the result is nonpositive. At 500 microseconds remaining it does not cache; at 1.5 milliseconds it retains for one millisecond.
That cap controls retention. Network delays and process pauses can still separate TTL calculation from the write. The stored absolute deadline is checked on a later hit, so retention cannot establish business validity by itself.
Changing the cache format safely
New records use short:v2:<code>. The updated code never interprets legacy short:<code> raw strings as JSON. Old entries remain untouched, including any without a TTL. Updated binaries start with a cold namespace, which can temporarily increase database reads. Older binaries retain their original behavior until upgraded.
At request time, a nil Redis client, read failure, or malformed record falls back to SQLite. Read errors other than misses and cache-write failures remain logged. A valid database result can still be returned after a failed cache write.
Startup is a separate limit: unchanged InitRedis still exits when its ping fails. The shipped binary therefore still requires Redis at startup. This cache fix does not change the public REST/gRPC contracts, protobufs, SQLite schema, or runtime dependencies.
What the tests established
The regression setup uses temporary SQLite databases and a go-redis process hook. It executes the real client command builder without a Redis server. The local regression runs established:
- Original handler:
TestRedirectShortener_WithoutRedispanicked on the nil client;TestRedirectShortener_ExpiredWarmCachereturnedOKinstead ofNotFound. - Before truncation: the submillisecond write test captured
PX 1when it expected no write. The fixed boundary tests pass. - Final full run: 14 top-level tests, 46 including subtests, passed; zero failed or skipped test cases. Three packages have no test files.
go test -race ./... -count=1,go vet ./..., and the changed Go files’ formatting checks passed.
The tests also cover exact-deadline equality in validAt, valid and permanent warm/cold records, bounded positive TTL arguments, missing database rows, malformed records, and cache read/write errors. No test relies on sleeps.
Run go test -json ./... -count=1 to check the full suite. The cache tests and redirect tests are included in the merged change.
The useful rule is simple: cache the facts needed to validate a result, and enforce that rule on every read path.