Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Das Backend von ComputerBase

Sponsored · Ship Features Fearlessly Turn features on and off without deploys. Used by thousands of Ruby developers.
Avatar for Steffen Weber Steffen Weber
September 01, 2014

Das Backend von ComputerBase

Vortrag im Rahmen der Webentwickler Paderborn – http://webentwickler-paderborn.de/events/2014/08/11/backend-computerbase.html

Avatar for Steffen Weber

Steffen Weber

September 01, 2014
Tweet

Other Decks in Technology

Transcript

  1. Reichweite 2,5 Mio Unique User ( ) AGOF Juni 2014

    CHIP.de / computerbild.de: ~ 13 Mio Golem.de / pcgameshardware.de: ~ 1,3 Mio 1 Mio Seitenaufrufe pro Tag
  2. MySQL-Performance 27 GB ≤ 64 GB InnoDB > MyISAM RAID-Controller

    mit 1 GB Cache + BBU Alternative SSD? Tuning-Tipps: MySQL Performance Blog InnoDB performance optimization basics (redux)
  3. Schema-Änderung großer Tabellen Problem: A L T E R T

    A B L E … ⇒ Lock ⇒ Downtime :- ( Lösung: pt-online-schema-change $ p t - o n l i n e - s c h e m a - c h a n g e - - a l t e r = " A D D l o g o i d I N T " D = c b , t = c b _ n e w s A l t e r i n g ` c b ` . ` c b _ n e w s ` . . . C r e a t i n g n e w t a b l e . . . C r e a t e d n e w t a b l e c b . _ c b _ n e w s _ n e w O K . A l t e r i n g n e w t a b l e . . . A l t e r e d ` c b ` . ` _ c b _ n e w s _ n e w ` O K . C r e a t i n g t r i g g e r s . . . C r e a t e d t r i g g e r s O K . C o p y i n g a p p r o x i m a t e l y 1 1 1 9 r o w s . . . C o p i e d r o w s O K . S w a p p i n g t a b l e s . . . S w a p p e d o r i g i n a l a n d n e w t a b l e s O K . D r o p p i n g o l d t a b l e . . . D r o p p e d o l d t a b l e ` c b ` . ` _ c b _ n e w s _ o l d ` O K . D r o p p i n g t r i g g e r s . . . D r o p p e d t r i g g e r s O K . S u c c e s s f u l l y a l t e r e d ` c b ` . ` c b _ n e w s ` .
  4. MySQL-Backups Problem: m y s q l d u m

    p ⇒ Lock ⇒ Downtime :- ( Lösung: Achtung: Ausgabe ist kein SQL-Dump! Percona XtraBackup XtraBackup → GnuPG → Amazon S3
  5. Aufwärmen nach Neustart Einfache Lösung: Neue Lösung (InnoDB-only): Percona Server

    5.5: MySQL 5.6+: c a t / d a t a / m y s q l / c o m p u t e r b a s e / * > / d e v / n u l l Buffer Pool Dump/Restore Buffer Pool Preloading
  6. „Live“-Daten in Entwicklungsumgebung Server: d b - d e v

    - c o p y . p h p 1. Struktur der Datenbank klonen 2. Selektiv Daten in geklonte DB kopieren 3. m y s q l d u m p der geklonten DB an s t d o u t senden Entwicklungsumgebung: m y s q l - s y n c - d e v . s h # ! / b i n / s h s s h - C d b 2 . c o m p u t e r b a s e . d e ' d b - d e v - c o p y . p h p ' | \ m y s q l - u c o m p u t e r b a s e c o m p u t e r b a s e $ t i m e m y s q l - s y n c - d e v . s h r e a l 0 m 5 7 . 6 5 1 s
  7. Sicherheit: phpMyAdmin-Zugriff Standard-Lösung: HTTP Auth Separate Benutzer-Datenbank Rate Limiting? Ergänzung:

    SSH-Tunnel Server: phpMyAdmin erreichbar auf Port 1443, der in nginx und iptables nur für 127.0.0.1 / ::1 freigegeben ist Client-Terminal: s s h - N - L 1 4 4 3 : l o c a l h o s t : 1 4 4 3 < s e r v e r > Client-Browser: h t t p s : / / l o c a l h o s t : 1 4 4 3 /
  8. Vollständige UTF-8-Unterstützung UTF-8 kodiert Zeichen mit 1–4 Byte u t

    f 8 in MySQL: maximal 3 Byte je Zeichen Lösung ab MySQL 5.5: u t f 8 m b 4 ὠ ὃ ἇ Ἂ
  9. PHP-Einbindung Steinzeit: PHP-Modul läuft in jedem Apache- Prozess Heute: FastCGI,

    PHP als separater Daemon Umstellung auf nginx um 9 Uhr (22. Juli 2008)
  10. nginx.conf Mehrere PHP-Prozess-Pools (2) php-fpm.conf [ w w w ]

    l i s t e n = 1 2 7 . 0 . 0 . 1 : 9 0 0 0 p m = s t a t i c p m . m a x _ c h i l d r e n = 1 6 [ p r e i s v e r g l e i c h ] l i s t e n = 1 2 7 . 0 . 0 . 1 : 9 0 0 1 p m = s t a t i c p m . m a x _ c h i l d r e n = 2 l o c a t i o n / p r e i s v e r g l e i c h { f a s t c g i _ p a s s 1 2 7 . 0 . 0 . 1 : 9 0 0 1 ; . . . }
  11. PHP-Performance PHP selten das Bottleneck, aber ... PHP 5.6 >

    PHP 5.5 > PHP 5.4 > PHP 5.3 APC / OpCache
  12. DoS-Schutz in iptables 1. Beschränken der Anzahl neuer Verbindungen →

    iptables-Modul h a s h l i m i t 2. Beschränken der Anzahl gleichzeitiger Verbindungen → iptables-Modul c o n n l i m i t 3. Connection-Tracking-Timeouts herabsetzen → Datei / e t c / s y s c t l . c o n f n e t . n e t f i l t e r . n f _ c o n n t r a c k _ t c p _ t i m e o u t _ c l o s e _ w a i t = 1 0 n e t . n e t f i l t e r . n f _ c o n n t r a c k _ t c p _ t i m e o u t _ e s t a b l i s h e d = 3 6 0 0 . . .
  13. DoS-Schutz in nginx Motivation: HTTP-Requests nach dynamischen Seiten sind sehr

    viel teurer als HTTP-Requests nach statischen Dateien h t t p { . . . l i m i t _ r e q _ z o n e $ b i n a r y _ r e m o t e _ a d d r z o n e = f l o o d : 1 0 m r a t e = 2 r / s ; s e r v e r { . . . l o c a t i o n ~ \ . p h p $ { l i m i t _ r e q z o n e = f l o o d b u r s t = 8 ; . . . } } }
  14. Bonus: gzip_static + Zopfli 1. nginx-Modul : auf Requests nach

    m a i n . c s s antwortet nginx mit m a i n . c s s . g z 2. Deployment: Erstellen der . g z -Dateien mit Größe [Byte] Zeit [ms] main.css 72.865 - gzip 14.471 10 gzip -9 14.366 13 zop i 13.618 355 gzip_static Zop i
  15. Website: Performance Erfahrung: PHP ist (fast) nie das Bottleneck, daher

    Konzentration auf die Datenbank! 1. Während der Entwicklung: jede neue SQL-Query mit E X P L A I N S E L E C T . . . analysieren 2. Im Live-Betrieb: Einen Blick werfen in … MySQL Slow Query Log PHP-FPM Slow Log 3. Caching: nächste Folie
  16. Startseiten-Aufruf Speichern einer News Website: Caching : Speichern von HTML-Fragmenten

    Memcached $ c o d e = $ t h i s - > m e m c a c h e - > g e t ( ' i n d e x - n e w s ' , f u n c t i o n ( ) { $ h t m l = . . . ; / / G e n e r a t e H T M L r e t u r n $ h t m l ; } ) ; $ t h i s - > m e m c a c h e - > d e l e t e ( ' i n d e x - n e w s ' ) ;
  17. Website: Message Queue : Aufgaben an andere Server verteilen Gearman

    Bei News-Veröffentlichung $ t h i s - > g e a r m a n - > d o B a c k g r o u n d ( ' f e e d - u p d a t e ' , ' n e w s ' ) ; Hintergrund-Prozess $ w o r k e r = n e w L i b \ G e a r m a n W o r k e r ( ) ; . . . $ w o r k e r - > a d d F u n c t i o n ( ' f e e d - u p d a t e ' , [ L i b \ F e e d s : : c l a s s , ' u p d a t e F e e d ' ] ) ; . . . w h i l e ( $ w o r k e r - > w o r k ( ) ) ; Weitere Beispiele: Bild-Upload, Twitter-Status, …
  18. HTTPS Drei Vorteile Sicherheit: Vertraulichkeit, Authentizität, Integrität Performance: SPDY /

    HTTP 2.0 SEO: Besseres Google-Ranking (angeblich) Warum (noch) nicht auf CB? HTTPS-inkompatible Anzeigen Ausnahmen: Login-Seite, Passwort ändern, …
  19. Deployment mehrmals täglich, möglichst atomar 1. g i t p

    u l l in temporärem Verzeichnis 2. CSS und JS komprimieren (wenn geändert) 3. Kopieren der Dateien ins Ziel-Verzeichnis via r s y n c mit dem Parameter - - d e l a y - u p d a t e s