how to scale php applications

39
© All rights reserved. Zend Technologies, Inc. How to scale PHP applications by Jan Burkl, Enrico Zimuel Zend Technologies http://www.zend.com [email protected], [email protected] 30 th October 2010 – PHP Barcelona Conference

Upload: enrico-zimuel

Post on 28-Jan-2015

110 views

Category:

Technology


0 download

DESCRIPTION

In this last years a lot of high traffic web sites have been built in PHP. One of the main problem to design a distributed PHP architecture is how to share session data between multiple servers. In this presentation we showed the most used solutions to scale a PHP application along multiple servers. We presented different solutions to share session data using open source solutions (nfs, databases, memcached, redis, etc). Moreover we talk about Zend Server Cluster Manager, an enterprise­ ready Web Application Server for running and managing an HA Cluster of PHP servers.

TRANSCRIPT

Page 1: How to scale PHP applications

© All rights reserved. Zend Technologies, Inc.

How to scale PHP applications

by Jan Burkl, Enrico ZimuelZend Technologies

http://[email protected], [email protected]

30th October 2010 – PHP Barcelona Conference

Page 2: How to scale PHP applications

© All rights reserved. Zend Technologies, Inc.

About us

● Jan Burkl ([email protected])

● Senior System Engineer at Zend Technologies in Stuttgart (Germany) since 2006

● Enrico Zimuel ([email protected])

● Senior Consultant & Architect at Zend Technologies in Milan (Italy) since 2008

● Blog on web dev't: http://www.zimuel.it/blog

Page 3: How to scale PHP applications

© All rights reserved. Zend Technologies, Inc.

Summary

● Scalability of a web application

● How to scale a PHP application

● PHP session management

● Sharing session data using:

▶ Network file system▶ Database▶ Memcached▶ Redis▶ Zend Server Cluster Manager

Page 4: How to scale PHP applications

© All rights reserved. Zend Technologies, Inc.

Scalability of a web application

Page 5: How to scale PHP applications

© All rights reserved. Zend Technologies, Inc.

Scalability: general definition

“Scalability is a desirable property of a system, a network, or a process, which indicates its ability to either handle growing amounts of work in a graceful manner or to be enlarged”

Source: Wikipedia

Page 6: How to scale PHP applications

© All rights reserved. Zend Technologies, Inc.

Scalability of a web application

A web application is scalable when is able to manage a growing traffic with additional resources (CPU, RAM) without software changes

Page 7: How to scale PHP applications

© All rights reserved. Zend Technologies, Inc.

Scale vertically vs. Scale horizontally

● Scale vertically (scale up)

▶ Add resources to a single node in a system▶ Enhance the server (more CPU, more RAM, etc)▶ High availability difficult to implement

● Scale horizontally (scale out)

▶ Add mores nodes to a system▶ More servers, distributing the load▶ High availability easy to implement

Page 8: How to scale PHP applications

© All rights reserved. Zend Technologies, Inc.

Scale up vs. Scale out

Scale up Scale out

vs.

Page 9: How to scale PHP applications

© All rights reserved. Zend Technologies, Inc.

The web scale out

● As Google taught, the best way to scale an high traffic web application is horizontally

● No expensive servers to scale horizontally

● We need load balancers to split the traffic between all the servers

A Google server

Page 10: How to scale PHP applications

© All rights reserved. Zend Technologies, Inc.

A typical load balancer architecture

Load Balancer

Web Servers

Internet

Firewall

Page 11: How to scale PHP applications

© All rights reserved. Zend Technologies, Inc.

How to scale a PHP application?

● The PHP application uses session/local data?

▶ Yes = we have to manage the session/local data across multiple servers

● Using a persistent load balancer● Share the session data

▶ No = we can scale very easy (stateless)● Most of the PHP applications are not stateless,

they use session data

Page 12: How to scale PHP applications

© All rights reserved. Zend Technologies, Inc.

Persistent load balancer

● A client request is assigned always to the same server (means same session/local data)

● Pros:

