Beyond relational database - Building high performance websites using Redis and PHP

24
Hanoi PHP Day 2009 December 19, 2009 Beyond relational database Building high performance websites using Redis and PHP Pham Cong Dinh Software Developer Vega Corporation

Transcript of Beyond relational database - Building high performance websites using Redis and PHP

Page 1: Beyond relational database - Building high performance websites using Redis and PHP

Hanoi PHP Day 2009December 19, 2009

Beyond relational database Building high performance websites using Redis and PHP

Pham Cong DinhSoftware DeveloperVega Corporation

Page 2: Beyond relational database - Building high performance websites using Redis and PHP

Go beyond relational database

What I am talking about

0

10

20

30

40

50

60

70

80

90

1st Qtr 2nd Qtr 3rd Qtr 4th Qtr

East

West

North

South

Relational database in our worldHigh performance web apps: new challengesRedis: A data structure storeIntroduction to some Redis's PHP clients

Page 3: Beyond relational database - Building high performance websites using Redis and PHP

Go beyond relational database

QuestionHow can we store our data?

AnswerRelational database

ACID is our loveAtomicity - all parts of a transaction succeed or none of then

succeed. Integrity.

Consistency - Nothing in your transaction will violate the rules of the database. Integrity.

Isolation - Each transaction operates independently of every other transaction.

Durability - Once the database says that data is committed there is no opportunity for that to be undone.

Part 1

Relational database in our world

Page 4: Beyond relational database - Building high performance websites using Redis and PHP

Go beyond relational database

Brewer's (CAP) TheoremC: Consistency - The client perceives that a set of

operations has occurred all at once.Strong consistency: ACID

A: Availability - Every operation must terminate in an intended response. Requests are to be served even when there is a partition in the cluster.

P: Partition tolerance - Operations will complete, even if individual components are unavailable.

Relational database in our world

Page 5: Beyond relational database - Building high performance websites using Redis and PHP

Go beyond relational database

BASE: An ACID Alternative Basically AvailableSoft stateEventually consistent.

Data partitioning and decision between Consistency and Availability.

Relational database in our world

Page 6: Beyond relational database - Building high performance websites using Redis and PHP

Go beyond relational database

Relational database in high performance environments

Google

Facebook

Wikipedia

Live Journal

Yahoo

FAILED?… but most companies does not need more

than a single database server

… but modern web apps today is facing a but modern web apps today is facing a real challengereal challenge

Relational database in our world

Page 7: Beyond relational database - Building high performance websites using Redis and PHP

Go beyond relational database

High performance web apps: new challenges

Web server is bottleneck

Part 2

Page 8: Beyond relational database - Building high performance websites using Redis and PHP

Go beyond relational database

Database is bottleneck

High performance web apps: new challenges

Page 9: Beyond relational database - Building high performance websites using Redis and PHP

Go beyond relational database

CPU is bottleneck

High performance web apps: new challenges

Page 10: Beyond relational database - Building high performance websites using Redis and PHP

Go beyond relational database

More requests: going distributed is easyApachePHP

Database scalability is hardStruggling with SMP: MySQLReplication is hard: replication is single threaded:

MySQLMost relational databases find hard to scale

WRITEsSchema constraintsDisk I/OMemory constraintsGlobal lockComplicated data types

High performance web apps: new challenges

Page 11: Beyond relational database - Building high performance websites using Redis and PHP

Go beyond relational database

Scaling for READ is easier for scaling for WRITE

E.x: Records inserted in 14h

High performance web apps: new challenges

mysql> select count(1) from setacc;

+----------+

| count(1) |

+----------+

| 3982438 |

+----------+

1 row in set (0.00 sec)

mysql> select count(1) from setacc_del;

+----------+

| count(1) |

+----------+

| 4003793 |

+----------+

1 row in set (0.00 sec)

Page 12: Beyond relational database - Building high performance websites using Redis and PHP

Go beyond relational database

Relational databases is SLOW in some speical cases

Alternatives but not drop-in solutionsDocument oriented databasesColumn-based databasesKey-value datastoresGraph databases

