Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

Will prefixing a random String (like a UUID4) cause hotspotting as the Primary Key in Spanner?

In Spanner, if I used a randomly generated String which contained a prefix as the only Primary Key, would this cause hotspotting? Does the length of the prefix play a role?

examples:
prefix-b4d0ab96-82ad-410f-a773-cd54e1eacfbe
prefix-901043f7-5ad6-422a-a632-080d1c0150e6
etc...

1 1 171
1 REPLY 1

Hi @nicolaev,

Welcome to Google Cloud Community!

To answer your question, yes, using a randomly generated string with a constraint or low-variation prefix as the only primary key in Cloud Spanner can still cause hotspotting. This happens even if the suffix is random, because Spanner organizes data by sorting keys. When many keys begin with the same value, they are stored close together, which can focus read and write operations on just a few nodes, leading to performance bottlenecks. To avoid hotspotting, you can try to:

  • Place the random or high-cardinality part of the key first.
  • Or, add a hash prefix or bucket number to the beginning of the key to ensure better data distribution.This approach helps Spanner distribute load more evenly, improving performance and scalability.

You may also refer to this document for best practices on choosing primary keys for Spanner.

 Was this helpful? If so, please accept this answer as “Solution”. If you need additional assistance, reply here within 2 business days and I’ll be happy to help.