API pagination: cursor vs offset

When to use cursor pagination and how to implement it safely.