<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Abhishek Sood</title>
    <description>The latest articles on DEV Community by Abhishek Sood (@itsabhisheksood).</description>
    <link>https://dev.to/itsabhisheksood</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F545956%2F7ee424e1-763b-4738-bc00-b3dae0769442.jpg</url>
      <title>DEV Community: Abhishek Sood</title>
      <link>https://dev.to/itsabhisheksood</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/itsabhisheksood"/>
    <language>en</language>
    <item>
      <title>How I Eliminated 90% of My Database Queries Using CRC32 as a Deterministic Random Seed (And You Can Too!)</title>
      <dc:creator>Abhishek Sood</dc:creator>
      <pubDate>Tue, 10 Jun 2025 10:43:23 +0000</pubDate>
      <link>https://dev.to/itsabhisheksood/how-i-eliminated-90-of-my-database-queries-using-crc32-as-a-deterministic-random-seed-and-you-can-lgp</link>
      <guid>https://dev.to/itsabhisheksood/how-i-eliminated-90-of-my-database-queries-using-crc32-as-a-deterministic-random-seed-and-you-can-lgp</guid>
      <description>&lt;p&gt;The Problem That Was Keeping Me Up at Night&lt;/p&gt;

&lt;p&gt;Picture this: I'm building a multi-tenant SaaS where each tenant needs unique designs, images, and content variations across thousands of city pages. My initial approach? Store every single assignment in the database.&lt;/p&gt;

&lt;p&gt;The nightmare stats:&lt;/p&gt;

&lt;p&gt;50 tenants × 10,000 cities × 5 design elements = 2.5 million database rows&lt;/p&gt;

&lt;p&gt;Query times getting slower by the day&lt;/p&gt;

&lt;p&gt;Storage costs climbing faster than my coffee consumption&lt;br&gt;
Manual assignment work that would take months&lt;/p&gt;

&lt;p&gt;There had to be a better way. And then it hit me like a perfectly timed console.log() - what if randomness didn't have to be... random?&lt;/p&gt;

&lt;p&gt;The "Aha!" Moment: Deterministic Randomization&lt;/p&gt;

&lt;p&gt;The breakthrough came when I realized that I don't need true randomness - I need consistent pseudo-randomness.&lt;/p&gt;

&lt;p&gt;Here's the magic: crc32() always returns the same integer for the same input string. Always.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;echo crc32('tenant-a.com'); // Always outputs: 3055645744&lt;br&gt;
echo crc32('tenant-b.com'); // Always outputs: 2847533407&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Different inputs = different "random" seeds. Same input = same seed every time.&lt;/p&gt;

&lt;h2&gt;
  
  
  Implementation: From Chaos to Consistency
&lt;/h2&gt;

&lt;p&gt;Step 1: Generate Domain-Based Seeds&lt;/p&gt;

&lt;p&gt;`class TenantManager {&lt;br&gt;
    public function createTenant($tenantData) {&lt;br&gt;
        // Generate consistent seed from domain&lt;br&gt;
        $domainSeed = crc32($tenantData['domain']);&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    // Use seed for all random assignments
    mt_srand($domainSeed);

    // Now every "random" choice is deterministic!
    $heroDesign = $heroVariations[array_rand($heroVariations)];
    $colorScheme = $colorSchemes[array_rand($colorSchemes)];

    return $tenant;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;}`&lt;/p&gt;

&lt;p&gt;Step 2: Layered Seeding for Complex Scenarios&lt;/p&gt;

&lt;p&gt;For city-specific content, I create layered seeds:&lt;/p&gt;

&lt;p&gt;`private function generateCityAssignment($tenant, $citySlug) {&lt;br&gt;
    // Extract city components&lt;br&gt;
    $parts = explode('-', $citySlug); // 'miami-fl' -&amp;gt; ['miami', 'fl']&lt;br&gt;
    $cityName = $parts[0];&lt;br&gt;
    $stateCode = strtoupper($parts[1]);&lt;br&gt;
    $firstLetter = $cityName[0];&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Create unique but consistent seed
$algorithmSeed = $tenant['domain_seed'] + 
                crc32($stateCode) +     // State variation
                crc32($firstLetter) +   // Alphabetic distribution
                crc32($cityName);       // City-specific touch

mt_srand($algorithmSeed);

// Same tenant + same city = same templates, always
// Different cities = different templates
return $this-&amp;gt;selectRandomTemplates();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;}`&lt;/p&gt;

&lt;p&gt;Step 3: Section-Specific Assignments&lt;/p&gt;

&lt;p&gt;Even individual page sections get their own deterministic randomness:&lt;/p&gt;

