11 ms·
They can be bad for performance. It all depends on your access patterns. A common caching pattern is called "temporal locality" which means that theres a high l
by chacham15 3y ago
They can be bad for performance. It all depends on your access patterns. A common caching pattern is called "temporal locality" which means that theres a high likelihood that data created at the same time will be accessed at the same time. Therefore, if these pieces of information are on the same machine, they can be queried / returned much faster than if they were both on separate machines. This is doubly true if theres a data dependency between them. E.g. SELECT x + y or SELECT x WHERE y = 'foo'.
- stepanhruda 3y agoYes but if that machine with sequential data receives 100x the traffic of other machines, it can be worse than splitting this traffic evenly across all available machines.
- kijin 3y agoIf your database simply shards keys sequentially, it's going to get hotspots in a lot of use cases, like plain old integer keys and timestamps, not just UUIDv7. In that case it would be fair to say that your database is doing it wrong. Fortunately, there's no rule that says you should shard your keys using the sequential part up front. One of the rules for generating randomness from environmental sources is to throw away the high bits and only use the low bits. Distributed databases should do the same if they want a good distribution.
- johncolanduoni 3y agoWhat distributed databases shard on the low bits? How do they do something like a range query? The closest I’ve ever heard of is sharding based on a hash (e.g. CockroachDB can do this on request[1]) but most distributed databases with strong consistency (Spanner descendants in particular) default to “doing it wrong”. [1]: https://www.cockroachlabs.com/docs/stable/hash-sharded-indexes https://www.cockroachlabs.com/docs/stable/hash-sharded-index...
- stepanhruda 3y agoAs I understood it, a big part of the premise of the post was that they see sequential storage (either in db or cache layer) as desirable
- paulddraper 3y agoIt depends if you have a request covers a lot of sequential data, or if you have a lot of requests of sequential data.
- stepanhruda 3y agoCorrect, it speeds up latency in best case scenario, and falls over in worst case scenario. Randomly sharded keys give a more consistent performance.
- chacham15 3y agoYou're painting with way too broad of a brush. It is not always better and not always worse. E.g. "give me a list of users who made two posts where both posts were created within 1 second of each other" This query would likely blow up on a system which has all the data completely randomly sharded (because you'd have to aggregate all the data centrally, unless you had a complicated shuffle setup (which most dbs dont)) whereas would work fine on a system which has posts sharded by time.
- hinkley 3y agoIf you're working on a multi-user system, particularly one with hundreds of requests per second, there is no locality of ids. Two of my actions are separated by a sea of actions by other users.
- berkes 3y agoWe solved that with UUIDS and updated_at and created_at columns. The latter the default sort in all views and queries. So the btree/indexing issues were hardly an issue. Whenever you fetch a set of rows, they will be bounded by these timestamps. We even sharded on these columns, because of this (our business case made it so that hardly ever did people need data over multiple months) But we never encountered distribution issues. I don't think the locality issue will be solved, as postgres doesn't consider other columns when distributing data, only the primary key IIRC. I don't know why we never saw this, though.