▶ No software architecture changes● Cons:

▶ No fault tolerant, if a server goes down the session data are lost!

▶ The load balancer is the bottleneck▶ The persistent load balancer are expensive

Page 13: How to scale PHP applications

© All rights reserved. Zend Technologies, Inc.

Share the session data

● Manage the session data across the servers

● Pros:

▶ Fault tolerant, if a server goes down the load balancer can user another server

▶ No bottleneck in the load balancer▶ The stateless load balancer are cheaper

● Cons:

▶ Software architecture changes

Page 14: How to scale PHP applications

© All rights reserved. Zend Technologies, Inc.

PHP session management

Page 15: How to scale PHP applications

© All rights reserved. Zend Technologies, Inc.

Sessions in PHP

● Session support in PHP consists of a way to preserve certain data across subsequent accesses

● To identify the subsequent accesses, from the same client, PHP uses a cookie variable (PHPSESSID)

▶ Example: PHPSESSID= tclq3an1ri8dsfiuo43845loo1

● By default, session data are stored in the file system of the server

● In PHP we manage the session data using the $_SESSION global variable

Page 16: How to scale PHP applications

© All rights reserved. Zend Technologies, Inc.

PHP session management

● Configure the PHP session management (php.ini directives):

▶ session.name

● name of the session cookie identifier (PHPSESSID by default)

▶ session.save_handler

● defines the name of the handler which is used for storing and retrieving data associated with a session (files by default).

▶ session.save_path

● defines the argument which is passed to the save handler (with files handler is the path to store the session data)

Page 17: How to scale PHP applications

© All rights reserved. Zend Technologies, Inc.

Example (PHP sessions using files)

● <?phpsession_start();$_SESSION['user']= 'enrico';

● In session folder (for instance, /tmp) the PHP creates a file named sess_fvi9r84f14sjel8r28o6aqspr2 (where fvi9r84f14sjel8r28o6aqspr2 is PHPSESSID) that contains:

user|s:6:"enrico";

Page 18: How to scale PHP applications

© All rights reserved. Zend Technologies, Inc.

Share session data

● How to share PHP sessions between multiple servers?

▶ Using a Network File System▶ Using a Database▶ Using Memcached▶ Using Redis▶ Using Zend Server Cluster Manager▶ etc

Page 19: How to scale PHP applications

© All rights reserved. Zend Technologies, Inc.

session_set_save_handler()

● You can write your PHP session handler using the session_set_save_handler():

bool session_set_save_handler (callback $open,callback $close,callback $read,callback $write,callback $destroy,callback $gc)

● More info: http://php.net/manual/en/function.session-set-save-handler.php

Page 20: How to scale PHP applications

© All rights reserved. Zend Technologies, Inc.

Session sharingusing NFS

Page 21: How to scale PHP applications

© All rights reserved. Zend Technologies, Inc.

Session sharing using NFS

● Use the default PHP session handler (session.session_handler= files)

● NFS to store the session data files (session.save_path= NFS folder)

● Pros:▶ No changes on the PHP side

● Cons:▶ Highly inefficient▶ Not designed for high read/write ratio▶ Performance problems and data corruptions

Page 22: How to scale PHP applications

© All rights reserved. Zend Technologies, Inc.

Session sharingusing Database

Page 23: How to scale PHP applications

© All rights reserved. Zend Technologies, Inc.

Session sharing using DB

● Use the user PHP session handler (session.session_handler= user)

● Use the session_set_save_handler() to implement your db session handler

● Which db?▶ MySQL

http://www.php.net/manual/en/function.session-set-save-handler.php#81761

▶ Ms SQL Server● http://www.zimuel.it/blog/?p=402

▶ PostgreSQL, Oracle, etc

Page 24: How to scale PHP applications

© All rights reserved. Zend Technologies, Inc.

Session sharing using DB (2)

● Pros:▶ Solves the scalability limitation▶ A lot of best practices available▶ Wide installation base▶ DB is (normally) available

