Mastering PHP Shared Memory: Fast IPC Techniques and Performance Benchmarks
This article explains shared memory concepts, demonstrates how to create, read, write, and delete PHP shmop segments with code examples, discusses atomic operations using semaphores, and compares its speed against memcache and file I/O, offering practical guidance for backend developers.
Usage Scenario
For a single machine, errors from a specific host can be aggregated and alerted; identical alerts within a minute are merged using shared memory for temporary storage, providing an efficient solution.
PHP Session
If a service runs on a single server with sessions enabled, replacing file‑based sessions with shared memory can significantly improve speed, though it lacks the flexibility of memcache.
What Is Shared Memory
Shared memory allows different processes on the same machine to exchange data by creating a memory segment identified by a unique shmid . Processes can read, write, or delete data in this segment without kernel involvement, making it a fast form of inter‑process communication (IPC).
Common PHP Shared Memory Methods
APC can cache PHP opcode and share data among PHP‑FPM processes (functions: apc_store , apc_fetch , apc_add , etc.).
Shmop (Unix) provides shmop_open , shmop_close , shmop_read , shmop_write , shmop_delete .
System V shared memory functions include ftok , shm_attach , shm_detach , shm_put_var , shm_get_var , shm_remove_var .
Semaphores ( sem_get , sem_acquire , sem_release ) are needed to ensure atomic operations.
Creating a Memory Segment
<code><?php
$key = ftok(__FILE__, 'h');
$mode = 'c';
$permissions = 0644;
$size = 1024;
$shmid = shmop_open($key, $mode, $permissions, $size);
?></code>The first argument is a key generated by ftok . The mode follows fopen conventions ( a read‑only, w read‑write, c create or open, n create only). Permissions are an octal value, and size is in bytes.
Writing Data
<code><?php
$shmid = shmop_open(ftok(__FILE__, 'h'), 'c', 0644, 1024);
shmop_write($shmid, "Hello World!", 0);
?></code>The function takes the shared memory ID, the data string, and the offset (usually 0). It returns the number of bytes written or FALSE on failure.
Reading Data
<code><?php
$shmid = shmop_open(ftok(__FILE__, 'h'), 'c', 0644, 1024);
var_dump(shmop_read($shmid, 0, 11));
?></code>Parameters are the shared memory ID, start offset (0 for the beginning), and the number of bytes to read (often shmop_size($shmid) ).
Deleting a Memory Segment
<code><?php
$shmid = shmop_open(ftok(__FILE__, 'h'), 'c', 0644, 1024);
shmop_delete($shmid);
?></code>Deletion marks the segment for removal; it is actually removed after all processes detach.
Closing a Memory Segment
<code><?php
$shmid = shmop_open(ftok(__FILE__, 'h'), 'c', 0644, 1024);
shmop_write($shmid, "Hello World!", 0);
shmop_delete($shmid);
shmop_close($shmid);
?></code>After operations, the segment must be closed with shmop_close .
Atomic Operations – Semaphore Control
Write operations are not atomic; to handle concurrent access, use semaphores:
<code><?php
$key = ftok(__FILE__, 'h');
$semid = sem_get($key);
if (sem_acquire($semid)) {
$shmid = shmop_open($key, 'c', 0644, 1024);
shmop_write($shmid, '13800138000', 0);
shmop_close($shmid);
sem_release($semid);
}
?></code>Performance Comparison
Reading/writing 1 KB of data 100 000 times shows the following average times (seconds):
Memcache – read 7.8, write 8.11
File – read 2.6, write 3.2
Shared memory – read 0.1, write 0.07
The results demonstrate that shared memory provides the fastest I/O among the three methods.
360 Zhihui Cloud Developer
360 Zhihui Cloud is an enterprise open service platform that aims to "aggregate data value and empower an intelligent future," leveraging 360's extensive product and technology resources to deliver platform services to customers.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.