Compares two Redis modeling strategies for profile data: storing the full object as a JSON string versus storing fields in a Redis hash. Useful for learning trade-offs between whole-object reads and field-based access.
Source: ../src/jsonVsHash.js
| Strategy | Key | Trade-off |
|---|---|---|
| JSON string | user:<id>:json |
Simple whole-object read/write; must fetch and parse the entire object to read one field |
| Hash | user:<id>:hash |
Field-level access and updates; values are stored as strings |
Store the full profile as a JSON string.
- Body:
{
"name": "Alice",
"email": "alice@example.com",
"age": 25
}- Response:
{
"savedAs": "json"
}Fetch and parse the JSON profile.
- Response:
{
"user": {
"name": "Alice",
"email": "alice@example.com",
"age": 25
}
}Store the profile as a Redis hash.
- Body:
{
"name": "Alice",
"email": "alice@example.com",
"age": "25"
}- Response:
{
"savedAs": "hash"
}Fetch all hash fields (values are returned as strings).
- Response:
{
"user": {
"name": "Alice",
"email": "alice@example.com",
"age": "25"
}
}Save user as JSON:
curl -X POST http://localhost:3000/user/101/json \
-H "Content-Type: application/json" \
-d '{"name":"Alice","email":"alice@example.com","age":25}'Get user from JSON:
curl http://localhost:3000/user/101/jsonSave user as Hash:
curl -X POST http://localhost:3000/user/101/hash \
-H "Content-Type: application/json" \
-d '{"name":"Alice","email":"alice@example.com","age":"25"}'Get user from Hash:
curl http://localhost:3000/user/101/hashSET/GET- store and read the JSON stringHSET- store hash fieldsHGETALL- read all hash fields