Should I check if something exists in the db and fail fast or wait for db exception
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
1
down vote
favorite
Having two classes:
public class Parent
public int Id get; set;
public int ChildId get; set;
public class Child ...
When assigning ChildId
to Parent
should I check first if it exists in the DB or wait for the DB (in my case Entity Framework Core) to throw an exception?
For example:
public async Task<ActionResult<Parent>> CreateParent(Parent parent)
// is this code redundant?
// NOTE: its probably better to use Any isntead of FindAsync because FindAsync selects *, and Any selects 1
var child = await _db.Children.FindAsync(parent.ChildId);
if (child == null)
return NotFound($"Child with id parent.ChildId could not be found.");
_db.Parents.Add(parent);
await _db.SaveChangesAsync();
return parent;
versus:
public async Task<ActionResult<Parent>> CreateParent(Parent parent)
_db.Parents.Add(parent);
await _db.SaveChangesAsync(); // handle exception somewhere globally when child with the specified id doesn't exist...
return parent;
The second example in Postgres will throw 23503 foreign_key_violation
error: https://www.postgresql.org/docs/9.4/static/errcodes-appendix.html
The downside of handling exceptions this way in ORM like EF is that it will work only with a specific database backend. If you ever wanted to switch to SQL server or something else this will not work anymore because the error code will change.
Not formatting the exception properly for the end-user could expose some things you don't want anybody but developers to see.
Without a proper benchmark, it's hard to say if the difference is significant but my conclusion is:
- If your audience is small it shouldn't matter, also you're being more explicit which increases readability and understandability
- If you need to read some properties of Child before saving Parent then I'd leave it as it is because you have to get it anyway
Handling the exception and formatting it properly is more work, but as others say it might be better performance-wise but it's better to do some benchmarks first.
premature optimization is the root of all evil.
c# entity-framework orm asp.net-core
add a comment |Â
up vote
1
down vote
favorite
Having two classes:
public class Parent
public int Id get; set;
public int ChildId get; set;
public class Child ...
When assigning ChildId
to Parent
should I check first if it exists in the DB or wait for the DB (in my case Entity Framework Core) to throw an exception?
For example:
public async Task<ActionResult<Parent>> CreateParent(Parent parent)
// is this code redundant?
// NOTE: its probably better to use Any isntead of FindAsync because FindAsync selects *, and Any selects 1
var child = await _db.Children.FindAsync(parent.ChildId);
if (child == null)
return NotFound($"Child with id parent.ChildId could not be found.");
_db.Parents.Add(parent);
await _db.SaveChangesAsync();
return parent;
versus:
public async Task<ActionResult<Parent>> CreateParent(Parent parent)
_db.Parents.Add(parent);
await _db.SaveChangesAsync(); // handle exception somewhere globally when child with the specified id doesn't exist...
return parent;
The second example in Postgres will throw 23503 foreign_key_violation
error: https://www.postgresql.org/docs/9.4/static/errcodes-appendix.html
The downside of handling exceptions this way in ORM like EF is that it will work only with a specific database backend. If you ever wanted to switch to SQL server or something else this will not work anymore because the error code will change.
Not formatting the exception properly for the end-user could expose some things you don't want anybody but developers to see.
Without a proper benchmark, it's hard to say if the difference is significant but my conclusion is:
- If your audience is small it shouldn't matter, also you're being more explicit which increases readability and understandability
- If you need to read some properties of Child before saving Parent then I'd leave it as it is because you have to get it anyway
Handling the exception and formatting it properly is more work, but as others say it might be better performance-wise but it's better to do some benchmarks first.
premature optimization is the root of all evil.
c# entity-framework orm asp.net-core
1
Sharing your research helps everyone. Tell us what you've tried and why it didn’t meet your needs. This demonstrates that you’ve taken the time to try to help yourself, it saves us from reiterating obvious answers, and most of all it helps you get a more specific and relevant answer. Also see How to Ask
– gnat
2 hours ago
updated my question with examples
– Konrad
2 hours ago
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
Having two classes:
public class Parent
public int Id get; set;
public int ChildId get; set;
public class Child ...
When assigning ChildId
to Parent
should I check first if it exists in the DB or wait for the DB (in my case Entity Framework Core) to throw an exception?
For example:
public async Task<ActionResult<Parent>> CreateParent(Parent parent)
// is this code redundant?
// NOTE: its probably better to use Any isntead of FindAsync because FindAsync selects *, and Any selects 1
var child = await _db.Children.FindAsync(parent.ChildId);
if (child == null)
return NotFound($"Child with id parent.ChildId could not be found.");
_db.Parents.Add(parent);
await _db.SaveChangesAsync();
return parent;
versus:
public async Task<ActionResult<Parent>> CreateParent(Parent parent)
_db.Parents.Add(parent);
await _db.SaveChangesAsync(); // handle exception somewhere globally when child with the specified id doesn't exist...
return parent;
The second example in Postgres will throw 23503 foreign_key_violation
error: https://www.postgresql.org/docs/9.4/static/errcodes-appendix.html
The downside of handling exceptions this way in ORM like EF is that it will work only with a specific database backend. If you ever wanted to switch to SQL server or something else this will not work anymore because the error code will change.
Not formatting the exception properly for the end-user could expose some things you don't want anybody but developers to see.
Without a proper benchmark, it's hard to say if the difference is significant but my conclusion is:
- If your audience is small it shouldn't matter, also you're being more explicit which increases readability and understandability
- If you need to read some properties of Child before saving Parent then I'd leave it as it is because you have to get it anyway
Handling the exception and formatting it properly is more work, but as others say it might be better performance-wise but it's better to do some benchmarks first.
premature optimization is the root of all evil.
c# entity-framework orm asp.net-core
Having two classes:
public class Parent
public int Id get; set;
public int ChildId get; set;
public class Child ...
When assigning ChildId
to Parent
should I check first if it exists in the DB or wait for the DB (in my case Entity Framework Core) to throw an exception?
For example:
public async Task<ActionResult<Parent>> CreateParent(Parent parent)
// is this code redundant?
// NOTE: its probably better to use Any isntead of FindAsync because FindAsync selects *, and Any selects 1
var child = await _db.Children.FindAsync(parent.ChildId);
if (child == null)
return NotFound($"Child with id parent.ChildId could not be found.");
_db.Parents.Add(parent);
await _db.SaveChangesAsync();
return parent;
versus:
public async Task<ActionResult<Parent>> CreateParent(Parent parent)
_db.Parents.Add(parent);
await _db.SaveChangesAsync(); // handle exception somewhere globally when child with the specified id doesn't exist...
return parent;
The second example in Postgres will throw 23503 foreign_key_violation
error: https://www.postgresql.org/docs/9.4/static/errcodes-appendix.html
The downside of handling exceptions this way in ORM like EF is that it will work only with a specific database backend. If you ever wanted to switch to SQL server or something else this will not work anymore because the error code will change.
Not formatting the exception properly for the end-user could expose some things you don't want anybody but developers to see.
Without a proper benchmark, it's hard to say if the difference is significant but my conclusion is:
- If your audience is small it shouldn't matter, also you're being more explicit which increases readability and understandability
- If you need to read some properties of Child before saving Parent then I'd leave it as it is because you have to get it anyway
Handling the exception and formatting it properly is more work, but as others say it might be better performance-wise but it's better to do some benchmarks first.
premature optimization is the root of all evil.
c# entity-framework orm asp.net-core
c# entity-framework orm asp.net-core
edited 54 mins ago
asked 2 hours ago