● Cons:▶ Sessions have almost 1:1 read/write ratio▶ Connection overhead▶ Single point of failure▶ Performance bottleneck

Page 25: How to scale PHP applications

© All rights reserved. Zend Technologies, Inc.

Session sharingusing Memcached

Page 26: How to scale PHP applications

© All rights reserved. Zend Technologies, Inc.

Session sharing using Memcached

● Use the memcached PHP session handler (session.session_handler= memcache)

● Pros:▶ Native session handler▶ Very fast (works in RAM)▶ Can be clustered

● Cons:▶ No persistent data (it's a caching system)▶ Cyclic memory (data can be overwritten if the

memory is full)

Page 27: How to scale PHP applications

© All rights reserved. Zend Technologies, Inc.

Session sharingusing Redis

Page 28: How to scale PHP applications

© All rights reserved. Zend Technologies, Inc.

What is Redis?

● Redis is an open-source, networked, in-memory, persistent, journaled, key-value data store (NoSQL).

● It's similar to memcached but the dataset is not volatile

● Developed by: Salvatore Sanfilippo

● Sponsored by vmware

● http://code.google.com/p/redis/

Page 29: How to scale PHP applications

© All rights reserved. Zend Technologies, Inc.

Session sharing using Redis

● Different PHP extensions for Redis:

▶ Predis, http://github.com/nrk/predis/

▶ Rediska, http://rediska.geometria-lab.net/

▶ redis.php, http://github.com/antirez/redisdotphp

▶ PHPRedis!, http://github.com/owlient/phpredis

● Custom session handler for Redis:▶ http://github.com/ivanstojic/redis-session-php

Page 30: How to scale PHP applications

© All rights reserved. Zend Technologies, Inc.

Session sharing using Redis (2)

● Pros:

▶ Fast (works in RAM)▶ Reliable (master-slave replication)

● Cons:

▶ PHP Redis extensions are 0.x version▶ Custom PHP session handler▶ Not so scalable* (data are stored in RAM)▶ No High Availability* (master-slave limitation)

* but Redis developers are working on a Cluster version!

Page 31: How to scale PHP applications

© All rights reserved. Zend Technologies, Inc.

Session sharingusing Zend ServerCluster Manager

Page 32: How to scale PHP applications

© All rights reserved. Zend Technologies, Inc.

What is Zend Server and Cluster Manager?

● Zend Server is a complete, enterprise-ready Web Application Server for running and managing PHP applications that require a high level of reliability, performance and security on Linux, Windows or IBM i.

● Zend Server Cluster Manager (ZSCM) extends the benefits of Zend Server across large-scale PHP deployments.

● With ZSCM you can build a real PHP cluster stack.

Page 33: How to scale PHP applications

© All rights reserved. Zend Technologies, Inc.

Session sharing with ZSCM

● Session Clustering Extension

● Session Clustering Daemon (SCD)

● Storage backends: Disk / Memory

● Peer-to-peer protocol to communicate between nodes

● Session management with master-backup architecture

Page 34: How to scale PHP applications

© All rights reserved. Zend Technologies, Inc.

Zend Server Cluster Manager architecture

Load Balancer

MySQL

Firewall

Page 35: How to scale PHP applications

© All rights reserved. Zend Technologies, Inc.

High availability: session cluster

Page 36: How to scale PHP applications

© All rights reserved. Zend Technologies, Inc.

High availability: session cluster (2)

Page 37: How to scale PHP applications

© All rights reserved. Zend Technologies, Inc.

Session cluster with ZSCM

● Pros:

▶ Very Fast▶ Reliable (data are stored in master-backup

servers)▶ High Availability▶ Scalable (session data are stored in all the

nodes of the cluster)▶ No changes to the PHP code

● Cons:

▶ You need Zend Server on each node of the cluster

Page 38: How to scale PHP applications

© All rights reserved. Zend Technologies, Inc.

Questions?

Page 39: How to scale PHP applications

© All rights reserved. Zend Technologies, Inc.

Thank you for attending!

More info:http://www.zend.comhttp://phpconference.es