Note: when user count exceeds 500 thousand and subscription count exceeds 10 million, a plain follows table without an index starts to slow down: counting followers becomes a full scan. We are a team with 5+ years of experience developing social applications; we have delivered over 30 projects with subscription systems handling millions of loads. We solve this problem comprehensively — from data schema with indexes and denormalization to push notifications and recommendations. An index on followee_id speeds up COUNT(*) by a thousand times at a million records, and denormalized counters in users give a response under 1 ms. Order the development of your subscription system from us — we guarantee stability under any load. Our experience covers all nuances: App Store Review Guidelines (Section 4.2/5.1), confidentiality requirements, and performance. In one project for a social network with 2M DAU, we designed a subsystem processing 10K actions per second without downtime. In this article, we'll discuss which database schema is optimal, how to avoid performance degradation under growth, and why optimistic UI updates are critical for user experience.
Data Schema and Queries
Table follows:
CREATE TABLE follows ( follower_id BIGINT NOT NULL, followee_id BIGINT NOT NULL, created_at TIMESTAMP DEFAULT NOW(), PRIMARY KEY (follower_id, followee_id) ); CREATE INDEX idx_follows_followee ON follows (followee_id); An index on followee_id is mandatory — it speeds up subscriber counting by 1000x at 1M records. Denormalization of counters in the users table (fields followers_count and following_count) updated via a trigger or queue gives response times under 1 ms.
| Method | Execution Time | Database Load |
|---|---|---|
| Denormalized field | <1 ms | None |
| SELECT COUNT(*) with index | 10–100 ms | Moderate |
Another way to speed up mutual subscription checking is caching in Redis: 100x faster than SQL at 1M records. For frequent queries (who follows me), we store a hash set follower_id → Set<followee_id>.
How to Implement a Follow/Unfollow Button with Optimistic Update
Optimistic update is mandatory — the button toggles instantly before the server responds. On error, the state reverts. On iOS we use Combine, on Android — StateFlow. Implementation steps:
- Toggle the UI button state (e.g., from “Follow” to “Following”).
- Send an asynchronous follow/unfollow request.
- On success, commit the new state.
- On error, revert the UI to the original state.
// iOS func toggleFollow(userId: String, currentlyFollowing: Bool) { let optimisticState = !currentlyFollowing updateFollowButton(isFollowing: optimisticState) let request = optimisticState ? apiService.follow(userId) : apiService.unfollow(userId) request.sink( receiveCompletion: { [weak self] completion in if case .failure = completion { self?.updateFollowButton(isFollowing: currentlyFollowing) // revert } }, receiveValue: { _ in } ).store(in: &cancellables) } Three button states: “Follow”, “Following”, and “Unfollow” (shown on long press). It's important not to make “Unfollow” the primary text — users might mistake it for confirming a subscription. For SwiftUI we use @State with the optimistic update pattern, for Jetpack Compose — mutableStateOf.
Private Accounts and Follow Requests
If the app supports private profiles, we introduce a table follow_requests (requester_id, target_id, status, created_at). The target user sees incoming requests, accepts or rejects them. Upon acceptance, the record moves to follows, and a push notification is sent to the requester.
How Pagination of the Subscriber List is Organized
Pagination is cursor-based by created_at DESC. For each user in the list, a batch query checks isFollowedByMe: SELECT followee_id FROM follows WHERE follower_id = ? AND followee_id IN (?) — one query per page.
Optimized query for isFollowedByMe check
SELECT followee_id FROM follows WHERE follower_id = ? AND followee_id IN (?, ?, ?); On iOS we use UITableViewDiffableDataSource with prefetching 3 cells before the end. On Android — LazyColumn with Paging 3 and RemoteMediator. For SwiftUI — List with PrefetchingDataSource, for Jetpack Compose — LazyColumn with PagingData.
Notifications on Subscription
On a new subscription, we send a push notification: “John subscribed to you” (FCM/APNs). Deeplink leads to the subscriber's profile. Batching: if 5 people subscribe within a minute, a single notification “5 new subscribers” is sent.
| Notification Type | Channel | Batching |
|---|---|---|
| New subscription | Push (FCM/APNs) | Up to 5 events per minute |
| Request accepted | Push | None |
Configuring push notifications requires correct handling of APNs certificates and FCM keys. We prepare provisioning profiles for iOS and google-services.json for Android.
Recommendations: “Who to Follow”
A simple heuristic — friends of friends. SQL:
SELECT DISTINCT f2.followee_id FROM follows f1 JOIN follows f2 ON f1.followee_id = f2.follower_id WHERE f1.follower_id = :me AND f2.followee_id != :me AND NOT EXISTS (SELECT 1 FROM follows WHERE follower_id = :me AND followee_id = f2.followee_id) LIMIT 20; For large graphs, precomputation via a worker in Redis reduces response time to 5 ms.
How We Develop the Subscription Module: Work Stages
| Stage | Duration | Result |
|---|---|---|
| Analysis | 1 day | Determine load and requirements |
| Design | 1 day | DB schema, indexes, cache |
| API Implementation | 1–2 days | Endpoints: follow/unfollow, list, recommendations |
| Client Integration | 1–2 days | iOS (Swift/SwiftUI), Android (Kotlin/Jetpack Compose) |
| Testing | 1 day | Load testing up to 1 million subscribers |
| Deployment and Monitoring | 0.5 day | Deployment instructions |
What's Included in the Work
- Database schema with indexes and denormalization
- Server API endpoints (REST or GraphQL)
- Client code for iOS (Swift/SwiftUI) and Android (Kotlin/Jetpack Compose)
- Push notifications with batching
- API and integration documentation
- Deployment and monitoring instructions
Timelines
Basic system (follow/unfollow, counters, list) — 1‑2 days. With private accounts, notifications, and recommendations — 3‑5 days. Pricing is determined individually. Get a consultation — we'll evaluate your project in one day. Contact us to discuss details and order development. We also assist with publication on App Store and Google Play, considering App Store Review Guidelines and Google Play Console requirements.







