Build a Multi-Tenant Task Tracker API at Scale
System DesignModel answerLast updated
Design a backend for a personal task manager serving millions, covering API contracts and versioning, tenant isolation, per-user throttling, caching, and data partitioning.
Design a Personal Task Tracker Backend
You are asked to design the backend for an application where each person keeps a private list of tasks. The product has millions of users, so it must stay fast and highly available.
What the service must do
- A user can create a task with a title, a due date, and a priority.
- A user can list, edit, and remove only their own tasks.
- The API must be versioned (for example v1 and v2) so that new features can ship without breaking older clients.
Quality attributes
- Authentication and isolation: Identify callers securely. One user must never be able to read or change another user's data.
- Throttling: Cap how many requests a single user can send per minute to prevent abuse.
- Speed: Reading a task list must be quick, especially for data that is requested often.
- Consistency: Decide where updates must be strongly consistent and where eventual consistency is acceptable.
What to cover in your answer
- API contract: Main endpoints, how versions are expressed, and how credentials are sent.
- Data model: Schemas for tasks and users, plus how you would partition or shard the data.
- Architecture: The main components (gateway, load balancer, cache, datastore, etc.) and how a request flows through them.
- Request-time logic: How you would implement rate limiting (for example a token bucket) and how you keep the cache correct when tasks change.
State your assumptions about scale and latency targets before diving into the design.
Hints
Hint 1
Every request touches exactly one user's data. Ask how that single fact can drive your partition key, your cache keys, and your tenant-isolation strategy.
Hint 2
Take the user identity from the verified token, never from the URL or request body. Think about what status code to return when someone asks for a task that belongs to someone else.
Hint 3
For rate limiting, think about an atomic per-user counter in a shared store, and what the system should do if that store is down. For caching, walk through the race where a slow reader writes a stale value back after a writer has invalidated the key.
Model answer
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The product looks like plain CRUD, so keep the core simple and spend depth on five things: tenant isolation, partitioning, rate limiting, cache correctness, and versioning.
1. Assumptions and scale
- 50M registered users, 10M daily active, about 20 requests per active user per day, giving about 200M requests/day. That is roughly 2.3K QPS on average and about 10K QPS at peak, with around 80% reads.
- About 100 tasks per user at about 500 B each gives about 5B tasks and about 2.5 TB before replication. This is small in bytes but large enough to need partitioning for availability.
- Targets: p99 under 100 ms for reads, under 200 ms for writes, availability of 99.95% or better.
- Consistency: a user must see their own writes immediately. Cross-region replicas may lag.
The key observation is that every access is scoped to one user. This drives the rest of the design.
2. API design
Use URI versioning because it is explicit, cache-friendly, and easy to route at the gateway.
POST /v1/tasks create (Idempotency-Key header)
GET /v1/tasks?sort=due_date&cursor=...&limit=50
GET /v1/tasks/{id}
PATCH /v1/tasks/{id} partial update (If-Match: <etag>)
DELETE /v1/tasks/{id}
Authorization: Bearer <JWT>
429 responses carry Retry-After
- Identity from the token. The user id comes from the verified JWT subject, never from the path or body. This removes a whole class of object-reference bugs.
- 404 for foreign tasks, not 403, so existence is not leaked.
- Optimistic concurrency. An ETag/
If-Matchcheck on a version field returns 412 on stale writes and prevents lost updates. - Idempotent creates. An
Idempotency-Keymakes client retries safe. - Cursor pagination stays stable under concurrent inserts, unlike offsets.
- Versioning policy. Bump the major version only for breaking changes (for example v2 adds tags and subtasks and turns
priorityfrom an integer into an enum). Both versions call one shared domain layer through thin request/response adapters, so v1 does not become a code fork.
3. Data model and partitioning
A partitioned key-value or document store (DynamoDB, Cosmos DB) fits the purely key-based access pattern. Sharded Postgres also works.
Tasks
partition key: user_id
sort key: task_id (time-ordered ULID)
fields: title, description, due_date, priority, status,
created_at, updated_at, version, deleted_at
Users
partition key: user_id
fields: email, idp_subject, plan_tier
- Listing my tasks becomes a single-partition range query.
- Because the partition key is derived from the token, a query cannot physically reach another tenant's rows.
- Hashing
user_idspreads load evenly. One human generates little traffic, so hot partitions are rare (watch for bots and handle them with rate limits). deleted_atgives soft delete for undo and audit.versionbacks the ETag check.
4. Architecture
Client -> DNS/CDN -> Load balancer -> API gateway -> Task service (stateless)
| |-> Redis cache
| |-> Database (multi-region)
| '-> event stream (search, notifications)
'-> Redis (rate-limit counters)
- Gateway: terminates TLS, validates JWT signature and expiry using cached signing keys from the identity provider, enforces rate limits, and routes by version. Bad traffic is rejected before it costs app compute.
- Task service: stateless and autoscaled. It re-checks ownership anyway as defense in depth.
- Redis: clustered with replicas, used for cache and limiter state.
- Database: writes go to a home region (strong consistency within the partition). Remote regions serve eventually consistent reads.
5. Rate limiting
Use a token bucket per user in Redis.
- Key
rl:{user_id}holdstokensandlast_refill. - Each request runs a single Lua script (atomic in Redis): refill with
tokens = min(cap, tokens + elapsed * rate), then iftokens >= 1decrement and allow, otherwise return 429 withRetry-After. - Example: capacity 100, refill 100/minute allows short bursts but caps sustained rate. Tier limits by plan and make writes stricter than reads.
- Set a TTL on idle keys to bound memory.
Trade-offs:
- Compared with a fixed window, the token bucket avoids double bursts at window edges. It is cheaper than a sliding log.
- If Redis is down, fail open with a coarse per-instance local limiter as backstop. Failing closed would turn a cache outage into a full outage.
- Unauthenticated endpoints such as login need per-IP limits.
6. Caching and invalidation
Use cache-aside:
- Cache the first page of a user's list (
tasks:{user_id}:{sort}) and single tasks (task:{id}), with a TTL of about 5 minutes. - On write: update the database first, then delete the cache keys (do not update them). Deletion is idempotent and avoids ordering problems between concurrent writers. The next read repopulates.
Classic race: reader A misses and reads the old value, writer B updates the DB and deletes the key, then A writes the stale value into the cache. Mitigations:
- A per-user version counter (
list_ver:{user_id}) incremented on each write and embedded in cache keys, so stale entries are never read again. - A short TTL as a safety net, and optionally delayed double-delete.
Read-your-writes holds if the owner's reads in the home region go to the primary or use the version counter.
7. Common mistakes
- Accepting
user_idfrom the client. - Offset pagination on a list that changes.
- Updating cache values on write instead of invalidating.
- Failing closed when the limiter store is unavailable.
- Forking the codebase per API version.
- Over-engineering (microservices per entity) for a problem that is a single-partition key lookup.
8. Likely follow-ups
- Sharing a list between users would break the single-partition assumption. Add a membership table and a secondary index, and accept more complex queries.
- Reminders can use a time-bucketed index of due dates feeding a queue.
- Offline sync would need per-task versions and conflict resolution (last-writer-wins or merge).