Hi, I'm creating a video game with SFS2X, I have a logical doubt about server programming.
Is it convenient to load all user data into RAM when the server is started, or to use database queries to access each user?
When is it convenient to write user data to the database?
- On OnLogout User; (It will happen very often)
- At server shutdown (will rarely occur);
- To the destruction of the user's clan room? (Will often)
- Every 10 minutes?
Thanks for your attention
Logical doubt LOAD/WRITE on DB
-
- Posts: 14
- Joined: 09 May 2019, 17:24
Re: Logical doubt LOAD/WRITE on DB
Hello,
Generally speaking no, it isn't.
The main reasons are... wasting too much memory and loosing the ability to perform queries.
Normally it is whenever you need it.
I am not sure what you're afraid of... maybe database performance?
If you run the database on a dedicated server and in the local network you shouldn't be too concerned. Databases such as MySQL can handle hundreds of operations per second without problems.
On the other hand if you find yourself writing to the database too often for a single user session, then you may need to optimize the amount of calls. It really depends on the specific use case and there is no general solution for everything.
Hope it helps
Is it convenient to load all user data into RAM when the server is started, or to use database queries to access each user?
Generally speaking no, it isn't.
The main reasons are... wasting too much memory and loosing the ability to perform queries.
When is it convenient to write user data to the database?
Normally it is whenever you need it.
I am not sure what you're afraid of... maybe database performance?
If you run the database on a dedicated server and in the local network you shouldn't be too concerned. Databases such as MySQL can handle hundreds of operations per second without problems.
On the other hand if you find yourself writing to the database too often for a single user session, then you may need to optimize the amount of calls. It really depends on the specific use case and there is no general solution for everything.
Hope it helps
-
- Posts: 14
- Joined: 09 May 2019, 17:24
Re: Logical doubt LOAD/WRITE on DB
Thank you
-
- Posts: 14
- Joined: 09 May 2019, 17:24
Re: Logical doubt LOAD/WRITE on DB
Hi, but for a chat, it's the same?
If a user sends so many messages, do they do too many queries?
If a user sends so many messages, do they do too many queries?
Re: Logical doubt LOAD/WRITE on DB
A chat could be different, yes.
If you want to store every message to a database it may be best to use a "batching" technique, where you don't hit the DB for every new message being sent. Instead you accumulate a number of messages in memory and then store them in the DB with one call.
A similar approach could be to use a time interval where, for example, you accumulate all messages from a user in a queue and flush the queue to the database every 1 or 2 minutes or something like that.
Hope it helps
If you want to store every message to a database it may be best to use a "batching" technique, where you don't hit the DB for every new message being sent. Instead you accumulate a number of messages in memory and then store them in the DB with one call.
A similar approach could be to use a time interval where, for example, you accumulate all messages from a user in a queue and flush the queue to the database every 1 or 2 minutes or something like that.
Hope it helps
-
- Posts: 14
- Joined: 09 May 2019, 17:24
Re: Logical doubt LOAD/WRITE on DB
Thank you
-
- Posts: 14
- Joined: 09 May 2019, 17:24
Re: Logical doubt LOAD/WRITE on DB
What is the difference between:
or
It seems to me that they are the same thing
Code: Select all
IDBManager dbmanager = getParentExtension().getParentZone().getDBManager();
Connection connection = null;
try
{
connection = dbmanager.getConnection();
ResultSet result_query = connection.prepareStatement("SELECT * FROM table).executeQuery();
}
catch (Exception e)
{
trace("sql error" + e);
}
finally
{
try
{
if(connection != null) connection.close();
}
catch(SQLException e)
{
trace("Connection error " + e);
}
}
or
Code: Select all
IDBManager dbmanager = getParentExtension().getParentZone().getDBManager();
try {
ISFSArray array= (ISFSArray)dbmanager.executeQuery("SELECT * FROM table", null);
}
catch (Exception e) {
trace("sql error" + e);
}
It seems to me that they are the same thing
Re: Logical doubt LOAD/WRITE on DB
The first one returns a native JDBC ResultSet and it is generally more flexible, as it allows to do all kinds of advanced operations with the DB.
The second one is more high-level, it wraps JDBC and it returns the results in SFSObject/SFSArray format, which is easy to use for sending to clients etc...
They are both valid and useful in different situations.
The second one is more high-level, it wraps JDBC and it returns the results in SFSObject/SFSArray format, which is easy to use for sending to clients etc...
They are both valid and useful in different situations.
-
- Posts: 14
- Joined: 09 May 2019, 17:24
Re: Logical doubt LOAD/WRITE on DB
Hello world again,
Another doubt arose, I am currently working on the user statistics of my project, this involves taking a large amount of data from the database, if there is a small possibility that I have this data in RAM, it is worth checking if the user is Online, and in case I take them from him?
Or do I take them directly from the database?
To get this data I use 2 queries with various LEFT JOINs.
What is the most efficient way?
Another doubt arose, I am currently working on the user statistics of my project, this involves taking a large amount of data from the database, if there is a small possibility that I have this data in RAM, it is worth checking if the user is Online, and in case I take them from him?
Or do I take them directly from the database?
To get this data I use 2 queries with various LEFT JOINs.
What is the most efficient way?
Re: Logical doubt LOAD/WRITE on DB
Yes, if the stats data is available in RAM when a User is logged in, it is certainly better to check if the User is connected.
To do that you can simply call
or
in your Extension code.
Cheers
To do that you can simply call
Code: Select all
getApi().getUserByName(...)
or
Code: Select all
getApi().getUserById(...)
in your Extension code.
Cheers
Who is online
Users browsing this forum: Google [Bot] and 68 guests