Forest header image

Symfony Finland
Random things on PHP, Symfony and web development

Symfony has no model, but many

The Symfony Framework is promoted as a complete HMVC web application framework written in PHP. In practice most applications are built with MySQL using Doctrine or Propel ORM (Object Relational Mapper). It's easy to assume that this is something you're coupled with. But the Symfony Framework itself has no defined model.

As an example, let's consider a fictional online video service similar to Foxplay - a popular online video service in Finland and other countries.

At the core of the service stands the video content itself. Just like any other content managed on the web, it can be a complex domain in itself; You need to manage user permissions, versioning, user management, access control, content imports, language versions, etc. Video storage and distribution is provided from a separate tool.

In addition to the challenge of managing content, the delivery itself can be a huge challenge. Live sports are likely the most profitable business in online video today. Live broadcasting of online video content to subscribers creates a surge in traffic that is not trivial to cache. This can lead to capacity problems and a flood of negative customer feedback.

Adding to all of this, you've likely got marketing activities and what not that require creation of short urls to content automatically. For display on TV or for measuring effectiveness of offline marketing campaigns or something similar.

So let's see how the Symfony Framework could be used to solve those three different domains with different data storages:

  1. Fully featured Content Management
  2. High performance, session specific content
  3. Flexible creation of short URLs from external sources

1. Fully featured Content Management

As the main data source we could use a number of content management tools, but to keep this all in Symfony we'll use eZ Platform. With the Long Term Support (LTS) releases of both Symfony and eZ Platform this is a stable option for building your business on.

eZ Platform is a feature complete content management system built on the experience gained from eZ Publish. eZ Platform is built on the Symfony Framework and as such it integrates fully to the system, which will allow us to leverage the framework security facilities, cloud I/O, context aware HTTP caching and a more.

As the stable central storage of content eZ Platform will also import data from a number sources such as broadcasting systems, subscription systems, etc. It also feeds other systems such as mobile clients and set top boxes. This can be done using the complete REST API. This API has been around for years and is used for the administration interface so it can be considered as robust.

For data storage eZ Platform uses a combination of a Relational Database (such as MySQL) and Search Engines (Solr, ElasticSearch).

2. High performance, session specific content

For traffic peaks the Symfony Framework itself can handle quite a few requests. A relational database with automatically generated queries is the likely bottle neck. You could leverage the eZ Platform's integrated search solutions (Solr and ElasticSearch) to improve performance. But for the sake of argument let's push in a Document Database, MongoDB.

To store data from the eZ Platform content repository to MongoDB we can build upon Doctrine ODM and eZ Platform signals. Upon creation, modification and deletion of content signals are triggered and update the MongoDB collection(s). This will ensure that the data is in sync with the main content repository and you have full control of what you want to push to the database.

With the data in a Document Database you can allow other teams and systems access to this data. This storage can be used by microservices providing authentication tokens for streaming video or smart phone apps, for example. In addition to this data, the microservices can make calls to the main content repository for user authentication via REST APIs, etc.

This external MongoDB data storage provides high performance, up-to-date access to the content you wish to expose. It can be scaled and accessed independently of the master data storage.

3. Flexible creation of short URLs from external sources

A video site with a library hundreds of thousands of items will have a lot of URLs. While eZ Platform itself as the core platform has advanced URL management utilities, we will create and additional layer for automatically generated short URLs used in TV inserts or QR codes.

We could store this data in the eZ Platform repository, but for disposable short URIs having a full version history, etc. is too much overhead. So we'll choose to use Redis, a key-value store, to store these values. Adding Redis to our application stack has minimum overhead, since eZ Platform already uses it for content query caching via the StashBundle.

So we'll create a simple REST API using the FOSRestBundle that will use Redis as a data storage. This API can then be accessed via external services such as production systems for creation of short URLs programatically. For authentication we can rely on the eZ Platform user database as eZ Platform integrates with the framework security layer.

To integrate this URL layer to our application we'll simply add it to our list of chained routes. Upon page load the routing system will first look up if there is a matching URL (e.g. http://video.fi/foobar) in the key-value storage and will perform a simple redirect to the final URL. Note that this is only done once, since our main documents are stored in a Varnish Reverse Proxy.

Conclusion

As described in the fictional use case above, Symfony is indeed not coupled to any specific Model in the MVC architecture. It can reliably accomodate different types of data storages for different use cases and exposing data to other applications best built in Node, for example.

For adding purchase capabilities you could integrate Sylius and a separate model for eCommerce capabilities. There are no limits. But consider if you really need another Model. Just so you could use a Graph Database like Neo4J with Symfony.

Web services are becoming increasingly integrated services, mixing various application domains (custom functionality, content management, authentication, etc.). Symfony stands as a viable alternative to traditional .NET and Java frameworks such as .NET MVC and Spring.


Written by Jani Tarvainen on Saturday August 15, 2015
Permalink - Tags: symfony, php, node, mysql, mongodb, elasticsearch, redis

« Spotlight: Puli, Deployer, Async execution of the Slim Framework - eZ Publish 4.x to 5.x Upgrade Paths »