💾 Archived View for chirale.org › 2016-11-12_2455.gmi captured on 2024-07-08 at 23:32:41. Gemini links have been rewritten to link to archived content

View Raw

More Information

⬅️ Previous capture (2024-05-12)

-=-=-=-=-=-=-

Reduce Time to the First Byte – TTFB on web applications

How to speed up the time to the first byte and what are the causes of a long TTFB? Main causes are network and server-side and I will focus on server-side causes. I’m not covering any CMS here but you can try to apply some of these techniques starting from how to interpret the browser Timing.

Get reliable timing

Take a website with cache enabled: at the 9th visit on a page you can be sure your page is in cache, the connection with the webserver is alive, the SSL/TLS connection is established, the SQL queries are cached and so on. Open the network tab and enjoy your site speed: well, very few real users will experience that speed.

Here a comparison of a first time, no-cache connection to a nginx webserver explored with Chrome (F12 \> Network \> Timing) and a second request with the same page refreshed right after the first:

performance-01

I got a +420% on a first time request compared with a connected-and-cached case. To obtain a reliable result (1st figure) you should usually:

This technique bypass the Django view cache and similar cache systems on other frameworks. To check the framework cache impact, do a Ctrl+shift+R just after the first request obtaining a similar result of the 2nd figure. There are better ways to do the same, this is the easiest.

Break up the time report

Unpack the time report of the first-time request:

* Queueing: slight, nothing to do.

* Stalled: slight, nothing to do.

* DNS lookup: slight, nothing to do.

* Initial connection: significant, skip for now.

* SSL: significant, client establish a SSL/TLS connection with the webserver. Disabling ciphers or tuning SSL can reduce the time but the priority here is best security for the visitor, not pure speed. However, take a look at this case study if you want to tune SSL/TLS for speed.

* Request sent: slight, browser-related, nothing to do.

* Waiting (TTFB): significant, time to first byte is the time the user wait after the request was sent to the web server. The waiting time includes:

* Framework elaboration.

* Database queries.

* Content Download: significant, page size, network, server and client related. To speed up content download of a HTML page you should add compression: here an howto for nginx and for Apache webservers: these covers proxy servers, applying directly on a virtualhost is even simplier and the performance gain is huge.

Not surprisingly, the time of a first time request is elapsed most in Request / response than on connection setup. Among the Request / response times is the Waiting (TTFB) the prominent. Luckyly it is the same segment covered by cache mechanics of the framework and consequently is the most eroded passing from the first (not cached) to the second figure (cached by the framework). To erode the TTFB, database queries and elaboration must be optimized.

Optimize elaboration: program optimization

When Google, the web-giant behind the most used web search engine in history, try to suggest some tips to optimize PHP to programmers they react badly starting from daily programmers going up to the PHP team bureau.

daily programmers

In a long response, the PHP team teach Google how to program the web offering unsolicited advice offering “some thoughts aimed at debunking these claims” with stances like “Depending on the way PHP is set up on your host, echo can be slower than print in some cases”, a totally confusing comment for a real-world programmer.

long response

Google put offline the PHP performance page that can be misleading but still contains valid optimization tips, especially if you compare with some of comments on php.net itself. Google have interests to speed and code optimization and the writer has the know-how to talk about it, the PHP team here just want to be right and defend their language and starting from good points crossed the line of scientific dialectic.

PHP performance page

Program optimization mottos are:

Look for the best language that suits to your work and the best tools you can and look for programmers from the real-world sharing their approaches to the program optimization.

PHP team’s whining will not change the fact that avoiding SQL inside a loop like Google employee suggested is the right thing to do to enhance performance. This leads to database optimization.

Dude, where is my data?

The standard web application nowadays has this structure:

A typical web application: application server run the application so from now on  – oversimplifying – I will treat application and application servers as synonyms.

After the client requests pass through the firewall, webserver serve static files and ask to Application server the dynamic content.

Cache server can serve application or web server but in this example the earlier has the control: an example of cache controlled by application is on the Django docs about Memcache, an example of cache by web server is the HTTP Redis module or the standard use of Varnish cache.

Django docs about Memcache

HTTP Redis module

Varnish cache

Database server (DBMS) stores the structured data for the application. DBMS on standard use cases can be optimized with little effort. More difficult is to optimize the way the web application get the data from the database.

Database query optimization: prefetch and avoid duplicates

To optimize database queries you have to check the timing, again. Depending on the language and framework you are using there are tools to get information about queries to optimize:

Since I’m using Python I go with Django Debug Toolbar, a de-facto standard for application profiling. Here a sample of SQL query timing on a PostgreSQL database:

Timing of SQL queries on Django Debug Toolbar.

The total time elapsed on queries is 137,07 milliseconds, the total number of queries executed are Among these, 85 are duplicates. Below any query you’ll find how many times the same query is executed. The objective is to reduce the number of queries executed.

