<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Jie Zhang on Medium]]></title>
        <description><![CDATA[Stories by Jie Zhang on Medium]]></description>
        <link>https://medium.com/@jayzhang0614061?source=rss-2bd763f0c1f4------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*M1Czel7wHAfpMnm0.jpg</url>
            <title>Stories by Jie Zhang on Medium</title>
            <link>https://medium.com/@jayzhang0614061?source=rss-2bd763f0c1f4------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 23 Nov 2026 00:53:32 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@jayzhang0614061/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Hotspot Issue in Google Cloud Data Storage System]]></title>
            <link>https://medium.com/@jayzhang0614061/hotspot-issue-in-google-cloud-data-storage-system-20b6cd5496be?source=rss-2bd763f0c1f4------2</link>
            <guid isPermaLink="false">https://medium.com/p/20b6cd5496be</guid>
            <category><![CDATA[firestore]]></category>
            <category><![CDATA[cloud-storage]]></category>
            <category><![CDATA[hotspots]]></category>
            <category><![CDATA[spanner]]></category>
            <category><![CDATA[big-table]]></category>
            <dc:creator><![CDATA[Jie Zhang]]></dc:creator>
            <pubDate>Tue, 21 Sep 2021 21:03:08 GMT</pubDate>
            <atom:updated>2021-09-21T21:03:08.583Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nuv-bGL9RIU_0_1HbRwY5g.png" /></figure><p>Hotspot issue happens in indexed, horizontally scalable, distributed data storage systems. In GCP, there are quite a few services that can suffer from this issue. In the article, I will discuss why it happened and how to avoid it when we design a database/storage system.</p><h3>1, Cloud Spanner</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*isU4cy6Uq5tk0uq-TwbY2g.png" /></figure><p>Cloud Spanner is a relational, horizontally scalable, distributed database. It divides data among servers by key range. Monotonically increasing primary key can cause read and write operations to happen in a few servers at once instead of evenly distributed.</p><p>According to the <a href="https://cloud.google.com/spanner/docs/schema-design#primary-key-prevent-hotspots">document from Google</a>, if you using timestamps at the first part of your key, it definitely creating a hotspot issue.</p><p>To prevent it happens, you could follow the below best practice recommended by Google.</p><p><strong>1,</strong><a href="https://cloud.google.com/spanner/docs/schema-design#fix_swap_key_order"><strong> Swap the order of keys</strong></a></p><p>Simply, if we move the monotonically increasing or decreasing value out of the first part of the key, the insert will be randomly distributed. For example, if we use UserId as the first part of the key and timestamp attached as the second part of the key, it will solve the problem.</p><p><strong>2, </strong><a href="https://cloud.google.com/spanner/docs/schema-design#fix_hash_the_key"><strong>Hash the unique key and spread the writes across logical shards</strong></a></p><p>Obviously, if you hash the actual unique key, it generates some random value and it for sure will be evenly distributed. For example, you can create a column using the below formula:</p><blockquote>ShardId = hash(LastAccess and UserId) % N</blockquote><p>where N is the logical shards you would like to create.</p><p><strong>3, </strong><a href="https://cloud.google.com/spanner/docs/schema-design#uuid_primary_key"><strong>Use a Universally Unique Identifier (UUID)</strong></a></p><p>Another simple way to avoid hotspots is just to use uuid as your primary key. However, this method has some drawbacks:</p><ul><li>They are slightly large, using 16 bytes or more. Other options for primary keys don’t use this much storage.</li><li>They carry no information about the record. For example, a primary key of SingerId and AlbumId has an inherent meaning, while a UUID does not.</li><li>You lose locality between records that are related, which is why using a UUID eliminates hotspots.</li></ul><p><strong>4, </strong><a href="https://cloud.google.com/spanner/docs/schema-design#bit_reverse_primary_key"><strong>Bit-reverse sequential values</strong></a></p><p>Reversing the bits maintains unique values across the primary keys and you can easily recalculate the original value in your application code.</p><h3><strong>2, Cloud BigTable</strong></h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*A7Gl8CUxXFhTfAVNtYVwpA.png" /></figure><p>Not surprisingly, BigTable is on this list. Using row-key as the only index and enable horizontally scale makes BigTable has the hotspot issue.</p><p><a href="https://cloud.google.com/bigtable/docs/schema-design?hl=en#row-keys">The general guidance for designing row keys</a> is that you need to consider the performance of retrieving the data. To have the best performance out of Bigtable, <strong>Well-designed row keys should consider the below factors,</strong> which, at some level, may causing the hotspot issue.</p><p><strong>1, Design your row key based on the queries you will use to retrieve the data.</strong></p><p>Yes, think about what query will be called most frequently and what you really need BigTable to do for you.</p><p><strong>2, Keep your row keys short.</strong></p><p>A row key must be 4 KB or less. Saving some money on space is a good idea.</p><p><strong>3, Store multiple delimited values in each row key.</strong></p><p>Well-planned row key prefixes let you take advantage of Bigtable’s built-in sorting order to store related data in contiguous rows. Storing related data in contiguous rows lets you access related data as a range of rows, rather than running inefficient table scans.</p><p><strong>4, Use human-readable string values in your row keys</strong></p><p>This is for the <a href="https://cloud.google.com/bigtable/docs/keyvis-overview">Key Visualizer tool</a> to troubleshoot issues.</p><p><strong>5, Start with a common value and end with a granular value</strong></p><p>Like Region-Country-State format. AMER#US#NY</p><p>The best practices of row-key design recommended by google may cause hotspot issues because it focuses on the best performance. If we take the best practice to avoid hotspots guidance as the sole rule to design row-key for BigTable, it may worsen the performance and make BigTable useless. So there is no single best rule for it, we need to think about it using human intelligence.</p><p><strong>Below are some antipatterns we should avoid when design row-key.</strong></p><p><strong>1, Row keys that start with a timestamp.</strong></p><p>I mentioned this in Cloud Spanner.</p><p><strong>2, Row keys that cause the related data to not be grouped together.</strong></p><p>Yes, we wanna them to be together for the best performance.</p><p><strong>3, Sequential numeric IDs</strong></p><p>This is like monotonically increasing or decreasing numbers which can cause hotspots</p><p><strong>4, Frequently updated identifiers</strong></p><p>This is inefficient and can also cause a row to exceed its size limit. For example, if you store memory-usage data for a number of devices once per second, <em>do not</em> use a single row key for each device that is made up of the device ID and the metric being stored, such as 4c410523#memusage, and update the row repeatedly.</p><p>Instead, store each new reading in a new row. Using the memory usage example, each row key can contain the device ID, the type of metric, and a timestamp, so the row keys are similar to 4c410523#memusage#1423523569918. This strategy is efficient because in Bigtable, creating a new row takes no more time than creating a new cell. In addition, this strategy enables you to quickly read data from a specific date range by calculating the appropriate start and end keys.</p><p><strong>5, Hashed values.</strong></p><p>Yup, a little surprised, in Cloud Spanner, we said we could use Hashed Value to avoid hotspots. But BigTable is NoSQL database, if we using hashed value, it removes our ability to take advantage of Bigtable’s natural sorting order, making it impossible to store rows in a way that are optimal for querying.</p><p><strong>6, Values expressed as raw bytes</strong></p><p>This made us difficult for troubleshooting and reduced readability.</p><p>Ok, we now know what we should do and what we should not do, what will be the best of best practices. Google summarized some special cases and if you do encounter those cases, you should follow the below guidance.</p><p><strong>1, </strong><a href="https://cloud.google.com/bigtable/docs/schema-design?hl=en#domain-names"><strong>Using Reverse Domain Name</strong></a></p><p>If you’re storing data about entities that can be represented as domain names, consider using a reverse domain name (for example, com.company.product) as the row key. Using a reverse domain name is an especially good idea if each row&#39;s data tends to overlap with adjacent rows. In this case, Bigtable can compress your data more efficiently.</p><p>If you expect to store a lot of data for only <em>one or a small number of domain names</em>, consider other values for your row key.</p><p><a href="https://cloud.google.com/bigtable/docs/schema-design?hl=en#multi-tenancy"><strong>2, Multi-tenancy</strong></a></p><p>Row key prefixes provide a scalable solution for a “multi-tenancy” use case, a scenario in which you store similar data, using the same data model, on behalf of multiple clients. <strong>Using one table for all tenants is the most efficient way to store and access multi-tenant data.</strong></p><p>In contrast, if you store data on behalf of each company in its own table, you can experience performance and scalability issues. You are also more likely to inadvertently reach Bigtable’s limit of 1,000 tables per instance. After an instance reaches this limit, Bigtable prevents you from creating more tables in the instance.</p><p><strong>3, String Identifiers</strong></p><p>CustomerID or SensorID are good candidates for row-key. <a href="https://cloud.google.com/bigtable/docs/schema-design?hl=en#time-based"><strong>Include a timestamp as part of your row key</strong></a> if you often need to retrieve data based on the time when it was recorded. This is called Field Promotion.</p><p><strong>Don’t use a timestamp by itself or at the beginning of a row key</strong>, because this will cause sequential writes to be pushed onto a single node, creating a hotspot.</p><p>If you usually retrieve the most recent records first, you can use a <strong>reversed timestamp</strong> in the row key by subtracting the timestamp from your programming language’s maximum value for long integers.</p><p><strong>4, Salting.</strong></p><p>This is the process of adding a derived value to the key to make writes noncontiguous. For example, you can use a hash of a timestamp divided by the number of nodes.</p><h3>3, Cloud Firestore</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/860/1*h75OR0gQGEB4_OFWQl8fPw.png" /></figure><p>Firestore is a fully indexed and horizontally scalable document NoSQL database. It supposes to have the hotspot issue and it does, but not that notorious as Cloud spanner or BigTable as Firestore in Datastore mode provides <a href="https://cloud.google.com/datastore/docs/concepts/indexes#built_in_indexes">built-in</a> indexes automatically.</p><p>Avoid high read or write rates to lexicographically close documents, or your application will experience contention errors if it does any of the following:</p><ul><li>Creates new entities at a very high rate and allocates its own monotonically increasing IDs.</li><li>Datastore mode allocates keys using a scatter algorithm. You should not encounter hotspotting on writes if you create new entities using automatic entity ID allocation.</li><li>Creates new entities at a very high rate using the legacy sequential ID allocation policy.</li><li>Creates new entities at a high rate for a kind with few entities.</li><li>Creates new entities with an indexed and monotonically increasing property value, like a timestamp, at a very high rate.</li><li>Deletes entities from a kind at a high rate.</li><li>Writes to the database at a very high rate without gradually increasing traffic.</li></ul><p>Fortunately, Firestore in Datastore mode will eventually split the key space to support high load.</p><p>In some cases, a hotspot can have wider impact to an application than preventing reads or writes to a small range of keys. For example, the hot keys might be read or written during instance startup, causing loading requests to fail.</p><p>To prevent the issue, you can:</p><p>1, Prepend a random hash to ensure that the keys are sharded onto multiple tablets.</p><p>2, If you need to query on a monotonically increasing (or decreasing) property using a sort or filter, you could instead index on a new property, for which you prefix the monotonic value with a value that has high cardinality across the dataset, but is common to all the entities in the scope of the query you want to perform. For instance, if you want to query for entries by timestamp but only need to return results for a single user at a time, you could prefix the timestamp with the user id and index that new property instead. This would still permit queries and ordered results for that user, but the presence of the user id would ensure the index itself is well sharded.</p><h3>4, Cloud Storage</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pxVX6vfbTP7OuMnV1NKmjA.png" /></figure><p>Cloud storage requires a unique namespace to organizing Objects, so if you sequential names or timestamps when you uploading files in parallel, you can have a hotspot issue.</p><p>Therefore, avoid using sequential names or timestamps for your files.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=20b6cd5496be" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Google BigQuery: Partitioning vs Clustering]]></title>
            <link>https://medium.com/@jayzhang0614061/google-bigquery-partitioning-vs-clustering-ef46ca135427?source=rss-2bd763f0c1f4------2</link>
            <guid isPermaLink="false">https://medium.com/p/ef46ca135427</guid>
            <category><![CDATA[bigquery]]></category>
            <category><![CDATA[google-cloud-platform]]></category>
            <dc:creator><![CDATA[Jie Zhang]]></dc:creator>
            <pubDate>Fri, 13 Aug 2021 03:31:45 GMT</pubDate>
            <atom:updated>2021-08-13T03:38:38.362Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/500/1*aFHgqMKV3Ftlh7_yGBQYyA.png" /></figure><p>When I study Google cloud BigQuery, there are two important concepts, partitioning, and clustering. According to GCS document, it states:</p><p><em>Prefer clustering over partitioning under the following circumstances:</em></p><ul><li><em>Partitioning results in a small amount of data per partition (approximately less than 1 GB).</em></li><li><em>Partitioning results in a large number of partitions beyond the </em><a href="https://cloud.google.com/bigquery/quotas#partitioned_tables"><em>limits on partitioned tables</em></a><em>.</em></li><li><em>Partitioning results in your mutation operations modifying the majority of partitions in the table frequently (for example, every few minutes).</em></li></ul><p>But what does this means? This article is trying to explain this using real-life examples.</p><p>The concept of partition and cluster itself is contradictory. Partition is dividing and clustering is gathering. So how these two strategies can apple simultaneously?</p><p>The answer is, you can <a href="https://cloud.google.com/bigquery/docs/clustered-tables#clustering_partitioned_tables">clustering partitioned tables</a>, which means, you can divide your data into different places and then put them together in some ways. Let me give an example.</p><p>I am living in a rental building and we have 10 floors and each floor has 50 units. The concierge receives hundreds of packages every day. When they receive the resident’s package, they need to store it in an efficient way and then send us an email/text to ask residents to pickup. When we get to the lobby, the concierge needs to search for our package and give it back to us.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*u6KQ_DybCfePws2KTSBwnQ.jpeg" /></figure><p>Efficient is the keyword here. How can s/he store and find it in an efficient way? The solution is that they have 3 giant boxes behind frontdesk, and each box takes 3-floor packages, the first box is the package for floors 2 to 4 (the first floor is lobby and retail), the second box is floor 5 to 7 and the third one is 8 to 10. When packages come, the concierge drops the package into the designated box first. In each box, there are 3 segments, unit numbers 1 to 16 in the first segment, 17 to 32 in the second segment, and 33 to 50 in the third segment. So let’s say you are unit 547, I can quickly tell it should be in the second box and third segment. Most likely there are only one or two packages in that segment and the concierge picks it up and passes it to you.</p><p>I think you get my idea now. The three boxes are partitions, and segments in each box are clustering. All we try to do is for an “efficient” search. Why don’t we have 10 boxes? Why don’t we have 50 segments? Well, like I said, by estimating the volume of packages, when we do 3 partitions and 3 clusterings, we left very limited results in the search target, that’s what I called, efficient. Not too many resources are wasted and fast enough to get the answer.</p><p>This is the idea behind BigQuery’s concept of partitioning and clustering. Now let us re-visit the statement.</p><ul><li><em>Partitioning results in a small amount of data per partition (approximately less than 1 GB).</em></li></ul><p>This means that if we have only one or two packages in each box, we probably don’t need 3 boxes, just keep one box and have some segments in the box should be fast enough.</p><ul><li><em>Partitioning results in a large number of partitions beyond the </em><a href="https://cloud.google.com/bigquery/quotas#partitioned_tables"><em>limits on partitioned tables</em></a><em>.</em></li></ul><p>This means that each box has too many packages, let’s say the box can only contain 50 packages but you have 100 packages which makes the box overflow. So either re-design the partition sets so each partition can hold below maximum tables or just forget about partition because you have too many tables. In the above example is that you should have more boxes or just put them on the floor in more segments. There is a limitation of the partition tables, unfortunately.</p><ul><li><em>Partitioning results in your mutation operations modifying the majority of partitions in the table frequently (for example, every few minutes).</em></li></ul><p>This means that every time you search a package, you only get the unit number, let’s say unit X47, you don’t exactly know which floor, you have to search each box. And that happens a lot. Under this situation, you’d better not using a partition.</p><p>Hope this article helps.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ef46ca135427" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>