DEV Community

Matt Kenefick
Matt Kenefick

Posted on

Reminder: Surrogate keys should also represent query params

Short diary entry about cache keys:

I've run into a scenario where I'm unable to bust cache on an API endpoint that returns an empty value.

For pages that need to be busted with new items, it's easy enough to add a "Prefix-N" or "Prefix-." representing that it can be busted for something *N*ew.

For pages that are built by existing models, you can iterate through their IDs and append "Prefix-{id}" which will bust cache if it's been edited or removed.

The scenario I ran into was checking for a specific ID which didn't exist, like:

api.example.com/followers?entity_id=10
Enter fullscreen mode Exit fullscreen mode

If there were no results, the key you're searching for wouldn't be represented in the surrogate keys. Even after you DO add the entry, it won't find the key to purge and so it stays cached.

This hadn't been an issue up until now, but it's making me rethink my caching/key strategy. I'll have to take into account items I'm looking for, regardless if they exist. Because of that, I have to think if this is something that should be a global strategy or if it's scenario-based.

Top comments (0)