PHP Gearman too much mysql connections -


i'm using gearman in custom joomla application , using gearman ui track active workers , jobs number.

i'm facing issue mysql load , number of connections, i'm unable track issue, i've few questions might me.

1- gearman workers launch new database connection each job or share same connection?

2- if gearman launches new connection everytime job runs how can change make jobs share same connection?

3- how can balance load between more 1 server?

4- there "pay-as-you-go" package mysql hosting? if yes, please mention them.

thanks lot!

this overlooked issue when using kind of job queue workers. 100 workers open separate database connection each (they separate php processes). if mysql configured allow 50 connections, workers start failing. answer questions:

1) each worker runs inside 1 php process each, , process open 1 database connection. workers not share database connections.

2) if 1 worker processing jobs, 1 database connection opened. if have 50 workers running, expect 50 database connections. since these not web requests, persistent connections not work, sharing not work.

3) can balance load adding read slaves, , using mysql proxy distribute load.

4) i've never seen pay-as-you-go mysql hosting solution. ask provider increase number of connections. if won't, might time run own server.

also, gearman server process use 1 database connection maintain queue (if have enabled mysql storage).

strategies can use try , make worker code play nicely database:

  1. after each job, terminate worker , start again. don't open new database connection until new job received. use supervisor keep workers running time.

  2. close database connections after every query. if see lot of connections open in 'sleep' state, clean them , keep database connections low. try $pdo = null; after each query (if use pdo).

  3. cache used queries result doesn't change, keep database connections low.

  4. ensure tables indexed queries run fast possible.

  5. ensure database exceptions caught in try/catch block. add retry logic (while loop), worker fail gracefully after say, 10 attempts. make sure job put on queue after failure.


Comments

Popular posts from this blog

php - How to display all orders for a single product showing the most recent first? Woocommerce -

asp.net - How to correctly use QUERY_STRING in ISAPI rewrite? -

angularjs - How restrict admin panel using in backend laravel and admin panel on angular? -