Konrad
168214
168214
1
Sharing your research helps everyone. Tell us what you've tried and why it didn’t meet your needs. This demonstrates that you’ve taken the time to try to help yourself, it saves us from reiterating obvious answers, and most of all it helps you get a more specific and relevant answer. Also see How to Ask
– gnat
2 hours ago
updated my question with examples
– Konrad
2 hours ago
add a comment |Â
1
Sharing your research helps everyone. Tell us what you've tried and why it didn’t meet your needs. This demonstrates that you’ve taken the time to try to help yourself, it saves us from reiterating obvious answers, and most of all it helps you get a more specific and relevant answer. Also see How to Ask
– gnat
2 hours ago
updated my question with examples
– Konrad
2 hours ago
1
1
Sharing your research helps everyone. Tell us what you've tried and why it didn’t meet your needs. This demonstrates that you’ve taken the time to try to help yourself, it saves us from reiterating obvious answers, and most of all it helps you get a more specific and relevant answer. Also see How to Ask
– gnat
2 hours ago
Sharing your research helps everyone. Tell us what you've tried and why it didn’t meet your needs. This demonstrates that you’ve taken the time to try to help yourself, it saves us from reiterating obvious answers, and most of all it helps you get a more specific and relevant answer. Also see How to Ask
– gnat
2 hours ago
updated my question with examples
– Konrad
2 hours ago
updated my question with examples
– Konrad
2 hours ago
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
9
down vote
Checking for uniqueness and then setting is an antipattern; it can always happen that the ID is inserted concurrently between checking time and writing time. Databases are equipped to deal with this problem through mechanisms like constraints and transactions; most programming languages aren't. Therefore, if you value data consistency, leave it to the expert (the database), i.e. do the insert and catch an exception if it occurs.
I updated my question.
– Konrad
2 hours ago
But isn't it better to fail fast?
– Konrad
2 hours ago
I think it depends, because if some Parent properties would depend on child properties then you have to fetch a child first.
– Konrad
2 hours ago
1
checking and fail is not faster than just "trying" and hope for the best. Former implies 2 operations to be implemented and performed by your system and 2 by the DB, while the latest only implies one of them. Checking is delegated to the DB server. It also implies one less hop into the network and one less task to be attended by the DB. We might think that one more query to the DB is affordable, but we often forget to think in big. Think in high concurrency triggering the query over and over a hundred times. It could dup the whole traffic to the DB. If that matters is up to you to decide.
– Laiv
2 hours ago
add a comment |Â
up vote
2
down vote
I think what you call “fail fast†and what I call it is not the same.
Telling the database to make a change and handling the failure, that is fast. Your way is complicated, slow and not particularly reliable.
That technique of yours is not fail fast, it is “preflightingâ€Â. There are sometimes good reasons, but not when you use a database.
There are cases when you need 2nd query when one class depend on another, so you have no choice in cases like that.
– Konrad
2 hours ago
But not here. And database queries can be quite clever, so I generally doubt the “no choiceâ€Â.
– gnasher729
2 hours ago
I think it also depends on the application, if you create it just for a few users then it shouldn't make a difference and code is more readable with 2 queries.
– Konrad
2 hours ago
You are assuming that your DB is storing inconsistent data. In other words, looks like you don't trust in your DB and the consistency of the data. If that were the case, you have a really big problem and your solution is a walkaround. A palliative solution fated to be overruled sooner than later. There could be cases where you are forced to consume a DB out of your control and management. From other applications. In those cases, I would consider such validations. In any case, @gnasher is right, yours is not failing fast or it's not what we understand as fail fast.
– Laiv
1 hour ago
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
9
down vote
Checking for uniqueness and then setting is an antipattern; it can always happen that the ID is inserted concurrently between checking time and writing time. Databases are equipped to deal with this problem through mechanisms like constraints and transactions; most programming languages aren't. Therefore, if you value data consistency, leave it to the expert (the database), i.e. do the insert and catch an exception if it occurs.
I updated my question.
– Konrad
2 hours ago
But isn't it better to fail fast?
– Konrad
2 hours ago
I think it depends, because if some Parent properties would depend on child properties then you have to fetch a child first.
– Konrad
2 hours ago
1
checking and fail is not faster than just "trying" and hope for the best. Former implies 2 operations to be implemented and performed by your system and 2 by the DB, while the latest only implies one of them. Checking is delegated to the DB server. It also implies one less hop into the network and one less task to be attended by the DB. We might think that one more query to the DB is affordable, but we often forget to think in big. Think in high concurrency triggering the query over and over a hundred times. It could dup the whole traffic to the DB. If that matters is up to you to decide.
– Laiv
2 hours ago
add a comment |Â
up vote
9
down vote
Checking for uniqueness and then setting is an antipattern; it can always happen that the ID is inserted concurrently between checking time and writing time. Databases are equipped to deal with this problem through mechanisms like constraints and transactions; most programming languages aren't. Therefore, if you value data consistency, leave it to the expert (the database), i.e. do the insert and catch an exception if it occurs.
I updated my question.
– Konrad
2 hours ago
But isn't it better to fail fast?
– Konrad
2 hours ago
I think it depends, because if some Parent properties would depend on child properties then you have to fetch a child first.
– Konrad
2 hours ago
1
checking and fail is not faster than just "trying" and hope for the best. Former implies 2 operations to be implemented and performed by your system and 2 by the DB, while the latest only implies one of them. Checking is delegated to the DB server. It also implies one less hop into the network and one less task to be attended by the DB. We might think that one more query to the DB is affordable, but we often forget to think in big. Think in high concurrency triggering the query over and over a hundred times. It could dup the whole traffic to the DB. If that matters is up to you to decide.
– Laiv
2 hours ago
add a comment |Â
up vote
9
down vote
up vote
9
down vote
Checking for uniqueness and then setting is an antipattern; it can always happen that the ID is inserted concurrently between checking time and writing time. Databases are equipped to deal with this problem through mechanisms like constraints and transactions; most programming languages aren't. Therefore, if you value data consistency, leave it to the expert (the database), i.e. do the insert and catch an exception if it occurs.
Checking for uniqueness and then setting is an antipattern; it can always happen that the ID is inserted concurrently between checking time and writing time. Databases are equipped to deal with this problem through mechanisms like constraints and transactions; most programming languages aren't. Therefore, if you value data consistency, leave it to the expert (the database), i.e. do the insert and catch an exception if it occurs.
answered 2 hours ago
Kilian Foth
85k31228257
85k31228257
I updated my question.
– Konrad
2 hours ago
But isn't it better to fail fast?
– Konrad
2 hours ago
I think it depends, because if some Parent properties would depend on child properties then you have to fetch a child first.
– Konrad
2 hours ago
1
checking and fail is not faster than just "trying" and hope for the best. Former implies 2 operations to be implemented and performed by your system and 2 by the DB, while the latest only implies one of them. Checking is delegated to the DB server. It also implies one less hop into the network and one less task to be attended by the DB. We might think that one more query to the DB is affordable, but we often forget to think in big. Think in high concurrency triggering the query over and over a hundred times. It could dup the whole traffic to the DB. If that matters is up to you to decide.
– Laiv
2 hours ago
add a comment |Â
I updated my question.
– Konrad
2 hours ago
But isn't it better to fail fast?
– Konrad
2 hours ago
I think it depends, because if some Parent properties would depend on child properties then you have to fetch a child first.
– Konrad
2 hours ago
1
checking and fail is not faster than just "trying" and hope for the best. Former implies 2 operations to be implemented and performed by your system and 2 by the DB, while the latest only implies one of them. Checking is delegated to the DB server. It also implies one less hop into the network and one less task to be attended by the DB. We might think that one more query to the DB is affordable, but we often forget to think in big. Think in high concurrency triggering the query over and over a hundred times. It could dup the whole traffic to the DB. If that matters is up to you to decide.
– Laiv
2 hours ago
I updated my question.
– Konrad
2 hours ago
I updated my question.
– Konrad
2 hours ago
But isn't it better to fail fast?
– Konrad
2 hours ago
But isn't it better to fail fast?
– Konrad
2 hours ago
I think it depends, because if some Parent properties would depend on child properties then you have to fetch a child first.
– Konrad
2 hours ago
I think it depends, because if some Parent properties would depend on child properties then you have to fetch a child first.
– Konrad
2 hours ago
1
1
checking and fail is not faster than just "trying" and hope for the best. Former implies 2 operations to be implemented and performed by your system and 2 by the DB, while the latest only implies one of them. Checking is delegated to the DB server. It also implies one less hop into the network and one less task to be attended by the DB. We might think that one more query to the DB is affordable, but we often forget to think in big. Think in high concurrency triggering the query over and over a hundred times. It could dup the whole traffic to the DB. If that matters is up to you to decide.
– Laiv
2 hours ago
checking and fail is not faster than just "trying" and hope for the best. Former implies 2 operations to be implemented and performed by your system and 2 by the DB, while the latest only implies one of them. Checking is delegated to the DB server. It also implies one less hop into the network and one less task to be attended by the DB. We might think that one more query to the DB is affordable, but we often forget to think in big. Think in high concurrency triggering the query over and over a hundred times. It could dup the whole traffic to the DB. If that matters is up to you to decide.
– Laiv
2 hours ago
add a comment |Â
up vote
2
down vote
I think what you call “fail fast†and what I call it is not the same.
Telling the database to make a change and handling the failure, that is fast. Your way is complicated, slow and not particularly reliable.
That technique of yours is not fail fast, it is “preflightingâ€Â. There are sometimes good reasons, but not when you use a database.
There are cases when you need 2nd query when one class depend on another, so you have no choice in cases like that.
– Konrad
2 hours ago
But not here. And database queries can be quite clever, so I generally doubt the “no choiceâ€Â.
– gnasher729
2 hours ago
I think it also depends on the application, if you create it just for a few users then it shouldn't make a difference and code is more readable with 2 queries.
– Konrad
2 hours ago
You are assuming that your DB is storing inconsistent data. In other words, looks like you don't trust in your DB and the consistency of the data. If that were the case, you have a really big problem and your solution is a walkaround. A palliative solution fated to be overruled sooner than later. There could be cases where you are forced to consume a DB out of your control and management. From other applications. In those cases, I would consider such validations. In any case, @gnasher is right, yours is not failing fast or it's not what we understand as fail fast.
– Laiv
1 hour ago
add a comment |Â
up vote
2
down vote
I think what you call “fail fast†and what I call it is not the same.
Telling the database to make a change and handling the failure, that is fast. Your way is complicated, slow and not particularly reliable.
That technique of yours is not fail fast, it is “preflightingâ€Â. There are sometimes good reasons, but not when you use a database.
There are cases when you need 2nd query when one class depend on another, so you have no choice in cases like that.
– Konrad
2 hours ago
But not here. And database queries can be quite clever, so I generally doubt the “no choiceâ€Â.
– gnasher729
2 hours ago
I think it also depends on the application, if you create it just for a few users then it shouldn't make a difference and code is more readable with 2 queries.
– Konrad
2 hours ago
You are assuming that your DB is storing inconsistent data. In other words, looks like you don't trust in your DB and the consistency of the data. If that were the case, you have a really big problem and your solution is a walkaround. A palliative solution fated to be overruled sooner than later. There could be cases where you are forced to consume a DB out of your control and management. From other applications. In those cases, I would consider such validations. In any case, @gnasher is right, yours is not failing fast or it's not what we understand as fail fast.
– Laiv
1 hour ago
add a comment |Â
up vote
2
down vote
up vote
2
down vote
I think what you call “fail fast†and what I call it is not the same.
Telling the database to make a change and handling the failure, that is fast. Your way is complicated, slow and not particularly reliable.
That technique of yours is not fail fast, it is “preflightingâ€Â. There are sometimes good reasons, but not when you use a database.
I think what you call “fail fast†and what I call it is not the same.
Telling the database to make a change and handling the failure, that is fast. Your way is complicated, slow and not particularly reliable.
That technique of yours is not fail fast, it is “preflightingâ€Â. There are sometimes good reasons, but not when you use a database.
answered 2 hours ago
gnasher729
18.5k22452
18.5k22452
There are cases when you need 2nd query when one class depend on another, so you have no choice in cases like that.
– Konrad
2 hours ago
But not here. And database queries can be quite clever, so I generally doubt the “no choiceâ€Â.
– gnasher729
2 hours ago
I think it also depends on the application, if you create it just for a few users then it shouldn't make a difference and code is more readable with 2 queries.
– Konrad
2 hours ago
You are assuming that your DB is storing inconsistent data. In other words, looks like you don't trust in your DB and the consistency of the data. If that were the case, you have a really big problem and your solution is a walkaround. A palliative solution fated to be overruled sooner than later. There could be cases where you are forced to consume a DB out of your control and management. From other applications. In those cases, I would consider such validations. In any case, @gnasher is right, yours is not failing fast or it's not what we understand as fail fast.
– Laiv
1 hour ago
add a comment |Â
There are cases when you need 2nd query when one class depend on another, so you have no choice in cases like that.
– Konrad
2 hours ago
But not here. And database queries can be quite clever, so I generally doubt the “no choiceâ€Â.
– gnasher729
2 hours ago
I think it also depends on the application, if you create it just for a few users then it shouldn't make a difference and code is more readable with 2 queries.
– Konrad
2 hours ago
You are assuming that your DB is storing inconsistent data. In other words, looks like you don't trust in your DB and the consistency of the data. If that were the case, you have a really big problem and your solution is a walkaround. A palliative solution fated to be overruled sooner than later. There could be cases where you are forced to consume a DB out of your control and management. From other applications. In those cases, I would consider such validations. In any case, @gnasher is right, yours is not failing fast or it's not what we understand as fail fast.
– Laiv
1 hour ago
There are cases when you need 2nd query when one class depend on another, so you have no choice in cases like that.
– Konrad
2 hours ago
There are cases when you need 2nd query when one class depend on another, so you have no choice in cases like that.
– Konrad
2 hours ago
But not here. And database queries can be quite clever, so I generally doubt the “no choiceâ€Â.
– gnasher729
2 hours ago
But not here. And database queries can be quite clever, so I generally doubt the “no choiceâ€Â.
– gnasher729
2 hours ago
I think it also depends on the application, if you create it just for a few users then it shouldn't make a difference and code is more readable with 2 queries.
– Konrad
2 hours ago
I think it also depends on the application, if you create it just for a few users then it shouldn't make a difference and code is more readable with 2 queries.
– Konrad
2 hours ago
You are assuming that your DB is storing inconsistent data. In other words, looks like you don't trust in your DB and the consistency of the data. If that were the case, you have a really big problem and your solution is a walkaround. A palliative solution fated to be overruled sooner than later. There could be cases where you are forced to consume a DB out of your control and management. From other applications. In those cases, I would consider such validations. In any case, @gnasher is right, yours is not failing fast or it's not what we understand as fail fast.
– Laiv
1 hour ago
You are assuming that your DB is storing inconsistent data. In other words, looks like you don't trust in your DB and the consistency of the data. If that were the case, you have a really big problem and your solution is a walkaround. A palliative solution fated to be overruled sooner than later. There could be cases where you are forced to consume a DB out of your control and management. From other applications. In those cases, I would consider such validations. In any case, @gnasher is right, yours is not failing fast or it's not what we understand as fail fast.
– Laiv
1 hour ago
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsoftwareengineering.stackexchange.com%2fquestions%2f378670%2fshould-i-check-if-something-exists-in-the-db-and-fail-fast-or-wait-for-db-except%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
1
Sharing your research helps everyone. Tell us what you've tried and why it didn’t meet your needs. This demonstrates that you’ve taken the time to try to help yourself, it saves us from reiterating obvious answers, and most of all it helps you get a more specific and relevant answer. Also see How to Ask
– gnat
2 hours ago
updated my question with examples
– Konrad
2 hours ago