[System Design] Scaling a Web Application I


Background

System Design Notes from System Design Interview By Alex Xu

Single Server Setup

  • Users access the website by entering the domain name in their web browser.
  • The Domain Name System (DNS) translates the domain name into the corresponding IP address.
  • Once the IP address is obtained, HTTP requests are sent to the web server.
  • The web server responds by returning HTML pages or JSON responses for rendering.

Load Balancer

  • Users connect to the public IP of the load balancer, which acts as a gateway to the web servers. Direct access to the web servers by clients is no longer possible.
  • To enhance security, communication between the servers utilizes private IPs. Private IPs are only reachable within the same network and are not accessible over the internet.
  • In the event that server 1 becomes offline, all traffic is automatically routed to server 2. This ensures the website remains accessible and prevents downtime.

Database Replication

  • A master database typically handles write operations exclusively. It is responsible for receiving and processing commands such as insertions, deletions, and updates.
  • On the other hand, a slave database receives copies of the data from the master database and is designed to support read operations. It does not allow direct data modification commands.
  • To perform any data-modifying actions, such as inserts, deletes, or updates, it is necessary to send those commands to the master database. The slave database is primarily used for retrieving data and performing read operations.

Advantages

  • Enhanced Performance: The master-slave model optimizes performance by centralizing all write and update operations in the master nodes, while distributing read operations across slave nodes. This approach enables concurrent processing of a higher volume of queries, resulting in improved performance.
  • Enhanced Reliability: In the event of a natural disaster, such as a typhoon or an earthquake, the master-slave model ensures data preservation. With data replicated across multiple locations, you can rest assured that your data remains safe, minimizing the risk of data loss.
  • Increased Availability: Through data replication across different locations, your website maintains high availability even if a particular database server goes offline. This redundancy allows you to access data stored in alternative database servers, ensuring uninterrupted operation of your website.

Cache

  • A cache is a temporary storage area that stores the results of resource-intensive or commonly accessed data in memory, allowing subsequent requests to be served faster.
  • When a request is received, a web server initially checks if the cache contains the desired response. If it does, the server directly sends the cached data to the client. If the response is not available in the cache, the server queries the database, saves the response in the cache, and then sends it back to the client.

Considerations

  • Use caching when data is frequently read but infrequently modified. Cached data is stored in volatile memory, so it's not ideal for persisting important data.
  • Implement an expiration policy to remove expired data from the cache, balancing the expiration date to avoid excessive reloading or stale data.
  • Ensuring consistency between the data store and cache can be challenging, especially when scaling across multiple regions.
  • Mitigate failures by employing multiple cache servers across different data centers to avoid a single point of failure. Overprovision memory to accommodate increased usage.

Content delivery network (CDN)

What is CDN?

A CDN (Content Delivery Network) is a network of distributed servers used to deliver static content such as images, videos, CSS, and JavaScript files.

How CDN works?

At a high level, when a user visits a website, the closest CDN server to the user delivers the static content. The farther the user is from the CDN server, the slower the website loads.

Source