AgentSkillsCN

latency-principles

全面指导如何诊断、优化并隐藏软件系统的延迟问题。适用于:(1) 排查性能缓慢的问题;(2) 设计低延迟架构(高频交易、实时系统);(3) 优化数据层与计算层;或 (4) 学习延迟理论(利特尔定律、协同遗漏)。

SKILL.md
--- frontmatter
name: latency-principles
description: "Comprehensive guide for diagnosing, optimizing, and hiding latency in software systems. Use when: (1) Debugging slow performance, (2) Designing low-latency architectures (High Frequency Trading, Real-time), (3) Optimizing Data/Compute layers, or (4) Learning about latency theory (Little's Law, Coordinated Omission)."

Latency Principles

This skill provides a systematic framework for minimizing delay in software systems, covering the entire stack from Physics and Hardware to Application Architecture and User Experience.

1. Core Principles

Start here for theory and diagnosis.

2. Optimization Strategies

Choose the strategy based on the bottleneck layer:

Data Layer (Storage & Access)

Problem: Database queries are slow, network round-trips are killing performance, or throughput is low. Solution: references/data_patterns.md

  • Colocation: Moving code to data (Edge/Kernel-bypass).
  • Partitioning: Sharding data to increase parallelism.
  • Caching: Storing hot data in memory (Strategies & Eviction policies).
  • Replication: Consistency models (Strong vs Eventual) and topologies.

Compute Layer (Processing Logic)

Problem: High CPU usage, lock contention, Garbage Collection pauses, or slow algorithms. Solution: references/compute_optimization.md

  • Eliminating Work: Better algorithms, Zero-copy serialization, Object pooling.
  • Wait-Free Sync: Replacing Mutexes with Atomics, Ring Buffers, and Lock-free structures.
  • Concurrency: Thread-per-core, Coroutines, and parallel execution models.

UX Layer (Hiding Latency)

Problem: The backend cannot be made faster, but the user experience feels sluggish. Solution: references/hiding_latency.md

  • Asynchronous Processing: Event loops, Non-blocking I/O, Request Hedging.
  • Predictive Techniques: Prefetching data, Optimistic UI updates, Speculative execution.

Decision Guide: Which technique to use?

SymptomProbable CauseRecommended Strategy
High Avg LatencySequential processing / Slow I/OConcurrency (Async I/O) or Partitioning
High Tail Latency (p99)Lock contention / GC / Neighbor noiseWait-free Sync (Atomics) or Request Hedging
Network SlownessDistance / Protocol overheadColocation (Edge) or Binary Serialization (Protobuf)
Database LoadHot keys / Complex queriesCaching (Read-through) or Materialized Views
Slow WritesACID guarantees / IndexingWrite-Behind Caching or Sharding
"It feels slow"UI blocking on networkOptimistic Updates or Prefetching