&lt;p&gt;`class SectionDesignManager {&lt;br&gt;
    public function generateSectionDesignAssignments($domainSeed) {&lt;br&gt;
        $assignments = [];&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    foreach ($this-&amp;gt;sectionTypes as $sectionType =&amp;gt; $config) {
        // Section-specific seed prevents correlation
        $sectionSeed = $domainSeed + crc32($sectionType);
        mt_srand($sectionSeed);

        $variation = mt_rand(1, $config['variations']);
        $assignments[$sectionType] = $variation;
    }

    return $assignments;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;}`&lt;/p&gt;

&lt;h2&gt;
  
  
  Real-World Results That Made Me Do a Happy Dance
&lt;/h2&gt;

&lt;p&gt;Before vs After Metrics:&lt;br&gt;
Database Queries:&lt;/p&gt;

&lt;p&gt;Before: ~50 queries per page load&lt;br&gt;
After: ~5 queries per page load&lt;br&gt;
Reduction: 90%&lt;/p&gt;

&lt;p&gt;Storage Requirements:&lt;br&gt;
Before: 2.5M assignment records&lt;br&gt;
After: ~500 tenant records&lt;br&gt;
Reduction: 99.98%&lt;/p&gt;

&lt;p&gt;Page Load Time:&lt;br&gt;
Before: 800ms average&lt;br&gt;
After: 120ms average&lt;br&gt;
Improvement: 85%&lt;/p&gt;

&lt;p&gt;Development Time:&lt;br&gt;
Before: Manual assignment = weeks of work&lt;br&gt;
After: Algorithmic assignment = zero maintenance&lt;br&gt;
Time Saved: Infinite&lt;/p&gt;

&lt;p&gt;The Beauty of Consistent Inconsistency&lt;/p&gt;

&lt;p&gt;Here's what blew my mind: each tenant gets a completely unique experience, but it's 100% reproducible:&lt;/p&gt;

&lt;p&gt;`// Tenant A visiting Miami always sees:&lt;br&gt;
// - Hero Design #3&lt;br&gt;
// - Blue color scheme images&lt;br&gt;&lt;br&gt;
// - Template set #127&lt;br&gt;
// - "Expert Miami Plumbers" spintax variation&lt;/p&gt;

&lt;p&gt;// Tenant B visiting Miami always sees:&lt;br&gt;
// - Hero Design #1&lt;br&gt;&lt;br&gt;
// - Red color scheme images&lt;br&gt;
// - Template set #203&lt;br&gt;
// - "Professional Miami Plumbing" spintax variation&lt;/p&gt;

&lt;p&gt;// But Tenant A visiting Tampa gets completely different content!`&lt;/p&gt;

&lt;h2&gt;
  
  
  Advanced Techniques I Discovered
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Spintax Consistency
Even dynamic text spinning uses seeded randomization:&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;`private function processSpintax($content, $seed) {&lt;br&gt;
    mt_srand($seed);&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;return preg_replace_callback('/\{([^}]+)\}/', function($matches) {
    $options = explode('|', $matches[1]);
    return $options[array_rand($options)]; // Deterministic choice!
}, $content);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;}`&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Image Assignment Algorithm&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;`public function getAlgorithmicImageAssignments($niche, $citySlug, $domainSeed) {&lt;br&gt;
    // Multi-layered seed for image variety&lt;br&gt;
    $imageSeed = $domainSeed + crc32($citySlug) + crc32($niche);&lt;br&gt;
    mt_srand($imageSeed);&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$assignments = [];
foreach ($this-&amp;gt;imageCategories as $category =&amp;gt; $images) {
    $assignments[$category] = $images[array_rand($images)];
}

return $assignments;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;}`&lt;/p&gt;

&lt;h2&gt;
  
  
  Why This Approach is a Game-Changer
&lt;/h2&gt;

&lt;p&gt;✅ Infinite Scalability: Add 100,000 cities? Zero additional storage.&lt;br&gt;
✅ Perfect Consistency: Same input always produces same output.&lt;br&gt;
✅ Zero Maintenance: No assignments to manage or update.&lt;br&gt;
✅ Lightning Fast: No database lookups for assignments.&lt;br&gt;
✅ Truly Random Distribution: CRC32 provides excellent distribution.&lt;br&gt;
✅ Debugging Friendly: Reproducible results make testing easy.&lt;/p&gt;

&lt;p&gt;Potential Gotchas (Learned the Hard Way)&lt;/p&gt;

