Setting Up Autoscaling for Mobile App Servers
We know what a 503 looks like on a user's screen after a push notification blast. When 3000 rps hit a single pod, the server crashes and the app store rating plummets. Autoscaling solves this, but configuring it requires architectural insight. In our practice, over 50 projects have seen a 30–50% reduction in infrastructure costs thanks to properly tuned HPA and KEDA. This article breaks down how to achieve zero-downtime for a mobile API with guaranteed SLAs.
How to Choose Between HPA and KEDA?
| Type |
Description |
When to Use |
| HPA |
Scale by CPU/memory |
Predictable load, standard metrics |
| VPA |
Change requests/limits |
JVM services with heap growth |
| Cluster Autoscaler |
Add nodes |
Cluster resource shortage |
| KEDA |
Scale by external events |
Queues, Kafka lag, push notifications |
For a mobile API with push notifications, KEDA reacts to load changes 2–3 times faster than HPA by CPU, because scaling starts before traffic arrives.
Types of Autoscaling and When to Apply Them
Horizontal Pod Autoscaler in Kubernetes adds pods under load and removes them when it subsides. The basic metric is CPU utilization, but for a mobile API, better metrics are p99 latency, request queue depth, or custom Prometheus metrics exposed via the Custom Metrics API.
Vertical Pod Autoscaler changes a pod's requests/limits. Useful for JVM services where memory grows as the heap warms up. However, VPA requires a pod restart on resource changes, so it's unsuitable for stateful services. Use VPA in recommendation mode initially to gather data.
Cluster Autoscaler adds/removes Kubernetes nodes in the cloud (AWS EC2, GCP GKE, Azure AKS). It works alongside HPA: HPA wants 5 pods but there's no room — Cluster Autoscaler adds a node.
KEDA scales based on external metrics: RabbitMQ queue length, Kafka lag, number of messages in Redis Streams. For a mobile app with a push notification queue: workers scale by the number of tasks in the queue, not by CPU.
Setting Up HPA for a Mobile API
The problem with standard CPU scaling: during a request spike, CPU first rises, then HPA decides to add a pod (15–30 seconds), the pod starts (another 10–30 seconds), and readiness probes pass. Total: 30–60 seconds before the new pod begins accepting traffic. By then, some mobile clients have already received a 503.
Solutions:
- Predictive scaling — scale out before the expected peak (send push → immediately scale out)
- ScaleUp faster, ScaleDown slower —
scaleUp.stabilizationWindowSeconds: 0 (immediate scaling up), scaleDown.stabilizationWindowSeconds: 300 (wait 5 minutes before scaling down to avoid thrashing)
- MinReplicas: 2 — never drop to 1 pod to prevent downtime during rolling updates
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: mobile-api-hpa
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: mobile-api
minReplicas: 2
maxReplicas: 20
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 60
behavior:
scaleUp:
stabilizationWindowSeconds: 0
policies:
- type: Pods
value: 4
periodSeconds: 60
scaleDown:
stabilizationWindowSeconds: 300
Step-by-step HPA configuration:
- Define the metric (CPU, memory, or custom via Prometheus Adapter).
- Set target utilization (50–70% for CPU).
- Configure behavior for scaleUp (fast) and scaleDown (slow).
- Set minReplicas >= 2.
- Test with load testing (e.g., k6 or Locust).
- Monitor latency and errors using SLO-based alerts.
Why CPU-Based Scaling Isn't Suitable for a Mobile API?
CPU lags behind requests — that's a fact. While HPA detects the peak and adds a pod, some clients already see a 503. For a mobile API, it's better to use p99 latency or request queue depth metrics. In our case with an iOS news app, we solved this with KEDA and an SQS queue — scaling started before traffic arrived, and 503s disappeared.
Cold Start Problem for Mobile Traffic
Go and Node.js start in 1–3 seconds — acceptable. JVM applications (Spring Boot) take 10–20 seconds. Lambda (serverless) cold starts take 500ms–3 seconds depending on runtime and package size.
For JVM: keep at least 2 pods hot at all times. GraalVM Native Image — starts in 0.1–0.3 seconds, but requires reflection configuration. Spring Boot 3 + GraalVM Native — a production-ready combination. Standard JVM instances cost ~$200/month, GraalVM ~$300, Provisioned Concurrency ~$500.
For serverless (AWS Lambda, Google Cloud Functions): Provisioned Concurrency keeps N instances warm. It's more expensive, but cold starts disappear for those instances.
Case: iOS news app. After an editorial push, 40,000 concurrent opens in 2 minutes. One pod on 2 vCPU handled 400 rps. HPA set to 60% CPU — by the time a pod was added, the peak load had passed. Solution: KEDA with a CloudWatch metric (SQS queue depth) — on push send, automatically added 8 pods before traffic arrived. Zero 503s on the next three sends.
| Solution |
Startup Time |
Suitable For |
Cost |
| Standard JVM |
10–20 s |
Stateful, large services |
~$200/mo |
| GraalVM Native |
0.1–0.3 s |
Microservices, serverless |
~$300/mo |
| Provisioned Concurrency |
0 (warm) |
Critical paths |
~$500/mo |
What's Included in Turnkey Setup
- Audit of current architecture and load testing.
- Configuration of HPA, VPA, Cluster Autoscaler, or KEDA.
- Custom metrics setup (Prometheus, CloudWatch, Datadog).
- Cold start optimization (GraalVM, Provisioned Concurrency).
- Documentation on scaling schemes.
- Monitoring and alerts based on SLO.
- One team training session.
- 2 weeks of support after delivery.
Turnkey setup available from $2,500, with timelines from 2 to 14 days depending on complexity. An average project saves $1,200 per month on cloud costs after autoscaling configuration.
Kubernetes official documentation on HPA
With the right configuration using HPA's behavior scale policies and VPA's recommender mode, cloud resource savings reach 40%, and operating costs drop by $500–2000 per month for an average project.
Mobile App Architecture
The app is built in a single ViewController with 2000 lines. Network calls, business logic, UI updates—all in one place. Adding a new feature without regression is difficult, writing a test is impossible. This isn’t “bad code”—it’s a lack of architecture. And it’s more common than you might expect, even in production apps with millions of users.
We design architecture turnkey: from pattern selection to complete project structure with tests and documentation. In 7–10 days you get clean, modular code ready for scaling.
Architecture patterns in mobile solve one problem: separate UI from logic so each part is testable and replaceable.
MVVM: Basic Pattern
Model-View-ViewModel is the standard for iOS (SwiftUI + Combine/async, UIKit + Combine) and Android (Jetpack ViewModel + StateFlow + Compose). The ViewModel holds UI state and business logic. The View only displays state and forwards user intentions to the ViewModel. The Model represents data and its source.
Key rule: ViewModel knows nothing about UIKit or Android View classes. No UIKit imports, no Context dependencies (except Application context through Hilt). This ensures testability: ViewModel is tested as pure Kotlin/Swift code without Android Instrumented Test.
MVVM covers 70% of needs. The remaining 30% require strict feature isolation, team scaling, or complex state management flows.
Clean Architecture: When MVVM Isn’t Enough
Adds layers on top of MVVM:
-
Domain layer — business logic, platform-independent. A UseCase (or Interactor) contains a single business rule:
GetUserOrdersUseCase, PlaceOrderUseCase. Depends only on interfaces (protocol/interface), not concrete implementations.
-
Data layer — repository implementations.
OrderRepositoryImpl implements OrderRepository from domain. Knows about Retrofit, Room, UserDefaults. The ViewModel doesn’t know where data comes from—network or cache.
-
Presentation layer — ViewModel + View. Knows about Domain, not Data.
Dependency rule: dependencies point inward only. Domain depends on nothing. Data and Presentation depend on Domain.
Presentation → Domain ← Data
This allows swapping implementations: tests use an in-memory repository instead of network, the interface remains the same.
Practical caveat: Clean Architecture adds files and layers. For small apps, this is overhead. It’s justified starting from ~15 features and teams of 3+ developers.
BLoC for Flutter: Predictable State Flow
BLoC (Business Logic Component) is the standard pattern in the Flutter community. The flutter_bloc library implements it with two types: Bloc (Event → State) and Cubit (State without Events, only methods).
Bloc processes Event and emits a new State via on<EventType> handlers. State is immutable—a new object for each change. BlocBuilder re-renders only the part of the tree where state changed.
// Event
abstract class CartEvent {}
class AddItemToCart extends CartEvent {
final String productId;
AddItemToCart(this.productId);
}
// State
abstract class CartState {}
class CartLoaded extends CartState {
final List<CartItem> items;
CartLoaded(this.items);
}
// Bloc
class CartBloc extends Bloc<CartEvent, CartState> {
CartBloc(this._cartRepository) : super(CartLoaded([])) {
on<AddItemToCart>(_onAddItem);
}
Future<void> _onAddItem(AddItemToCart event, Emitter<CartState> emit) async {
final current = state as CartLoaded;
final updated = await _cartRepository.addItem(event.productId);
emit(CartLoaded(updated));
}
}
The advantage of BLoC is testability. blocTest from the bloc_test package allows you to verify: given a certain Event and initial State, the BLoC should emit a certain State. No UI, no mocks for the Flutter framework.
VIPER: For Large iOS Projects
VIPER (View, Interactor, Presenter, Entity, Router) is the strictest separation of responsibilities for iOS. Each component has a protocol and concrete implementation.
-
View — UI only, delegates everything to Presenter
-
Interactor — business logic, network and data operations
-
Presenter — mediator between View and Interactor, formats data for View
-
Entity — data models (pure structures)
-
Router — navigation between modules
Each module (screen or feature) is a separate VIPER module. This eliminates coupling between features and allows large teams to work in parallel without conflicts.
The cost: many files, many protocols. Boilerplate is generated via Sourcery or custom Xcode templates. VIPER is justified for apps with 10+ developers and 50+ screens.
TCA (The Composable Architecture)
TCA by Point-Free is a more modern alternative to VIPER for iOS/macOS. Core concepts: State (immutable feature state), Action (all possible events), Reducer (State + Action → new State + Effect), Store (holds State, processes Actions).
Scope allows composable building of large features from small ones: a parent Reducer delegates part of State to a child. Each feature is tested in isolation via TestStore with precise control over Effects.
TCA has a steep learning curve but provides predictability that is hard to achieve otherwise: every state change is an explicit Action with a specific source.
Which Pattern to Choose for Your Project?
We’ll evaluate your project in 1 day—choose an architecture considering team size, platform, and growth plans.
| Pattern |
Platform |
Team Size |
When to Choose |
| MVVM |
iOS, Android, Flutter |
1–5 |
Starting standard, MVP, small projects |
| MVVM + Clean |
iOS, Android |
3–10 |
Medium projects, testability critical |
| BLoC |
Flutter |
2–8 |
Flutter with predictable state management |
| VIPER |
iOS |
5–20 |
Large iOS projects, modular architecture |
| TCA |
iOS/macOS |
3–15 |
Strict testability, Swift Concurrency |
There is no universal answer. Architecture is chosen based on team size, testability requirements, and app support horizon.
What Components Are Included in Our Architecture Work?
-
Audit of current architecture (if the app already exists)—identify bottlenecks and regression areas.
-
Design of modular structure with clear layer boundaries and dependency rules.
-
Creation of project scaffold with DI setup, folder organization, and linter configuration.
-
Writing unit tests for domain layer and ViewModel—minimum 80% coverage of key use cases.
-
Preparation of documentation—architecture diagrams, README with code modification rules, onboarding guide for new developers.
-
Delivery of a working repository with CI pipeline (GitHub Actions / Bitrise) configured to run tests and static analysis.
All this is included in the design cost. Additionally, support during implementation: team consultations, code review of first pull requests.
How Does Lack of Architecture Affect Development Speed?
Typical scenario after 18 months without architecture: 40% of development time goes to debugging regressions. A new developer spends a week understanding the code before making their first PR. Tests aren’t written “because it’s hard to mock.” Adding a new feature requires understanding half the codebase.
Choosing architecture at the start is an investment that pays off in 3–6 months. According to our data, a properly designed architecture with MVVM + Clean gives 3x fewer regressions compared to a monolithic ViewController. And the cost of implementation is recouped in 2–3 sprints.
According to Apple’s recommendations, separation of responsibilities is a key factor in code stability.
Why Trust Our Team with Architecture?
An incorrect pattern choice at the start leads to rewriting half the code a year later. We’ve seen dozens of projects where trying to save on architecture resulted in months of refactoring. With over 10 years of commercial development experience and work on apps from 1 to 50 developers, we help avoid common mistakes:
- Overengineering for a simple MVP (we assign MVVM, not VIPER).
- Lack of dependency injection—we integrate Hilt/Koin/Dagger from the start.
- Ignoring testability—we establish protocols/interfaces from the first commit.
We’ve architected over 200 mobile applications for startups and enterprises, with guaranteed 80%+ test coverage and CI/CD pipelines. Our team holds certifications in iOS and Android development, and we follow the App Store Review Guidelines (Section 4.2/5.1) to ensure smooth store approvals.
Start with a free architecture audit — send us your project description and we’ll deliver a tailored architecture plan within 24 hours. Reach out via Telegram or email to get started.