Why do sites perform poorly at peak season? During peak hours, lot of people would be placing orders and most often (due to discounts, promotions etc.), people would be buying the same product at the same time. So, what is the issue if people try to place an order on same product at the same time? In IT terms, if two threads are trying to access and concurrently update the same object at the same time, one can get into locking and transaction related issues. Impact of this would be that an attribute of an object may wrongly get update or may not get updated leaving it in an inconsistent state. In extreme cases, it can lead to deadlock and potentially bring down the servers and hence the site.
If we are using a sophisticated product such as ATG, handling concurrent updates is absolute necessary. Specifically, in ATG, there are few things we should keep in mind and force in the code while developing/implementation. Lets look at them:
– Use Simple cache modes for user, order etc. item descriptors, as these will not be generally accessed and changed from multiple servers. Avoid using locked caching modes for custom item descriptors. Also, use distributed modes for item descriptors, which are constantly accessed and changed through multiple servers.
– ClientLock Managers and ServerLock Manager are to be configured correctly (address and port). If their configurations are incorrect then the sites can behave in a crazy manner and also will be really difficult to debug.
– Scheduled Jobs running at a shorter interval or jobs, which are needed to run on a single instance at a time, should be using SingletonSchedulableService. This will make use of the ClientLockManager of a cluster to take and release local locks before and after the scheduled task. It will also release the lock even if the job fails, thus making sure that anything in not stuck at any place. This helps in two ways
- It will take care for not starting a job on the same instance if the first job is not completed.
- It will also not start the job on any other instance on the cluster if it is running on any one of the instance in the cluster
- Some of the jobs, which should be developed using this are: sending messages for order fulfillment or shipment, periodic report generation etc.
– Always use a Transaction Demarcation whenever doing a database operation. If items are updated without the demarcation it will also be costly on the performance of the system, as for every update system will automatically create a demarcation, begin a new transaction, update the database and finally commit the transaction and if an update fails the code will not be able to rollback the earlier updates. Avoid using TransactionDemarcation.RequiresNew at the begin of the transaction and always ends the TransactionDemarcation in the finally block.
– Always use correct steps to update an order. This is mandatory for any ATG application but sometime developers miss these for a particular scenario. PurchaseProcessFromHandler’s beforeSet and afterSet methods handles few steps out of the box, but if order update is done outside the scope of PurchaseProcessFormHandler all the steps need to be built and followed accordingly.