Search code examples
javasql-serverspringspring-batchjtds

Throttling(reducing) the database insert speed in spring application


I am working on a spring batch application where I am migrating millions or records from source db to destination db. While inserting the records into destination database, I am doing it in bulk inserts (1000 in each batch) and this way it inserts 3000 approx in a second.

destination db type - MS SQL server 2012, JDBC driver - JTDS

Now, I have a requirement where I should be able to reduce(not improve) the performance of the migration rate say 1000 records in 1 sec instead of inserting 3000. Is there a straight forward way to do this either using JDBC driver or any other configuration in spring?

Thanks,


Solution

  • I would use Guava's RateLimiter.

    It's pretty simple. Instantiate it somewhere (probably as a Spring bean):

    double CALLS_PER_SECOND = 100;
    
    RateLimiter rateLimiter = RateLimiter.create(CALLS_PER_SECOND);
    

    Then use it in your loops:

    rateLimiter.acquire();
    

    You can't really do records/second, but you can do bytes/second or calls/second.