Skip to content

Commit 2a7c059

Browse files
authored
Adding new tutorial on Redis use cases
1 parent c5f59c7 commit 2a7c059

File tree

3 files changed

+391
-0
lines changed

3 files changed

+391
-0
lines changed

src/manifest.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,16 @@
77
"initialIsOpen": true
88
},
99
"children": [
10+
{
11+
"type": "internal-link",
12+
"id": "redis_use_cases",
13+
"label": "Redis Use Cases",
14+
"summary": "A collection of several common use cases for Redis.",
15+
"args": {
16+
"initialIsOpen": true,
17+
"path": "/uc/intro.md"
18+
}
19+
},
1020
{
1121
"type": "group",
1222
"id": "ds",

src/uc/intro.md

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
**Redis** is a powerful in-memory data structure store that can be used for various use cases due to its speed, simplicity, and versatility. In this tutorial, we'll explore several common use cases: matchmaking, location-based search, job queue, leaderboard, and session store.
2+
3+
If you haven't done so already, you can upload the sample data related to this tutorial by clicking the button below. You will also need support for [JSON](https://redis.io/docs/latest/develop/data-types/json/) and [Search & query](https://redis.io/docs/latest/develop/interact/search-and-query/) in your database to take advantage of this tutorial fully.
4+
5+
```redis-upload:[/uc/sample_data.txt] Upload Sample Data
6+
```
7+
8+
### Matchmaking
9+
10+
Redis serves as a powerful document store, well-suited for matchmaking use cases. For instance, consider a bike shop where you need to pair shoppers with bikes based on their preferences and budget.
11+
12+
You can store your bike inventory using the JSON data structure, where bikes have attributes such as type (e.g., mountain, road, electric), condition (new or used), price, etc.
13+
14+
```redis:[run_confirmation=true]
15+
// Add a bike as JSON
16+
JSON.SET sample_bicycle:2048 $ '{
17+
"model": "Ranger",
18+
"brand": "TrailBlazer",
19+
"price": 450,
20+
"type": "Mountain",
21+
"specs": {
22+
"material": "carbon",
23+
"weight": 12
24+
},
25+
"description": "The Ranger is designed for rugged trails and adventurous rides!",
26+
"addons": [
27+
"water bottle holder",
28+
"rear rack"
29+
],
30+
"helmet_included": false
31+
}'
32+
```
33+
34+
In Redis, you can easily index these attributes and perform complex queries efficiently.
35+
36+
```redis:[run_confirmation=true]
37+
// Create a secondary index of your bike data
38+
FT.CREATE idx:smpl_bicycle
39+
ON JSON
40+
PREFIX 1 sample_bicycle:
41+
SCHEMA
42+
$.brand AS brand TEXT
43+
$.model AS model TEXT
44+
$.description AS description TEXT
45+
$.price AS price NUMERIC
46+
$.condition AS condition TAG SEPARATOR ,
47+
$.type AS type TAG
48+
$.helmet_included AS helmet_included TAG
49+
$.specs.material AS material TAG
50+
$.specs.weight AS weight NUMERIC
51+
```
52+
53+
You can then easily match a user to their preferred type of bike within a requested price bracket.
54+
55+
```redis:[run_confirmation=true]
56+
// Match leisure bikes within a price of 200 and 300
57+
FT.SEARCH idx:smpl_bicycle "@type:{leisure} @price:[200 300]" RETURN 4 brand model type price
58+
```
59+
60+
### Location-based search
61+
62+
Location-based search involves finding and retrieving data that is relevant to a specific geographic location. Redis can be used to power location-based search applications by storing and indexing geospatial data, such as latitude and longitude coordinates, and providing fast and efficient search capabilities
63+
64+
```redis:[run_confirmation=true]
65+
// Add a restaurant as JSON
66+
JSON.SET sample_restaurant:341 $ '{
67+
"name": "Galusca",
68+
"cuisine": "Moldovan",
69+
"location": "47.0159,28.8107"
70+
}'
71+
```
72+
73+
```redis:[run_confirmation=true]
74+
//Create an index of your restaurant data
75+
FT.CREATE "idx:smpl_restaurant"
76+
ON JSON
77+
PREFIX 1 "sample_restaurant:"
78+
SCHEMA
79+
"$.cuisine" AS "cuisine" TAG
80+
"$.name" AS "restaunt_name" TEXT
81+
"$.location" AS "location" GEO
82+
```
83+
84+
```redis:[run_confirmation=true]
85+
// Find a Japanese restaurant within a 20 mile radius
86+
FT.SEARCH idx:smpl_restaurant "@cuisine:{japanese} @location:[-98.1179,30.671 20 mi]" RETURN 2 restaunt_name location
87+
```
88+
89+
### Session store
90+
91+
Redis shines as a session store, offering speed and scalability for web applications. Using commands like HSET to store session data as Hash and HGET to retrieve it, Redis ensures rapid access to user sessions.
92+
93+
```redis:[run_confirmation=true]
94+
// Store new session
95+
HSET sample_session:074529275 user_id 7254 username gabe_jones email gabe@example.com last_activity "2024-06-01 10:24:00"
96+
97+
// Set an expiration time for the new session entry
98+
EXPIRE sample_session:074529275 7344000
99+
```
100+
101+
```redis:[run_confirmation=true]
102+
// Retrieve an existing session
103+
HGETALL sample_session:074529275
104+
```
105+
106+
### Job queue
107+
108+
In many applications, tasks need to be processed asynchronously, such as maintaining a waiting list for a helpdesk where incoming support tickets are queued for processing. A job queue helps manage these tasks efficiently. In Redis, a [job queue](https://redis.io/glossary/redis-queue/) can be implemented using the List data structure. When a new task needs to be queued, it is added to the left end of the list (using LPUSH command). When a worker is ready to process a task, it dequeues it from the right end of the list (using RPOP command).
109+
110+
111+
```redis:[run_confirmation=true]
112+
// Create a new job as a Hash
113+
HSET sample_jobQueue:ticket:101 id 101 user_id 123 description "Unable to access console" priority "High" created_at "2024-04-20T12:00:00Z"
114+
115+
// Enqueue the new job
116+
LPUSH sample_jobQueue:helpdesk uuid86106205
117+
```
118+
119+
```redis:[run_confirmation=true]
120+
// Dequeue a job
121+
RPOP sample_jobQueue:helpdesk
122+
```
123+
124+
### Leaderboard
125+
126+
[Leaderboards](https://redis.io/solutions/leaderboards/) are crucial for gaming applications to display rankings based on scores. Sorted Sets in Redis are perfect for this purpose due to their ability to store elements with associated scores, which can be easily queried and sorted.
127+
128+
```redis:[run_confirmation=true]
129+
// Add a new score to leaderboard
130+
ZADD sample_leaderboard:tetris 670000 "user100"
131+
```
132+
133+
```redis:[run_confirmation=true]
134+
// Get the top 5 users on the leaderboard, with scores
135+
ZRANGE sample_leaderboard:tetris 0 4 REV WITHSCORES
136+
```

0 commit comments

Comments
 (0)