B1officesNote11 February 20262-minute read

Name your Redis keys like someone else will read them

A Redis key is the only documentation most data in Redis will ever get. Open redis-cli on a busy server at the wrong moment and the key names are all you have.

The rate limiter in the firewall I built writes keys like this:

rate_limit:{product}:{action}:{rule}:{type}:{subject hash}:{window}

Broad to narrow, left to right. That's what makes SCAN useful: rate_limit:* is everything the limiter owns, and adding the product narrows it to one product's counters.

The subject is hashed. It's a phone number or an email address, and a key name is about as private as a log line. A hash keeps the key stable and the personal data out of it.

A missing part gets a placeholder, default, rather than an empty segment, so every part keeps its position and a pattern written for one key works for all of them.

The key also decides who shares a budget. Put the action in it, and sending a code and resending one get separate limits. Leave it out, and they share one. That's a product decision hiding in a string, so make it on purpose.

Two things will still surprise you. Libraries add keys of their own: Laravel's rate limiter keeps a :timer key next to every counter. And prefixes stack: the cache prefix goes in front of yours, so the key you search for isn't quite the key that's there.

One last thing, from experience: be careful what a "clear" command clears. FLUSHALL doesn't empty your keys, it empties the whole instance, queues and sessions included.

Ring the bell

Got a question one of these didn't answer? Ask me directly.

Get in touch