&lt;p&gt;Seed Overflow: Use mt_srand((int)$seed) to handle large CRC32 values&lt;br&gt;
Correlation Risk: Add unique offsets (crc32($sectionType)) to prevent patterns&lt;/p&gt;

&lt;p&gt;Distribution Quality: CRC32 is good, but test your specific use case&lt;br&gt;
Backwards Compatibility: Store the seed method for future algorithm changes&lt;/p&gt;

&lt;p&gt;The Code That Changed Everything&lt;br&gt;
Here's the core pattern I use everywhere now:&lt;/p&gt;

&lt;p&gt;`class DeterministicRandomizer {&lt;br&gt;
    public static function getSeed(...$inputs) {&lt;br&gt;
        return array_reduce($inputs, function($carry, $input) {&lt;br&gt;
            return $carry + crc32((string)$input);&lt;br&gt;
        }, 0);&lt;br&gt;
    }&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public static function selectWithSeed(array $options, ...$seedInputs) {
    $seed = self::getSeed(...$seedInputs);
    mt_srand($seed);
    return $options[array_rand($options)];
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;}&lt;/p&gt;

&lt;p&gt;// Usage examples:&lt;br&gt;
$heroDesign = DeterministicRandomizer::selectWithSeed(&lt;br&gt;
    $heroDesigns, &lt;br&gt;
    $tenant['domain'], &lt;br&gt;
    'hero_section'&lt;br&gt;
);&lt;/p&gt;

&lt;p&gt;$cityTemplate = DeterministicRandomizer::selectWithSeed(&lt;br&gt;
    $templates, &lt;br&gt;
    $tenant['domain'], &lt;br&gt;
    $citySlug, &lt;br&gt;
    'city_page'&lt;br&gt;
);`&lt;/p&gt;

&lt;h2&gt;
  
  
  What I'd Do Differently Next Time
&lt;/h2&gt;

&lt;p&gt;Abstract the pattern earlier - I rebuilt this logic 3 times before creating a reusable class&lt;/p&gt;

&lt;p&gt;Document the seeds - Keep track of what contributes to each seed for debugging&lt;/p&gt;

&lt;p&gt;Add seed versioning - Plan for algorithm changes from day one&lt;br&gt;
Test distribution - Verify your CRC32 seeds actually distribute well for your data&lt;/p&gt;

&lt;h3&gt;
  
  
  The Bottom Line
&lt;/h3&gt;

&lt;p&gt;Sometimes the most elegant solutions hide in plain sight. CRC32 isn't just for checksums - it's a perfect deterministic randomizer that can eliminate massive amounts of database complexity.&lt;/p&gt;

&lt;p&gt;The best part? This pattern works for any scenario where you need consistent pseudo-randomness:&lt;/p&gt;

&lt;p&gt;A/B test assignments&lt;br&gt;
Feature rollouts&lt;br&gt;
Content personalization&lt;br&gt;
Load balancing&lt;br&gt;
Cache key distribution&lt;/p&gt;

&lt;p&gt;What's your most creative use of "boring" built-in functions? Have you found similar hidden gems that solved big problems?&lt;br&gt;
Drop a comment below - I'd love to hear your stories and see if we can inspire more creative problem-solving in the dev community!&lt;/p&gt;

&lt;p&gt;P.S. If this saved you some database headaches, give it a ❤️ and share it with other devs who might benefit. Let's spread the word about thinking outside the conventional database box!&lt;/p&gt;

&lt;p&gt;Tags: #php #database #optimization #algorithms #saas #multitenant #performance #webdev #backend #architecture&lt;/p&gt;

</description>
      <category>php</category>
      <category>database</category>
      <category>productivity</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Scrapping Google Map Data With Puppeteer</title>
      <dc:creator>Abhishek Sood</dc:creator>
      <pubDate>Wed, 23 Dec 2020 15:18:45 +0000</pubDate>
      <link>https://dev.to/itsabhisheksood/scrapping-google-map-data-with-puppeteer-2i29</link>
      <guid>https://dev.to/itsabhisheksood/scrapping-google-map-data-with-puppeteer-2i29</guid>
      <description>&lt;p&gt;Scrapping Google map data and reviews with Puppeteer and saving data in mySql.&lt;/p&gt;

&lt;p&gt;My First Puppeteer Project.&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/nlMGZnOvJ_E"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;-- &lt;a href="https://spidertechs.net/" rel="noopener noreferrer"&gt;Spider Techs&lt;/a&gt; &lt;/p&gt;

</description>
      <category>puppeteer</category>
      <category>scrapping</category>
      <category>node</category>
      <category>mysql</category>
    </item>
  </channel>
</rss>
