Database check every time anvil.users.get_user() call?

Hi,

Is there database check every time it is made

user = anvil.users.get_user()

Or somehow Anvil manage it with cookies and session without checking database?

Thanks so much.

1 Like

I don’t know the specific details of how this method can give you the current logged user, but it takes a not insignificant time (if I remember correctly, it takes around 0.5s) to get it’s result every time you use it, so it might be interesting to you to make only one check and store it’s value in a variable instead of multiple checks.

I’m saying this but I don’t know the reason for your specific question. Share more info on why you want to know this and maybe more people will be able to help you.

1 Like

Thanks, from docs, it does make database call.
It is a matter when you scale your app.

Like @gabriel.duro.s says, details are missing in the question.

What do you mean by making that call?
Are you asking about making it on the client or server side?

On the server side it will require a database read every time, but the time for that is negligible.

On the client side it will require at least one round trip, and maybe more if the table has simple object or link columns.

In some cases using anvil.users.get_user() on the client side works just fine, but when performance are important I never use any code that triggers round trips. I call one server function that packs all the info I need in one dictionary and I’m done with round trips. Same with storing to the database: send a dictionary with everything that needs to be saved in one call.

What do you mean by “It is a matter when you scale your app”?

One database read on the server side is negligible. I’m sure Anvil does more than just that one read on each round trip, while managing cookies, sessions, etc. And I’m sure all of that doesn’t make it non scalable.

If you pay attention to not making more than one round trip per form, the client is definitely scalable.

2 Likes

Unfortunately, not all apps can afford that because sensitive data must be processed on the server.

It is what I want to find out more, which one Anvil used and how to use both of them:

There are two types of sessions commonly used in web development (php, python, nodejs ...):

1 Client-side - sessions are stored client-side in browser cookies

2 Server-side - sessions are stored server-side (typically a session identifier is then created and stored client-side in browser cookies)

I’m confused… what has this to do with the subject of the post anvil.users.get_user()?

Please see the docs

get_user([allow_remembered=True])
Get the row from the users table that corresponds to the currently logged-in user. If allow_remembered is true (the default), the user may have logged in in a previous session. Returns None if no user is logged in.

Still confused…

In the same thread you ask “Database check every time anvil.users.get_user() call?”, say “It is a matter when you scale your app.” and talk about “Sessions in Flask”?

They look like three unrelated subjects to me.

It is session management in general (php, python, nodejs…) I make it clear in the post. Flask is an example since it is python similar to anvil

Can you show me the code where you call anvil.users.get_user()?

It is easier to understand what you are talking about with an example.

@anvil.server.callable
def print_my_permissions():
  super_user = 'ryan@anvil.works'
  if anvil.users.get_user() is None:
    print("Nobody is logged in.")
  elif anvil.users.get_user()['email'] == super_user:
    print(f"{super_user} is allowed to see this.")
  else:
    print("This path is for minimum-access users.")

Here I see server code calling twice anvil.users.get_user(), I don’t see any relation with scalability or with sessions in Flask.

It would be slightly nicer to first do user = anvil.users.get_user() and then access user twice, rather than repeating the call twice, but it’s more a matter of elegance than performance.

As I said in my previous answer: “One database read on the server side is negligible. I’m sure Anvil does more than just that one read on each round trip, while managing cookies, sessions, etc. And I’m sure all of that doesn’t make it non scalable.”

And I assume that when @gabriel.duro.s said “if I remember correctly, it takes around 0.5s” he assumed the call was done on the client side.

Sorry, you need to learn more about these two type of session management.

1 Client-side - sessions are stored client-side in browser cookies

2 Server-side - sessions are stored server-side (typically a session identifier is then created and stored client-side in browser cookies)

If I needed to learn more, I would ask a question.

Here you are asking a question, and it is not clear what you are asking.

The answer to the original question is obvious. As the documentation says, anvil.users.get_user() returns a row from the Users table, so it does access the database at least once. I think the second time it is cached.

If you are worried about one database read, you shouldn’t use Anvil, and you shouldn’t even use Python.

I still don’t understand what Flask and scalability have to do with this question, but since you already have the answer, you could ask a new one for that.

I found that answer and mark solved already.

It is not cached.

Using 1 Client-side - sessions, no server database is called, can be used with Python, Flask is an example.

In fact, I can make option 1 work with Anvil as well.

Yes, I was. If someone tells me it’s concerned about the cost of a database call I immediately think it’s on client. On server code, queries time are a no problem usually.