# Service Architecture and API Design

> Compare monoliths and microservices, design clean REST APIs with pagination and versioning, and add rate limiting, observability and resilience patterns. Free...

Canonical: https://www.internhack.in/roadmaps/tracks/cs-fundamentals/service-architecture-api-design
Last updated: 2026-11-28

Compare monoliths and microservices, design clean REST APIs with pagination and versioning, and add rate limiting, observability and resilience patterns.

Intermediate · about 5 hours · difficulty 3/5

Learn first: [Message Queues and Asynchronous Processing](https://www.internhack.in/roadmaps/tracks/cs-fundamentals/message-queues-async)

Every HLD answer includes some APIs and some service boundaries. This topic covers how to define them sensibly.

### Monolith vs microservices

- A **monolith** is one deployable app. Simple to build, test and deploy; the right default for small teams and most college projects.
- **Microservices** split by business capability (users, orders, payments), each owning its data. They allow independent scaling and deployment but add network calls, distributed failures and operational overhead.
- A **modular monolith** is a common middle path. Do not say "microservices" in an interview without naming the benefit you need.

### API design

- REST resources as nouns (`/users/42/orders`), correct methods and status codes.
- **Pagination**: offset/limit is simple but slow for deep pages; **cursor-based** pagination is stable and scales.
- **Idempotency keys** for POSTs like payments, so retries do not double-charge.
- Versioning (`/v1/`), consistent error formats, filtering and sorting.
- Alternatives: **gRPC** for fast internal calls, **GraphQL** for flexible client queries, **WebSockets** or server-sent events for real-time updates, and long polling.

### Protecting services

- **Rate limiting** with token bucket or sliding window, usually at the API gateway, returning 429.
- **Timeouts**, **retries with backoff**, **circuit breakers** (stop calling a failing dependency) and bulkheads.
- Service discovery and health checks.

### Observability

Logs, metrics (latency percentiles, error rates) and distributed traces. Mention monitoring and alerting in every design.

Good enough to move on: you can write the core API endpoints for a given system with request and response shapes, choose cursor pagination where appropriate, and explain what a circuit breaker prevents.

## Key concepts

- **Monolith vs microservices**
- **REST resource design**
- **Offset vs cursor pagination**
- **Idempotency keys**
- **gRPC, GraphQL and WebSockets**
- **Rate limiting at the gateway**
- **Timeouts, retries and circuit breakers**
- **Logs, metrics and tracing**

## Mini project

Design and implement the API for a small e-commerce backend: products with cursor pagination and filters, an orders endpoint that accepts an Idempotency-Key header, a per-user token bucket rate limiter returning 429, and a circuit breaker around a mock payment service.

## Check yourself

Why does offset pagination become slow and inconsistent on a large, frequently updated feed, and how does cursor pagination fix both problems?

## Free resources

- [Microsoft: Web API design best practices](https://learn.microsoft.com/en-us/azure/architecture/best-practices/api-design) (Microsoft Learn)
- [Martin Fowler: Microservices](https://martinfowler.com/articles/microservices.html) (martinfowler.com)
- [Martin Fowler: Circuit Breaker](https://martinfowler.com/bliki/CircuitBreaker.html) (martinfowler.com)

Previous: [CAP Theorem and Consistency Models](https://www.internhack.in/roadmaps/tracks/cs-fundamentals/cap-and-consistency) · Next: [The HLD Interview Framework](https://www.internhack.in/roadmaps/tracks/cs-fundamentals/hld-interview-framework)

## More in Distributed Systems and HLD Interviews

- [Message Queues and Asynchronous Processing](https://www.internhack.in/roadmaps/tracks/cs-fundamentals/message-queues-async)
- [CAP Theorem and Consistency Models](https://www.internhack.in/roadmaps/tracks/cs-fundamentals/cap-and-consistency)
- [Service Architecture and API Design](https://www.internhack.in/roadmaps/tracks/cs-fundamentals/service-architecture-api-design)
- [The HLD Interview Framework](https://www.internhack.in/roadmaps/tracks/cs-fundamentals/hld-interview-framework)
- [Classic HLD Case Studies](https://www.internhack.in/roadmaps/tracks/cs-fundamentals/hld-case-studies)

## Related

- [Roadmaps](https://www.internhack.in/roadmaps)
- [CS Fundamentals and Placement Prep Roadmap](https://www.internhack.in/roadmaps/tracks/cs-fundamentals)
