This article covers a number of concepts critical for building scalable applications.

what is vertical scaling

In vertical scaling (“scaling up”), you’re adding more compute power to your existing instances/nodes.

what is horizontal scaling

In horizontal scaling (“scaling out”), you get the additional capacity in a system by adding more instances to your deployment.

what is better, horizontal or vertical scaling?

Horizontal scaling is often preferable to vertical scaling.

what is x , y and z axis scaling

The book, The Art of Scalability, describes a really useful, three dimension scalability model: the scale cube.

what is sharding 

The word “Shard” means “a small part of a whole“. Hence Sharding means dividing a larger part into smaller parts. Sharding is also referred as horizontal partitioning. Sharding is also called z axis scaling.

Sharding techniques

     Algorithmic/key based/hash based sharding , a partition key is used for sharding, it can be (table column) / (attribute of an entity) like  customer id,  location, zip code etc . This colum is passed to a hash function to get the shard number and insert the row into the shard. When read request comes for row ,the partion key is used to determine the shard in which the row resides. The partion key can be

The downside of this approach is that reshard the database is required (update hash function and rebalance data in nodes) every time a new server is added to the node. This problem can be solved by using consistent hashing .Consistent hashing is a special kind of hashing technique such that when a hash table is resized, only n/m keys need to be remapped on average where n is the number of keys and m is the number of slots. In contrast, in most traditional hash tables, a change in the number of array slots causes nearly all keys to be remapped because the mapping between the keys and the slots is defined by a modular operation. Another downside of this approach is that query operations for multiple records / range quereis are more likely to get distributed across multiple shards . 

The advantage of this approach is that it distributes data algorithmically hence no need to maintain lookup table . Also data is evenly distributed between shards and risk of hotspot is reduced.

     Range based/Dynamic sharding

In this method, we split the data based on the ranges of an (attribute of entity) or (table column).The column on which the range is based is also known as the shard key. Let’s say you have a database of your online customers’ names and age. You can split all rows in 2 shards. In one shard you can keep the info of customers whose age is between 0 to 30  and in another shard, keep the information of the rest of the customers.It's useful for applications that frequently retrieve sets of items using range queries (queries that return a set of data items for a shard key that falls within a given range).  For example, if an application regularly needs to find all orders placed in a given month, this data can be retrieved more quickly if all orders for a month are stored in date and time order in the same shard. One disadvantage of range based sharding is that row distribution across shards may not be even and hence it can create hotspots. Another disadvantage is that Ranged sharding requires there to be a lookup table or service available for all queries .

   Lookup/Directory based sharding

In directory-based sharding have a lookup table also known as location service. It stores a map of shard key value and shard. To read or write data, the client engine first consults the lookup table to find the shard number for the corresponding data using the shard-key and then visits a particular shard to perform the further operation. One advantage is that directory-based sharding allows you to use whatever system or algorithm we want to use to assign data entries to shards, and also it’s relatively easy to dynamically add shards using this approach (rebalancing is easier). The downside can be the lookup table can become a single point of failure.

Vertical partitioning

While in horizontal partitioning rows of a table are stored in different database nodes, in vertical partitioning entire columns are separated out and put into new, distinct tables . For example binary blobs col would be a good candidate to put in different table. Also in vertical partioning different tables can be in different databases.(eg in microservices architecture). Vertical partioning is almost always handled at application layer. Note that vertical partitioning and sharding are complimentary.

Challenges associated with sharding

Stateless applications for horiznotal scaling

A Stateless app is an application program that does not save client data generated in one session for use in the next session with that client. Hence requests from a client can go to any instance of the application. You can simply scale a stateless application by deploying it on multiple servers. Scales beautifully horizontally. Note that Stateless communication is one of the REST principles . The server should not contain client state. Note that in java using synchronization with respect to database operations can lead to nasty dead locks as jvm lock + db lock are both coming into play. synchronization across jvms should be done with the help of database. In a synchronized block reading a counter from database , incrementing and then using it is again very bad design which will not work across servers (bug). 

Database 

Scaling based on server side threading patterns

Scaling using microservices architecture

Scaling using event driven microservices

Caching

If your application is bound by read performance, you can add caches or database replicas. They provide additional read capacity without heavily modifying your application. read more https://clarifyforme.com/posts/5734331951611904/Server-side-caching-patterns and https://clarifyforme.com/posts/5683115607457792/http-caching-by-browser 

serving a global user base