c# - Calling an external API whenever a field in the database is updated -


my applicationuser model contains property:

public bool subscribedtonewsletter { get;set; } 

i make sure whenever update value in database, external api called add or remove user list in email automation system, without manually calling method myself ensure synchronization regardless of programmer's intention.

is there built-in functionality provided in asp.net? or have extend usermanager class , centralize calls updating database?

calling external api keep in sync application data little more complicated making simple change in domain model.

if did this, call api before or after persist changes database? if before:

  1. how make sure change going accepted db?
  2. what if api call fails? refuse update db?
  3. what if api call succeeds application crashes before updating db or db connection temporarily lost?

if after:

  1. the api unavailable (e.g. outage). how make sure gets called later keep things in sync?
  2. the application crashes after updating db. how make sure api gets called when restarts?

there few different ways potentially solve this. however, bear in mind synchronising external system have lost acid semantics may used , application have deal eventual consistency.

a simple solution have database table acts queue of api calls made (it's important ordered time). when user's email updated, add row part of db transaction relevant details needed. ensures request call api recorded update.

then have separate process (or thread) polls table. use pg_notify support push notifications rather polling.

this process can read row (in order) call relevant api make change in external system. if succeeds, can remove row. if fails, can try again using exponential back-off. continued failures should logged investigation.

the worst case scenario have at-least-once delivery semantics updating system (e.g. if api call succeeded process crashed before removing row call made again when process restarted). if needed at-most-once, remove row before attempting make call.

this glossing on of details , need modified high through-put system should explain of principles.


Comments

Popular posts from this blog

python - Selenium remoteWebDriver (& SauceLabs) Firefox moseMoveTo action exception -

html - How to custom Bootstrap grid height? -

transpose - Maple isnt executing function but prints function term -