0
0
Redisquery~5 mins

Key naming conventions (colons for namespacing) in Redis

Choose your learning style9 modes available
Introduction

Using colons in Redis keys helps organize data clearly. It groups related keys like folders on a computer.

When storing user data like profiles and settings separately but related.
When managing different parts of an application like sessions and caches.
When you want to quickly find or delete groups of keys together.
When you want to avoid key name conflicts in a shared Redis database.
Syntax
Redis
namespace:subnamespace:keyname

Colons separate parts of the key to show hierarchy.

Each part can be a word or identifier to describe the data.

Examples
This key stores the profile data for user with ID 1001.
Redis
user:1001:profile
This key holds session data for session ID abcd1234.
Redis
session:abcd1234:data
This key stores cached HTML for the homepage.
Redis
cache:homepage:html
Sample Program

We save a user's profile using a key with colons to show it belongs to user 42. Then we get the profile back.

Redis
SET user:42:profile "{\"name\":\"Alice\", \"age\":30}"
GET user:42:profile
OutputSuccess
Important Notes

Use colons only as separators, avoid spaces or special characters in keys.

Keep key parts meaningful and consistent for easier maintenance.

Summary

Colons help organize Redis keys like folders in a file system.

They make it easier to find and manage related data.

Use clear, consistent naming to avoid confusion.