4Developers 2021: API na sterydach
Grzegorz Kotfis details 4Developers 2021: API na sterydach.
Summary
Grzegorz Korpus presents 'API na sterydach' (API on Steroids) at 4Developers 2021, focusing on three practical API optimization strategies: pagination, sorting, and field selection (delta shaping).
**Pagination Approaches:** Korpus discusses multiple pagination strategies with their trade-offs. The basic offset/limit approach using SKIP and TAKE is simple but problematic at scale—the database must scan and skip many rows to retrieve results from later pages, causing performance degradation on large datasets. He introduces cursor-based pagination using a marker or key to filter results instead of scanning, eliminating the offset overhead. However, traditional cursor pagination struggles with non-unique values (low-cardinality fields). To address this, Korpus demonstrates SQL window functions like ROW_NUMBER to create synthetic unique markers, enabling reliable cursor navigation. He also covers 'page drifting'—where results shift between requests due to concurrent data modifications—which cursor approaches mitigate. The response can include metadata (page info, total count) via response body or HTTP headers to support client navigation.
**Sorting Implementation:** Sorting conventions range from predefined field lists to dynamic sorting. Korpus shows hardcoding allowed fields in a switch statement for simplicity, then progresses to the Dynamic LINQ library for programmatic expression building from string parameters. He emphasizes pragmatism: starting with simple, documented approaches may suffice before adopting complex generic mechanisms, keeping code maintainable for the team.
**Field Selection (Delta Shaping):** Using C# reflection and LINQ expression trees, Korpus demonstrates dynamic projection—letting clients specify which fields to return. The implementation builds LINQ Select expressions at runtime, then uses an ExpandoObject 'shaper' to reduce the response to only requested fields. He addresses practical concerns: default field sets when no selection is provided, case-insensitive field matching, silent omission of non-existent fields, and excluding expensive-to-serialize columns from the database query itself rather than filtering after materialization.
**Core Principles:** Korpus stresses that APIs should not expose database structure directly. Filtering, sorting, and pagination should occur at the database level to minimize resource consumption and response sizes. He advocates for consistency in naming conventions and parameter formats across all endpoints, proper documentation, and avoiding premature optimization with complex reflection-based mechanisms when simpler approaches suffice.
Related talks
PROIDEA Events·53 min
Fuzzing 101 details 4Developers 2022: Kamil Frankowicz.
PROIDEA Events·31 min
API security examines 4Developers 2022: Bryant Schuck.
PROIDEA Events·43 min
Bartłomiej Szymański
Bartłomiej Szymański examines 4Developers 2022: GraphQL API czyli kto pyta, nie błądzi.


