SlideShare a Scribd company logo
Building a Scalable Architecture for Web Apps -  Part I (Lessons Learned @ Directi) By Bhavin Turakhia  CEO, Directi  ( http://www.directi.com  |  http://wiki.directi.com  |  http://careers.directi.com ) Licensed under Creative Commons Attribution Sharealike Noncommercial
Agenda Why is Scalability important Introduction to the Variables and Factors Building our own Scalable Architecture (in incremental steps) Vertical Scaling Vertical Partitioning Horizontal Scaling Horizontal Partitioning …  etc Platform Selection Considerations Tips Creative Commons Sharealike Attributions Noncommercial
Why is Scalability Important in a Web 2.0 world Viral marketing can result in instant successes RSS / Ajax / SOA pull based / polling type XML protocols - Meta-data > data Number of Requests exponentially grows with user base RoR / Grails – Dynamic language landscape gaining popularity In the end you want to build a Web 2.0 app that can serve  millions of users  with  ZERO downtime Creative Commons Sharealike Attributions Noncommercial
The Variables Scalability  -  Number of users / sessions / transactions / operations the entire system can perform Performance  –  Optimal utilization of resources Responsiveness  –  Time taken per operation Availability  -  Probability of the application or a portion of the application being available at any given point in time Downtime Impact  -  The impact of a downtime of a server/service/resource - number of users, type of impact etc Cost Maintenance Effort Creative Commons Sharealike Attributions Noncommercial High : scalability, availability, performance & responsiveness Low : downtime impact, cost & maintenance effort
The Factors Platform selection Hardware Application Design Database/Datastore Structure and Architecture Deployment Architecture Storage Architecture Abuse prevention Monitoring mechanisms …  and more Creative Commons Sharealike Attributions Noncommercial
Lets Start … We will now build an example architecture for an example app using the following iterative incremental steps – Inspect current Architecture Identify Scalability Bottlenecks Identify SPOFs and Availability Issues Identify Downtime Impact Risk Zones Apply one of - Vertical Scaling Vertical Partitioning Horizontal Scaling Horizontal Partitioning Repeat process Creative Commons Sharealike Attributions Noncommercial
Step 1 – Lets Start … Creative Commons Sharealike Attributions Noncommercial Appserver & DBServer
Step 2 – Vertical Scaling Creative Commons Sharealike Attributions Noncommercial Appserver, DBServer CPU CPU RAM RAM
Step 2 - Vertical Scaling Introduction Increasing the hardware resources without changing the number of nodes Referred to as “Scaling up” the Server Advantages Simple to implement Disadvantages Finite limit Hardware does not scale linearly (diminishing returns for each incremental unit) Requires downtime Increases Downtime Impact Incremental costs increase exponentially Creative Commons Sharealike Attributions Noncommercial Appserver, DBServer CPU CPU RAM RAM CPU CPU RAM RAM
Step 3 – Vertical Partitioning (Services) Creative Commons Sharealike Attributions Noncommercial AppServer DBServer Introduction Deploying each service on a separate node Positives Increases per application Availability Task-based specialization, optimization and tuning possible Reduces context switching Simple to implement for out of band processes No changes to App required Flexibility increases Negatives Sub-optimal resource utilization May not increase overall availability Finite Scalability
Understanding Vertical Partitioning Creative Commons Sharealike Attributions Noncommercial The term Vertical Partitioning denotes – Increase in the number of nodes by distributing the tasks/functions Each node (or cluster) performs separate Tasks Each node (or cluster) is different from the other Vertical Partitioning can be performed at various layers (App / Server / Data / Hardware etc)
Step 4 – Horizontal Scaling (App Server) Creative Commons Sharealike Attributions Noncommercial AppServer AppServer AppServer Load Balancer DBServer Introduction Increasing the number of nodes of the App Server through Load Balancing Referred to as “Scaling out” the App Server
Understanding Horizontal Scaling Creative Commons Sharealike Attributions Noncommercial The term Horizontal Scaling denotes – Increase in the number of nodes by replicating the nodes Each node performs the same Tasks Each node is identical Typically the collection of nodes maybe known as a cluster (though the term cluster is often misused) Also referred to as “Scaling Out” Horizontal Scaling can be performed for any particular type of node (AppServer / DBServer etc)
Load Balancer – Hardware vs Software Creative Commons Sharealike Attributions Noncommercial Hardware Load balancers are faster Software Load balancers are more customizable With HTTP Servers load balancing is typically combined with http accelerators
Load Balancer – Session Management Creative Commons Sharealike Attributions Noncommercial Sticky Sessions Requests for a given user are sent to a fixed App Server Observations Asymmetrical load distribution (especially during downtimes) Downtime Impact – Loss of session data AppServer AppServer AppServer Load Balancer Sticky Sessions User 1 User 2
Load Balancer – Session Management Creative Commons Sharealike Attributions Noncommercial Central Session Store Introduces SPOF An additional variable Session reads and writes generate Disk + Network I/O Also known as a Shared Session Store Cluster AppServer AppServer AppServer Load Balancer Session Store Central Session Storage
Load Balancer – Session Management Creative Commons Sharealike Attributions Noncommercial Clustered Session Management Easier to setup No SPOF Session reads are instantaneous Session writes generate Network I/O Network I/O increases exponentially with increase in number of nodes In very rare circumstances a request may get stale session data User request reaches subsequent node faster than intra-node message Intra-node communication fails AKA Shared-nothing Cluster AppServer AppServer AppServer Load Balancer Clustered Session Management
Load Balancer – Session Management Creative Commons Sharealike Attributions Noncommercial Sticky Sessions with Central Session Store Downtime does not cause loss of data Session reads need not generate network I/O Sticky Sessions with Clustered Session Management No specific advantages Sticky Sessions AppServer AppServer AppServer Load Balancer User 1 User 2
Load Balancer – Session Management Creative Commons Sharealike Attributions Noncommercial Recommendation Use Clustered Session Management if you have – Smaller Number of App Servers Fewer Session writes Use a Central Session Store elsewhere Use sticky sessions only if you have to
Load Balancer – Removing SPOF Creative Commons Sharealike Attributions Noncommercial In a Load Balanced App Server Cluster the LB is an SPOF Setup LB in Active-Active or Active-Passive mode Note: Active-Active nevertheless assumes that each LB is independently able to take up the load of the other If one wants ZERO downtime, then Active-Active becomes truly cost beneficial only if multiple LBs (more than 3 to 4) are daisy chained as Active-Active forming an LB Cluster AppServer AppServer AppServer Load Balancer Active-Passive LB Load Balancer AppServer AppServer AppServer Load Balancer Active-Active LB Load Balancer Users Users
Step 4 – Horizontal Scaling (App Server) Creative Commons Sharealike Attributions Noncommercial DBServer Our deployment at the end of Step 4 Positives Increases Availability and Scalability No changes to App required Easy setup Negatives Finite Scalability Load Balanced  App Servers
Step 5 – Vertical Partitioning (Hardware) Creative Commons Sharealike Attributions Noncommercial DBServer Introduction Partitioning out the Storage function using a SAN SAN config options Refer to “Demystifying Storage” at  http://wiki.directi.com  -> Dev University -> Presentations Positives Allows “Scaling Up” the DB Server Boosts Performance of DB Server Negatives Increases Cost SAN Load Balanced  App Servers
Step 6 – Horizontal Scaling (DB) Creative Commons Sharealike Attributions Noncommercial DBServer Introduction Increasing the number of DB nodes Referred to as “Scaling out” the DB Server Options Shared nothing Cluster Real Application Cluster (or Shared Storage Cluster) DBServer DBServer SAN Load Balanced  App Servers
Shared Nothing Cluster Creative Commons Sharealike Attributions Noncommercial Each DB Server node has its  own complete  copy of the database Nothing is shared between the DB Server Nodes This is achieved through DB Replication at DB / Driver / App level or through a proxy Supported by most RDBMs natively or through 3 rd  party software DBServer Database DBServer Database DBServer Database Note: Actual DB files maybe stored on a central SAN
Replication Considerations Creative Commons Sharealike Attributions Noncommercial Master-Slave Writes are sent to a single master which replicates the data to multiple slave nodes Replication maybe cascaded Simple setup No conflict management required Multi-Master Writes can be sent to any of the multiple masters which replicate them to other masters and slaves Conflict Management required Deadlocks possible if same data is simultaneously modified at multiple places
Replication Considerations Creative Commons Sharealike Attributions Noncommercial Asynchronous Guaranteed, but out-of-band replication from Master to Slave Master updates its own db and returns a response to client Replication from Master to Slave takes place asynchronously Faster response to a client  Slave data is marginally behind the Master Requires modification to App to send critical reads and writes to master, and load balance all other reads Synchronous Guaranteed, in-band replication from Master to Slave Master updates its own db, and confirms all slaves have updated their db before returning a response to client Slower response to a client  Slaves have the same data as the Master at all times Requires modification to App to send writes to master and load balance all reads
Replication Considerations Creative Commons Sharealike Attributions Noncommercial Replication at RDBMS level Support may exists in RDBMS or through 3 rd  party tool Faster and more reliable App must send writes to Master, reads to any db and critical reads to Master Replication at Driver / DAO level Driver / DAO layer ensures  writes are performed on all connected DBs Reads are load balanced Critical reads are sent to a Master In most cases RDBMS agnostic Slower and in some cases less reliable
Real Application Cluster Creative Commons Sharealike Attributions Noncommercial All DB Servers in the cluster share a common storage area on a SAN All DB servers mount the same block device The filesystem must be a clustered file system (eg GFS / OFS) Currently only supported by Oracle Real Application Cluster Can be very expensive (licensing fees) DBServer SAN DBServer DBServer Database
Recommendation Creative Commons Sharealike Attributions Noncommercial Try and choose a DB which natively supports Master-Slave replication Use Master-Slave Async replication Write your DAO layer to ensure writes are sent to a single DB reads are load balanced Critical reads are sent to a master DBServer DBServer DBServer Writes & Critical Reads Other Reads Load Balanced  App Servers
Step 6 – Horizontal Scaling (DB) Creative Commons Sharealike Attributions Noncommercial Our architecture now looks like this Positives As Web servers grow, Database nodes can be added DB Server is no longer SPOF Negatives Finite limit Load Balanced  App Servers DB Cluster DB DB DB SAN
Step 6 – Horizontal Scaling (DB) Creative Commons Sharealike Attributions Noncommercial Shared nothing clusters have a finite scaling limit Reads to Writes – 2:1 So 8 Reads => 4 writes 2 DBs Per db – 4 reads and 4 writes 4 DBs Per db – 2 reads and 4 writes 8 DBs Per db – 1 read and 4 writes At some point adding another node brings in negligible incremental benefit Reads Writes DB1 DB2
Step 7 – Vertical / Horizontal Partitioning (DB) Creative Commons Sharealike Attributions Noncommercial Introduction Increasing the number of DB Clusters by dividing the data Options Vertical Partitioning - Dividing tables / columns Horizontal Partitioning - Dividing by rows (value) Load Balanced  App Servers DB Cluster DB DB DB SAN
Vertical Partitioning (DB) Creative Commons Sharealike Attributions Noncommercial Take a set of tables and move them onto another DB Eg in a social network - the  users  table and the  friends  table can be on separate DB clusters Each DB Cluster has different tables Application code or DAO / Driver code or a proxy knows where a given table is and directs queries to the appropriate DB Can also be done at a column level by moving a set of columns into a separate table App Cluster DB Cluster 1 Table 1 Table 2 DB Cluster 2 Table 3 Table 4
Vertical Partitioning (DB) Creative Commons Sharealike Attributions Noncommercial Negatives One cannot perform SQL joins or maintain referential integrity (referential integrity is as such over-rated) Finite Limit App Cluster DB Cluster 1 Table 1 Table 2 DB Cluster 2 Table 3 Table 4
Horizontal Partitioning (DB) Creative Commons Sharealike Attributions Noncommercial Take a set of rows and move them onto another DB Eg in a social network – each DB Cluster can contain all data for 1 million  users Each DB Cluster has identical tables Application code or DAO / Driver code or a proxy knows where a given row is and directs queries to the appropriate DB Negatives SQL unions for search type queries must be performed within code App Cluster DB Cluster 1 Table 1 Table 2 Table 3 Table 4 DB Cluster 2 Table 1 Table 2 Table 3 Table 4 1 million users 1 million users
Horizontal Partitioning (DB) Creative Commons Sharealike Attributions Noncommercial Techniques FCFS 1 st  million users are stored on cluster 1 and the next on cluster 2 Round Robin Least Used (Balanced) Each time a new user is added, a DB cluster with the least users is chosen Hash based A hashing function is used to determine the DB Cluster in which the user data should be inserted Value Based User ids 1 to 1 million stored in cluster 1 OR  all users with names starting from A-M on cluster 1 Except for Hash and Value based all other techniques also require an independent lookup map – mapping user to Database Cluster This map itself will be stored on a separate DB (which may further need to be replicated)
Step 7 – Vertical / Horizontal Partitioning (DB) Creative Commons Sharealike Attributions Noncommercial Lookup Map Our architecture now looks like this Positives As App servers grow, Database Clusters can be added Note: This is not the same as table partitioning provided by the db (eg MSSQL) We may actually want to further segregate these into Sets, each serving a collection of users (refer next slide Load Balanced  App Servers DB Cluster DB DB DB DB Cluster DB DB DB SAN
Step 8 – Separating Sets Creative Commons Sharealike Attributions Noncommercial Lookup Map Lookup Map Global Redirector Global Lookup Map SET 1 – 10 million users SET 2 – 10 million users Now we consider each deployment as a single Set serving a collection of users Load Balanced  App Servers DB Cluster DB DB DB DB Cluster DB DB DB SAN Load Balanced  App Servers DB Cluster DB DB DB DB Cluster DB DB DB SAN
Creating Sets Creative Commons Sharealike Attributions Noncommercial The goal behind creating sets is easier manageability Each Set is independent and handles transactions for a set of users Each Set is architecturally identical to the other Each Set contains the entire application with all its data structures Sets can even be deployed in separate datacenters Users may even be added to a Set that is closer to them in terms of network latency
Step 8 – Horizontal Partitioning (Sets) Creative Commons Sharealike Attributions Noncommercial App Servers Cluster DB Cluster SAN Global Redirector SET 1 DB Cluster App Servers Cluster DB Cluster SAN SET 2 DB Cluster Our architecture now looks like this Positives Infinite Scalability Negatives Aggregation of data across sets is complex Users may need to be moved across Sets if sizing is improper Global App settings and preferences need to be replicated across Sets
Step 9 – Caching Creative Commons Sharealike Attributions Noncommercial Add caches within App Server Object Cache Session Cache (especially if you are using a Central Session Store) API cache Page cache Software Memcached Teracotta (Java only) Coherence (commercial expensive data grid by Oracle)
Step 10 – HTTP Accelerator Creative Commons Sharealike Attributions Noncommercial If your app is a web app you should add an HTTP Accelerator or a Reverse Proxy A good HTTP Accelerator / Reverse proxy performs the following – Redirect static content requests to a lighter HTTP server (lighttpd) Cache content based on rules (with granular invalidation support) Use Async NIO on the user side Maintain a limited pool of Keep-alive connections to the App Server Intelligent load balancing Solutions Nginx (HTTP / IMAP) Perlbal Hardware accelerators plus Load Balancers
Step 11 – Other cool stuff Creative Commons Sharealike Attributions Noncommercial CDNs IP Anycasting Async Nonblocking IO (for all Network Servers) If possible - Async Nonblocking IO for disk Incorporate multi-layer caching strategy where required L1 cache – in-process with App Server L2 cache – across network boundary L3 cache – on disk Grid computing Java – GridGain Erlang – natively built in
Platform Selection Considerations Creative Commons Sharealike Attributions Noncommercial Programming Languages and Frameworks Dynamic languages are slower than static languages Compiled code runs faster than interpreted code -> use accelerators or pre-compilers Frameworks that provide Dependency Injections, Reflection, Annotations have a marginal performance impact ORMs hide DB querying which can in some cases result in poor query performance due to non-optimized querying RDBMS MySQL, MSSQL and Oracle support native replication Postgres supports replication through 3 rd  party software (Slony) Oracle supports Real Application Clustering MySQL uses locking and arbitration, while Postgres/Oracle use MVCC (MSSQL just recently introduced MVCC) Cache Teracotta vs memcached vs Coherence
Tips Creative Commons Sharealike Attributions Noncommercial All the techniques we learnt today can be applied in any order Try and incorporate Horizontal DB partitioning by value from the beginning into your design Loosely couple all modules Implement a REST-ful framework for easier caching Perform application sizing ongoingly to ensure optimal utilization of hardware
Questions?? bhavin.t@directi.com  http://directi.com http://careers.directi.com   Download slides:  http://wiki.directi.com

More Related Content

PPT
Managing Software from Development to Deployment in the Cloud
PPTX
How to Build Scalable Websites in the Cloud
PPTX
uberSVN introduction by WANdisco
PPTX
Demystifying Azure Compute
PPTX
Gomez Blazing Fast Cloud Best Practices
PDF
Deploying to and Configuring WebSphere Application Server with UrbanCode Deploy
PPTX
VMWare based Cloud Computing - KNOWARTH
PPTX
Harbour IT & VMware - vForum 2010 Wrap
Managing Software from Development to Deployment in the Cloud
How to Build Scalable Websites in the Cloud
uberSVN introduction by WANdisco
Demystifying Azure Compute
Gomez Blazing Fast Cloud Best Practices
Deploying to and Configuring WebSphere Application Server with UrbanCode Deploy
VMWare based Cloud Computing - KNOWARTH
Harbour IT & VMware - vForum 2010 Wrap

What's hot (17)

PDF
Datasheet webspheremessagebroker midvisionextensionforibmraf
PPT
Cloud computing v2final
PDF
Dynacache in WebSphere Portal Server
PPTX
Deep Dive into FME Server 2013
PDF
AAI-2013 Preparing to Fail: Practical WebSphere Application Server High Avail...
PPTX
What is the PaaS?
PDF
AAI-3281 Smarter Production with WebSphere Application Server ND Intelligent ...
PPTX
Relational Won't Cut It: Architecting Content Centric Apps
PPTX
App V
PDF
Datasheet was pluginforrd
PPTX
JAXLondon 2015 "DevOps and the Cloud: All Hail the (Developer) King"
PDF
Presentation business critical applications in a virtual env
PPTX
Collaborative lifecycle development for Mobile Software
PPTX
EVOLVE'14 | Enhance | Paul McMahon | AEM & Transactional Portal Systems
DOC
DistributedMediaApplicationProject
PPT
Spring Integration
PPT
4156 Twist and cloud-how ibm customers make cics dance
Datasheet webspheremessagebroker midvisionextensionforibmraf
Cloud computing v2final
Dynacache in WebSphere Portal Server
Deep Dive into FME Server 2013
AAI-2013 Preparing to Fail: Practical WebSphere Application Server High Avail...
What is the PaaS?
AAI-3281 Smarter Production with WebSphere Application Server ND Intelligent ...
Relational Won't Cut It: Architecting Content Centric Apps
App V
Datasheet was pluginforrd
JAXLondon 2015 "DevOps and the Cloud: All Hail the (Developer) King"
Presentation business critical applications in a virtual env
Collaborative lifecycle development for Mobile Software
EVOLVE'14 | Enhance | Paul McMahon | AEM & Transactional Portal Systems
DistributedMediaApplicationProject
Spring Integration
4156 Twist and cloud-how ibm customers make cics dance
Ad

Viewers also liked (6)

PPTX
This that these those
PPT
Blended Learning
PPT
What are they doing?
PPT
Demystifying Storage
PPT
Present progressive
PPT
Interaction With Moodle
This that these those
Blended Learning
What are they doing?
Demystifying Storage
Present progressive
Interaction With Moodle
Ad

Similar to Building A Scalable Architecture (20)

PPT
Building a Scalable Architecture for web apps
PPTX
Handling Data in Mega Scale Systems
PDF
Scaling WordPress for High Traffic - Server Architecture
DOCX
Synopsis cloud scalability_jatinchauhan
PDF
How to Build a Scalable Web Application for Your Project
PDF
Application-Servers.pdf
PPTX
Building Scalable Websites for the Cloud
PPT
Alfresco WCM For High Scalability
PPTX
Scalable Web Architecture and Distributed Systems
PDF
Patterns & Practices of Microservices
PPTX
5 Quick Wins for the Cloud
PDF
Dallas Breakfast Seminar
PPT
Server Farms and XML Web Services
PDF
IRJET- Commercial Web Application Load Balancing based on Hybrid Cloud
PPTX
Si so product 1 day technical
PPT
GigaSpaces CCF 4 Xap
PPT
Forrester Research on Globally Distributed Development Using Subversion
PPT
Forrester Research on Optimizing Globally Distributed Software Development Us...
PDF
Implementing a Solution to the Cloud Vendor Lock-In Using Standardized API
PPTX
Cloud Computing:An Economic Solution for Libraries
Building a Scalable Architecture for web apps
Handling Data in Mega Scale Systems
Scaling WordPress for High Traffic - Server Architecture
Synopsis cloud scalability_jatinchauhan
How to Build a Scalable Web Application for Your Project
Application-Servers.pdf
Building Scalable Websites for the Cloud
Alfresco WCM For High Scalability
Scalable Web Architecture and Distributed Systems
Patterns & Practices of Microservices
5 Quick Wins for the Cloud
Dallas Breakfast Seminar
Server Farms and XML Web Services
IRJET- Commercial Web Application Load Balancing based on Hybrid Cloud
Si so product 1 day technical
GigaSpaces CCF 4 Xap
Forrester Research on Globally Distributed Development Using Subversion
Forrester Research on Optimizing Globally Distributed Software Development Us...
Implementing a Solution to the Cloud Vendor Lock-In Using Standardized API
Cloud Computing:An Economic Solution for Libraries

Recently uploaded (20)

PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Electronic commerce courselecture one. Pdf
DOCX
The AUB Centre for AI in Media Proposal.docx
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Assigned Numbers - 2025 - Bluetooth® Document
PDF
Approach and Philosophy of On baking technology
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PPTX
MYSQL Presentation for SQL database connectivity
PPTX
sap open course for s4hana steps from ECC to s4
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PPTX
Machine Learning_overview_presentation.pptx
PDF
gpt5_lecture_notes_comprehensive_20250812015547.pdf
PPTX
A Presentation on Artificial Intelligence
PDF
Spectral efficient network and resource selection model in 5G networks
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Electronic commerce courselecture one. Pdf
The AUB Centre for AI in Media Proposal.docx
Programs and apps: productivity, graphics, security and other tools
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Mobile App Security Testing_ A Comprehensive Guide.pdf
Digital-Transformation-Roadmap-for-Companies.pptx
Reach Out and Touch Someone: Haptics and Empathic Computing
Assigned Numbers - 2025 - Bluetooth® Document
Approach and Philosophy of On baking technology
20250228 LYD VKU AI Blended-Learning.pptx
MYSQL Presentation for SQL database connectivity
sap open course for s4hana steps from ECC to s4
Unlocking AI with Model Context Protocol (MCP)
The Rise and Fall of 3GPP – Time for a Sabbatical?
Machine Learning_overview_presentation.pptx
gpt5_lecture_notes_comprehensive_20250812015547.pdf
A Presentation on Artificial Intelligence
Spectral efficient network and resource selection model in 5G networks

Building A Scalable Architecture

  • 1. Building a Scalable Architecture for Web Apps - Part I (Lessons Learned @ Directi) By Bhavin Turakhia CEO, Directi ( http://www.directi.com | http://wiki.directi.com | http://careers.directi.com ) Licensed under Creative Commons Attribution Sharealike Noncommercial
  • 2. Agenda Why is Scalability important Introduction to the Variables and Factors Building our own Scalable Architecture (in incremental steps) Vertical Scaling Vertical Partitioning Horizontal Scaling Horizontal Partitioning … etc Platform Selection Considerations Tips Creative Commons Sharealike Attributions Noncommercial
  • 3. Why is Scalability Important in a Web 2.0 world Viral marketing can result in instant successes RSS / Ajax / SOA pull based / polling type XML protocols - Meta-data > data Number of Requests exponentially grows with user base RoR / Grails – Dynamic language landscape gaining popularity In the end you want to build a Web 2.0 app that can serve millions of users with ZERO downtime Creative Commons Sharealike Attributions Noncommercial
  • 4. The Variables Scalability - Number of users / sessions / transactions / operations the entire system can perform Performance – Optimal utilization of resources Responsiveness – Time taken per operation Availability - Probability of the application or a portion of the application being available at any given point in time Downtime Impact - The impact of a downtime of a server/service/resource - number of users, type of impact etc Cost Maintenance Effort Creative Commons Sharealike Attributions Noncommercial High : scalability, availability, performance & responsiveness Low : downtime impact, cost & maintenance effort
  • 5. The Factors Platform selection Hardware Application Design Database/Datastore Structure and Architecture Deployment Architecture Storage Architecture Abuse prevention Monitoring mechanisms … and more Creative Commons Sharealike Attributions Noncommercial
  • 6. Lets Start … We will now build an example architecture for an example app using the following iterative incremental steps – Inspect current Architecture Identify Scalability Bottlenecks Identify SPOFs and Availability Issues Identify Downtime Impact Risk Zones Apply one of - Vertical Scaling Vertical Partitioning Horizontal Scaling Horizontal Partitioning Repeat process Creative Commons Sharealike Attributions Noncommercial
  • 7. Step 1 – Lets Start … Creative Commons Sharealike Attributions Noncommercial Appserver & DBServer
  • 8. Step 2 – Vertical Scaling Creative Commons Sharealike Attributions Noncommercial Appserver, DBServer CPU CPU RAM RAM
  • 9. Step 2 - Vertical Scaling Introduction Increasing the hardware resources without changing the number of nodes Referred to as “Scaling up” the Server Advantages Simple to implement Disadvantages Finite limit Hardware does not scale linearly (diminishing returns for each incremental unit) Requires downtime Increases Downtime Impact Incremental costs increase exponentially Creative Commons Sharealike Attributions Noncommercial Appserver, DBServer CPU CPU RAM RAM CPU CPU RAM RAM
  • 10. Step 3 – Vertical Partitioning (Services) Creative Commons Sharealike Attributions Noncommercial AppServer DBServer Introduction Deploying each service on a separate node Positives Increases per application Availability Task-based specialization, optimization and tuning possible Reduces context switching Simple to implement for out of band processes No changes to App required Flexibility increases Negatives Sub-optimal resource utilization May not increase overall availability Finite Scalability
  • 11. Understanding Vertical Partitioning Creative Commons Sharealike Attributions Noncommercial The term Vertical Partitioning denotes – Increase in the number of nodes by distributing the tasks/functions Each node (or cluster) performs separate Tasks Each node (or cluster) is different from the other Vertical Partitioning can be performed at various layers (App / Server / Data / Hardware etc)
  • 12. Step 4 – Horizontal Scaling (App Server) Creative Commons Sharealike Attributions Noncommercial AppServer AppServer AppServer Load Balancer DBServer Introduction Increasing the number of nodes of the App Server through Load Balancing Referred to as “Scaling out” the App Server
  • 13. Understanding Horizontal Scaling Creative Commons Sharealike Attributions Noncommercial The term Horizontal Scaling denotes – Increase in the number of nodes by replicating the nodes Each node performs the same Tasks Each node is identical Typically the collection of nodes maybe known as a cluster (though the term cluster is often misused) Also referred to as “Scaling Out” Horizontal Scaling can be performed for any particular type of node (AppServer / DBServer etc)
  • 14. Load Balancer – Hardware vs Software Creative Commons Sharealike Attributions Noncommercial Hardware Load balancers are faster Software Load balancers are more customizable With HTTP Servers load balancing is typically combined with http accelerators
  • 15. Load Balancer – Session Management Creative Commons Sharealike Attributions Noncommercial Sticky Sessions Requests for a given user are sent to a fixed App Server Observations Asymmetrical load distribution (especially during downtimes) Downtime Impact – Loss of session data AppServer AppServer AppServer Load Balancer Sticky Sessions User 1 User 2
  • 16. Load Balancer – Session Management Creative Commons Sharealike Attributions Noncommercial Central Session Store Introduces SPOF An additional variable Session reads and writes generate Disk + Network I/O Also known as a Shared Session Store Cluster AppServer AppServer AppServer Load Balancer Session Store Central Session Storage
  • 17. Load Balancer – Session Management Creative Commons Sharealike Attributions Noncommercial Clustered Session Management Easier to setup No SPOF Session reads are instantaneous Session writes generate Network I/O Network I/O increases exponentially with increase in number of nodes In very rare circumstances a request may get stale session data User request reaches subsequent node faster than intra-node message Intra-node communication fails AKA Shared-nothing Cluster AppServer AppServer AppServer Load Balancer Clustered Session Management
  • 18. Load Balancer – Session Management Creative Commons Sharealike Attributions Noncommercial Sticky Sessions with Central Session Store Downtime does not cause loss of data Session reads need not generate network I/O Sticky Sessions with Clustered Session Management No specific advantages Sticky Sessions AppServer AppServer AppServer Load Balancer User 1 User 2
  • 19. Load Balancer – Session Management Creative Commons Sharealike Attributions Noncommercial Recommendation Use Clustered Session Management if you have – Smaller Number of App Servers Fewer Session writes Use a Central Session Store elsewhere Use sticky sessions only if you have to
  • 20. Load Balancer – Removing SPOF Creative Commons Sharealike Attributions Noncommercial In a Load Balanced App Server Cluster the LB is an SPOF Setup LB in Active-Active or Active-Passive mode Note: Active-Active nevertheless assumes that each LB is independently able to take up the load of the other If one wants ZERO downtime, then Active-Active becomes truly cost beneficial only if multiple LBs (more than 3 to 4) are daisy chained as Active-Active forming an LB Cluster AppServer AppServer AppServer Load Balancer Active-Passive LB Load Balancer AppServer AppServer AppServer Load Balancer Active-Active LB Load Balancer Users Users
  • 21. Step 4 – Horizontal Scaling (App Server) Creative Commons Sharealike Attributions Noncommercial DBServer Our deployment at the end of Step 4 Positives Increases Availability and Scalability No changes to App required Easy setup Negatives Finite Scalability Load Balanced App Servers
  • 22. Step 5 – Vertical Partitioning (Hardware) Creative Commons Sharealike Attributions Noncommercial DBServer Introduction Partitioning out the Storage function using a SAN SAN config options Refer to “Demystifying Storage” at http://wiki.directi.com -> Dev University -> Presentations Positives Allows “Scaling Up” the DB Server Boosts Performance of DB Server Negatives Increases Cost SAN Load Balanced App Servers
  • 23. Step 6 – Horizontal Scaling (DB) Creative Commons Sharealike Attributions Noncommercial DBServer Introduction Increasing the number of DB nodes Referred to as “Scaling out” the DB Server Options Shared nothing Cluster Real Application Cluster (or Shared Storage Cluster) DBServer DBServer SAN Load Balanced App Servers
  • 24. Shared Nothing Cluster Creative Commons Sharealike Attributions Noncommercial Each DB Server node has its own complete copy of the database Nothing is shared between the DB Server Nodes This is achieved through DB Replication at DB / Driver / App level or through a proxy Supported by most RDBMs natively or through 3 rd party software DBServer Database DBServer Database DBServer Database Note: Actual DB files maybe stored on a central SAN
  • 25. Replication Considerations Creative Commons Sharealike Attributions Noncommercial Master-Slave Writes are sent to a single master which replicates the data to multiple slave nodes Replication maybe cascaded Simple setup No conflict management required Multi-Master Writes can be sent to any of the multiple masters which replicate them to other masters and slaves Conflict Management required Deadlocks possible if same data is simultaneously modified at multiple places
  • 26. Replication Considerations Creative Commons Sharealike Attributions Noncommercial Asynchronous Guaranteed, but out-of-band replication from Master to Slave Master updates its own db and returns a response to client Replication from Master to Slave takes place asynchronously Faster response to a client Slave data is marginally behind the Master Requires modification to App to send critical reads and writes to master, and load balance all other reads Synchronous Guaranteed, in-band replication from Master to Slave Master updates its own db, and confirms all slaves have updated their db before returning a response to client Slower response to a client Slaves have the same data as the Master at all times Requires modification to App to send writes to master and load balance all reads
  • 27. Replication Considerations Creative Commons Sharealike Attributions Noncommercial Replication at RDBMS level Support may exists in RDBMS or through 3 rd party tool Faster and more reliable App must send writes to Master, reads to any db and critical reads to Master Replication at Driver / DAO level Driver / DAO layer ensures writes are performed on all connected DBs Reads are load balanced Critical reads are sent to a Master In most cases RDBMS agnostic Slower and in some cases less reliable
  • 28. Real Application Cluster Creative Commons Sharealike Attributions Noncommercial All DB Servers in the cluster share a common storage area on a SAN All DB servers mount the same block device The filesystem must be a clustered file system (eg GFS / OFS) Currently only supported by Oracle Real Application Cluster Can be very expensive (licensing fees) DBServer SAN DBServer DBServer Database
  • 29. Recommendation Creative Commons Sharealike Attributions Noncommercial Try and choose a DB which natively supports Master-Slave replication Use Master-Slave Async replication Write your DAO layer to ensure writes are sent to a single DB reads are load balanced Critical reads are sent to a master DBServer DBServer DBServer Writes & Critical Reads Other Reads Load Balanced App Servers
  • 30. Step 6 – Horizontal Scaling (DB) Creative Commons Sharealike Attributions Noncommercial Our architecture now looks like this Positives As Web servers grow, Database nodes can be added DB Server is no longer SPOF Negatives Finite limit Load Balanced App Servers DB Cluster DB DB DB SAN
  • 31. Step 6 – Horizontal Scaling (DB) Creative Commons Sharealike Attributions Noncommercial Shared nothing clusters have a finite scaling limit Reads to Writes – 2:1 So 8 Reads => 4 writes 2 DBs Per db – 4 reads and 4 writes 4 DBs Per db – 2 reads and 4 writes 8 DBs Per db – 1 read and 4 writes At some point adding another node brings in negligible incremental benefit Reads Writes DB1 DB2
  • 32. Step 7 – Vertical / Horizontal Partitioning (DB) Creative Commons Sharealike Attributions Noncommercial Introduction Increasing the number of DB Clusters by dividing the data Options Vertical Partitioning - Dividing tables / columns Horizontal Partitioning - Dividing by rows (value) Load Balanced App Servers DB Cluster DB DB DB SAN
  • 33. Vertical Partitioning (DB) Creative Commons Sharealike Attributions Noncommercial Take a set of tables and move them onto another DB Eg in a social network - the users table and the friends table can be on separate DB clusters Each DB Cluster has different tables Application code or DAO / Driver code or a proxy knows where a given table is and directs queries to the appropriate DB Can also be done at a column level by moving a set of columns into a separate table App Cluster DB Cluster 1 Table 1 Table 2 DB Cluster 2 Table 3 Table 4
  • 34. Vertical Partitioning (DB) Creative Commons Sharealike Attributions Noncommercial Negatives One cannot perform SQL joins or maintain referential integrity (referential integrity is as such over-rated) Finite Limit App Cluster DB Cluster 1 Table 1 Table 2 DB Cluster 2 Table 3 Table 4
  • 35. Horizontal Partitioning (DB) Creative Commons Sharealike Attributions Noncommercial Take a set of rows and move them onto another DB Eg in a social network – each DB Cluster can contain all data for 1 million users Each DB Cluster has identical tables Application code or DAO / Driver code or a proxy knows where a given row is and directs queries to the appropriate DB Negatives SQL unions for search type queries must be performed within code App Cluster DB Cluster 1 Table 1 Table 2 Table 3 Table 4 DB Cluster 2 Table 1 Table 2 Table 3 Table 4 1 million users 1 million users
  • 36. Horizontal Partitioning (DB) Creative Commons Sharealike Attributions Noncommercial Techniques FCFS 1 st million users are stored on cluster 1 and the next on cluster 2 Round Robin Least Used (Balanced) Each time a new user is added, a DB cluster with the least users is chosen Hash based A hashing function is used to determine the DB Cluster in which the user data should be inserted Value Based User ids 1 to 1 million stored in cluster 1 OR all users with names starting from A-M on cluster 1 Except for Hash and Value based all other techniques also require an independent lookup map – mapping user to Database Cluster This map itself will be stored on a separate DB (which may further need to be replicated)
  • 37. Step 7 – Vertical / Horizontal Partitioning (DB) Creative Commons Sharealike Attributions Noncommercial Lookup Map Our architecture now looks like this Positives As App servers grow, Database Clusters can be added Note: This is not the same as table partitioning provided by the db (eg MSSQL) We may actually want to further segregate these into Sets, each serving a collection of users (refer next slide Load Balanced App Servers DB Cluster DB DB DB DB Cluster DB DB DB SAN
  • 38. Step 8 – Separating Sets Creative Commons Sharealike Attributions Noncommercial Lookup Map Lookup Map Global Redirector Global Lookup Map SET 1 – 10 million users SET 2 – 10 million users Now we consider each deployment as a single Set serving a collection of users Load Balanced App Servers DB Cluster DB DB DB DB Cluster DB DB DB SAN Load Balanced App Servers DB Cluster DB DB DB DB Cluster DB DB DB SAN
  • 39. Creating Sets Creative Commons Sharealike Attributions Noncommercial The goal behind creating sets is easier manageability Each Set is independent and handles transactions for a set of users Each Set is architecturally identical to the other Each Set contains the entire application with all its data structures Sets can even be deployed in separate datacenters Users may even be added to a Set that is closer to them in terms of network latency
  • 40. Step 8 – Horizontal Partitioning (Sets) Creative Commons Sharealike Attributions Noncommercial App Servers Cluster DB Cluster SAN Global Redirector SET 1 DB Cluster App Servers Cluster DB Cluster SAN SET 2 DB Cluster Our architecture now looks like this Positives Infinite Scalability Negatives Aggregation of data across sets is complex Users may need to be moved across Sets if sizing is improper Global App settings and preferences need to be replicated across Sets
  • 41. Step 9 – Caching Creative Commons Sharealike Attributions Noncommercial Add caches within App Server Object Cache Session Cache (especially if you are using a Central Session Store) API cache Page cache Software Memcached Teracotta (Java only) Coherence (commercial expensive data grid by Oracle)
  • 42. Step 10 – HTTP Accelerator Creative Commons Sharealike Attributions Noncommercial If your app is a web app you should add an HTTP Accelerator or a Reverse Proxy A good HTTP Accelerator / Reverse proxy performs the following – Redirect static content requests to a lighter HTTP server (lighttpd) Cache content based on rules (with granular invalidation support) Use Async NIO on the user side Maintain a limited pool of Keep-alive connections to the App Server Intelligent load balancing Solutions Nginx (HTTP / IMAP) Perlbal Hardware accelerators plus Load Balancers
  • 43. Step 11 – Other cool stuff Creative Commons Sharealike Attributions Noncommercial CDNs IP Anycasting Async Nonblocking IO (for all Network Servers) If possible - Async Nonblocking IO for disk Incorporate multi-layer caching strategy where required L1 cache – in-process with App Server L2 cache – across network boundary L3 cache – on disk Grid computing Java – GridGain Erlang – natively built in
  • 44. Platform Selection Considerations Creative Commons Sharealike Attributions Noncommercial Programming Languages and Frameworks Dynamic languages are slower than static languages Compiled code runs faster than interpreted code -> use accelerators or pre-compilers Frameworks that provide Dependency Injections, Reflection, Annotations have a marginal performance impact ORMs hide DB querying which can in some cases result in poor query performance due to non-optimized querying RDBMS MySQL, MSSQL and Oracle support native replication Postgres supports replication through 3 rd party software (Slony) Oracle supports Real Application Clustering MySQL uses locking and arbitration, while Postgres/Oracle use MVCC (MSSQL just recently introduced MVCC) Cache Teracotta vs memcached vs Coherence
  • 45. Tips Creative Commons Sharealike Attributions Noncommercial All the techniques we learnt today can be applied in any order Try and incorporate Horizontal DB partitioning by value from the beginning into your design Loosely couple all modules Implement a REST-ful framework for easier caching Perform application sizing ongoingly to ensure optimal utilization of hardware
  • 46. Questions?? bhavin.t@directi.com http://directi.com http://careers.directi.com Download slides: http://wiki.directi.com