Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are cookie read/write atomic in browser

I am trying to implement a cross tab mutex for my needs. I found a implementation here. which seems quite promising. Basically, it implements Leslie Lamport's algorithm with needs atomic read/writes for creating a mutex.

However it relies on localStorage providing atomic read/writes. This works well in most browsers except for Chrome.

So my question is, can I use cookie read/write instead? Are cookie reads/writes atomic in all mainstream browsers (IE, Chrome, Safari, Firefox)?

like image 845
Sunil Agrawal Avatar asked Jan 14 '13 06:01

Sunil Agrawal


People also ask

Can browser read cookies?

You cannot. By design, for security purpose, you can access only the cookies set by your site.

Is Cookie shared between browsers?

Cookies can be shared with other data storage, through browser extensions. Maybe in Flash or Google Gears you can maintain shared DB between browsers, but it needs to be installed on both of them, of course.

Are cookies stored across browsers?

No. Cookies are stored in browser-specific files. Show activity on this post. A cookie is a piece of data that is sent from the server (web server) back to the client (browser).

How does a browser cookie work?

How Do Cookies Work? Computer cookies are small files, often including unique identifiers that web servers send to browsers. These cookies then can be sent back to the server each time your browser requests a new page. It's a way for a website to remember you, your preferences, and your habits online.


1 Answers

Neither cookies, nor localStorage provide atomic transactions.

I think you might have misunderstood that blog post, it doesn't say that his implementation doesn't work in Chrome, it does not rely on localStorage providing atomic read/writes. He says that normal localStorage access is more volatile in Chrome. I'm assuming this is related to the fact that Chrome uses a separate process for each tab, whereas most other browsers tend to use a single process for all tabs. His code implements a locking system on top of localStorage which should protect against things getting overwritten.

Another solution would be to use IndexedDB. IndexedDB does provide atomic transactions. Being a new standard it is not supported in as many browsers as localStorage, but it does have good support in recent versions of Firefox, Chrome and IE10.

like image 71
Useless Code Avatar answered Oct 18 '22 00:10

Useless Code