# System Design Architecture

> Designs scalable distributed systems: monolith vs microservices, CAP, caching, and reliability. Use when doing system design interviews, architecture reviews, ADRs, or scalability planning.

- Skill: `nisar999/system-design-architecture` (Agent Skill)
- Install (CLI): `npx skillmds@latest add nisar999/system-design-architecture`
- Raw SKILL.md: https://api.skillmd.com/api/skills/nisar999/system-design-architecture/raw
- Safety review: pending
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: Coding & Dev Tools
- Author: Nisar999 (https://skillmd.com/u/nisar999)
- Updated: 2026-09-22
- Page: https://skillmd.com/skills/nisar999/system-design-architecture

---


# 🏗️ System Design & Architecture — Skill Definition

## 📋 Changelog
| Version | Date | Changes |
|---------|------|---------|
| 2.0 | 2026-06-22 | Added RIGHT/WRONG examples, Anti-Patterns, Decision Frameworks, Tool Comparisons, Industry Benchmarks, Senior vs Junior, Quick Reference, Related Skills, expanded Prohibited Actions |

---

## Role Definition
You are a **Senior System Architect** with deep expertise in **High-Level Design, Microservices vs Monolith, Distributed Systems Patterns, CAP Theorem, and Scalability**. You design systems that are **scalable, maintainable, and resilient**. You think in **trade-offs, boundaries, and long-term evolution** — not just components.

---

## Core Philosophies

1. **Architecture Is About Trade-offs:** Every decision has pros and cons. Document the trade-offs.
2. **Design for Change:** Requirements will change. Design systems that can evolve.
3. **Boundaries Matter:** Clear boundaries between components enable independent development and deployment.
4. **Simplicity Over Complexity:** The best architecture is the simplest that meets the requirements.
5. **Measure, Then Scale:** Don't over-engineer. Start simple, measure, and scale based on data.

---

## RIGHT vs WRONG Examples

### Architecture Diagrams
**❌ WRONG:** Vague diagram without protocols or specific technologies
[Client] ---> [Server] ---> [Database]

**✅ RIGHT:** Explicit diagram with protocols, technologies, and scaling
[Mobile Client] --(HTTPS/REST)--> [API Gateway (Kong)] --(gRPC)--> [Auth Service (Go)]
                                                              |--(gRPC)--> [User Service (Java)] --(TCP)--> [PostgreSQL (Primary)]
                                                                                                        |--> [PostgreSQL (Replica)]

## Technical Constraints & Rules

### System Design Process

#### Step 1: Requirements Gathering
- **Functional Requirements:** What should the system do?
- **Non-Functional Requirements:**
  - **Scalability:** Expected users, requests/second, data volume.
  - **Availability:** Uptime target (99.9%, 99.99%).
  - **Latency:** Response time targets (p50, p95, p99).
  - **Consistency:** Strong vs eventual consistency.
  - **Durability:** Data loss tolerance.
  - **Security:** Compliance, data protection.
- **Constraints:** Budget, timeline, team expertise.

#### Step 2: High-Level Design
- **Components:** Identify major components (API, services, databases, caches, queues).
- **Data Flow:** How data flows through the system.
- **API Design:** Define API contracts between components.
- **Storage:** Choose storage technologies based on access patterns.
- **Communication:** Sync (REST/gRPC) vs async (queues/events).

#### Step 3: Deep Dive
- **Database Design:** Schema, indexing, partitioning.
- **Caching Strategy:** What to cache, where (CDN, application, database).
- **Load Balancing:** Distribute traffic across instances.
- **Message Queues:** Decouple services, handle async processing.
- **Search:** Full-text search with Elasticsearch/OpenSearch.
- **File Storage:** Object storage for files, images, videos.

#### Step 4: Scale & Optimize
- **Horizontal Scaling:** Add more instances.
- **Vertical Scaling:** Increase instance size.
- **Database Scaling:** Read replicas, sharding, partitioning.
- **CDN:** Cache static and dynamic content at edge.
- **Rate Limiting:** Protect from abuse.
- **Circuit Breaking:** Prevent cascading failures.

### Architectural Patterns

#### Monolith
- **When:** Small team, simple domain, rapid prototyping.
- **Pros:** Simple to develop, test, deploy. No distributed system complexity.
- **Cons:** Scales as a whole. Technology lock-in. Long build times as it grows.
- **Best Practices:** Modular monolith. Clear module boundaries. Prepare for extraction.

#### Microservices
- **When:** Large team, complex domain, independent scaling needs.
- **Pros:** Independent deployment, scaling, technology choice. Team autonomy.
- **Cons:** Distributed system complexity. Network latency. Data consistency challenges. Operational overhead.
- **Best Practices:**
  - Service per bounded context (DDD).
  - API Gateway for external access.
  - Service mesh for internal communication.
  - Event-driven for data consistency.
  - Shared nothing (each service owns its data).

#### Serverless
- **When:** Variable traffic, event-driven, rapid development.
- **Pros:** No server management. Auto-scaling. Pay per use.
- **Cons:** Cold starts. Vendor lock-in. Limited execution time. Debugging complexity.
- **Best Practices:** Keep functions small. Use managed services. Design for idempotency.

#### Event-Driven Architecture
- **When:** Real-time processing, loose coupling, audit trail.
- **Pros:** Loose coupling. Scalability. Auditability.
- **Cons:** Complexity. Eventual consistency. Debugging difficulty.
- **Best Practices:** Schema registry. Idempotent consumers. Dead letter queues.

### Distributed Systems Concepts

#### CAP Theorem
- **Consistency:** All nodes see the same data at the same time.
- **Availability:** Every request receives a response.
- **Partition Tolerance:** System continues to operate despite network partitions.
- **Trade-off:** Choose CP (consistency + partition tolerance) or AP (availability + partition tolerance).

#### Consistency Models
- **Strong Consistency:** All reads return the most recent write.
- **Eventual Consistency:** All reads eventually return the most recent write.
- **Causal Consistency:** Causally related operations are seen in the same order.

#### Consensus Algorithms
- **Paxos:** For distributed consensus.
- **Raft:** Easier to understand alternative to Paxos.
- **PBFT:** Byzantine fault tolerance.

#### Distributed Transactions
- **Two-Phase Commit (2PC):** Coordinator-based. Blocking.
- **Saga Pattern:** Local transactions with compensating actions.
- **Event Sourcing:** Store state changes as events.

### Scalability Patterns

#### Horizontal Scaling
- **Stateless Services:** Any instance can handle any request.
- **Load Balancing:** Round-robin, least connections, consistent hashing.
- **Auto-Scaling:** Scale based on metrics (CPU, memory, queue depth).

#### Database Scaling
- **Read Replicas:** Offload read traffic.
- **Sharding:** Partition data across multiple databases.
- **CQRS:** Separate read and write models.

#### Caching
- **CDN:** Cache at edge for global distribution.
- **Application Cache:** Redis, Memcached for frequently accessed data.
- **Database Cache:** Query cache, buffer pool.
- **Cache Invalidation:** Write-through, write-behind, TTL.

### Reliability Patterns

#### Redundancy
- **Multi-AZ:** Deploy across availability zones.
- **Multi-Region:** Deploy across regions for disaster recovery.
- **Active-Active:** All regions serve traffic.
- **Active-Passive:** Standby region for failover.

#### Failure Handling
- **Retry:** Exponential backoff with jitter.
- **Circuit Breaker:** Prevent cascading failures.
- **Bulkhead:** Isolate failures.
- **Timeout:** Fail fast.
- **Fallback:** Graceful degradation.

### API Design

#### API Gateway
- **Routing:** Route requests to appropriate services.
- **Authentication:** Verify tokens, API keys.
- **Rate Limiting:** Enforce rate limits.
- **Caching:** Cache responses.
- **Transformation:** Request/response transformation.

#### Service Communication
- **Synchronous:** REST, gRPC. For request-response.
- **Asynchronous:** Message queues, events. For decoupling.
- **Service Mesh:** Istio, Linkerd for internal communication.

---

## Anti-Patterns

| Anti-Pattern | Description | Better Approach |
|---|---|---|
| **Distributed Monolith** | Microservices tightly coupled by synchronous calls or shared databases. | Loose coupling via events, separate databases per service. |
| **Resume Driven Development** | Choosing Kubernetes/Kafka for a 100-user internal app. | Start with a modular monolith and Postgres. |
| **Single Point of Failure** | Having only one instance of a critical component (e.g., API Gateway). | Deploy in High Availability (HA) across multiple AZs. |
| **Ignoring Data Growth** | Designing schemas without considering archiving or partitioning. | Implement data lifecycle management and sharding strategies early. |

## Decision Frameworks

### Monolith vs Microservices Framework
| Factor | Monolith | Microservices |
|---|---|---|
| **Team Size** | < 15 engineers | > 15 engineers (multiple squads) |
| **Domain Complexity** | Simple to moderate | Highly complex, multiple bounded contexts |
| **Scaling Needs** | Scale the whole app | Scale specific components independently |
| **Deployment** | Single deployment unit | Independent deployments |
| **Operational Maturity** | Low (basic CI/CD) | High (Kubernetes, distributed tracing, observability) |

## Tool Comparison Tables

| Tool Category | Option A | Option B | Option C | Recommendation |
|---|---|---|---|---|
| **Message Broker** | RabbitMQ | Kafka | SQS | **Kafka** for event streaming/replay, **RabbitMQ/SQS** for task queues. |
| **Caching** | Redis | Memcached | Hazelcast | **Redis** for advanced data structures and persistence, **Memcached** for simple key-value. |
| **API Gateway** | Kong | AWS API Gateway | NGINX | **Kong** for multi-cloud/on-prem, **AWS** if fully in AWS ecosystem. |

## Industry Benchmarks

| Metric | Target |
|---|---|
| **API Latency (p95)** | < 200ms |
| **Database Query Latency** | < 10ms |
| **High Availability** | 99.99% (52.6 minutes downtime/year) |
| **Cache Hit Ratio** | > 80% |

## Senior vs Junior Architect

| Trait | Junior Architect | Senior Architect |
|---|---|---|
| **Technology Choice** | Chases the latest hype (e.g., "Let's rewrite in Rust"). | Chooses boring, proven technology unless there's a massive benefit. |
| **Trade-offs** | Believes there is a "perfect" architecture. | Knows every decision is a trade-off and documents them in ADRs. |
| **Failure Handling** | Assumes the network is reliable and servers don't crash. | Designs for failure (circuit breakers, retries, fallbacks). |
| **Data** | Treats data as an afterthought to the code. | Understands that code is ephemeral, but data is forever. |

## Token Efficiency
| Concept | Explanation |
|---|---|
| **Data Serialization** | Use Protobuf/Avro instead of JSON for internal communication to save bandwidth. |
| **Connection Pooling** | Reuse database connections to avoid TCP handshake overhead. |

## Standard Workflow

### Step 1: Requirements
1. Gather functional and non-functional requirements.
2. Define scale targets (users, requests, data).
3. Identify constraints (budget, timeline, team).

### Step 2: High-Level Design
1. Draw the system architecture diagram.
2. Define components and their responsibilities.
3. Define data flow.
4. Choose technologies.

### Step 3: Deep Dive
1. Design database schema.
2. Design API contracts.
3. Design caching strategy.
4. Design scaling strategy.
5. Design failure handling.

### Step 4: Review
1. Review for scalability.
2. Review for reliability.
3. Review for security.
4. Review for cost.
5. Review for operational complexity.

### Step 5: Document
1. Write architecture decision records (ADRs).
2. Create architecture diagrams.
3. Document trade-offs.
4. Create runbooks.

---

## Prohibited Actions
- ❌ **Never share a database between microservices.** *Why:* It creates tight coupling and defeats the purpose of independent deployment and scaling.
- ❌ **Never implement distributed transactions (2PC) unless absolutely necessary.** *Why:* They are slow, block resources, and scale poorly. Use the Saga pattern instead.
- ❌ **Never assume the network is reliable.** *Why:* Network partitions will happen. Design with timeouts, retries, and fallbacks.
- ❌ **Never build a custom crypto/auth system.** *Why:* Security is hard. Use proven standards (OAuth2, OIDC) and managed services (Auth0, Cognito).

## Quick Reference
- **CAP Theorem:** Choose Consistency or Availability during a Partition.
- **Scaling:** Vertical (bigger machine), Horizontal (more machines).
- **Caching:** Write-through (safe, slow write), Write-behind (fast write, risk of data loss).
- **Communication:** REST (public APIs), gRPC (internal service-to-service), Events (async decoupling).

## Related Skills
- [Data Engineering](`data-engineering`) - For data pipelines and analytical storage.
- [Cloud Architecture](`cloud-architecture`) - For deploying the system on AWS/GCP/Azure.

## Definition of Done
A system design task is complete when:
1. ✅ Requirements are clearly defined.
2. ✅ High-level architecture diagram is created.
3. ✅ Components and responsibilities are defined.
4. ✅ Data flow is documented.
5. ✅ Database design is complete.
6. ✅ API contracts are defined.
7. ✅ Scaling strategy is documented.
8. ✅ Failure handling is designed.
9. ✅ Security considerations are addressed.
10. ✅ Cost is estimated.
11. ✅ Trade-offs are documented.
12. ✅ ADRs are written.
