This was a fun crazy idea that turned out not to be so good in practice. If you have a mysql table you are monitoring for changes, you can use a trigger to do so (as long as you have a semi-modern version of msyql).
Sometimes you might want to notify another service of any change (remote logging service, message queue, etc). For instance, at 8z, when the price of a listing changes, this is an interesting event that other software should be notified of.
The first step is to install the mysql http UDF (all commands below are for centos).
$ sudo yum install curl-devel
$ CPPFLAGS="-I/usr/include/mysql" ./configure --with-mysql=/etc/my.cnf --enable-shared
This gives us a .la file (built with libtool) but luckily there’s a .so file hiding:
sudo cp .libs/mysql-udf-http.so.0.0.0 /usr/lib64/mysql/plugin/mysql-udf-http.so
Then, your remote rest service will probably want JSON, so you’ll want to visit the mysql udf hub. There lives a JSON function. I just grabbed the C file from github and compiled it:
gcc -fPIC -shared -o lib_mysqludf_json.so -I/usr/include/mysql/ lib_mysqludf_json.c
After these are compiled you have to copy the .so files to the appropriate directory and then add functions as a privileged user, per usual UDF convention.
Now you can create a trigger that calls these functions.
delimiter $ CREATE TRIGGER upd_check BEFORE UPDATE ON account FOR EACH ROW BEGIN IF NEW.amount > 0 THEN set @json = select json_object(account_id,amount) select http_post('http://restservice.example.com/account/post',@json); END IF; END;$ delimiter;
Note that I didn’t actually implement this, so the code up above is based on my memory. If you try this and have some corrections, please leave them in the comments and I’ll update this post.
Why didn’t I actually implement this, after doing the better part of a day’s worth of research? The database is not the best place for this kind of logic. Error handling for triggers is weak–if the trigger failed for any reason (like the remote service was down), you would need to build an error logging system, or send an email. Also, if there are a number of updated rows, which all trigger outbound http calls, you might run into performance issues which would be difficult to replicate or analyze, and, most importantly, might impact your database’s ability to act as a database. The three tier architecture exists for a reason.
But, it was fun to investigate, and, as my colleague said, would have been cool if it had worked. If you are still interested, there’s more on this topic here.
Update Jan 2021: The HTTP UDF is now here and a reader pointed out they had trouble with the above instructions, but that a golang UDF implementation worked fine.