an introduction to scaling symfony

15
Scaling Symfony Whip your Website into Shape Brent Shaffer @bshaffer CentreSource Interactive Agency www.brentertainment.com 1

Upload: brent-shaffer

Post on 06-May-2015

15.691 views

Category:

Technology


0 download

DESCRIPTION

A brief introduction to cache strategy and other performance-enhancing techniques when using the symfony full-stack framework.

TRANSCRIPT

Page 1: An Introduction to Scaling Symfony

Scaling SymfonyWhip your Website into Shape

Brent Shaffer@bshaffer

CentreSource Interactive Agencywww.brentertainment.com

1

Page 2: An Introduction to Scaling Symfony

Can Symfony Scale?Dan Grossman: Symfony is used for Delicious and Yahoo! Bookmarks, which “[proves] it’s enterprise-ready and able to scale up to millions of active users”

Delicious says: “our new platform has been stable and is meeting our performance goals. In the days of the old Delicious, we had to put a lot of effort into just keeping the lights on and struggling to keep up with “growth. With our new infrastructure, these problems are largely gone and our pagers have never been so quiet. But more importantly the Delicious service is now faster and more reliable, which was a key goal of this project. In fact, I’m very happy to say that Delicious has experienced zero downtime since the day after launch”

*http://www.dangrossman.info/2008/08/03/delicious-proof-that-symfony-is-a-scalable-framework/

2

Page 3: An Introduction to Scaling Symfony

It’s All About The CacheCache == Time

Performance time, processing time, etc.

Time == Money

Money == Cash

Cache == Cash?*

*Yes, this is a bad joke. Please disregard it.

3

Page 4: An Introduction to Scaling Symfony

Where Do I Spend my Cache?

Byte-Code Cache

Doctrine Query Cache

Doctrine Result Cache

Page Cache

Partial Cache

Memcache

APC

FileCache*

XCache

E Accelerator*

DB Cache (Doctrine Only)

Array Cache (Memory Storage)

Types of Cache Cache Drivers

*Not available in Doctrine

4

Page 5: An Introduction to Scaling Symfony

Query Caching1.Init new DQL query2.Parse DQL query3.Build database specific SQL query4.Execute the SQL query5.Build the result set6.Return the result set

“The query cache has no disadvantages... always use query caching in your

production environment”- Doctrine Website

$query->where('a.id = ?', $id); $query->where('a.id = '. $id);

Query Lifecycle:

There IS a disadvantage: Query Cache must be cleared upon updates to your DB. - Less-than Doctrine2: Restart Apache, remove database if applicable, etc. - Doctrine 2: use “clear cache” task Query Caching can be specified at the manager, connection, and query levels

WRONG!Right!

$managerOrConn->setAttribute(Doctrine::ATTR_QUERY_CACHE, new Doctrine_Cache_Apc());

Always use Prepared Statements (placeholders for dynamic content)

$q = Doctrine_Query::create() ->useQueryCache(new Doctrine_Cache_Memcache());

5

Page 6: An Introduction to Scaling Symfony

Result Caching1.Init new DQL query2.Parse DQL query3.Build database specific SQL query4.Execute the SQL query5.Build the result set6.Return the result set

Use Doctrine Attributes to set result caching at the manager, connection, or query level

$query->useResultCache(); // Designate query to use result cache$query->expireResultCache(); // Force result cache expiration$query->expireQueryCache(); // Force query cache expiration

Activate Result Cache on a query level

Query Lifecycle:

$managerOrConn->setAttribute( Doctrine::ATTR_RESULT_CACHE, new Doctrine_Cache_Apc());$managerOrConn->setAttribute( Doctrine::ATTR_RESULT_CACHE_LIFESPAN, new Doctrine_Cache_Apc());

$q = Doctrine_Query::create() ->useResultCache(new Doctrine_Cache_Memcache());

6

Page 7: An Introduction to Scaling Symfony

View Cache# /path/to/project/apps/myapp/modules/mymodule/config/cache.ymllist: enabled: true with_layout: false # Default Value lifetime: 86400 # Default Value

Action Cache

Partial/Component/Slot Cache

Template Fragment Cache

# /path/to/project/apps/myapp/modules/mymodule/config/cache.yml_partial_or_component: enabled: true with_layout: false # Default Value lifetime: 86400 # Default Value

<?php use_helper('Cache') ?>

7

Page 8: An Introduction to Scaling Symfony

Action CachingAction Cache Lifecycle

An incoming request with GET parameters or submitted with the POST, PUT, or DELETE method will never be cached by symfony, regardless of the configuration.

8

Page 9: An Introduction to Scaling Symfony

Action CachingLayout Cache Lifecycle

This time, the whole response object is stored in cache. This means that the cache with layout is much faster than the cache without it.

9

Page 10: An Introduction to Scaling Symfony

Doctrine View Cache

Can specify if cache is cleared on Insert, Update, and Delete

Limitations:

(http://symplist.net/plugins/sfDoctrineViewCachePlugin)

1. No application control for individual routes

2. Redeclaration of routing rules (name to module/action/params)

3. No official release

4. All configurations take place in your model’s SCHEMA?!

5. Great concept, lacks polish

Utilizes Doctrine Routes and a Doctrine Behavior to clear the view cache for specified actions of the same class

# /path/to/project/config/doctrine/schema.yml BlogPost: actAs: sfViewCache: global: true # clear cache globally clear_routes: frontend # clear sfDoctrineRoutes for this object class on_delete: false # trigger on event (also: on_create and on_update)

10

Page 11: An Introduction to Scaling Symfony

Partial/Component/Slot Caching

Slots are part of the template, and caching an action will also

store the value of the slots defined in this action's template. So

the cache works natively for slots.

Partial/component caching is only useful when utilized outside of

action caching or with partials/components within a layout.

Caching for global partials and components (the ones located in

the application templates/ directory) takes place in the application

cache.yml.

11

Page 12: An Introduction to Scaling Symfony

Fragment Caching

<!-- Template Code (not cached) --><h1>Welcome, <?php echo $sf_user->getGuardUser() ?>!</h1>

<!-- Cached code --><?php if (!cache('products', 43200)): ?> <?php foreach ($products as $product): ?> <li><?php echo link_to($product, '@product?slug='.$product['slug']) ?></li> <?php endforeach; ?> <?php cache_save() ?><?php endif; ?>

Use Cache Helper

Fragment caching is useful only if contained in an uncached view

12

Page 13: An Introduction to Scaling Symfony

Tips and Tricks

http://symplist.net/plugins/npAssetsOptimizerPlugin

npAssetsOptimizer - minification

sfTesterViewCache

public function isCached($boolean, $with_layout = false)

sfSuperCache

Writes view cache to web directory to allow apache to serve up pages without starting symfony

13

Page 14: An Introduction to Scaling Symfony

Tips and Tricks

$ php symfony cc --type=template --env=prod --app=frontend

Clear individual templates / environments:

System TuningMysql - http://www.day32.com/MySQL/

(config, i18n, routing, module and template types)

all: routing: class: sfPatternRouting param: generate_shortest_url: true extra_parameters_as_query_string: true lazy_routes_deserialize: true

Lazy Route Serialization

14

Page 15: An Introduction to Scaling Symfony

Questions?

Brent Shaffer@bshaffer

CentreSource Interactive Agencywww.brentertainment.com

15