Segmentation fault (core dumped) error while using bref Laravel
TL;DR
If you are using Elasticsearch then remove the method bulk() from your code and use a normal HTTP request (you can guzzle for this) to invoke the _bulk API of ES to update your documents instead of using Elasticsearch official Laravel library.
Laravel is a web application framework with expressive and elegant syntax. It's free and open-source.
You can use it as BE framework to build your serverless applications/services as well and for that, you can use Laravel vapor or bref.sh. Bref provides Vapor will charge you some dollars on a subscription basis but you can leverage the power of the open-source version of bref.sh
Background :
If you are using bref for serverless and using Elasticsearch as a data store point then you might see the Segmentation fault (core dumped) error
. Mostly this error happens due to memory-related issues. This error can occur due to many reasons and one of the reason is that you might have been using the official library of the ES (elasticsearch-php) in your project. This is an awesome official package from the ES team but it causes segmentation fault issues when using the bulk()
method.
The real cause of the error :
When you want to create/update multiple documents in ES at once, you use the bulk()
method given by the official library to invoke the _bulk
API of ES. When you use this method and your serverless application (Lambda function) gets invoked then you will see this error. This often happens because of file issues. the bulk
method uses a file internally to handle the request.
Fix for this error :
You need to remove the method bulk() from your code and use a normal HTTP request (you can guzzle for this) to invoke the _bulk API of ES to update your documents. In this way you won't see the segmentation fault errors.
Sample code :
use GuzzleHttp\Client;
$client = new Client([
'base_uri' => "HOST URL" . "ES Index" . '/',
'headers' => [
'Content-Type' => 'application/json'
],
'auth' => ["username", "password"]
]);
$data = []; // this is your post data for the API
$response = $client->post("_bulk", ['body' => $data]);
This should fix the issue (it did for my application) but if you still see the same issue then reach out to me or leave a comment down below.