DEV Community

Cover image for Create 1 Million Fake Users in Laravel Fast in Under 5 Minutes
Don Cadavona
Don Cadavona

Posted on • Edited on

Create 1 Million Fake Users in Laravel Fast in Under 5 Minutes

Laravel has a neat way of generating fake data to your database for development and testing purposes.

Using Laravel Seeding, you can quickly seed a hundred records to your users table. That's done in less than a second, no problem.



User::factory(100)->create();


Enter fullscreen mode Exit fullscreen mode

How about a thousand? Okay, that's done in around 4 seconds:



User::factory(1000)->create();


Enter fullscreen mode Exit fullscreen mode

How about a hundred-thousand? That takes around 6 minutes, and starts hurting your server:



User::factory(100000)->create();


Enter fullscreen mode Exit fullscreen mode

Now how about a million? Don't even bother, your server will freeze to death:



User::factory(1000000)->create();


Enter fullscreen mode Exit fullscreen mode

So, how can we actually seed fake 1-million users to our database? We use chunking and database transactions.

Here is our OneMillionUsersSeeder class generated via php artisan make:seeder OneMillionUsersSeeder:



<?php

namespace Database\Seeders;

use App\Models\User;
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;

class OneMillionUsersSeeder extends Seeder
{
    /**
     * The number of fake users to create.
     */
    protected static $users_count = 1000000;

    /**
     * The number of fake users to chunk
     * based on the users count.
     */
    protected static $chunk_size = 1000;

    /**
     * Create 1-million users quickly (~5 minutes)
     * in non-production environments such as
     * local and staging environments.
     * 
     * To do this, we implement chunking and database transactions
     * to speed up the process significantly and reliably.
     */
    public function run(): void
    {
        if (app()->environment() == 'production') {
            return;
        }

        for ($i = 0; $i < self::$users_count / self::$chunk_size; $i++) {
            DB::beginTransaction();
            try {
                User::factory()
                    ->count(self::$chunk_size)
                    ->create();

                DB::commit();
            } catch (Exception $e) {
                DB::rollback();
            }
        }
    }
}


Enter fullscreen mode Exit fullscreen mode

Here it is run by the cheapest 5USD VM in Linode:

Image description

1-million fake users created in 332,443 ms (5 minutes).

How about a billion? We shall see.

You may see the demo Laravel source code here.

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay