Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Reordering items at scale: From O(n) to O(1)

Reordering items at scale: From O(n) to O(1)

Other Decks in Programming

Transcript

  1. Inserting a New Book 1. Initial state 2. A new

    book arrives 3. Conflict 4. Relabeling
  2. Moving a Book: A → B 1. Initial state 2.

    Request move book 2 to 4 3. After book 2 is moved 4. Relabeling
  3. This pattern is everywhere! • Ecommerce shopping list • Music

    Playlists • Kanban boards • Photos, Bookmarks, Menus ...
  4. Sorting lists at scale Small List 1,000 items. Reorder one?

    up to 1,000 writes. Medium List 25,000 items. Reorder one? up to 25,000 writes. Large List N items. Reorder one? up to N writes.
  5. New JOIN table Table A Table B Table C Table

    D Monitors: Database Writes per Table
  6. The timeline 1. Identified a problem Database writes Observed two

    bad patterns caused by Insert and Reorder. Established target and baseline. 2. Monitoring 3. Explore and Prototype Investigate
  7. X 1 X 10 X 5 X 3 X 2

    Bisections = Log 2 (Gap) Bisections
  8. • Integers (consecutive numbers) 1, X, 2 ❌ • Decimal

    1, 1.5, 2 ✅ • Big int 1000, 1500, 2000 ✅ • String aa, ab, ac ✅ Data Types: Exploring a Solution
  9. • Storage Size: Fixed vs Variable • # Bisections before

    rebalance: Log₂(Gap) • Performance on reads & writes • Development cost Data Types: Exploring a Solution
  10. • Lexicographically ranking system developed by Atlassian. • The Format

    "0 | aaab", "0 | aaac" • When no room: extend the string length or rebalance (e.g., "aa" and "ab" -> "aai") • Rebalance: no room, strings too long, and periodically. Strings with optimizations - Lexorank Rebalancing
  11. Example: 3 Buckets: 🟢 Active → 📥 Destination | 💤

    Spare Lexorank - Rebalance with buckets *The example is illustrative, it uses a few digits for simplicity Before (Bucket 0 — cramped): 😫 no room 0|aa 0|ae 0|aaj 0|aaji 0|aajii Bucket 0
  12. Example: 3 Buckets: 🟢 Active → 📥 Destination | 💤

    Spare Lexorank - Rebalance with buckets *The example is illustrative, it uses a few digits for simplicity Before (Bucket 0 — cramped): Bucket 0 😫 no room
  13. Example: 3 Buckets: 🟢 Active → 📥 Destination | 💤

    Spare Lexorank - Rebalance with buckets *The example is illustrative, it uses a few digits for simplicity Before (Bucket 0 — cramped): During migration (0 —> 1): Bucket 0 Bucket 1 After migration (Bucket 1): Bucket 1 Bucket 0 Bucket 0 😫 no room
  14. Optimizations • The element size is variable. It depends on

    the list size. • The Gap is variable and unevenly distributed. • Optimized for usage patterns. • API optimization. Thanks to P. Racicot, A. Richner, A. Pham, M. Ariss, E. Rumley & I. Chepelev for their optimization contributions!
  15. Move request (2 , 3) → 4 == (4→ 2)

    Optimizing User Requests
  16. Observed two bad patterns caused by Insert/Reorder Established target and

    baseline. The timeline 1. Identified a problem 2. Monitoring Database writes 5. Measure the impact Compare with target 3. Explore and Prototype Investigate Do things; Tell people! 4. Implementation Ship, ship, ship! # alerts, tests, etc. 6. Prevent degradation
  17. Ignacio Chiazzo ignacio-chiazzo Key Takeaways • Simple problems become hard

    problems at scale. • Using Integers works well for small lists, but they don't scale. • The six-step framework applies to any performance problem. • And now you know why the AI asked for the list size.