+ All Categories
Home > Documents > How to Scale Php Application

How to Scale Php Application

Date post: 25-Nov-2014
Category:
Upload: jellydn
View: 116 times
Download: 0 times
Share this document with a friend
Popular Tags:
38
ow to scale PHP applications by Jan Burkl, Enrico Zimuel Zend Technologies http://www.zend.com [email protected], [email protected] 30th October 2010 – PHP Barcelona Conference © All rights reserved. Zend Technologies, Inc.
Transcript
Page 1: How to Scale Php Application

How to scale PHP applications

by Jan Burkl, Enrico ZimuelZend Technologies

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

30th October 2010 – PHP Barcelona Conference

© All rights reserved. Zend Technologies, Inc.

Page 2: How to Scale Php Application

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

© All rights reserved. Zend Technologies, Inc.

Page 3: How to Scale Php Application

Scalability of a webapplication

© All rights reserved. Zend Technologies, Inc.

Page 4: How to Scale Php Application

Scalability: general definition

“Scalability is a desirable property of asystem, a network, or a process, whichindicates its ability to either handlegrowing amounts of work in a gracefulmanner or to be enlarged”

Source: Wikipedia

© All rights reserved. Zend Technologies, Inc.

Page 5: How to Scale Php Application

Scalability of a web application

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

© All rights reserved. Zend Technologies, Inc.

Page 6: How to Scale Php Application

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

© All rights reserved. Zend Technologies, Inc.

Page 7: How to Scale Php Application

Scale up vs. Scale out

vs.

Scale up

© All rights reserved. Zend Technologies, Inc.

Scale out

Page 8: How to Scale Php Application

The web scale out

As Google taught, the best way to scale an high trafficweb application is horizontally

No expensive servers to scale horizontally

A Google server

We need load balancers to split the trafficbetween all the servers

© All rights reserved. Zend Technologies, Inc.

Page 9: How to Scale Php Application

A typical load balancer architecture

Load Balancer

Firewall

Internet

Web Servers

© All rights reserved. Zend Technologies, Inc.

Page 10: How to Scale Php Application

How to scale a PHP application?

The PHP application uses session/local data?

Yes = we have to manage the session/localdata 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

© All rights reserved. Zend Technologies, Inc.

Page 11: How to Scale Php Application

Persistent load balancer

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

● Pros:

No software architecture changes●Cons:

No fault tolerant, if a server goes down thesession data are lost!

The load balancer is the bottleneck

The persistent load balancer are expensive

© All rights reserved. Zend Technologies, Inc.

Page 12: How to Scale Php Application

Share the session data

Manage the session data across the servers

● Pros:

Fault tolerant, if a server goes down the loadbalancer can user another server

No bottleneck in the load balancer

The stateless load balancer are cheaper●Cons:

Software architecture changes

© All rights reserved. Zend Technologies, Inc.

Page 13: How to Scale Php Application

PHP session management

© All rights reserved. Zend Technologies, Inc.

Page 14: How to Scale Php Application

Sessions in PHP

Session support in PHP consists of a way to preserve certaindata 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 theserver

In PHP we manage the session data using the $_SESSIONglobal variable

© All rights reserved. Zend Technologies, Inc.

Page 15: How to Scale Php Application

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 usedfor storing and retrieving data associated witha session (files by default).

session.save_path

defines the argument which is passed to thesave handler (with files handler is the path tostore the session data)

© All rights reserved. Zend Technologies, Inc.

Page 16: How to Scale Php Application

Example (PHP sessions using files)

<?phpsession_start();

$_SESSION['user']= 'enrico';

In session folder (for instance, /tmp) the PHP creates a filenamed sess_fvi9r84f14sjel8r28o6aqspr2 (wherefvi9r84f14sjel8r28o6aqspr2 is PHPSESSID) that contains:

user|s:6:"enrico";

© All rights reserved. Zend Technologies, Inc.

Page 17: How to Scale Php Application

Share session data

How to share PHP sessions between multipleservers?

Using a Network File System

Using a Database

Using Memcached

Using Redis

Using Zend Server Cluster Manager▶ etc

© All rights reserved. Zend Technologies, Inc.

Page 18: How to Scale Php Application

session_set_save_handler()

You can write your PHP session handler usingthe 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

© All rights reserved. Zend Technologies, Inc.

Page 19: How to Scale Php Application

Session sharingusing NFS

© All rights reserved. Zend Technologies, Inc.

Page 20: How to Scale Php Application

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

© All rights reserved. Zend Technologies, Inc.

Page 21: How to Scale Php Application

Session sharingusing Database

© All rights reserved. Zend Technologies, Inc.

Page 22: How to Scale Php Application

Session sharing using DB

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

Use the session_set_save_handler() to implementyour db session handler

Which db?▶MySQL

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

Ms SQL Serverhttp://www.zimuel.it/blog/?p=402

PostgreSQL, Oracle, etc

© All rights reserved. Zend Technologies, Inc.

Page 23: How to Scale Php Application

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

© All rights reserved. Zend Technologies, Inc.

Page 24: How to Scale Php Application

Session sharingusing Memcached

© All rights reserved. Zend Technologies, Inc.

Page 25: How to Scale Php Application

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 thememory is full)

© All rights reserved. Zend Technologies, Inc.

Page 26: How to Scale Php Application

Session sharingusing Redis

© All rights reserved. Zend Technologies, Inc.

Page 27: How to Scale Php Application

What is Redis?

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

It's similar to memcached but thedataset is not volatile

Developed by: Salvatore Sanfilippo

Sponsored by vmware

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

© All rights reserved. Zend Technologies, Inc.

Page 28: How to Scale Php Application

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

© All rights reserved. Zend Technologies, Inc.

Page 29: How to Scale Php Application

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!

© All rights reserved. Zend Technologies, Inc.

Page 30: How to Scale Php Application

Session sharingusing Zend ServerCluster Manager

© All rights reserved. Zend Technologies, Inc.

Page 31: How to Scale Php Application

What is Zend Server and Cluster Manager?

Zend Server is a complete, enterprise-readyWeb Application Server for running andmanaging PHP applications that require ahigh level of reliability, performance andsecurity on Linux, Windows or IBM i.

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

With ZSCM you can build a real PHP clusterstack.

© All rights reserved. Zend Technologies, Inc.

Page 32: How to Scale Php Application

Session sharing with ZSCM

Session Clustering Extension

Session Clustering Daemon (SCD)

Storage backends: Disk / Memory

Peer-to-peer protocol to communicatebetween nodes

Session managementwith master-backuparchitecture

© All rights reserved. Zend Technologies, Inc.

Page 33: How to Scale Php Application

Zend Server Cluster Manager architecture

Load Balancer Firewall

MySQL

© All rights reserved. Zend Technologies, Inc.

Page 34: How to Scale Php Application

High availability: session cluster

© All rights reserved. Zend Technologies, Inc.

Page 35: How to Scale Php Application

High availability: session cluster (2)

© All rights reserved. Zend Technologies, Inc.

Page 36: How to Scale Php Application

Session cluster with ZSCM

● Pros:

Very Fast

Reliable (data are stored in master-backupservers)

High Availability

Scalable (session data are stored in all thenodes of the cluster)

No changes to the PHP code●Cons:

You need Zend Server on each node of thecluster

© All rights reserved. Zend Technologies, Inc.

Page 37: How to Scale Php Application

Questions?

© All rights reserved. Zend Technologies, Inc.

Page 38: How to Scale Php Application

Thank you for attending!

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

© All rights reserved. Zend Technologies, Inc.


Recommended