Iris: Persisten session after server restart

Created on 2 Aug 2017  ·  7Comments  ·  Source: kataras/iris

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?

question

All 7 comments

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

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?

@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.

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?

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.

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

.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 .

Was this page helpful?
0 / 5 - 0 ratings