Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shared vs dedicated php library

I have a server which consists of several Zend Framework application.

I want to know if it is a good idea to upload Zend Library on the server and share it among all the applications instead of uploading it per application.

Does it influence speed if for example multiple applications request a library simultaneously or not.

What is its Pros and Cons?

Thx in advance.

like image 859
rahim asgari Avatar asked Dec 06 '25 20:12

rahim asgari


2 Answers

My answer applies in general to shared libraries, as this should not be specific to Zend Library:

PROS of sharing:

  • Less disk space usage
  • Possibly less memory usage (depends on many factors, including OS)
  • Update once, update all (you do not have to update the library for every single app

CONS of sharing:

  • If you need a particular version of the library for a certain application (for compatibility reasons for example), you cannot do it by sharing the library
  • Risk of breaking apps by updating to an incompatible library version.
like image 106
Bitgamma Avatar answered Dec 08 '25 11:12

Bitgamma


As others have pointed out, there are pros and cons. The big con is that every time you're going to upgrade your library code, you need to test every application, not just the one that needs the upgrade right now.

The big pro is that if you're using an opcode cache like APC (and you should be), you're wasting a fair bit of memory loading identical chunks of library code. Depending on the size of your opcode cache, and how much of the library code actually ever gets run, this could become an issue at some point. If the size of your opcode cache is not big enough to hold everything, you'll end up with a performance hit of some magnitude.

A middle-ground solution is to keep all your libraries some place on the server where they can be shared. Build your apps to use some configuration value for loading.

APP1: config.php

<?PHP
define('ZEND_LIB_PATH','/path/to/ZendFramework-1.9/library');
set_include_path(ZEND_LIB_PATH . PATH_SEPERATOR . get_include_path());

APP2: config.php

define('ZEND_LIB_PATH','/path/to/ZendFramework-1.10.1/library');
set_include_path(ZEND_LIB_PATH . PATH_SEPERATOR . get_include_path());

That way if two apps happen to be using the same version, they can share opcode caches for that version, but you're not tied to it.

DISCLAIMER: I haven't actually done this, so you probably want to test the theory before putting it into practice.

like image 27
timdev Avatar answered Dec 08 '25 10:12

timdev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!