Hugo ʕ•ᴥ•ʔ Bear Blog

1. Introduction to system scaling

Chapter 1: Scale from zero to millions of users

I. Database replication

Master-slave model offers several benefits

  1. Better performance: Parallel processing by multiple read nodes.
  2. Reliability: Data replication all over nodes.
  3. High availability: if a database is offline as you can access data stored in another database server.

In this model, when a master goes offline, a slave node will be selected and promoted to a new master. In real life, this process can be challenging because the slave node might not have the up-to-date data. Please refer to following materials if you want to understand techniques to solve this problem, which may be complex, 1, 2.

II. Improve latency

Considerations for using cache

Cache is used when data is read frequently but modified infrequently.

Consistency is the caching problem that ensure to keep data in cache and data storage in sync. When scaling across multiple regions, maintaining consistency is challenging, for further details, please referring to “Scaling Memcache at Facebook” published by Facebook.

Consideration using CDN

Data center

Supporting multiple data centers is crucial to improve availability and user experience when you have users spreading across geographical areas (internationally).

Some considerations:

Message queue

To allow each component of the system to scale independently, we need to decouple them into different servers (called services). Message queue is a tool enable these services communicate to each other.

When we have a message queue, producers don’t need to wait for acknowledgement from consumers to publish a message, thereby free them from being idle.

Logging, metrics, automation

III. Database scaling

NoSQL vs SQL

This is an excerpt from the book, non-relational databases might be the right choice if:

Here is an article that covers many use cases of NoSQL.

Readers need to dig deeper into these database architectures to gain a better understanding of why it is the case.

Vertical scaling

Vertical scaling comes with some serious drawback

Horizontal scaling

Sharding is a strategy to distribute data to different database servers, which also distribute traffic, and thus providing an approach to scale database. Sharding is implemented by using a hash function, which hash based on user identity (such as ID), to determine which server will be used to populate the user’s data.

We aim to have a hash function so that it can distribute data evenly across servers.

Sharding may be far more from perfect, as it introduces several problems:

  1. Resharding data: Due to certain reasons, a shard may experience significant data growth, called shard exhaustion, which requires the need to use another hash function. Resharding requires you to move data around databases, which may be very complex. Commonly used technique is consistent hashing, which will be learned in Chapter 5.
  2. Celebrity problem: A popular user, like Justin Bieber, may overload a server. Solving this problem might need to provide a shard for each celebrity.
  3. Join and de-normalization: Joining is obviously complex if data is distributed across servers. De-normalization may be needed to allow query data without the need for joining.

IV. Summary

Scaling a system is an iterative process. More fine-tuning and new strategies are needed to scale beyond millions of users. For example, you might need to optimize your system and decouple the system to even smaller services.

#Markdown #Syntax