Capacity Planning and Application Scaling

Calculating the workload before choosing capacity, vertical, horizontal, queue and database scaling, the data races scaling can expose, and queue age versus depth.

  • Advanced
  • About 17 minutes
  • Includes an exercise and a knowledge check

The scenario Traffic is not constant

At month-end, finance receives a large settlement batch and the queue grows. The customer asks: Can the platform handle it? The honest answer needs numbers. Apex now gives its own planning assumptions, which replace the illustrative estimate used in Module 06:

AssumptionValue
Payment events a day400,000
Settlement records on a busy day100,000
Primary business processing window10 hours
Peak arrival rateUp to 5 times the average
Planning assumptions: 400,000 events in 10 business hours is 400,000 divided by 36,000 seconds, about 11.1 events a second, and with peak factor 5 about 55.6 a second. Four ways to scale: vertical, horizontal, queue buffering and database. A warning: two workers both see an unmatched payment and could match it twice; the fix is a unique constraint on the match. Two queue signals: depth, how much work waits, and the oldest message age, how late the business is; a short queue whose oldest message is six hours old is still failing finance.
Measure the load, then scale safely.

400,000 ÷ (10 × 3,600 seconds) ≈ 11.1 events a second; × 5 ≈ 55.6 at peak. That is the payment workload only, and a starting point, not a guarantee. You still need payload sizes, database writes per event, downstream calls, retries, consumer processing time, batch sizes, concurrency, how long the peak lasts and the latency objectives.

The 400,000 payment events and the 100,000 settlement records are two separate workloads with different shapes, and at month-end they hit the same database at the same time. Calculate each, then add them (example per-item figures, to measure in a load test):

Payment eventsSettlement records
ShapeA stream through the day, peaking at about 55.6 a secondOne file of 100,000 records, due within 15 minutes
Rate55.6 events a second at peak100,000 ÷ 900 s ≈ 111 records a second
Database writes3 per event: 55.6 × 3 ≈ 167 a second4 per record: 111 × 4 ≈ 444 a second
Connections busy20 ms of database time per event: 55.6 × 0.02 ≈ 1.1 on average20 ms per record: 111 × 0.02 ≈ 2.2 on average

Together at month-end, that is about 611 writes a second and about 3.3 busy connections on average, more in bursts. Busy connections are not the limit that bites; pools are. Each task opens its whole pool. Four API tasks with 20 connections and six workers with 10 open 140 connections before any work arrives. Check that against the database's own limit (for example, 200 on the chosen instance, to confirm), leave room for administration and migrations, and size pools from the busy-connection numbers plus headroom, not from defaults.

The hidden problem Scaling can expose data races

Two workers receive the same settlement event, both read an unmatched payment, both match it. More workers made the system faster and less correct. Business invariants must be enforced by database constraints, transactions and idempotent processing (Module 07, Lesson 05), never by the hope that only one worker runs.

Practice Write a capacity plan

Practical exercise

Exercise 05: the Apex capacity plan

Estimate the workload, then test normal, peak and a burst above peak. Which resource fails first?

  1. Average and peak ingestion rates, and settlement processing workload.
  2. Database operations, worker concurrency and connection demand.
  3. Queue retention, retry requirements and scaling thresholds.
Show sample numbers
  • Settlement: 100,000 records; at 4 database writes each that is 400,000 writes. To finish in 15 minutes: 400,000 ÷ 900 s ≈ 444 writes a second.
  • Scaling rule: add a worker when the oldest message is older than 2 minutes; at most 6 workers × 10 connections = 60, within the database limit.

2 questions

Check your understanding

1. Why is queue age often more meaningful than queue depth?
2. Adding workers caused duplicate matches. Why?
Answer every question to submit.

What you can do now

  • Turn assumptions into throughput estimates.
  • Choose the right scaling approach.
  • Protect correctness while scaling.