System ArchitectureDevOps Engineer

How to implement horizontal and vertical scaling of applications and what criteria influence the choice of approach?

Pass interviews with Hintsage AI assistant

Answer.

Horizontal scaling involves adding new instances of the application (for example, new servers or containers) to distribute the load among them. Vertical scaling means increasing resources on a single server (adding CPU, RAM, disks).

When choosing an approach, the following criteria are taken into account:

  • Application capabilities (stateless applications are easy to scale horizontally);
  • Infrastructure limitations (is there the ability to quickly add servers);
  • Economic factors (horizontal is more expensive with a large number of nodes, vertical is limited by hardware constraints).

An example of horizontal scaling through Kubernetes:

apiVersion: apps/v1 kind: Deployment metadata: name: my-app spec: replicas: 5 # number of pods — horizontal scale selector: matchLabels: app: my-app template: metadata: labels: app: my-app spec: containers: - name: my-app image: myimage:v1

Key features:

  • Horizontal scaling provides fault tolerance and flexibility
  • Vertical scaling is easier to implement but limited by hardware constraints
  • A hybrid approach is often used: critical components are scaled horizontally, stateful ones vertically

Trick questions.

Is horizontal scaling always better than vertical scaling?

No. For certain tasks (for example, for monolithic or stateful services), vertical scaling can be simpler and more efficient.

Does horizontal scaling require no modification to the application?

No. The application must be stateless, support session sharing (for example, using an external cache), and correctly respond to scaling.

Is database scaling always done horizontally?

No. Not all DBMS can be easily scaled horizontally. Classic relational databases often scale vertically (scale-up) or utilize sharding/replication.