Redis noSQL database design example

This article explains how we made our article section on

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:

Because Redis is key / value, we will need to decide what kind of lookups we will need to be able to to:

Database design:

We put all articles in hash keys such "art:1", "art:2" etc.

redis> 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> 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> 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> smembers "tags:art:php"
1) "1"
2) "8"
redis> 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> hmset "art:9" "file" "009" "data" "2011-09-02"
redis> hmset "art:9" "name" "Redis noSQL database design example"
redis> hmset "art:9" "desc" "This article explains how we made our article section on"

redis> sadd "art:9:tags" "programming"
(integer) 1
redis> sadd "art:9:tags" "database design"
(integer) 1
redis> sadd "art:9:tags" "optimization"
(integer) 1

redis[1]> sadd "tags:art:optimization" 9
(integer) 1
redis[1]> sadd "tags:art:programming" 9
(integer) 1
redis[1]> sadd "tags:art:database design" 9
(integer) 1

redis[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(

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: php system administration

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 cloud system administration

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: optimization php programming system administration

NoSQL database design example

This article explains how we made our article section
Date: 2011-09-02

Tags: mysql database design programming

Redis save and backup script

Simple backup script for Redis
Date: 2011-09-28

Tags: optimization system administration

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: memcached optimization mysql programming pgsql

Redis high traffic connection issue

Explanation on 'Cannot assign requested address' connection error
Date: 2011-11-12

Tags: optimization scaling system administration

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 security system administration

PHP Redis bug with PHP 5.4

Description of bug PHP Redis bug
Date: 2012-07-02

Tags: php system administration

Case Study: Using Redis intersect on very large sets

Intersection of 2 x 120M MySQL records
Date: 2012-08-29

Tags: optimization cloud php mysql 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: optimization php scaling mysql programming python

Redis database file examination

Examination of Redis rdb file in order to find huge memory consumption
Date: 2013-08-23

Tags: optimization system administration

© Jul.2011 - 2019,
This website uses cookies to improve user experience. By using our website you consent to all cookies in accordance with our Cookie Policy.