2

I am using a perl script that upon receiving parameters, it check one value on a database and then executes other actions accordingly. Since traffic is increasing there are a lot of mysql reads/writes being executed and might be affecting performance.

Since the data stored in mysql is not really complicated, I am wondering if it is better to store an array in memory which can be then read/modified by other perl instances as they ran.

Is that possible?

serenesat
  • 4,611
  • 10
  • 37
  • 53
Diego
  • 39
  • 1
  • 1
  • 3
  • 2
    http://perldoc.perl.org/perlipc.html or perhaps Redis (via unix socket). – mpapec Jul 02 '15 at 05:27
  • 2
    Remember the database also has it's own caching to optimize disk IO. If you are reading the same rows often, the dbase may already hold them in RAM. You would need to profile mysql to check. It also supports temorary tables. – Kim Ryan Jul 02 '15 at 05:52
  • 3
    *"mysql reads/writes ... might be affecting perfomance"* You shouldn't make guesses about where there may be bottlenecks. You should use one of Perl's excellent profilers from CPAN to locate the real problem, and work on that area to speed things up. That is, assuming your application really does run too slowly in the first place? – Borodin Jul 02 '15 at 06:57
  • 1
    A key/value store like Redis sounds like a good idea. You can also have the Perl application not be a script that has single invocations, but run as a daemon. Then you can have the lookup table in memory there. If it's a web-app, consider Plack and friends, like Dancer2. If not, there are ways to daemonize stuff. – simbabque Jul 02 '15 at 09:26

1 Answers1

1

You can use shared memory

IPC::SharedMem is the low level core module

To non reinvent the wheel, take a look to:

IPC::Shareable

IPC::Shareable allows you to tie a variable to shared memory making it easy to share the contents of that variable with other Perl processes. Scalars, arrays, and hashes can be tied. The variable being tied may contain arbitrarily complex data structures - including references to arrays, hashes of hashes, etc.

frhack
  • 4,862
  • 2
  • 28
  • 25
  • Thanks, this is an interesting choice. I have been looking at it and seems like this library relies on a "server" applcation running in order to keep the variable in memory? in my case, I have a perl script that gets executed everytime a new event is received, it is always the same script, so do I need to create a daemon to keep the shared variables? – Diego Jul 04 '15 at 01:33
  • No it does't need any server process. In the examples the author used the names server/clients just to show that it works like client/server communication, but it's much more simple. – frhack Jul 04 '15 at 06:59