# Redis in 21 Minutes
Redis is an open source (BSD licensed), in-memory data structure store, used as a database, cache, and message broker.
# Online Playground
# Installation Optional
# Use Cases
Redis features will be detailed using "Blog Web app like Medium" in their simplified form.
- Manage user session
- User profile:
- name
- followers
- following
- status
- User profile:
- Blog post statistics
- Views, reads and claps statistics
- Number of people who clapped
- Notifications
- New
- Recent notifications
- Latest Blogs
- From the followings
- Blog search by title and tags
# Implementation
Redis key pattern should be decided for any use case.
# Manage User Session
- Key - session:{user-id}:profile
- Value - Hash data type
> hmset session:prasad_j:profile name Prasad followers 90 following 6 status online
OK
> hmget session:prasad_j:profile name status
1) "Prasad"
2) "online"
> hset session:prasad_j:profile status offline
(integer) 0
> hget session:prasad_j:profile status
"offline"
2
3
4
5
6
7
8
9
# Blog Post Statistics
# Views, reads and claps statistics
- Key - blog:{id}:stats
- Value - Hash data type
> hmset blog:1001:stats views 0 reads 0 claps 0
OK
> hincrby blog:1001:stats claps 5
(integer) 5
> hmget blog:1001:stats views reads claps
1) "0"
2) "0"
3) "5"
> hgetall blog:1001:stats
1) "views"
2) "0"
3) "reads"
4) "0"
5) "claps"
6) "5"
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# Number of people who clapped for a blog
- Key - blog:{id}:claps
- Value - Set data type
- Add fan user-ids into a set
> sadd blog:1001:fans john
(integer) 1
> sadd blog:1001:fans harry mary
(integer) 1
> smembers blog:1001:fans
1) "mary"
2) "harry"
3) "john"
2
3
4
5
6
7
8
- Add an existing fan into a set
> sadd blog:1001:fans harry
(integer) 0
> sismember blog:1001:fans harry
(integer) 1
> sadd blog:1002:fans harry
(integer) 1
2
3
4
5
6
- Get the total members in a set
> scard blog:1001:fans
(integer) 3
2
# Data Types
Redis is a key-value pair database. Key is a kind of string datatype. Value can be one of the following datatype
- String
- Hashes
- Lists
- Sets
- Sorted Sets
An introduction to Redis data types and abstractions (opens new window)
# String
String is Simple Dynamic String (SDS) (opens new window). String can be used as number, array, bit set and binary data
struct sdshdr {
long len; // The free field stores the number of additional bytes available for use
long free; // The len field stores the length of buf
char buf[]; // The buf character array stores the actual string
};
2
3
4
5
TIP
- The len field makes obtaining the length of a Redis string an O(1) operation.
- Together the len and free field can be thought of as holding the metadata of the buf character array.
# Basic Commands
Full List of Commands (opens new window)
Command | Args | Purpose | Time Complexity |
---|---|---|---|
SET (opens new window) | key value [EX seconds] | Set the string value of a key | O(1) |
SETNX (opens new window) | key value | Set the value of a key, only if the key does not exist | O(1) |
GET (opens new window) | key | Get the value of a key | O(1) |
For numerics only
Command | Args | Purpose | Time Complexity |
---|---|---|---|
INCR (opens new window) | key | Increment the integer value of a key by one | O(1) |
INCRBY (opens new window) | key increment | Increment the integer value of a key by the given amount | O(1) |
INCRBYFLOAT (opens new window) | key increment | Increment the float value of a key by the given amount | O(1) |
Bitmaps are not an actual data type, but a set of bit-oriented operations defined on the String type.
Command | Args | Purpose | Time Complexity |
---|---|---|---|
SETBIT (opens new window) | key offset value | Sets or clears the bit at offset in the string value stored at key | O(1) |
GETBIT (opens new window) | key offset | Get the bit value at offset in the string value stored at key | O(1) |
BITCOUNT (opens new window) | key [start end] | Count set bits in a string | O(N) |
BITOP (opens new window) | operation destkey key [key ...] | Perform a bitwise operation between multiple keys and store the result in the destination key | O(N) |
# Hash Map
Hashes, which are maps composed of fields associated with values. Both the field and the value are strings.
# Basic Commands
Full List of Commands (opens new window)
Command | Args | Purpose | Time Complexity |
---|---|---|---|
HSET (opens new window) | key field value [field value ...] | Set the string value of a hash field | O(1) / O(N) |
HGET (opens new window) | key field | Get the value of a hash field | O(1) |
HGETALL (opens new window) | key field | Get all the fields and values in a hash | O(N) |
HMGET (opens new window) | key field [field ...] | Get the values of all the given hash fields | O(1) / O(N) |
# Sandbox Commands
Commands to keep your sandbox environment clean.
WARNING
Don't run these commands on any development or production server without proper understanding
$ redis-cli
> keys *
1) "entity:id"
> flushdb
OK
> keys *
(empty array)
>
2
3
4
5
6
7
8