If you’re using Django, create a manager for your models.py to use like this:

 class GenericManager(models.Manager): """ prefetch_related: join via ORM select_related: join via database """ related_models = ['people', 'photo_set'] def per_organizer(self, orgz, **kwargs): p = kwargs.get('pubblicato', None) ret = self.filter(organizer = orgz) return ret class People(models.Model): name = models.CharField(max_length=50) ... class Party(models.Model): organizer = models.ForeignKey('People') objects = GenericManager() class Photo(models.Model): party = models.ForeignKey('Party') ... 

Then in views.py call your custom method on GenericManager:

 def all_parties(request, organizer_name): party_organizer = People.objects.get(name=organizer_name) all_parties = Party.objects.per_organizer(party_organizer) return render(request, 'myfunnywebsite/parties.html', { 'parties' : all_parties }) 

When you want to optimize data retreival for Party, instead of comb through objects.filter() methods on views.py you will fix only the per_organizer method like this:

 class GenericManager(models.Manager): """ prefetch_related: join via ORM select_related: join via database """ related_models = ['people', 'photo_set'] def per_organizer(self, orgz, **kwargs): ret = self.filter(organizer = orgz) return ret.prefetch_related(*self.related_models) 

Using prefetch_related queries are grouped via ORM and all objects are available, avoiding many query duplicates. Here a result of this first optimization:

prefetch_related

django_sql_query_debug_toolbar_2

An alternative method is select_related, but in this case the ORM will produce a join and the above code will give an error because photo_set is not accessible in this way. If your models are structured in a way you got a better performance with select_related go with it but remember this limitation. In this use case the results of select_related are worse than prefetch_related.

select_related

Recap:

In my experience, inefficient code and a lot of cache are a frail solution compared with the right balance between caching and query + program optimization.

If you’ve tried everything and the application is still slow, consider to rewrite it or even to change the framework you’re using if speed is critical. When any optimization failed, I went from a Drupal 6 to a fresh Django 8 installation, and Google understood the difference in milliseconds elapsed to download the pages during indexing:

downloadtime

Since you can’t win a fight with windmills, a fresh start may be the only effective option on the table.

https://web.archive.org/web/20161112000000*/https://chirale.wordpress.com/2016/11/12/reduce-time-to-the-first-byte-ttfb-on-web-applications/performance-01/

https://web.archive.org/web/20161112000000*/http://dojo.techsamurais.com/?p=1384

https://web.archive.org/web/20161112000000*/https://en.wikipedia.org/wiki/Time_To_First_Byte

https://web.archive.org/web/20161112000000*/http://chirale.org/2015/09/18/how-to-enable-gzip-on-proxy-servers-on-nginx/

https://web.archive.org/web/20161112000000*/http://chirale.org/2013/07/15/how-to-enable-gzip-on-proxy-servers-on-apache/

https://web.archive.org/web/20161112000000*/https://www.sitepoint.com/a-note-on-googles-so-called-best-practises/

https://web.archive.org/web/20161112000000*/https://groups.google.com/forum/#!topic/make-the-web-faster/3fvoLdgECMw

https://web.archive.org/web/20161112000000*/https://web.archive.org/web/20140625191431/https://developers.google.com/speed/articles/optimizing-php

https://web.archive.org/web/20161112000000*/https://adambard.com/blog/you-write-php-because-you-dont-know-better/

https://web.archive.org/web/20161112000000*/https://chirale.wordpress.com/2016/11/12/reduce-time-to-the-first-byte-ttfb-on-web-applications/web-application/

https://web.archive.org/web/20161112000000*/https://docs.djangoproject.com/en/1.10/topics/cache/#memcached

https://web.archive.org/web/20161112000000*/https://www.nginx.com/resources/wiki/modules/redis/

https://web.archive.org/web/20161112000000*/https://www.varnish-cache.org/docs/4.0/tutorial/introduction.html

https://web.archive.org/web/20161112000000*/http://django-debug-toolbar.readthedocs.io/en/stable/

https://web.archive.org/web/20161112000000*/https://xdebug.org/

https://web.archive.org/web/20161112000000*/http://php.net/manual/en/book.xhprof.php

https://web.archive.org/web/20161112000000*/https://www.drupal.org/project/devel

https://web.archive.org/web/20161112000000*/http://dev.mysql.com/doc/en/slow-query-log.html

https://web.archive.org/web/20161112000000*/https://chirale.wordpress.com/2016/11/12/reduce-time-to-the-first-byte-ttfb-on-web-applications/django_sql_query_debug_toolbar/

https://web.archive.org/web/20161112000000*/https://docs.djangoproject.com/en/1.10/ref/models/querysets/#prefetch-related

https://web.archive.org/web/20161112000000*/https://chirale.wordpress.com/2016/11/12/reduce-time-to-the-first-byte-ttfb-on-web-applications/django_sql_query_debug_toolbar_2/

https://web.archive.org/web/20161112000000*/https://docs.djangoproject.com/en/1.10/ref/models/querysets/#select-related

https://web.archive.org/web/20161112000000*/http://redis.io/

https://web.archive.org/web/20161112000000*/https://memcached.org/

https://web.archive.org/web/20161112000000*/https://chirale.wordpress.com/2015/10/10/guide-to-migrate-a-drupal-website-to-django-after-the-release-of-drupal-8/downloadtime/