flamingo-commerce icon indicating copy to clipboard operation
flamingo-commerce copied to clipboard

cart: Cleanup cache after failed adapter call

Open jeinfeldt opened this issue 4 years ago • 0 comments

Hello friends, we noticed we sometimes have inconsistencies in our flamingo cache compared with the source of our adapters for the cart.

Let us look at the addProduct function:

...
func (cs *CartService) AddProduct(...) (productDomain.BasicProduct, error) {
	if deliveryCode == "" {
		deliveryCode = cs.defaultDeliveryCode
	}
       
	cart, behaviour, err := cs.cartReceiverService.GetCart(ctx, session)
	if err != nil {
		cs.logger.WithContext(ctx).WithField(flamingo.LogKeySubCategory, "AddProduct").Error(err)

		return nil, err
	}
	// cart cache must be updated - with the current value of cart
	var defers cartDomain.DeferEvents
	defer func() {
		cs.updateCartInCacheIfCacheIsEnabled(ctx, session, cart)

		cs.handleEmptyDelivery(ctx, session, cart, deliveryCode)
		cs.dispatchAllEvents(ctx, defers)
	}()
      ...
}

In case this function is successfull, it stores the new cart in the cache following the logic in the defer function.

Let us assume the getCart call fails, this results with an error return, but the cache is kept in whatever state it is until it is stale. For a get operation this is fine, but in case the cart is already changed by the adapter but fails nonetheless this results in an inconsistency between cache and adapter.

This behaviour can be observed across all application services of the cart module. I would suggest to enhance the behaviour in defer to check for the presence of an error and in case it is not nil throw a InvalidateCartEvent to reset the cart in the cache. Whenever a client now receives an error and fetches the cart, at least it is the same cart as provided by the port implementations.

This would affect various methods in the cart application package. I am unsure whether this is the right approach to tackle the problem, I am looking for a discussion on this issue.

jeinfeldt avatar Mar 03 '20 15:03 jeinfeldt