Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Persisten session after server restart #704

Closed
speedwheel opened this issue Aug 2, 2017 · 7 comments
Closed

Persisten session after server restart #704

speedwheel opened this issue Aug 2, 2017 · 7 comments

Comments

@speedwheel
Copy link
Contributor

speedwheel commented Aug 2, 2017

Hello, glad to see there is a lot of activity and more user are taking part to make Iris better and better.

Now, I am not really sure how to implement this myself, but what would be the easiest way to make a session persistent after server restart? I tried the example with Redis, but the session was destroyed after server restart.

Any ideas how to achieve this?

@kataras
Copy link
Owner

kataras commented Aug 2, 2017

It's the first opened issue...

Redis should work, it works on me if you have any issues please contact on the chat, we may find what's wrong with your setup.

However, you can use the file storage session database, example: https://github.com/kataras/iris/blob/master/_examples/sessions/database/file/main.go

@speedwheel
Copy link
Contributor Author

speedwheel commented Aug 2, 2017

I made it work with the file storage from the example above.

One thing I noticed is that on line 108 inside this file "github.com/kataras/iris/sessions/sessiondb/file", that there is no error checking for ioutil.WriteFile, in case you don't have the right permissions to create a file.

@kataras Is the file session storage slower than default sessions?

@kataras
Copy link
Owner

kataras commented Aug 2, 2017

@speedwheel thanks for the info, it has been fixed.

Yes anything else than memory storage is slower. This session manager uses always the memory (high performance but may need more RAM) and if you want persistence then you attach a session database (like file storage and redis), simple and efficient. We did use this session manager(the iris one which I authored) at production, but nothing stops you to use an external one, remember? Iris is compatible with everything.

@speedwheel
Copy link
Contributor Author

I understand, one last question:

Is sess.Start(ctx).Get() slower using the file session storage? I am not really sure how it works, but I am thinking the file session is than dumped in the memory, so it should be the same speed?

@kataras
Copy link
Owner

kataras commented Aug 2, 2017

I'll try to explain you the flow:

  • .Start -> if session database attached then load from that storage and save to the memory, otherwise load from memory. The load from database is done once on the initialize of each session.
  • .Get (important) -> load from memory, if database attached then it already loaded the values from database on the .Start action, so it will retrieve the data from the memory (fast)
  • .Set -> set to the memory, if database attached then update the storage
  • .Delete -> clear from memory, if database attached then update the storage
  • .Destroy -> destroy from memory and client cookie, if database attached then update the storage with empty values, empty values means delete the storage with that specific session id.

Using everything else except memory is slower than memory but database is fetched once at each session and its updated on every Set, Delete, Destroy at call-time (all other external sessions managers out there work different than Iris one as far as I know, you may find them more suited to your application, it depends).

I hope that your questions are being answered.

@speedwheel
Copy link
Contributor Author

Thanks for the answer, it works similar like I was thinking and I consider that Iris Sessions are suitable for my project.

@heyouzhi
Copy link

heyouzhi commented Jan 24, 2018

.Get (important) -> load from memory, if database attached then it already loaded the values from database on the .Start action, so it will retrieve the data from the memory (fast)

As @kataras said, session Get only fetch values from database once,but if the values in database updated by other server node,the session in memory will not update! This behavior will cause memory session in many server nodes inconsistent! for example, issue #885 .

WingGao pushed a commit to WingGao/iris that referenced this issue Oct 9, 2020
…d) as requested kataras#704 (comment)

Former-commit-id: 6a96da0bde5ae8d999fc9c1e6af49db2a893a6d5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants