Beyond relational database
Building high performance website using
Redis and PHP




Pham Cong Dinh                   Hanoi PHP Day 2009
Software Developer               December 19, 2009
Vega Corporation
What I am talking about


               •     Relational database in our world
               •     High performance web apps: new challenges
               •     Redis: A data structure store
               •     Using some of Redis's PHP clients
                                90
                                80
                                70
                                60
                                                                             East
                                50
                                                                             West
                                40
                                                                             North
                                30                                           South
                                20
                                10
                                0
Go beyond relational database        1st Qtr   2nd Qtr   3rd Qtr   4th Qtr
Relational database in our world


                                  •    Question
                                        • How can we store our data?
                                  •    Answer
Part     1                              • Relational database
                                  •    ACID is our love
                                        • Atomicity - 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.




       Go beyond relational database
Relational database in our world


                          •     Brewer's (CAP) Theorem
                                •   C: 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.


Go beyond relational database
Relational database in our world


                          •     BASE: An ACID Alternative
                                •   Basically Available
                                •   Soft state
                                •   Eventually consistent.
                          •     Data partitioning and decision between
                                Consistency and Availability.




Go beyond relational database
Relational database in our world


                          •     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
                                real challenge
Go beyond relational database
High performance web apps: new
             challenges

                                  •    Web server is bottleneck


Part     2




       Go beyond relational database
High performance web apps: new
      challenges

                          •     Database is bottleneck




Go beyond relational database
High performance web apps: new
      challenges

                          •     CPU is bottleneck




Go beyond relational database
High performance web apps: new
      challenges

                          •     More requests: going distributed is easy
                                •   Apache
                                •   PHP
                          •     Database scalability is hard
                                •   Struggling with SMP: MySQL
                                •   Replication is hard: replication is single
                                    threaded: MySQL
                                •   Most relational databases find hard to scale
                                    WRITEs
                                     •   Schema constraints
                                     •   Disk I/O
                                     •   Memory constraints
                                     •   Global lock
                                     •   Complicated data types
Go beyond relational database
High performance web apps: new
      challenges

                          •     Scaling for READ is easier for scaling for
                                WRITE
                          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)

Go beyond relational database
REDIS: A data structure datastore


                                  •    Redis (REmote DIctionary Server)
                                  •    Network interface to language dependent
                                       client library: Python, Java, Ruby, Erlang,
Part     3                             C# …
                                  •    Written in C
                                  •    Latest version: 1.1




       Go beyond relational database
REDIS: A data structure datastore


                          •     In a most basic form, it is a key – value
                                store: think Memcached
                          •     Not memory only
                          •     Not immediately durable
                          •     Supports set of complicated data structure
                          •     Can be found at
                                http://code.google.com/p/redis/




Go beyond relational database
REDIS: A data structure datastore


                          •     Database concept
                                •   SELECT index




Go beyond relational database
REDIS: A data structure datastore


                          •     Key => Value
                                •   SET mykey myvalue
                                •   GET mykey
                                •   More: MGET, MSET

                          •




Go beyond relational database
REDIS: A data structure datastore


                          •     LIST
                                •   RPUSH mylistkey string
                                •   LPUSH mylistkey string
                                •   LLEN mylistkey
                                •   LPOP mylistkey
                                •   RPOP mylistkey
                                •   More: LGET/LSET ...




Go beyond relational database
REDIS: A data structure datastore


                          •     SET
                                •   SADD mysetkey member
                                •   SREM key member
                          •     Sorted SET




Go beyond relational database
Redis PHP clients


                               •    Owlient's phpredis
                                    •   Can be found at
                                        http://github.com/owlient/phpredis

Phần 4                              •   C extension




    Go beyond relational database
Any question?




Phần 4




    Go beyond relational database

redis

  • 1.
    Beyond relational database Buildinghigh performance website using Redis and PHP Pham Cong Dinh Hanoi PHP Day 2009 Software Developer December 19, 2009 Vega Corporation
  • 2.
    What I amtalking about • Relational database in our world • High performance web apps: new challenges • Redis: A data structure store • Using some of Redis's PHP clients 90 80 70 60 East 50 West 40 North 30 South 20 10 0 Go beyond relational database 1st Qtr 2nd Qtr 3rd Qtr 4th Qtr
  • 3.
    Relational database inour world • Question • How can we store our data? • Answer Part 1 • Relational database • ACID is our love • Atomicity - 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. Go beyond relational database
  • 4.
    Relational database inour world • Brewer's (CAP) Theorem • C: 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. Go beyond relational database
  • 5.
    Relational database inour world • BASE: An ACID Alternative • Basically Available • Soft state • Eventually consistent. • Data partitioning and decision between Consistency and Availability. Go beyond relational database
  • 6.
    Relational database inour world • 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 real challenge Go beyond relational database
  • 7.
    High performance webapps: new challenges • Web server is bottleneck Part 2 Go beyond relational database
  • 8.
    High performance webapps: new challenges • Database is bottleneck Go beyond relational database
  • 9.
    High performance webapps: new challenges • CPU is bottleneck Go beyond relational database
  • 10.
    High performance webapps: new challenges • More requests: going distributed is easy • Apache • PHP • Database scalability is hard • Struggling with SMP: MySQL • Replication is hard: replication is single threaded: MySQL • Most relational databases find hard to scale WRITEs • Schema constraints • Disk I/O • Memory constraints • Global lock • Complicated data types Go beyond relational database
  • 11.
    High performance webapps: new challenges • Scaling for READ is easier for scaling for WRITE 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) Go beyond relational database
  • 12.
    REDIS: A datastructure datastore • Redis (REmote DIctionary Server) • Network interface to language dependent client library: Python, Java, Ruby, Erlang, Part 3 C# … • Written in C • Latest version: 1.1 Go beyond relational database
  • 13.
    REDIS: A datastructure datastore • In a most basic form, it is a key – value store: think Memcached • Not memory only • Not immediately durable • Supports set of complicated data structure • Can be found at http://code.google.com/p/redis/ Go beyond relational database
  • 14.
    REDIS: A datastructure datastore • Database concept • SELECT index Go beyond relational database
  • 15.
    REDIS: A datastructure datastore • Key => Value • SET mykey myvalue • GET mykey • More: MGET, MSET • Go beyond relational database
  • 16.
    REDIS: A datastructure datastore • LIST • RPUSH mylistkey string • LPUSH mylistkey string • LLEN mylistkey • LPOP mylistkey • RPOP mylistkey • More: LGET/LSET ... Go beyond relational database
  • 17.
    REDIS: A datastructure datastore • SET • SADD mysetkey member • SREM key member • Sorted SET Go beyond relational database
  • 18.
    Redis PHP clients • Owlient's phpredis • Can be found at http://github.com/owlient/phpredis Phần 4 • C extension Go beyond relational database
  • 19.
    Any question? Phần 4 Go beyond relational database