Record vs Class
Records are useful for immutable/data-oriented models and support with expressions.
ref / out / in
- ref: caller initializes, callee can modify.
- out: caller need not initialize, callee must assign.
- in: readonly reference, useful for large structs.
Task vs Thread
Thread is an execution unit. Task is a higher-level abstraction representing work that may complete later and does not require a dedicated thread.
ThreadPool
Creating threads is expensive. With async I/O, a thread does not remain blocked while waiting and can serve other work.
CancellationToken
Source requests cancellation; token communicates it. Propagate request tokens through controller → service → repository → I/O.
Task.WhenAll
Do not confuse tasks with threads. Same EF Core DbContext should not run concurrent operations.
IEnumerable vs IQueryable
With EF, build filters before materialization so the provider can translate them to SQL.
ToList too early
db.Players.ToList().Where(...) may load the full table. Prefer db.Players.Where(...).ToList().
First vs Single
OrDefault changes the zero-result behavior. Single/SingleOrDefault throw if multiple rows match.
Stack vs Heap
Value types are not always on the stack. A value-type field inside a heap object lives with that object.
Boxing
Boxing can allocate on the managed heap and increase GC pressure.
Dependency Injection
Authentication vs Authorization
401 vs 403
PUT vs PATCH
PUT is idempotent by HTTP semantics.
Middleware
Can inspect/modify request/response, call next, or short-circuit. Order matters: authentication before authorization.
Idempotency
Important for retries after timeout/communication failure. Often uses operation/message IDs and unique constraints.
Exception Handling
Catch when you know how to handle. Preserve stack with throw;, not throw ex;.
SQL slow?
Sargability
Avoid functions/conversions on indexed columns when possible. Example: range predicates on dates instead of YEAR(Date)=....
Clustered / Nonclustered
SQL Server concept. One clustered index per table; multiple nonclustered indexes.
INNER / LEFT JOIN
WHERE / HAVING
Optimistic Concurrency
Version/timestamp or conditional UPDATE. 0 affected rows can signal conflict.
Pessimistic Concurrency
Useful when conflicts are frequent or exclusive access is required.
Race Condition
Lost update is a classic example.
Deadlock
Reduce risk with consistent lock order, short transactions and minimal lock duration.
Horizontal Scaling
Move shared state to DB/Redis/broker. Containers package; orchestrators/platforms scale.
Monolith vs Microservices
Microservices add timeouts, retries, partial failures, data consistency, messaging and observability concerns.
Cache-aside
On write, invalidate/update cache depending consistency requirements.
Cache Consistency
Use TTL, invalidation, events/outbox, retries depending business requirements.
Dependency Inversion
DI is a technique that can help implement DIP.
Testing Strategy
Don't mock everything automatically; mock where isolation adds value.
Message Broker
Receives, stores/routes and delivers messages. Examples: RabbitMQ, Azure Service Bus.
Queue vs Pub/Sub
Pub/Sub is a communication pattern; RabbitMQ is a broker that can implement messaging patterns.
Why async messaging?
Trade-offs: eventual consistency, duplicates, ordering and observability complexity.
Transactional Outbox
A worker later publishes/processes the pending event and retries safely.
At-least-once
At-most-once
Eventual Consistency
Acceptable when business can tolerate staleness/delay.
Retry / Timeout / Circuit Breaker
Circuit: closed → open → half-open → recovered or open again.
Azure Hosting
Secrets
Redis
Ask consistency requirements before caching.
Messaging
Observability
Azure Monitor / Application Insights; OpenTelemetry for vendor-neutral instrumentation.
Docker
Docker packages and runs; orchestration/platform handles dynamic scaling.
Kubernetes
Deployment manages replicas; Service gives stable access; scheduler replaces pods; node scaling is separate from pod scaling.
Health Checks
Rolling Deployment
Use readiness checks and graceful shutdown/draining.
Blue/Green / Canary
CI/CD
Automate repeatable delivery and keep rollback strategy.
System Design Opening
- Traffic / concurrency?
- Read vs write ratio?
- Latency target?
- Consistency requirements?
- Failure tolerance?
Typical Scalable Backend
Add replicas, partitions or async work only when measurements justify them.
Database Bottleneck
Hot Key / Cache Stampede
Read Replicas
Consider read-your-writes requirements.
Purchase Flow
Use idempotency for retries/duplicate messages.
Realtime / Gaming
Keep hot match state in memory, use durable events/checkpoints as appropriate.