Redis noSQL database design example
This article explains how we made our article section on http://redis4you.com/articles.php.
We use same technics for programming examples section.
Before we do database design we need to see what data we will have and what are "relations" between the data.
For articles we have:
- Article - contain name, description, filename, posting date.
- Each Article may have zero, one or more tags.
- Each tag may have zero, one or more articles.
Later we may decide to add more fields, but since Redis is schema-free this will be no problem.
Because Redis is key / value, we will need to decide what kind of lookups we will need to be able to to:
- List all articles.
- List single article, plus lising its tags.
- By given tag(s), list all articles in it.
Database design:
We put all articles in hash keys such "art:1", "art:2" etc.
redis 127.0.0.1> hgetall "art:1"
1) "desc"
2) "How to use Redis for session handler in PHP in a way similar to msession or PostgreSQL sessions"
3) "file"
4) "001"
5) "name"
6) "Redis as session handler in PHP"
Hashes are memory optimized and uses much less memory than reguar keys. Also, when we need to add more data, we just add additional hash field.
Because we need to be able to show all articles, we do set "all:art".
redis 127.0.0.1> smembers "all:art"
1) "1"
2) "2"
3) "3"
4) "4"
5) "5"
6) "6"
7) "7"
8) "8"
Set took memory, but later is much more faster than "keys art:*".
For each article we do a set such "art:1:tags", "art:2:tags" etc., with tags this:
redis 127.0.0.1> smembers "art:1:tags"
1) "system administration"
2) "php"
However this is just to display the information. We will need single set for each tag too:
redis 127.0.0.1> smembers "tags:art:php"
1) "1"
2) "8"
redis 127.0.0.1> smembers "tags:art:system administration"
1) "1"
2) "2"
3) "5"
4) "6"
5) "7"
6) "8"
We choosed tags:art:XXX because we use same for code examples page.
Now we can display all articles in one set, but also we can do easily intersects of two or more sets too.
To add new article we will need to do following:
redis 127.0.0.1> hmset "art:9" "file" "009" "data" "2011-09-02"
OK
redis 127.0.0.1> hmset "art:9" "name" "Redis noSQL database design example"
OK
redis 127.0.0.1> hmset "art:9" "desc" "This article explains how we made our article section on http://redis4you.com/articles.php"
OK
redis 127.0.0.1> sadd "art:9:tags" "programming"
(integer) 1
redis 127.0.0.1> sadd "art:9:tags" "database design"
(integer) 1
redis 127.0.0.1> sadd "art:9:tags" "optimization"
(integer) 1
redis 127.0.0.1:1001[1]> sadd "tags:art:optimization" 9
(integer) 1
redis 127.0.0.1:1001[1]> sadd "tags:art:programming" 9
(integer) 1
redis 127.0.0.1:1001[1]> sadd "tags:art:database design" 9
(integer) 1
redis 127.0.0.1:1001[1]> sadd "all:art" 9
This is all. Now we need to program some code that do the visualization.
For comparisson we will show simple MySQL schema that do same data structure:
CREATE TABLE articles(
id int not null primary key,
name varchar(100),
`desc` varchar(100),
`file` varchar(3),
data date
);
CREATE TABLE articles_tags(
id int not null,
tag varchar(100) not null,
primary key(
id,
tag
)
);
CREATE INDEX tag ON articles_tags(tag);
In this case MySQL way is very nice and clear, but because schema is fixed some future changes will be difficult to be implemented.
blog comments powered by Disqus
Articles library
Redis as session handler in PHP
How to use Redis for session handler in PHP in a way similar to msession or PostgreSQL sessions
Date: 2011-07
Tags: system administration php
Lazy starter script on Linux such Redhat CentOS or Fedora
How to quickly made Redis to start after Linux boot.
Date: 2011-07
Tags: system administration
Redis vs Memcached comparison
Here is basic comparison of Redis Memcached and Memcachedb.
Date: 2011-07
Tags: memcached
Why RDBMS and SQL are difficult...
...while sometimes NoSQL solution may be easier and way faster? This arcle deals with incremental counters and how they needs to be implemented properly in RDBMS-es such MySQL
Date: 2011-07
Updated: 2011-09-30
Tags: mysql programming anti-pattern
Seamless migration from one Redis server to another
How to use replication to migrate Redis server without service interruption
Date: 2011-07
Tags: system administration
Redis swap issue while save
Explanation why your server load average may spike and server to swap memory when running redis
Date: 2011-08-21
Tags: system administration
Amazon EC2 and Amazon ElastiCache service
Memcached at Amazon first impressions
Date: 2011-08-23
Tags: memcached system administration cloud
Understanding hash-max-zipmap-entries and 'hash of hashes' optimization
Explanation of config parameters such hash-max-zipmap-entries and how we can make huge memory savings in new Redis 2.2
Date: 2011-08-28
Tags: system administration php programming optimization
NoSQL database design example
This article explains how we made our article section
Date: 2011-09-02
Tags: database design mysql programming
Redis save and backup script
Simple backup script for Redis
Date: 2011-09-28
Tags: system administration optimization
Postgres 9.1 foreign data wrapper interface
Postgres 9.1 gives you ability to access data from different data sources including Redis
Date: 2011-10-20
Tags: mysql optimization memcached pgsql programming
Redis high traffic connection issue
Explanation on 'Cannot assign requested address' connection error
Date: 2011-11-12
Tags: system administration scaling optimization
Redis too many open files error on high traffic sites
Explanation on 'Accepting client connection: accept: Too many open files'
Date: 2011-12-21
Tags: memcached system administration security
PHP Redis bug with PHP 5.4
Description of bug PHP Redis bug
Date: 2012-07-02
Tags: system administration php
Case Study: Using Redis intersect on very large sets
Intersection of 2 x 120M MySQL records
Date: 2012-08-29
Tags: cloud mysql optimization php programming
Getting started with Python and Redis on CentOS 6.X
How to install and test Redis client for Python on CentOS
Date: 2013-03-21
Tags: python system administration
GeoIP in Redis
Put Maxmind's GeoIP in Redis for best berformance for cient apps written in PHP or Python
Date: 2013-03-24
Tags: mysql optimization python scaling php programming
Redis database file examination
Examination of Redis rdb file in order to find huge memory consumption
Date: 2013-08-23
Tags: system administration optimization