$30 off During Our Annual Pro Sale. View Details »

PSR-6 & Symfony Cache: fast by standards

Nicolas Grekas
September 16, 2016

PSR-6 & Symfony Cache: fast by standards

After several months of discussion, the PHP-FIG (PHP Framework Interop Group) published the PSR-6. Its goal is to enable interoperability between caching systems implementations - a mandatory aspect to improve performance in your applications. At this conference, we will see the benefits and limitations of PSR-6, we will discuss the various existing cache solutions (Doctrine, Stash, php-cache), and I will present the new Symfony 3.1 Cache component and its upcoming improvements in version 3.2.

Nicolas Grekas

September 16, 2016
Tweet

More Decks by Nicolas Grekas

Other Decks in Technology

Transcript

  1. PSR-6 & Symfony Cache
    fast by standards
    #Symfony_Live
    @nicolasgrekas

    View Slide

  2. @nicolasgrekas
    # Symfony_Live

    View Slide

  3. #Symfony_Live @nicolasgrekas

    View Slide

  4. #Symfony_Live @nicolasgrekas

    View Slide

  5. #Symfony_Live @nicolasgrekas

    View Slide

  6. “There are only two hard things
    in Computer Science:
    - cache invalidation
    - and naming things.”
    -- Phil Karlton

    View Slide

  7. PSR-6
    Caching Interface
    Draft: Sept. 2013
    Final: Dec. 2015

    View Slide

  8. #Symfony_Live @nicolasgrekas
    • A common interface for basic and
    intermediate-level caching needs.
    • Allow for multiple independent extensions
    without collision.
    PSR-6 Goals

    View Slide

  9. #Symfony_Live @nicolasgrekas
    • Architectural compatibility with all existing cache
    implementations.
    • Advanced caching features such as namespacing
    or tagging.
    PSR-6 Non-Goals

    View Slide

  10. #Symfony_Live @nicolasgrekas
    • doctrine/cache 16.8M
    • sonata-project/cache 1.3M
    • zendframework/zend-cache 1.1M
    • illuminate/cache 0.5M
    • tedivm/stash 0.4M
    PSR-6 Prior-art on Packagist

    View Slide

  11. #Symfony_Live @nicolasgrekas
    PSR-6 Concerns

    View Slide

  12. #Symfony_Live @nicolasgrekas
    • Crippled with “Bylaws”
    • Not always representative
    • Strategic hijacking
    • Way too much noise/overhead
    • Mission is unclear
    PHP-FIG Weaknesses

    View Slide

  13. PHP-FIG Strength
    Standards!
    Let’s do with!

    View Slide

  14. #Symfony_Live @nicolasgrekas
    PSR-6 CacheItemPoolInterface

    View Slide

  15. #Symfony_Live @nicolasgrekas
    PSR-6 CacheItemInterface

    View Slide

  16. #Symfony_Live @nicolasgrekas
    PSR-6 Example #1

    View Slide

  17. #Symfony_Live @nicolasgrekas
    PSR-6 Example #2

    View Slide

  18. #Symfony_Live @nicolasgrekas
    • CacheItemInterface instances are not
    interoperable
    • Restricted characters for keys
    • false as error reporting
    • No atomicity/transactionality
    • Lazyness is unspecified
    PSR-6 Edge-cases

    View Slide

  19. #Symfony_Live @nicolasgrekas
    • See on Packagist
    https://packagist.org/providers/psr/cache-implementation
    • Stash PHP – stashphp.com
    • PHP Cache – php-cache.com
    • and …
    PSR-6 Implementations

    View Slide

  20. Welcome
    Symfony Cache

    View Slide

  21. #Symfony_Live @nicolasgrekas
    • Be the most powerful PHP caching library
    • A strict PSR-6 implementation
    • Following Symfony’s quality processes*
    • Just the bare minimum to be fast by default
    * Thanks "cache/integration-tests"!
    Symfony Cache Goals in 3.1

    View Slide

  22. #Symfony_Live @nicolasgrekas
    • ApcuAdapter
    • ArrayAdapter
    • FilesystemAdapter
    • RedisAdapter
    • …it’s up to you to add more
    Base Symfony Cache Adapters

    View Slide

  23. #Symfony_Live @nicolasgrekas
    • AbstractAdapter
    • ChainAdapter
    • DoctrineAdapter
    • ProxyAdapter
    • + DoctrineProvider
    Special Symfony Cache Adapters

    View Slide

  24. #Symfony_Live @nicolasgrekas
    Symfony Cache CacheItem

    View Slide

  25. #Symfony_Live @nicolasgrekas
    Symfony Cache AbstractAdapter

    View Slide

  26. #Symfony_Live @nicolasgrekas
    Doctrine 834.863 items/ms
    PHP Cache 313.334 items/ms
    Stash PHP 55.346 items/ms
    Symfony 533.366 items/ms
    Bench Symfony Cache

    View Slide

  27. #Symfony_Live @nicolasgrekas
    Doctrine 705.001 items/ms
    PHP Cache 362.477 items/ms
    Stash PHP 153.803 items/ms
    Symfony 845.910 items/ms
    Bench Symfony Cache

    View Slide

  28. Symfony Framework
    Wiring

    View Slide

  29. #Symfony_Live @nicolasgrekas
    Symfony Framework Concepts
    Front 1 - APCu Front 2 - APCu
    Redis
    Pool 1: Code Annotations
    Pool 2: Validation
    Pool 3: SQL results Pool 4: API responses

    View Slide

  30. #Symfony_Live @nicolasgrekas
    • « cache.app » for model sourced items
    o Defaults to filesystem, change it to Redis
    • « cache.system » for code sourced items
    o Defaults to APCu+filesystem chain
    Symfony Framework Services

    View Slide

  31. #Symfony_Live @nicolasgrekas
    Symfony Framework Semantic Config

    View Slide

  32. #Symfony_Live @nicolasgrekas
    Symfony Framework Semantic Config

    View Slide

  33. #Symfony_Live @nicolasgrekas
    Symfony Bundle Config

    View Slide

  34. #Symfony_Live @nicolasgrekas
    Wrap Symfony Cache as Doctrine Providers

    View Slide

  35. What’s next?

    View Slide

  36. #Symfony_Live @nicolasgrekas
    • Cache warming-up
    o Regeneration Before Expiration
    o Desynchronized Cache Misses
    o Stampede Protection
    Beyond PSR-6

    View Slide

  37. #Symfony_Live @nicolasgrekas
    • Cache invalidation
    o Hierarchy?
    o Tagging?
    Beyond PSR-6

    View Slide

  38. #Symfony_Live @nicolasgrekas
    • New adapter: PDO/DBAL, PHP array/file, Null
    • Tags based invalidation
    o $cache->invalidateTags($tags);
    • Hierarchical invalidation
    o $cache->withContext($contextChecksum);
    • cache:pool:clear command
    • Warm-up Validator, Serializer, etc.
    • Deprecate per components *CacheInterface
    Symfony 3.2

    View Slide

  39. #Symfony_Live @nicolasgrekas
    • Serve stale items while refreshing?
    • Desynchronized Cache Misses?
    • Add a profiler panel with hits/misses?
    • …add your ideas here
    Symfony 3.3?

    View Slide

  40. Merci !
    #Symfony_Live
    @nicolasgrekas

    View Slide