Accounting / Vote example

Suppose you have document library website. Each client have balance and you decrease it with 5 cents for each document the user requests.

Voting on websites have similar set-up, but instead of deducting money - increasing the object rating (user "karma", picture views etc).

If you do it with MySQL, you need to run query such "select balance from user where id = 5", then "update user set balance = balance - 0.05 where id = 5".

The update query blocks the table, so performance will be very poor. Second and most important, you do not know if another thread updated the table meanwhile - this will ruin whole accounting and billing will be dead wrong.

There is a way to avoid this issue with MySQL, but you will need to use two separate tables. However you can do hybrid way using both Redis and MySQL.

$r = new Redis();
$r->connect("", "6379");

$userid = 5;

// Redis does not handle floating points, so we do conversion to cents.
$price  = 0.05 * 100;

$setkey = "all:users";

$userkey = "user:" . $userid;

// check if we had the data in Redis...
if (!$r->exists($userkey)){
	// No we don't.
	$balance = get_from_db("select balance from users where id = $userid");
	// We can be pretty sure there is no other thread who updated the balance, 
	// but anyway, we do it safe way.
	$r->setnx($userkey, $balance * 100);
	// Redis does not handle floating points, so we do conversion to cents.
	// Put the userid into a set, so later a crontab to be able to read the user balance 
	// and to put it back to the MySQL.
	$r->sadd($setkey, $userid);

$balance = $r->decrby($userkey, $price);

if ($balance < 0){
	// Opps, the user have no balance. Revert the "transaction"...
	$r->incrby($userkey, $price);
	// Exit

// We show the requested document here...

echo "Document";

Redis connection note

The example uses generic connection to the Redis server. To make the example work with our service you will need to use code similar to this one:

// change these parameter according to the information in your instance list
$host = '';
$port = 1234;
$password = 'somehashcode';
$db = 0;

$r = new Redis();
$r->connect($host, $port);

blog comments powered by Disqus

Code library

Because we want this page to be useful for memcached users, we tagged with memcached all examples that may be "recreated" for memcached server.

MySQL cache in PHP

How to use Redis for cache MySQL queries in a way similar to memcached
Date: 2011-07

Tags: memcached php cache mysql

How to prevent website to be web-scraped

How to use Redis to prevent web-scraping parsing and web-spam
Date: 2011-07

Tags: webstats memcached php security anti-spam

'Rotating' news

How to do 'Rotating' news list
Date: 2011-07

Tags: php

Show random element

How pool random element from news list
Date: 2011-07

Tags: php

Accounting / Vote example

How Redis can help us with Accounting / Vote / Like / Recommend / +1 clicks
Date: 2011-07

Tags: php mysql

Scoreboard example

How to implement fast hi-score table
Date: 2011-07

Tags: php

Scaling example

How to scale Redis on several servers using sharding
Date: 2011-07

Tags: cloud memcached php sharding scaling

Distributed work pool / pipeline example

How to implement distributed work pool and to scale some work across many computers
Date: 2011-07

Tags: cloud php scaling

Simple realtime web counter

How to collect and store information for page visitors
Date: 2011-07

Tags: php webstats

Emulation of expiration of the set members

How to make set members to expire
Date: 2011-07

Tags: php

Lock example

How Redis can help us with user level locks
Date: 2011-08-23

Tags: memcached php lock

Message queue example

Creating distributed message queue
Date: 2011-09-17

Tags: cloud php queue scaling

Index example

Building reverse index using sets
Date: 2011-11-01

Tags: php

Using Redis for MySQL autoincrement

Using Redis to speed up MySQL inserts
Date: 2011-01-13

Tags: cloud php scaling mysql

Using Redis as "data buffer"

Using Redis for caching MySQL or Cassandra data inserts
Date: 2012-08-04

Tags: webstats mysql cloud cassandra php sharding scaling

Calculating how many visitors are on the same webpage using rolling average

How to produce 'There are 123 visitors on this page' sign
Date: 2011-07

Tags: memcached php webstats

© Jul.2011 - 2017,