High performance web apps: new challenges

Page 13: Beyond relational database - Building high performance websites using Redis and PHP

Go beyond relational database

REDIS: A data structure datastore

Redis (REmote DIctionary Server)One of the hottest technologies in 2009

(Github)Network interface to language dependent

client library: Python, Java, Ruby, Erlang, C# …

Written in C, single process, single thread, event-based

Latest version: 1.1Can be found at http://code.google.com/p/redis/

Part 3

Page 14: Beyond relational database - Building high performance websites using Redis and PHP

Go beyond relational database

Redis is fast

REDIS: A data structure datastore

Page 15: Beyond relational database - Building high performance websites using Redis and PHP

Go beyond relational database

In a most basic form, it is a key – value storeNot yet another Memcached

Not memory onlyNot immediately durableRedis VM (upcoming)Supports set of complicated data structureBuilt-in replication

REDIS: A data structure datastore

Page 16: Beyond relational database - Building high performance websites using Redis and PHP

Go beyond relational database

Data can be partitioned using database conceptSELECT index

REDIS: A data structure datastore

Page 17: Beyond relational database - Building high performance websites using Redis and PHP

Go beyond relational database

Key => ValueSET mykey myvalueGET mykeySETNX (atomic operation)More: MGET, MSET

Key or Value is binary safe

REDIS: A data structure datastore

Page 18: Beyond relational database - Building high performance websites using Redis and PHP

Go beyond relational database

Redis supports manipulation on a list of values via LIST

Think of an ordered list with the operations you would expect: appending, indexed access, and access to a range of values

CommandRPUSH mylistkey stringLPUSH mylistkey stringLLEN mylistkeyLPOP mylistkeyRPOP mylistkeyMore: LGET/LSET ...

REDIS: A data structure datastore

Page 19: Beyond relational database - Building high performance websites using Redis and PHP

Go beyond relational database

SET in Redis is an unordered collection with no duplicate membersSET commands

SADD mysetkey member

SREM mysetkey memberSMEMBERS mysetkey

Sorted SET: similar to SET but every member is attached with a score (floating number score)Sorted Set commands

ZADD key score member ZREM key memberZRANGE key start end

REDIS: A data structure datastore

Page 20: Beyond relational database - Building high performance websites using Redis and PHP

Go beyond relational database

Use casesOperational dataPre-computed dataLoggingCacheMessage queue

Immediate data layer

Job server

REDIS: A data structure datastore

Page 21: Beyond relational database - Building high performance websites using Redis and PHP

Go beyond relational database

Redis PHP clients

Owlient's phpredisCan be found at

http://github.com/owlient/phpredisC extensionSample

Class: RedisKey-value

$redis->get('key');$redis->set('key', 'value');$redis->incr('key1');

List$redis->lpush('key1', 'C');

Set$redis->sadd('key1' , 'set1');

Part 4

Page 22: Beyond relational database - Building high performance websites using Redis and PHP

Go beyond relational database

Rediska: http://rediska.geometria-lab.net/Full Zend Framework integrationSample

Redis PHP clients

require_once 'Rediska/Key.php';

$key = new Rediska_Key('keyName');

// Set value

$key->setValue('hello');

// Get value

$key = new Rediska_Key('keyName');

$key->getValue(); #=> hello

Page 23: Beyond relational database - Building high performance websites using Redis and PHP

Go beyond relational database

Predis: http://github.com/nrk/predisPHP 5.3+ onlySupport PHP 5.3 namespace and functorSupport multiple Redis instances and

pipelining

Redis PHP clients

$redis = Predis\Client::create(

array('host' => '10.0.0.1', 'port' => 6379),

array('host' => '10.0.0.2', 'port' => 6379)

);

$replies = $redis->pipeline(function($pipe) {

for ($i = 0; $i < 1000; $i++) {

$pipe->set("key:$i", str_pad($i, 4, '0', 0));

$pipe->get("key:$i");

}

});

Page 24: Beyond relational database - Building high performance websites using Redis and PHP

Go beyond relational database

Any question?

Phần 5