A Scalable Solution to Ordering Data by Priority or Rank
It seems like such a simple request at first, “I want data to be ordered by rank or priority, and I want users to be able to re-order the data based on which is most important”. This is an extremely common scenario in app development. You will find this problem everywhere from Todo Lists and task management systems, to complex and specific use-cases. The solution might seem simple at first, depending on your language/db solution, there might even be native support for a priority queue, or perhaps an array could function here. The solution becomes much more complex when it has to scale.
In this article, we are going to go over several unscalable ways of handling this problem as well a scalable way of handling it. For the purpose of this problem, we are going to assume that we have a Todo list with 10 million items all ranked by their priority (scroll down to the solution section to skip to the meat). Obviously this is an unrealistic example, but the purpose forces us to come up with a solution that scales.
Naive Approaches
There are a number of naive approaches. They might be appropriate solutions, but will generally not scale well. The commonality that all of these approaches share is that changing the priority/rank of a single Todo item affects all other todo items before it can be…