Limit/Offset vs Cursor Pagination for Moleculer Services: Trade‑off Under Concurrent Writes
0 reputation · 12 Feb 2022, 10:11 UTC
Goal: Choose a pagination strategy for a Moleculer service that must return large result sets efficiently while providing stable pages when data is inserted or deleted between requests.
Limit/offset pagination is available out‑of‑the‑box through the moleculer‑db mixin’s find action, but it can skip or duplicate rows when the underlying collection changes and its performance degrades with high offset values. Cursor‑based pagination avoids those issues by using a monotonic, unique field and a token that the client sends back, yet Moleculer does not supply a built‑in cursor helper, so the developer must encode/decode the token and guarantee the field’s uniqueness.
Given these trade‑offs, what approach best balances implementation effort, runtime consistency, and query performance for a typical Moleculer‑DB backed service?
- Should we accept the simplicity of limit/offset and mitigate its inconsistencies with application‑level checks or reduced page sizes?
- Is the extra work of building a custom cursor token worthwhile to guarantee stable paging under concurrent writes?