简体   繁体   中英

Horizontal scale up issue with Spring Data JPA Batch/bulk insert : PostgreSQL

I have implemented the Spring data JPA batch insert and it's working perfectly fine. Also I have seen very good improvement in performance. For this implementation I have used Spring, Spring Data JPA and PostgreSQL.

If I scale up my application horizontally(like have 2 node in the cluster).The application is not working because the application in each node have same logic to generate the value of primary key. It means, two node are generating the same primary key and trying to insert the records. As this violating primary key constraints, service is getting failed.

Is there any strategy to generate the primary key which will be unique multi-threaded environment(The application is deployed in multiple nodes)?

I have referred this My implementation is perfectly working fine when there is only one node in cluster.

I came to know that if the entities use GenerationType.IDENTITY identifier generator, Hibernate will silently disable batch inserts/updates. Also I have tried with "GenerationType.SEQUENCE" but have seen the same issue.On further research, I have seen some comments in stack-overflow question that since mysql/PostgreSQL doesn't support sequence

The main two algorithms that are worth to consider are HiLo which reserves a batch of ids on the db side and uses them one by one on the application side. Or just using UUIDs which are constructed in a way that they are unique without requiring any synchronization .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM