# Sessions lock - not using memcached?

- Thread: https://externals.io/message/49788
- Original archive: https://news-web.php.net/php.internals/49788
- Started by ROZE Samuel on 2010-09-26T08:10:24+00:00
- Messages: 3
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #49788 — ROZE Samuel — 2010-09-26T08:10:24+00:00

URL: https://externals.io/message/49788#49788

Hello,

I'm working with sessions in my PHP applications. My pages can take long time to be executed (~500ms) and I've simultaneous requests for the same session (Ajax requests). My problem is that I close sessions at the end of my PHP script, so my sessions files are locked for half a second. So, simultaneous requests (for the same session) can't be executed... simultaneously!

My question is if I use memcached to store sessions, will my sessions be locked or can I access to them in the same time? If there aren't it should solve my problem.

A second question is performances: is memcached sessions faster the PHP default sessions? I think yes because it doesn't make disk I/O..?

Best regards,
Samuel ROZE.

---

## #49789 — Clint Byrum — 2010-09-26T15:23:40+00:00

URL: https://externals.io/message/49788#49789 · In reply to: #49788

> Hello,
> 
> I'm working with sessions in my PHP applications. My pages can take long time to be executed (~500ms) and I've simultaneous requests for the same session (Ajax requests). My problem is that I close sessions at the end of my PHP script, so my sessions files are locked for half a second. So, simultaneous requests (for the same session) can't be executed... simultaneously!
> 
> My question is if I use memcached to store sessions, will my sessions be locked or can I access to them in the same time? If there aren't it should solve my problem.
> 
> A second question is performances: is memcached sessions faster the PHP default sessions? I think yes because it doesn't make disk I/O..?
> 

No, the default memcache session handler has no locking.

But that presents you with a new problem of a race condition.

This is why you shouldn't keep any actual data in sessions:

R1:[initial request start, session is read and has X=1]
A1:[ajax request 1 reads session, X=1]
A2:[user clicks "add 1 to my shopping cart", X = X+1 = 2]
A2:[displays shopping cart with X == 2]
A2:[faster than A1, session over, write session with X = 2]
A1:[session over, write session with X = 1]

DOH!

There has to be some kind of locking on data between read/write to maintain consistency.

One way you can get around this is by *not* using the session for mutable data, and just using it to store a key to the data in your backend, and then only writing when you must.

The above becomes

R1:[initial request start, session is read and has shopping cart SID=1]
A1:[ajax request 1 reads session, SID=1]
A2:[user clicks "add 1 to my shopping cart", UPDATE carts SET X = X + 1 WHERE SID = 1]
A2:[displays shopping cart with X == 2]
A2:[faster than A1, session over, write session with SID=1]
A1:[session over, write session with SID=1]

Also, the memcache session handler can be dangerous in that it might choose to delete your session before expiration if memcached fills up, so be aware of this.

Good luck!

---

## #49790 — Ferenc Kovacs — 2010-09-26T18:04:37+00:00

URL: https://externals.io/message/49788#49790 · In reply to: #49789

>
> 
>
> > […]
> time to be executed (~500ms) and I've simultaneous requests for the same
> session (Ajax requests). My problem is that I close sessions at the end of
> my PHP script, so my sessions files are locked for half a second. So,
> simultaneous requests (for the same session) can't be executed...
> simultaneously!
> > […]
> locked or can I access to them in the same time? If there aren't it should
> solve my problem.
> > […]
> default sessions? I think yes because it doesn't make disk I/O..?
> > […]
>
> No, the default memcache session handler has no locking.
>
>
http://pecl.php.net/package-info.php?package=memcache&version=3.0.4

<http://pecl.php.net/package-info.php?package=memcache&version=3.0.4>it
seems that there will be locking with the next stable release for the
memcache library.

Tyrael
